26 Setup(
int argc,
char* argv[]);
38 MPI_Initialized(&mpi_init);
55 using U = std::remove_const_t<T>;
56 if constexpr (std::is_same_v<U, bool>)
60 if constexpr (std::is_same_v<U, char>)
64 if constexpr (std::is_same_v<U, double>)
68 if constexpr (std::is_same_v<U, float>)
72 if constexpr (std::is_same_v<U, int>)
76 if constexpr (std::is_same_v<U, std::size_t>)
78 return MPI_UNSIGNED_LONG;
80 if constexpr (std::is_same_v<U, unsigned int>)
89 std::vector<T> result(mpi.
size);
98static std::vector<T>
allgather(std::vector<T>
const& vector,
Mpi const& mpi)
100 std::size_t
const size = vector.size();
102 std::vector<T> result(mpi.
size * size);
104 MPI_Allgather(vector.data(), size,
mpiType<T>(), result.data(), size,
111static T
allreduce(T
const& value, MPI_Op
const& mpi_op,
Mpi const& mpi)
120static std::vector<T>
allreduce(std::vector<T>
const& vector,
121 MPI_Op
const& mpi_op,
Mpi const& mpi)
123 std::size_t
const size = vector.size();
124 std::vector<T> result(vector.size());
126 MPI_Allreduce(vector.data(), result.data(), size,
mpiType<T>(), mpi_op,
133 MPI_Op
const& mpi_op,
136 MPI_Allreduce(MPI_IN_PLACE,
148 std::span<T>
const send_buffer,
149 std::vector<std::remove_const_t<T>>& receive_buffer,
153 int const size =
static_cast<int>(send_buffer.size());
156 std::vector<int>
const sizes =
allgather(size, mpi);
162 receive_buffer.resize(offsets.back());
164 MPI_Allgatherv(send_buffer.data(), size,
mpiType<T>(),
165 receive_buffer.data(), sizes.data(), offsets.data(),
174static inline bool anyOf(
bool const val
190static inline bool allOf(
bool const val
211template <
typename BaseException>
212 requires std::derived_from<BaseException, std::exception> &&
214 !std::same_as<BaseException, std::exception>)
218 using BaseException::BaseException;
221 : BaseException{
"Another MPI rank threw an exception."}
235template <
typename Exception>
237 auto&& warning_callback)
241 static_assert(!std::is_same_v<Exception, std::exception>);
243 bool const exception_was_thrown =
anyOf(exception !=
nullptr);
245 [[unlikely]]
if (exception_was_thrown)
251 std::rethrow_exception(exception);
253 catch (Exception
const&)
258 catch (std::exception
const& e)
261 "An exception was thrown on this MPI rank, but it's not "
262 "derived from {}, but rather of type {}",
271 "An exception was thrown on this MPI rank, but it's not "
272 "derived from std::exception.");
282template <
typename Exception>
285 auto warning_callback =
286 []<
typename... Args>(fmt::format_string<Args...>
fmt, Args&&... args)
287 {
WARN(
fmt, std::forward<Args>(args)...); };
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
static void allreduceInplace(std::vector< T > &vector, MPI_Op const &mpi_op, Mpi const &mpi)
static bool anyOf(bool const val, Mpi const &mpi=Mpi{OGS_COMM_WORLD})
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 allOf(bool const val, Mpi const &mpi=Mpi{OGS_COMM_WORLD})
static T allreduce(T const &value, MPI_Op const &mpi_op, Mpi const &mpi)
void allRanksThrowOrNone(std::exception_ptr const &exception, auto &&warning_callback)
constexpr MPI_Datatype mpiType()
static std::vector< T > allgather(T const &value, Mpi const &mpi)
std::string demangle(const char *mangled_name)
std::string typeToString()
std::vector< ranges::range_value_t< R > > sizesToOffsets(R const &sizes)
Mpi(MPI_Comm const communicator=OGS_COMM_WORLD)
Setup(int argc, char *argv[])