15 #if defined(__parallel)
20 #define CHECK(status) \
21 if (status != MPI_SUCCESS) { \
22 fprintf(stderr, "MPI error in %s:%i\n", __FILE__, __LINE__); \
23 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE); \
32 #if defined(__parallel)
33 CHECK(MPI_Init(argc, argv));
45 #if defined(__parallel)
46 CHECK(MPI_Finalize());
55 #if defined(__parallel)
56 return MPI_COMM_WORLD;
67 #if defined(__parallel)
68 return MPI_Comm_f2c(fortran_comm);
80 #if defined(__parallel)
81 return MPI_Comm_c2f(comm);
93 #if defined(__parallel)
95 CHECK(MPI_Comm_rank(comm, &rank));
108 #if defined(__parallel)
110 CHECK(MPI_Comm_size(comm, &nranks));
123 #if defined(__parallel)
124 CHECK(MPI_Dims_create(nnodes, ndims, dims));
127 for (
int i = 1;
i < ndims;
i++) {
138 const int ndims,
const int dims[],
139 const int periods[],
const int reorder) {
140 #if defined(__parallel)
142 CHECK(MPI_Cart_create(comm_old, ndims, dims, periods, reorder, &comm_cart));
159 int periods[],
int coords[]) {
160 #if defined(__parallel)
161 CHECK(MPI_Cart_get(comm, maxdims, dims, periods, coords));
164 for (
int i = 0;
i < maxdims;
i++) {
177 #if defined(__parallel)
179 CHECK(MPI_Cart_rank(comm, coords, &rank));
193 const int remain_dims[]) {
194 #if defined(__parallel)
196 CHECK(MPI_Cart_sub(comm, remain_dims, &newcomm));
210 #if defined(__parallel)
211 CHECK(MPI_Comm_free(comm));
223 #if defined(__parallel)
225 CHECK(MPI_Comm_compare(comm1, comm2, &res));
226 return res == MPI_IDENT || res == MPI_CONGRUENT || res == MPI_SIMILAR;
239 #if defined(__parallel)
240 int *recvbuf = malloc(count *
sizeof(
int));
241 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT, MPI_MAX, comm));
242 memcpy(values, recvbuf, count *
sizeof(
int));
257 #if defined(__parallel)
258 double *recvbuf = malloc(count *
sizeof(
double));
259 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_DOUBLE, MPI_MAX, comm));
260 memcpy(values, recvbuf, count *
sizeof(
double));
274 #if defined(__parallel)
275 int *recvbuf = malloc(count *
sizeof(
int));
276 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT, MPI_SUM, comm));
277 memcpy(values, recvbuf, count *
sizeof(
int));
292 #if defined(__parallel)
293 int64_t *recvbuf = malloc(count *
sizeof(int64_t));
294 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT64_T, MPI_SUM, comm));
295 memcpy(values, recvbuf, count *
sizeof(int64_t));
310 #if defined(__parallel)
311 double *recvbuf = malloc(count *
sizeof(
double));
312 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_DOUBLE, MPI_SUM, comm));
313 memcpy(values, recvbuf, count *
sizeof(
double));
327 const int dest,
const int sendtag,
void *recvbuf,
328 const int recvcount,
const int source,
330 #if defined(__parallel)
332 CHECK(MPI_Sendrecv(sendbuf, sendcount, MPI_BYTE, dest, sendtag, recvbuf,
333 recvcount, MPI_BYTE, source, recvtag, comm, &status))
335 CHECK(MPI_Get_count(&status, MPI_BYTE, &count_received));
336 return count_received;
347 fprintf(stderr,
"Error: dbm_mpi_sendrecv_byte not available without MPI\n");
357 const int dest,
const int sendtag,
double *recvbuf,
358 const int recvcount,
const int source,
360 #if defined(__parallel)
362 CHECK(MPI_Sendrecv(sendbuf, sendcount, MPI_DOUBLE, dest, sendtag, recvbuf,
363 recvcount, MPI_DOUBLE, source, recvtag, comm, &status))
365 CHECK(MPI_Get_count(&status, MPI_DOUBLE, &count_received));
366 return count_received;
377 fprintf(stderr,
"Error: dbm_mpi_sendrecv_double not available without MPI\n");
388 #if defined(__parallel)
389 CHECK(MPI_Alltoall(sendbuf, sendcount, MPI_INT, recvbuf, recvcount, MPI_INT,
393 assert(sendcount == recvcount);
394 memcpy(recvbuf, sendbuf, sendcount *
sizeof(
int));
403 const int *sdispls,
void *recvbuf,
404 const int *recvcounts,
const int *rdispls,
406 #if defined(__parallel)
407 CHECK(MPI_Alltoallv(sendbuf, sendcounts, sdispls, MPI_BYTE, recvbuf,
408 recvcounts, rdispls, MPI_BYTE, comm));
411 assert(sendcounts[0] == recvcounts[0]);
412 assert(sdispls[0] == 0 && rdispls[0] == 0);
413 memcpy(recvbuf, sendbuf, sendcounts[0]);
422 const int *sdispls,
double *recvbuf,
423 const int *recvcounts,
const int *rdispls,
425 #if defined(__parallel)
426 CHECK(MPI_Alltoallv(sendbuf, sendcounts, sdispls, MPI_DOUBLE, recvbuf,
427 recvcounts, rdispls, MPI_DOUBLE, comm));
430 assert(sendcounts[0] == recvcounts[0]);
431 assert(sdispls[0] == 0 && rdispls[0] == 0);
432 memcpy(recvbuf, sendbuf, sendcounts[0] *
sizeof(
double));
int dbm_mpi_sendrecv_byte(const void *sendbuf, const int sendcount, const int dest, const int sendtag, void *recvbuf, const int recvcount, const int source, const int recvtag, const dbm_mpi_comm_t comm)
Wrapper around MPI_Sendrecv for datatype MPI_BYTE.
void dbm_mpi_finalize()
Wrapper around MPI_Finalize.
int dbm_mpi_comm_rank(const dbm_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
int dbm_mpi_cart_rank(const dbm_mpi_comm_t comm, const int coords[])
Wrapper around MPI_Cart_rank.
bool dbm_mpi_comms_are_similar(const dbm_mpi_comm_t comm1, const dbm_mpi_comm_t comm2)
Wrapper around MPI_Comm_compare.
int dbm_mpi_comm_size(const dbm_mpi_comm_t comm)
Wrapper around MPI_Comm_size.
void dbm_mpi_alltoallv_byte(const void *sendbuf, const int *sendcounts, const int *sdispls, void *recvbuf, const int *recvcounts, const int *rdispls, const dbm_mpi_comm_t comm)
Wrapper around MPI_Alltoallv for datatype MPI_BYTE.
void dbm_mpi_init(int *argc, char ***argv)
Wrapper around MPI_Init.
void dbm_mpi_sum_double(double *values, const int count, const dbm_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_DOUBLE.
dbm_mpi_comm_t dbm_mpi_cart_create(const dbm_mpi_comm_t comm_old, const int ndims, const int dims[], const int periods[], const int reorder)
Wrapper around MPI_Cart_create.
void dbm_mpi_sum_int64(int64_t *values, const int count, const dbm_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT64_T.
void dbm_mpi_max_double(double *values, const int count, const dbm_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_DOUBLE.
void dbm_mpi_alltoall_int(const int *sendbuf, const int sendcount, int *recvbuf, const int recvcount, const dbm_mpi_comm_t comm)
Wrapper around MPI_Alltoall for datatype MPI_INT.
int dbm_mpi_sendrecv_double(const double *sendbuf, const int sendcount, const int dest, const int sendtag, double *recvbuf, const int recvcount, const int source, const int recvtag, const dbm_mpi_comm_t comm)
Wrapper around MPI_Sendrecv for datatype MPI_DOUBLE.
void dbm_mpi_max_int(int *values, const int count, const dbm_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_INT.
dbm_mpi_comm_t dbm_mpi_cart_sub(const dbm_mpi_comm_t comm, const int remain_dims[])
Wrapper around MPI_Cart_sub.
void dbm_mpi_dims_create(const int nnodes, const int ndims, int dims[])
Wrapper around MPI_Dims_create.
int dbm_mpi_comm_c2f(const dbm_mpi_comm_t comm)
Wrapper around MPI_Comm_c2f.
void dbm_mpi_comm_free(dbm_mpi_comm_t *comm)
Wrapper around MPI_Comm_free.
void dbm_mpi_alltoallv_double(const double *sendbuf, const int *sendcounts, const int *sdispls, double *recvbuf, const int *recvcounts, const int *rdispls, const dbm_mpi_comm_t comm)
Wrapper around MPI_Alltoallv for datatype MPI_DOUBLE.
void dbm_mpi_cart_get(const dbm_mpi_comm_t comm, int maxdims, int dims[], int periods[], int coords[])
Wrapper around MPI_Cart_get.
dbm_mpi_comm_t dbm_mpi_comm_f2c(const int fortran_comm)
Wrapper around MPI_Comm_f2c.
dbm_mpi_comm_t dbm_mpi_get_comm_world()
Returns MPI_COMM_WORLD.
void dbm_mpi_sum_int(int *values, const int count, const dbm_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT.
static void const int const int i