OGS
BaseLib::MPI Namespace Reference

Classes

class  AnotherMPIRankThrew
struct  Mpi
struct  Setup

Functions

template<typename T>
constexpr MPI_Datatype mpiType ()
template<typename T>
static std::vector< T > allgather (T const &value, Mpi const &mpi)
template<typename T>
static std::vector< T > allgather (std::vector< T > const &vector, Mpi const &mpi)
template<typename T>
static T allreduce (T const &value, MPI_Op const &mpi_op, Mpi const &mpi)
template<typename T>
static std::vector< T > allreduce (std::vector< T > const &vector, MPI_Op const &mpi_op, Mpi const &mpi)
template<typename T>
static void allreduceInplace (std::vector< T > &vector, MPI_Op const &mpi_op, Mpi const &mpi)
template<typename T>
static std::vector< int > allgatherv (std::span< T > const send_buffer, std::vector< std::remove_const_t< T > > &receive_buffer, Mpi const &mpi)
static bool anyOf (bool const val, Mpi const &mpi=Mpi{OGS_COMM_WORLD})
static bool allOf (bool const val, Mpi const &mpi=Mpi{OGS_COMM_WORLD})
template<typename Exception>
void allRanksThrowOrNone (std::exception_ptr const &exception, auto &&warning_callback)
template<typename Exception>
void allRanksThrowOrNone (std::exception_ptr const &exception)
 Same as above but using the standard WARN() function for warnings.

Variables

MPI_Comm OGS_COMM_WORLD = MPI_COMM_WORLD

Function Documentation

◆ allgather() [1/2]

template<typename T>
std::vector< T > BaseLib::MPI::allgather ( std::vector< T > const & vector,
Mpi const & mpi )
static

Definition at line 98 of file MPI.h.

99{
100 std::size_t const size = vector.size();
101 // Flat in memory over all ranks;
102 std::vector<T> result(mpi.size * size);
103
104 MPI_Allgather(vector.data(), size, mpiType<T>(), result.data(), size,
105 mpiType<T>(), mpi.communicator);
106
107 return result;
108}
constexpr MPI_Datatype mpiType()
Definition MPI.h:53

References BaseLib::MPI::Mpi::communicator, mpiType(), and BaseLib::MPI::Mpi::size.

◆ allgather() [2/2]

template<typename T>
std::vector< T > BaseLib::MPI::allgather ( T const & value,
Mpi const & mpi )
static

◆ allgatherv()

template<typename T>
std::vector< int > BaseLib::MPI::allgatherv ( std::span< T > const send_buffer,
std::vector< std::remove_const_t< T > > & receive_buffer,
Mpi const & mpi )
static

Allgather for variable data. Returns offsets in the receive buffer. The receive buffer is resized to accommodate the gathered data.

Definition at line 147 of file MPI.h.

151{
152 // Determine the number of elements to send
153 int const size = static_cast<int>(send_buffer.size());
154
155 // Gather sizes from all ranks
156 std::vector<int> const sizes = allgather(size, mpi);
157
158 // Compute offsets based on counts
159 std::vector<int> const offsets = BaseLib::sizesToOffsets(sizes);
160
161 // Resize receive buffer to hold all gathered data
162 receive_buffer.resize(offsets.back());
163
164 MPI_Allgatherv(send_buffer.data(), size, mpiType<T>(),
165 receive_buffer.data(), sizes.data(), offsets.data(),
166 mpiType<T>(), mpi.communicator);
167
168 return offsets;
169}
static std::vector< T > allgather(T const &value, Mpi const &mpi)
Definition MPI.h:87
std::vector< ranges::range_value_t< R > > sizesToOffsets(R const &sizes)
Definition Algorithm.h:276

References allgather(), BaseLib::MPI::Mpi::communicator, mpiType(), and BaseLib::sizesToOffsets().

Referenced by MeshLib::computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition(), and MathLib::PETScVector::getGlobalVector().

◆ allOf()

bool BaseLib::MPI::allOf ( bool const val,
Mpi const & mpi = Mpi{OGS_COMM_WORLD} )
inlinestatic

The reduction is implemented transparently for with and without MPI. In the latter case the input value is returned.

Definition at line 190 of file MPI.h.

194#endif
195)
196{
197 return !anyOf(!val
198#ifdef USE_PETSC
199 ,
200 mpi
201#endif
202 );
203}
static bool anyOf(bool const val, Mpi const &mpi=Mpi{OGS_COMM_WORLD})
Definition MPI.h:174
MPI_Comm OGS_COMM_WORLD
Definition MPI.cpp:9

References OGS_COMM_WORLD.

◆ allRanksThrowOrNone() [1/2]

template<typename Exception>
void BaseLib::MPI::allRanksThrowOrNone ( std::exception_ptr const & exception)

Same as above but using the standard WARN() function for warnings.

Definition at line 283 of file MPI.h.

284{
285 auto warning_callback =
286 []<typename... Args>(fmt::format_string<Args...> fmt, Args&&... args)
287 { WARN(fmt, std::forward<Args>(args)...); };
288
289 allRanksThrowOrNone<Exception>(exception, warning_callback);
290}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
void allRanksThrowOrNone(std::exception_ptr const &exception, auto &&warning_callback)
Definition MPI.h:236
Definition AABB.h:277

References allRanksThrowOrNone(), and WARN().

◆ allRanksThrowOrNone() [2/2]

template<typename Exception>
void BaseLib::MPI::allRanksThrowOrNone ( std::exception_ptr const & exception,
auto && warning_callback )

This function throws if and only if the passed exception is non-null on any MPI rank.

The thrown exception is:

This function also checks if the thrown exception is derived from Exception.

Definition at line 236 of file MPI.h.

238{
239 // std::exception would lead to duplicate catch clauses below and would not
240 // work together with the current implementation of AnotherMPIRankThrew.
241 static_assert(!std::is_same_v<Exception, std::exception>);
242
243 bool const exception_was_thrown = anyOf(exception != nullptr);
244
245 [[unlikely]] if (exception_was_thrown)
246 {
247 if (exception)
248 {
249 try
250 {
251 std::rethrow_exception(exception);
252 }
253 catch (Exception const&)
254 {
255 // OK. Argument exception is derived from class Exception.
256 throw;
257 }
258 catch (std::exception const& e)
259 {
260 warning_callback(
261 "An exception was thrown on this MPI rank, but it's not "
262 "derived from {}, but rather of type {}",
265 typeid(e).name() /* demangle the runtime type of e */));
266 throw;
267 }
268 catch (...)
269 {
270 warning_callback(
271 "An exception was thrown on this MPI rank, but it's not "
272 "derived from std::exception.");
273 throw;
274 }
275 }
276
278 }
279}
std::string demangle(const char *mangled_name)
std::string typeToString()

References anyOf(), BaseLib::demangle(), and BaseLib::typeToString().

Referenced by allRanksThrowOrNone(), ProcessLib::AssemblyMixin< Process >::assemble(), and ProcessLib::AssemblyMixin< Process >::assembleWithJacobian().

◆ allreduce() [1/2]

template<typename T>
std::vector< T > BaseLib::MPI::allreduce ( std::vector< T > const & vector,
MPI_Op const & mpi_op,
Mpi const & mpi )
static

Definition at line 120 of file MPI.h.

122{
123 std::size_t const size = vector.size();
124 std::vector<T> result(vector.size());
125
126 MPI_Allreduce(vector.data(), result.data(), size, mpiType<T>(), mpi_op,
127 mpi.communicator);
128 return result;
129}

References BaseLib::MPI::Mpi::communicator, and mpiType().

◆ allreduce() [2/2]

◆ allreduceInplace()

template<typename T>
void BaseLib::MPI::allreduceInplace ( std::vector< T > & vector,
MPI_Op const & mpi_op,
Mpi const & mpi )
static

Definition at line 132 of file MPI.h.

135{
136 MPI_Allreduce(MPI_IN_PLACE,
137 vector.data(),
138 vector.size(),
139 mpiType<T>(),
140 mpi_op,
141 mpi.communicator);
142}

References BaseLib::MPI::Mpi::communicator, and mpiType().

Referenced by MeshLib::computeGhostBaseNodeGlobalNodeIDsOfSubDomainPartition().

◆ anyOf()

bool BaseLib::MPI::anyOf ( bool const val,
Mpi const & mpi = Mpi{OGS_COMM_WORLD} )
inlinestatic

The reduction is implemented transparently for with and without MPI. In the latter case the input value is returned.

Definition at line 174 of file MPI.h.

178#endif
179)
180{
181#ifdef USE_PETSC
182 return allreduce(val, MPI_LOR, mpi);
183#else
184 return val;
185#endif
186}
static T allreduce(T const &value, MPI_Op const &mpi_op, Mpi const &mpi)
Definition MPI.h:111

References OGS_COMM_WORLD.

Referenced by allRanksThrowOrNone(), and NumLib::NonlinearSolver< NonlinearSolverTag::Newton >::solve().

◆ mpiType()

template<typename T>
MPI_Datatype BaseLib::MPI::mpiType ( )
constexpr

Definition at line 53 of file MPI.h.

54{
55 using U = std::remove_const_t<T>;
56 if constexpr (std::is_same_v<U, bool>)
57 {
58 return MPI_C_BOOL;
59 }
60 if constexpr (std::is_same_v<U, char>)
61 {
62 return MPI_CHAR;
63 }
64 if constexpr (std::is_same_v<U, double>)
65 {
66 return MPI_DOUBLE;
67 }
68 if constexpr (std::is_same_v<U, float>)
69 {
70 return MPI_FLOAT;
71 }
72 if constexpr (std::is_same_v<U, int>)
73 {
74 return MPI_INT;
75 }
76 if constexpr (std::is_same_v<U, std::size_t>)
77 {
78 return MPI_UNSIGNED_LONG;
79 }
80 if constexpr (std::is_same_v<U, unsigned int>)
81 {
82 return MPI_UNSIGNED;
83 }
84}

Referenced by allgather(), allgather(), allgatherv(), allreduce(), allreduce(), and allreduceInplace().

Variable Documentation

◆ OGS_COMM_WORLD