90 [[gnu::always_inline]]
decltype(
auto)
reduce(T &&x,
communicator c = {},
int root = 0,
bool all =
false,
91 MPI_Op op = MPI_SUM) {
92 if constexpr (
requires {
mpi_reduce(x, c, root, all, op); }) {
95 std::remove_cvref_t<T> res;
116 template <
typename T>
118 MPI_Op op = MPI_SUM) {
139 template <
typename T1,
typename T2>
141 bool all =
false, MPI_Op op = MPI_SUM) {
160 template <
typename T>
162 if constexpr (
requires { mpi_scatter(x, c, root); }) {
163 return mpi_scatter(x, c, root);
165 std::remove_cvref_t<T> res;
186 template <
typename T1,
typename T2>
207 template <
typename T>
208 [[gnu::always_inline]]
decltype(
auto)
gather(T &&x,
communicator c = {},
int root = 0,
bool all =
false) {
209 if constexpr (
requires {
mpi_gather(x, c, root, all); }) {
212 std::remove_cvref_t<T> res;
234 template <
typename T1,
typename T2>
244 template <
typename T>
246 return reduce(x, c, 0,
true, op);
253 template <
typename T>
262 template <
typename T1,
typename T2>
272 return gather(x, c, 0,
true);
279 template <
typename T1,
typename T2>
298 if (!
has_env || c.size() < 2)
return true;
301 return min_obj == max_obj;
317 template <
typename T>
321 if (!
has_env || c.size() < 2)
return;
324 check_mpi_call(MPI_Bcast(&x, 1, mpi_type<T>::get(), root, c.get()),
"MPI_Bcast");
343 template <
typename T>
347 if (!
has_env || c.size() < 2)
return x;
352 check_mpi_call(MPI_Allreduce(&x, &res, 1, mpi_type<T>::get(), op, c.get()),
"MPI_Allreduce");
379 template <
typename T>
383 auto in_ptr =
static_cast<void const *
>(&x_in);
384 auto out_ptr =
static_cast<void *
>(&x_out);
385 bool const in_place = (in_ptr == out_ptr);
387 EXPECTS_WITH_MESSAGE(
all_equal(
static_cast<int>(in_place), c),
388 "Either zero or all receiving processes have to choose the in place option in mpi_reduce_into");
392 if (!
has_env || c.size() < 2) {
393 if (!in_place) x_out = x_in;
398 if (in_place && (c.rank() == root || all)) in_ptr = MPI_IN_PLACE;
419 template <
typename T>
422 std::vector<T> res(c.rank() == root || all ? c.size() : 0);
445 template <
typename T, MPICompatibleRange R>
446 requires(
has_mpi_type<T> && std::same_as<T, std::remove_cvref_t<std::ranges::range_value_t<R>>>)
449 if (c.rank() == root || all) {
450 EXPECTS_WITH_MESSAGE(c.size() == std::ranges::size(rg),
"Output range size is not equal the number of ranks in mpi_gather_into");
454 if (!
has_env || c.size() < 2) {
455 std::ranges::copy(std::views::single(x), std::ranges::begin(rg));
460 using value_t = std::ranges::range_value_t<R>;
C++ wrapper around MPI_Comm providing various convenience functions.
Provides a C++ wrapper class for an MPI_Comm object.
A concept that checks if a range type is contiguous and sized and has an MPI compatible value type.
Provides utilities to map C++ datatypes to MPI datatypes.
void scatter_into(T1 &&x_in, T2 &&x_out, communicator c={}, int root=0)
Generic MPI scatter that scatters directly into an existing output object.
decltype(auto) scatter(T &&x, mpi::communicator c={}, int root=0)
Generic MPI scatter.
void gather_into(T1 &&x_in, T2 &&x_out, communicator c={}, int root=0, bool all=false)
Generic MPI gather that gathers directly into an existing output object.
void all_gather_into(T1 &&x_in, T2 &&x_out, communicator c={})
Generic MPI all-gather that gathers directly into an existing output object.
auto mpi_reduce(std::array< T, N > const &arr, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an MPI reduce for a std::array.
void mpi_reduce_into(std::array< T1, N1 > const &arr_in, std::array< T2, N2 > &arr_out, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an MPI reduce for a std::array that reduces directly into an existing output array.
void mpi_scatter_into(std::vector< T > const &v_in, std::vector< T > &v_out, communicator c={}, int root=0)
Implementation of an MPI scatter for a std::vector that scatters directly into an existing output vec...
void mpi_gather_into(T const &x, R &&rg, communicator c={}, int root=0, bool all=false)
Implementation of an MPI gather that gathers directly into an existing output range for types that ha...
void reduce_into(T1 &&x_in, T2 &&x_out, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Generic MPI reduce that reduces directly into an existing output object.
void reduce_in_place(T &&x, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Generic in place MPI reduce.
void all_reduce_in_place(T &&x, communicator c={}, MPI_Op op=MPI_SUM)
Generic MPI all-reduce in place.
decltype(auto) reduce(T &&x, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Generic MPI reduce.
void mpi_broadcast(std::array< T, N > &arr, communicator c={}, int root=0)
Implementation of an MPI broadcast for a std::array.
bool all_equal(T const &x, communicator c={})
Checks if a given object is equal across all ranks in the given communicator.
std::vector< T > mpi_gather(T const &x, communicator c={}, int root=0, bool all=false)
Implementation of an MPI gather for types that have a corresponding MPI datatype.
decltype(auto) all_reduce(T &&x, communicator c={}, MPI_Op op=MPI_SUM)
Generic MPI all-reduce.
void broadcast(T &&x, communicator c={}, int root=0)
Generic MPI broadcast.
void all_reduce_into(T1 &&x_in, T2 &&x_out, communicator c={}, MPI_Op op=MPI_SUM)
Generic MPI all-reduce that reduces directly into an existing output object.
decltype(auto) gather(T &&x, communicator c={}, int root=0, bool all=false)
Generic MPI gather.
decltype(auto) all_gather(T &&x, communicator c={})
Generic MPI all-gather.
static const bool has_env
Boolean variable that is true, if one of the environment variables OMPI_COMM_WORLD_RANK,...
constexpr bool has_mpi_type
Type trait to check if a type T has a corresponding MPI datatype, i.e. if mpi::mpi_type has been spec...
void check_mpi_call(int errcode, const std::string &mpi_routine)
Check the success of an MPI call.
Macros used in the mpi library.
Map C++ datatypes to the corresponding MPI datatypes.
Provides general utilities related to MPI.