From 8f769890f5a33c60f2f821efd2a4d334efbab614 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Tue, 17 Sep 2024 14:02:22 -0700 Subject: [PATCH] `ParmParse:addFile`: User-Friendly Error 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. --- Src/Base/AMReX_ParmParse.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Src/Base/AMReX_ParmParse.cpp b/Src/Base/AMReX_ParmParse.cpp index 9d61fad89f..7d70533519 100644 --- a/Src/Base/AMReX_ParmParse.cpp +++ b/Src/Base/AMReX_ParmParse.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -1075,6 +1076,24 @@ ParmParse::addfile (std::string const& filename) { } #endif + // check the file exists + bool file_exists = false; + if (ParallelDescriptor::IOProcessor()) + { + if (std::FILE *fp = std::fopen(filename.c_str(), "r")) { + fclose(fp); + file_exists = true; + } + } + amrex::ParallelDescriptor::Bcast( + &file_exists, + 1, + amrex::ParallelDescriptor::IOProcessorNumber() + ); + AMREX_ALWAYS_ASSERT_WITH_MESSAGE(file_exists, + "ParmParse::addfile: file does not exist: " + filename); + + // add the file auto file = FileKeyword; std::vector val{{filename}}; addDefn(file, val, g_table);