Skip to content

Commit ac5504f

Browse files
committed
Avoid MPI mocking, use ifdefs
Also use parallel logical or for file existence check
1 parent 77215b9 commit ac5504f

File tree

6 files changed

+51
-77
lines changed

6 files changed

+51
-77
lines changed

include/openPMD/IO/ADIOS/ADIOS2IOHandler.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ class ADIOS2IOHandlerImpl
232232

233233
private:
234234
adios2::ADIOS m_ADIOS;
235-
std::optional<auxiliary::Mock_MPI_Comm> m_communicator;
235+
#if openPMD_HAVE_MPI
236+
std::optional<MPI_Comm> m_communicator;
237+
#endif
236238
/*
237239
* If the iteration encoding is variableBased, we default to using the
238240
* 2021_02_09 schema since it allows mutable attributes.

include/openPMD/IO/HDF5/HDF5IOHandlerImpl.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ class HDF5IOHandlerImpl : public AbstractIOHandlerImpl
9595
hid_t m_H5T_CLONG_DOUBLE;
9696

9797
protected:
98-
std::optional<auxiliary::Mock_MPI_Comm> m_mockedMpiComm;
98+
#if openPMD_HAVE_MPI
99+
/*
100+
* Not defined in ParallelHDF5IOHandlerImpl, so we don't have to write
101+
* some methods twice.
102+
*/
103+
std::optional<MPI_Comm> m_communicator;
104+
#endif
99105

100106
private:
101107
json::TracingJSON m_config;

include/openPMD/auxiliary/Mpi.hpp

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -79,67 +79,5 @@ namespace
7979
[[maybe_unused]] MPI_Datatype const MPI_Types<char>::value = MPI_CHAR;
8080
} // namespace
8181

82-
template <typename Functor>
83-
void runOnRankZero(Mock_MPI_Comm comm, Functor &&functor)
84-
{
85-
int rank;
86-
int status = MPI_Comm_rank(comm, &rank);
87-
if (status != 0)
88-
{
89-
throw std::runtime_error("Can't inquire MPI rank!");
90-
}
91-
if (rank == 0)
92-
{
93-
functor();
94-
}
95-
}
96-
97-
template <typename T>
98-
void MPI_Bcast_fromRankZero(Mock_MPI_Comm comm, T *value)
99-
{
100-
int status = MPI_Bcast(value, 1, MPI_Types<T>::value, 0, comm);
101-
if (status != 0)
102-
{
103-
throw std::runtime_error("MPI_Bcast failed!");
104-
}
105-
}
106-
107-
#else
108-
109-
struct Mock_MPI_Comm
110-
{};
111-
112-
template <typename Functor>
113-
void runOnRankZero(Mock_MPI_Comm, Functor &&functor)
114-
{
115-
functor();
116-
}
117-
118-
template <typename T>
119-
void MPI_Bcast_fromRankZero(Mock_MPI_Comm, T *)
120-
{}
121-
12282
#endif
123-
124-
template <typename Functor>
125-
void runOnRankZero(std::optional<Mock_MPI_Comm> comm, Functor &&functor)
126-
{
127-
if (comm.has_value())
128-
{
129-
runOnRankZero(comm.value(), std::forward<Functor>(functor));
130-
}
131-
else
132-
{
133-
functor();
134-
}
135-
}
136-
137-
template <typename T>
138-
void MPI_Bcast_fromRankZero(std::optional<Mock_MPI_Comm> comm, T *value)
139-
{
140-
if (comm.has_value())
141-
{
142-
MPI_Bcast_fromRankZero(comm.value(), value);
143-
}
144-
}
14583
} // namespace openPMD::auxiliary

src/IO/ADIOS/ADIOS2IOHandler.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,26 @@ bool ADIOS2IOHandlerImpl::checkFile(std::string fullFilePath) const
531531
*/
532532
fullFilePath += ".sst";
533533
}
534-
char fileExists = false;
535-
auxiliary::runOnRankZero(m_communicator, [&fileExists, &fullFilePath]() {
536-
fileExists = auxiliary::file_exists(fullFilePath) ||
537-
auxiliary::directory_exists(fullFilePath);
538-
});
539-
auxiliary::MPI_Bcast_fromRankZero(m_communicator, &fileExists);
534+
bool fileExists = auxiliary::directory_exists(fullFilePath) ||
535+
auxiliary::file_exists(fullFilePath);
536+
537+
#if openPMD_HAVE_MPI
538+
if (m_communicator.has_value())
539+
{
540+
int status = MPI_Allreduce(
541+
&fileExists,
542+
&fileExists,
543+
1,
544+
MPI_C_BOOL,
545+
MPI_LOR, // logical or
546+
m_communicator.value());
547+
if (status != 0)
548+
{
549+
throw std::runtime_error("MPI Reduction failed!");
550+
}
551+
}
552+
#endif
553+
540554
return fileExists;
541555
}
542556

src/IO/HDF5/HDF5IOHandler.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,26 @@ void HDF5IOHandlerImpl::checkFile(
293293
{
294294
name += ".h5";
295295
}
296-
char fileExists = false;
297-
auxiliary::runOnRankZero(m_mockedMpiComm, [&fileExists, &name]() {
298-
fileExists =
299-
auxiliary::file_exists(name) || auxiliary::directory_exists(name);
300-
});
301-
auxiliary::MPI_Bcast_fromRankZero(m_mockedMpiComm, &fileExists);
296+
bool fileExists =
297+
auxiliary::file_exists(name) || auxiliary::directory_exists(name);
298+
299+
#if openPMD_HAVE_MPI
300+
if (m_communicator.has_value())
301+
{
302+
int status = MPI_Allreduce(
303+
&fileExists,
304+
&fileExists,
305+
1,
306+
MPI_C_BOOL,
307+
MPI_LOR, // logical or
308+
m_communicator.value());
309+
if (status != 0)
310+
{
311+
throw std::runtime_error("MPI Reduction failed!");
312+
}
313+
}
314+
#endif
315+
302316
using FileExists = Parameter<Operation::CHECK_FILE>::FileExists;
303317
*parameters.fileExists = fileExists ? FileExists::Yes : FileExists::No;
304318
}

src/IO/HDF5/ParallelHDF5IOHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ ParallelHDF5IOHandlerImpl::ParallelHDF5IOHandlerImpl(
6767
{
6868
// Set this so the parent class can use the MPI communicator in functions
6969
// that are written with special implemenations for MPI-enabled HDF5.
70-
m_mockedMpiComm = m_mpiComm;
70+
m_communicator = m_mpiComm;
7171
m_datasetTransferProperty = H5Pcreate(H5P_DATASET_XFER);
7272
m_fileAccessProperty = H5Pcreate(H5P_FILE_ACCESS);
7373
m_fileCreateProperty = H5Pcreate(H5P_FILE_CREATE);

0 commit comments

Comments
 (0)