In this example, we show
- how to register a new MPI datatype by providing a tie_data function for our C++ type and
- how to use mpi::map_C_function and mpi::map_add to define MPI operations for it.
#include <iostream>
struct my_complex {
double real;
double imag;
};
inline auto operator+(const my_complex &z1, const my_complex &z2) { return my_complex{.real = z1.real + z2.real, .imag = z1.imag + z2.imag}; }
inline auto tie_data(const my_complex &z) { return std::tie(z.real, z.imag); }
int main(int argc, char *argv[]) {
my_complex z = {.real = world.
rank() + 1.0, .imag =
static_cast<double>(world.
rank())};
auto my_product = [](const my_complex &z1, const my_complex &z2) {
return my_complex{.real = z1.real * z2.real - z1.imag * z2.imag, .imag = z1.real * z2.imag + z1.imag * z2.real};
};
std::cout << "sum = (" << sum.real << ", " << sum.imag << ")\n";
std::cout << "product = (" << product.real << ", " << product.imag << ")\n";
}
}
C++ wrapper around MPI_Comm providing various convenience functions.
int rank() const
Get the rank of the calling process in the communicator.
decltype(auto) reduce(T &&x, communicator c={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Generic MPI reduce.
MPI_Op map_add()
Create a new MPI_Op for a generic addition by calling MPI_Op_create.
MPI_Op map_C_function()
Create a new MPI_Op from a given binary function by calling MPI_Op_create.
Includes all relevant mpi headers.
RAII class to initialize and finalize MPI.
Output (running with -n 5):
sum = (15, 10)
product = (-185, 180)