Skip to content

Commit

Permalink
ParmParse:addFile: User-Friendly Error
Browse files Browse the repository at this point in the history
If a file added via `ParmParse:addFile` does not exist, we did not
yet receive a user-friendly error message. This fixes this in a way
that does not hammer the file system from all MPI ranks.
  • Loading branch information
ax3l committed Sep 17, 2024
1 parent 73716d9 commit 41d0284
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Src/Base/AMReX_ParmParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
Expand Down Expand Up @@ -1075,6 +1076,24 @@ ParmParse::addfile (std::string const& filename) {
}
#endif

// check the file exists
int file_exists = int(false);
if (ParallelDescriptor::IOProcessor())
{
if (std::FILE *fp = std::fopen(filename.c_str(), "r")) {
fclose(fp);
file_exists = int(true);
}
}
amrex::ParallelDescriptor::Bcast(
&file_exists,
1,
amrex::ParallelDescriptor::IOProcessorNumber()
);
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(bool(file_exists),
"ParmParse::addfile: file does not exist: " + filename);

// add the file
auto file = FileKeyword;
std::vector<std::string> val{{filename}};
addDefn(file, val, g_table);
Expand Down

0 comments on commit 41d0284

Please sign in to comment.