5#ifdef TERRANEO_USE_NESMIK
6#include <nesmik/nesmik.hpp>
16 MPI_Comm_rank( MPI_COMM_WORLD, &
rank );
23 MPI_Comm_rank( comm, &
rank );
43 MPI_Barrier(MPI_COMM_WORLD);
56 static void initialize(
int* argc,
char*** argv ) { instance( argc, argv ); }
62 MPI_Initialized( &flag );
70 MPI_Finalized( &flag );
75 bool mpi_initialized_ =
false;
82 throw std::runtime_error(
"MPI already initialized!" );
85 int err = MPI_Init( argc, argv );
86 #ifdef TERRANEO_USE_NESMIK
89 if ( err != MPI_SUCCESS )
91 char errstr[MPI_MAX_ERROR_STRING];
93 MPI_Error_string( err, errstr, &len );
94 throw std::runtime_error( std::string(
"MPI_Init failed: " ) + std::string( errstr, len ) );
97 mpi_initialized_ =
true;
99 MPI_Comm_set_errhandler( MPI_COMM_WORLD, MPI_ERRORS_RETURN );
107 #ifdef TERRANEO_USE_NESMIK
110 int err = MPI_Finalize();
111 if ( err != MPI_SUCCESS )
113 char errstr[MPI_MAX_ERROR_STRING];
115 MPI_Error_string( err, errstr, &len );
116 std::cerr <<
"[MPI] MPI_Finalize failed: " << std::string( errstr, len ) << std::endl;
122 static MPIContext& instance(
int* argc =
nullptr,
char*** argv =
nullptr )
133 char errstr[MPI_MAX_ERROR_STRING];
135 MPI_Error_string( err, errstr, &len );
136 return { errstr,
static_cast< size_t >( len ) };
139template <
typename T >
142 static_assert(
sizeof( T ) == 0,
"No MPI datatype mapping for this type." );
143 return MPI_DATATYPE_NULL;
155 return MPI_SIGNED_CHAR;
161 return MPI_UNSIGNED_CHAR;
185 return MPI_UNSIGNED_SHORT;
197 return MPI_UNSIGNED_LONG;
203 return MPI_LONG_LONG;
209 return MPI_UNSIGNED_LONG_LONG;
227 return MPI_LONG_DOUBLE;
MPIContext(const MPIContext &)=delete
static bool is_finalized()
Query whether MPI is finalized.
Definition mpi.hpp:67
MPIContext(MPIContext &&)=delete
static void initialize(int *argc, char ***argv)
Initialize MPI once. Safe to call only once.
Definition mpi.hpp:56
static bool is_initialized()
Query whether MPI is initialized.
Definition mpi.hpp:59
MPIContext & operator=(MPIContext &&)=delete
MPIContext & operator=(const MPIContext &)=delete
Definition level_comms.hpp:10
MPI_Datatype mpi_datatype< unsigned long long >()
Definition mpi.hpp:207
MPI_Datatype mpi_datatype< float >()
Definition mpi.hpp:213
std::string mpi_error_string(int err)
Definition mpi.hpp:131
MPI_Datatype mpi_datatype< char >()
Definition mpi.hpp:147
MPI_Datatype mpi_datatype< unsigned long >()
Definition mpi.hpp:195
MPI_Datatype mpi_datatype< short >()
Definition mpi.hpp:177
MPI_Datatype mpi_datatype< unsigned char >()
Definition mpi.hpp:159
void barrier()
Definition mpi.hpp:41
int num_processes()
Definition mpi.hpp:27
MPI_Datatype mpi_datatype< unsigned int >()
Definition mpi.hpp:171
int MPIRank
Definition mpi.hpp:11
MPI_Datatype mpi_datatype< bool >()
Definition mpi.hpp:231
MPI_Datatype mpi_datatype< double >()
Definition mpi.hpp:219
MPI_Datatype mpi_datatype< long long >()
Definition mpi.hpp:201
MPI_Datatype mpi_datatype< int >()
Definition mpi.hpp:165
MPI_Datatype mpi_datatype< unsigned short >()
Definition mpi.hpp:183
MPI_Datatype mpi_datatype< long double >()
Definition mpi.hpp:225
MPIRank rank()
Definition mpi.hpp:13
MPI_Datatype mpi_datatype()
Definition mpi.hpp:140
MPI_Datatype mpi_datatype< signed char >()
Definition mpi.hpp:153
MPI_Datatype mpi_datatype< long >()
Definition mpi.hpp:189