Skip to content

Commit

Permalink
Avoid MPI mocking, use ifdefs
Browse files Browse the repository at this point in the history
Also use parallel logical or for file existence check
  • Loading branch information
franzpoeschel committed Aug 17, 2022
1 parent 4b56d1b commit 2bc1ac4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 77 deletions.
4 changes: 3 additions & 1 deletion include/openPMD/IO/ADIOS/ADIOS2IOHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ class ADIOS2IOHandlerImpl

private:
adios2::ADIOS m_ADIOS;
std::optional<auxiliary::Mock_MPI_Comm> m_communicator;
#if openPMD_HAVE_MPI
std::optional<MPI_Comm> m_communicator;
#endif
/*
* If the iteration encoding is variableBased, we default to using the
* 2021_02_09 schema since it allows mutable attributes.
Expand Down
8 changes: 7 additions & 1 deletion include/openPMD/IO/HDF5/HDF5IOHandlerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ class HDF5IOHandlerImpl : public AbstractIOHandlerImpl
hid_t m_H5T_CLONG_DOUBLE;

protected:
std::optional<auxiliary::Mock_MPI_Comm> m_mockedMpiComm;
#if openPMD_HAVE_MPI
/*
* Not defined in ParallelHDF5IOHandlerImpl, so we don't have to write
* some methods twice.
*/
std::optional<MPI_Comm> m_communicator;
#endif

private:
json::TracingJSON m_config;
Expand Down
62 changes: 0 additions & 62 deletions include/openPMD/auxiliary/Mpi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,67 +79,5 @@ namespace
[[maybe_unused]] MPI_Datatype const MPI_Types<char>::value = MPI_CHAR;
} // namespace

template <typename Functor>
void runOnRankZero(Mock_MPI_Comm comm, Functor &&functor)
{
int rank;
int status = MPI_Comm_rank(comm, &rank);
if (status != 0)
{
throw std::runtime_error("Can't inquire MPI rank!");
}
if (rank == 0)
{
functor();
}
}

template <typename T>
void MPI_Bcast_fromRankZero(Mock_MPI_Comm comm, T *value)
{
int status = MPI_Bcast(value, 1, MPI_Types<T>::value, 0, comm);
if (status != 0)
{
throw std::runtime_error("MPI_Bcast failed!");
}
}

#else

struct Mock_MPI_Comm
{};

template <typename Functor>
void runOnRankZero(Mock_MPI_Comm, Functor &&functor)
{
functor();
}

template <typename T>
void MPI_Bcast_fromRankZero(Mock_MPI_Comm, T *)
{}

#endif

template <typename Functor>
void runOnRankZero(std::optional<Mock_MPI_Comm> comm, Functor &&functor)
{
if (comm.has_value())
{
runOnRankZero(comm.value(), std::forward<Functor>(functor));
}
else
{
functor();
}
}

template <typename T>
void MPI_Bcast_fromRankZero(std::optional<Mock_MPI_Comm> comm, T *value)
{
if (comm.has_value())
{
MPI_Bcast_fromRankZero(comm.value(), value);
}
}
} // namespace openPMD::auxiliary
26 changes: 20 additions & 6 deletions src/IO/ADIOS/ADIOS2IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,26 @@ bool ADIOS2IOHandlerImpl::checkFile(std::string fullFilePath) const
*/
fullFilePath += ".sst";
}
char fileExists = false;
auxiliary::runOnRankZero(m_communicator, [&fileExists, &fullFilePath]() {
fileExists = auxiliary::file_exists(fullFilePath) ||
auxiliary::directory_exists(fullFilePath);
});
auxiliary::MPI_Bcast_fromRankZero(m_communicator, &fileExists);
bool fileExists = auxiliary::directory_exists(fullFilePath) ||
auxiliary::file_exists(fullFilePath);

#if openPMD_HAVE_MPI
if (m_communicator.has_value())
{
int status = MPI_Allreduce(
&fileExists,
&fileExists,
1,
MPI_C_BOOL,
MPI_LOR, // logical or
m_communicator.value());
if (status != 0)
{
throw std::runtime_error("MPI Reduction failed!");
}
}
#endif

return fileExists;
}

Expand Down
26 changes: 20 additions & 6 deletions src/IO/HDF5/HDF5IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,26 @@ void HDF5IOHandlerImpl::checkFile(
{
name += ".h5";
}
char fileExists = false;
auxiliary::runOnRankZero(m_mockedMpiComm, [&fileExists, &name]() {
fileExists =
auxiliary::file_exists(name) || auxiliary::directory_exists(name);
});
auxiliary::MPI_Bcast_fromRankZero(m_mockedMpiComm, &fileExists);
bool fileExists =
auxiliary::file_exists(name) || auxiliary::directory_exists(name);

#if openPMD_HAVE_MPI
if (m_communicator.has_value())
{
int status = MPI_Allreduce(
&fileExists,
&fileExists,
1,
MPI_C_BOOL,
MPI_LOR, // logical or
m_communicator.value());
if (status != 0)
{
throw std::runtime_error("MPI Reduction failed!");
}
}
#endif

using FileExists = Parameter<Operation::CHECK_FILE>::FileExists;
*parameters.fileExists = fileExists ? FileExists::Yes : FileExists::No;
}
Expand Down
2 changes: 1 addition & 1 deletion src/IO/HDF5/ParallelHDF5IOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ParallelHDF5IOHandlerImpl::ParallelHDF5IOHandlerImpl(
{
// Set this so the parent class can use the MPI communicator in functions
// that are written with special implemenations for MPI-enabled HDF5.
m_mockedMpiComm = m_mpiComm;
m_communicator = m_mpiComm;
m_datasetTransferProperty = H5Pcreate(H5P_DATASET_XFER);
m_fileAccessProperty = H5Pcreate(H5P_FILE_ACCESS);
m_fileCreateProperty = H5Pcreate(H5P_FILE_CREATE);
Expand Down

0 comments on commit 2bc1ac4

Please sign in to comment.