Skip to content

Commit

Permalink
ParmParse::addfile needs Init (AMReX-Codes#3440)
Browse files Browse the repository at this point in the history
## Summary

`ParmParse` can be used before AMReX is initialized for most
functionality, which is greatly helpful for us. One function that cannot
be used before init is reading from a file, because it needs an MPI
context to broadcast.

Add a clean error message instead of a segfault.

## Additional background

cc @EZoni @RevathiJambunathan 

## Checklist

The proposed changes:
- [x] fix a bug or incorrect behavior in AMReX
- [ ] add new capabilities to AMReX
- [ ] changes answers in the test suite to more than roundoff level
- [ ] are likely to significantly affect the results of downstream AMReX
users
- [ ] include documentation in the code and/or rst files, if appropriate
  • Loading branch information
ax3l authored Sep 18, 2023
1 parent 69b019f commit ad2a89a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Src/Base/AMReX_ParmParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <list>
#include <numeric>
#include <regex>
#include <stdexcept>
#include <set>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -538,6 +539,13 @@ read_file (const char* fname, std::list<ParmParse::PP_entry>& tab)
//
if ( fname != nullptr && fname[0] != 0 )
{
#ifdef AMREX_USE_MPI
if (ParallelDescriptor::Communicator() == MPI_COMM_NULL)
{
throw std::runtime_error("read_file: AMReX must be initialized");
}
#endif

Vector<char> fileCharPtr;
std::string filename = fname;
ParallelDescriptor::ReadAndBcastFile(filename, fileCharPtr);
Expand Down Expand Up @@ -1069,6 +1077,13 @@ ParmParse::prefixedName (const std::string& str) const

void
ParmParse::addfile (std::string const& filename) {
#ifdef AMREX_USE_MPI
if (ParallelDescriptor::Communicator() == MPI_COMM_NULL)
{
throw std::runtime_error("ParmParse::addfile: AMReX must be initialized");
}
#endif

auto l = std::list<std::string>{filename};
auto file = FileKeyword;
addDefn(file,
Expand Down

0 comments on commit ad2a89a

Please sign in to comment.