47 template <
typename T>
inline constexpr bool is_std_vector =
false;
50 template <
typename T>
inline constexpr bool is_std_vector<std::vector<T>> =
true;
53 template <
typename T,
typename V> T convert(V v) {
54 if constexpr (is_std_vector<T>) {
56 res.reserve(v.size());
57 for (
auto &x : v) res.emplace_back(convert<typename T::value_type>(std::move(x)));
60 return T{std::move(v)};
77 static_assert(not std::is_const_v<T>,
"mpi::broadcast cannot be called on const objects");
97 [[gnu::always_inline]]
inline decltype(
auto)
reduce(T &&x,
communicator c = {},
int root = 0,
bool all =
false, MPI_Op op = MPI_SUM) {
99 using r_t =
decltype(
mpi_reduce(std::forward<T>(x), c, root, all, op));
101 return mpi_reduce(std::forward<T>(x), c, root, all, op);
104 return mpi_reduce(std::forward<T>(x), c, root, all, op);
106 return detail::convert<r_t>(std::forward<T>(x));
123 template <
typename T>
125 static_assert(not std::is_const_v<T>,
"In-place mpi functions cannot be called on const objects");
144 using r_t =
decltype(
mpi_scatter(std::forward<T>(x), c, root));
151 return detail::convert<r_t>(std::forward<T>(x));
169 template <
typename T> [[gnu::always_inline]]
inline decltype(
auto)
gather(T &&x,
mpi::communicator c = {},
int root = 0,
bool all =
false) {
171 using r_t =
decltype(
mpi_gather(std::forward<T>(x), c, root, all));
173 return mpi_gather(std::forward<T>(x), c, root, all);
176 return mpi_gather(std::forward<T>(x), c, root, all);
178 return detail::convert<r_t>(std::forward<T>(x));
186 template <
typename T> [[gnu::always_inline]]
inline decltype(
auto)
all_reduce(T &&x,
communicator c = {}, MPI_Op op = MPI_SUM) {
187 return reduce(std::forward<T>(x), c, 0,
true, op);
203 return gather(std::forward<T>(x), c, 0,
true);
217 template <
typename T>
220 check_mpi_call(MPI_Bcast(&x, 1, mpi_type<T>::get(), root, c.get()),
"MPI_Bcast");
237 template <
typename T>
241 auto d = mpi_type<T>::get();
244 check_mpi_call(MPI_Reduce(
const_cast<T *
>(&x), &b, 1, d, op, root, c.get()),
"MPI_Reduce");
246 check_mpi_call(MPI_Allreduce(
const_cast<T *
>(&x), &b, 1, d, op, c.get()),
"MPI_Allreduce");
263 template <
typename T>
267 check_mpi_call(MPI_Reduce((c.rank() == root ? MPI_IN_PLACE : &x), &x, 1, mpi_type<T>::get(), op, root, c.get()),
"MPI_Reduce");
269 check_mpi_call(MPI_Allreduce(MPI_IN_PLACE, &x, 1, mpi_type<T>::get(), op, c.get()),
"MPI_Allreduce");
291 return min_obj == max_obj;
C++ wrapper around MPI_Comm providing various convenience functions.
Provides utilities to map C++ datatypes to MPI datatypes.
decltype(auto) scatter(T &&x, mpi::communicator c={}, int root=0)
Generic MPI scatter.
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 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.
auto mpi_scatter(std::vector< T > const &v, communicator c={}, int root=0)
Implementation of an MPI scatter for a std::vector.
void mpi_broadcast(std::array< T, N > &arr, communicator c={}, int root=0)
Implementation of an MPI broadcast for a std::arr.
bool all_equal(T const &x, communicator c={})
Checks if a given object is equal across all ranks in the given communicator.
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.
std::string mpi_gather(std::string const &s, communicator c={}, int root=0, bool all=false)
Implementation of an MPI gather for a std::string.
decltype(auto) gather(T &&x, mpi::communicator c={}, int root=0, bool all=false)
Generic MPI gather.
decltype(auto) all_gather(T &&x, communicator c={})
Generic MPI all-gather.
void mpi_reduce_in_place(std::array< T, N > &arr, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an in-place MPI reduce for a std::array.
static const bool has_env
Boolean variable that is true, if one of the environment variables OMPI_COMM_WORLD_RANK,...
constexpr bool is_mpi_lazy
Type trait to check if a type is mpi::lazy.
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.
Provides a struct and tags to represent lazy MPI communication.
Provides general utilities related to MPI.