13 MPI_Comm_rank( MPI_COMM_WORLD, &
rank );
26 MPI_Barrier(MPI_COMM_WORLD);
39 static void initialize(
int* argc,
char*** argv ) { instance( argc, argv ); }
45 MPI_Initialized( &flag );
53 MPI_Finalized( &flag );
58 bool mpi_initialized_ =
false;
65 throw std::runtime_error(
"MPI already initialized!" );
68 int err = MPI_Init( argc, argv );
69 if ( err != MPI_SUCCESS )
71 char errstr[MPI_MAX_ERROR_STRING];
73 MPI_Error_string( err, errstr, &len );
74 throw std::runtime_error( std::string(
"MPI_Init failed: " ) + std::string( errstr, len ) );
77 mpi_initialized_ =
true;
79 MPI_Comm_set_errhandler( MPI_COMM_WORLD, MPI_ERRORS_RETURN );
87 int err = MPI_Finalize();
88 if ( err != MPI_SUCCESS )
90 char errstr[MPI_MAX_ERROR_STRING];
92 MPI_Error_string( err, errstr, &len );
93 std::cerr <<
"[MPI] MPI_Finalize failed: " << std::string( errstr, len ) << std::endl;
99 static MPIContext& instance(
int* argc =
nullptr,
char*** argv =
nullptr )
110 char errstr[MPI_MAX_ERROR_STRING];
112 MPI_Error_string( err, errstr, &len );
113 return { errstr,
static_cast< size_t >( len ) };
116template <
typename T >
119 static_assert(
sizeof( T ) == 0,
"No MPI datatype mapping for this type." );
120 return MPI_DATATYPE_NULL;
132 return MPI_SIGNED_CHAR;
138 return MPI_UNSIGNED_CHAR;
162 return MPI_UNSIGNED_SHORT;
174 return MPI_UNSIGNED_LONG;
180 return MPI_LONG_LONG;
186 return MPI_UNSIGNED_LONG_LONG;
204 return MPI_LONG_DOUBLE;
MPIContext(const MPIContext &)=delete
static bool is_finalized()
Query whether MPI is finalized.
Definition mpi.hpp:50
MPIContext(MPIContext &&)=delete
static void initialize(int *argc, char ***argv)
Initialize MPI once. Safe to call only once.
Definition mpi.hpp:39
static bool is_initialized()
Query whether MPI is initialized.
Definition mpi.hpp:42
MPIContext & operator=(MPIContext &&)=delete
MPIContext & operator=(const MPIContext &)=delete
MPI_Datatype mpi_datatype< unsigned long long >()
Definition mpi.hpp:184
MPI_Datatype mpi_datatype< float >()
Definition mpi.hpp:190
std::string mpi_error_string(int err)
Definition mpi.hpp:108
MPI_Datatype mpi_datatype< char >()
Definition mpi.hpp:124
MPI_Datatype mpi_datatype< unsigned long >()
Definition mpi.hpp:172
MPI_Datatype mpi_datatype< short >()
Definition mpi.hpp:154
MPI_Datatype mpi_datatype< unsigned char >()
Definition mpi.hpp:136
void barrier()
Definition mpi.hpp:24
int num_processes()
Definition mpi.hpp:17
MPI_Datatype mpi_datatype< unsigned int >()
Definition mpi.hpp:148
int MPIRank
Definition mpi.hpp:8
MPI_Datatype mpi_datatype< bool >()
Definition mpi.hpp:208
MPI_Datatype mpi_datatype< double >()
Definition mpi.hpp:196
MPI_Datatype mpi_datatype< long long >()
Definition mpi.hpp:178
MPI_Datatype mpi_datatype< int >()
Definition mpi.hpp:142
MPI_Datatype mpi_datatype< unsigned short >()
Definition mpi.hpp:160
MPI_Datatype mpi_datatype< long double >()
Definition mpi.hpp:202
MPIRank rank()
Definition mpi.hpp:10
MPI_Datatype mpi_datatype()
Definition mpi.hpp:117
MPI_Datatype mpi_datatype< signed char >()
Definition mpi.hpp:130
MPI_Datatype mpi_datatype< long >()
Definition mpi.hpp:166