In this example, we show how to use mpi::mpi_type_from_tie, mpi::map_C_function and mpi::map_add to register a new MPI datatype and to define MPI operations for it.
#include <iostream>
struct my_complex {
double real;
double imag;
};
inline my_complex operator+(const my_complex& z1, const my_complex& z2) {
return { z1.real + z2.real, 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 = { world.
rank() + 1.0,
static_cast<double>(world.
rank()) };
auto my_product = [](const my_complex& z1, const my_complex& z2) {
return my_complex { z1.real * z2.real - z1.imag * z2.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.
Create an MPI_Datatype from some struct.
Map C++ datatypes to the corresponding MPI datatypes.
Output (running with -n 5
):
sum = (15, 10)
product = (-185, 180)