16#if defined(__parallel)
21#define CHECK(STATUS) \
23 if (MPI_SUCCESS != (STATUS)) { \
24 fprintf(stderr, "MPI error #%i in %s:%i\n", STATUS, __FILE__, __LINE__); \
25 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE); \
35#if defined(__parallel)
36 CHECK(MPI_Init(argc, argv));
48#if defined(__parallel)
49 CHECK(MPI_Finalize());
58#if defined(__parallel)
59 return MPI_COMM_WORLD;
70#if defined(__parallel)
71 return MPI_Comm_f2c(fortran_comm);
83#if defined(__parallel)
84 return MPI_Comm_c2f(comm);
96#if defined(__parallel)
98 CHECK(MPI_Comm_rank(comm, &rank));
111#if defined(__parallel)
113 CHECK(MPI_Comm_size(comm, &nranks));
126#if defined(__parallel)
127 CHECK(MPI_Dims_create(nnodes, ndims, dims));
130 for (
int i = 1;
i < ndims;
i++) {
141 const int ndims,
const int dims[],
142 const int periods[],
const int reorder) {
143#if defined(__parallel)
145 CHECK(MPI_Cart_create(comm_old, ndims, dims, periods, reorder, &comm_cart));
162 int periods[],
int coords[]) {
163#if defined(__parallel)
164 CHECK(MPI_Cart_get(comm, maxdims, dims, periods, coords));
167 for (
int i = 0;
i < maxdims;
i++) {
180#if defined(__parallel)
182 CHECK(MPI_Cart_rank(comm, coords, &rank));
196 const int remain_dims[]) {
197#if defined(__parallel)
199 CHECK(MPI_Cart_sub(comm, remain_dims, &newcomm));
213#if defined(__parallel)
214 CHECK(MPI_Comm_free(comm));
226#if defined(__parallel)
228 CHECK(MPI_Comm_compare(comm1, comm2, &res));
229 return res == MPI_IDENT || res == MPI_CONGRUENT || res == MPI_SIMILAR;
242#if defined(__parallel)
245 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT, MPI_MAX, comm));
246 memcpy(values, recvbuf, count *
sizeof(
int));
263#if defined(__parallel)
267 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_UINT64_T, MPI_MAX, comm));
268 memcpy(values, recvbuf, count *
sizeof(uint64_t));
285#if defined(__parallel)
289 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_DOUBLE, MPI_MAX, comm));
290 memcpy(values, recvbuf, count *
sizeof(
double));
306#if defined(__parallel)
309 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT, MPI_SUM, comm));
310 memcpy(values, recvbuf, count *
sizeof(
int));
327#if defined(__parallel)
331 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT64_T, MPI_SUM, comm));
332 memcpy(values, recvbuf, count *
sizeof(int64_t));
349#if defined(__parallel)
353 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_DOUBLE, MPI_SUM, comm));
354 memcpy(values, recvbuf, count *
sizeof(
double));
370 const int dest,
const int sendtag,
void *recvbuf,
371 const int recvcount,
const int source,
373#if defined(__parallel)
375 CHECK(MPI_Sendrecv(sendbuf, sendcount, MPI_BYTE, dest, sendtag, recvbuf,
376 recvcount, MPI_BYTE, source, recvtag, comm, &status));
378 CHECK(MPI_Get_count(&status, MPI_BYTE, &count_received));
379 return count_received;
390 fprintf(stderr,
"Error: dbm_mpi_sendrecv_byte not available without MPI\n");
400 const int dest,
const int sendtag,
double *recvbuf,
401 const int recvcount,
const int source,
403#if defined(__parallel)
405 CHECK(MPI_Sendrecv(sendbuf, sendcount, MPI_DOUBLE, dest, sendtag, recvbuf,
406 recvcount, MPI_DOUBLE, source, recvtag, comm, &status));
408 CHECK(MPI_Get_count(&status, MPI_DOUBLE, &count_received));
409 return count_received;
420 fprintf(stderr,
"Error: dbm_mpi_sendrecv_double not available without MPI\n");
431#if defined(__parallel)
432 CHECK(MPI_Alltoall(sendbuf, sendcount, MPI_INT, recvbuf, recvcount, MPI_INT,
436 assert(sendcount == recvcount);
437 memcpy(recvbuf, sendbuf, sendcount *
sizeof(
int));
446 const int *sdispls,
void *recvbuf,
447 const int *recvcounts,
const int *rdispls,
449#if defined(__parallel)
450 CHECK(MPI_Alltoallv(sendbuf, sendcounts, sdispls, MPI_BYTE, recvbuf,
451 recvcounts, rdispls, MPI_BYTE, comm));
454 assert(sendcounts[0] == recvcounts[0]);
455 assert(sdispls[0] == 0 && rdispls[0] == 0);
456 memcpy(recvbuf, sendbuf, sendcounts[0]);
465 const int *sdispls,
double *recvbuf,
466 const int *recvcounts,
const int *rdispls,
468#if defined(__parallel)
469 CHECK(MPI_Alltoallv(sendbuf, sendcounts, sdispls, MPI_DOUBLE, recvbuf,
470 recvcounts, rdispls, MPI_DOUBLE, comm));
473 assert(sendcounts[0] == recvcounts[0]);
474 assert(sdispls[0] == 0 && rdispls[0] == 0);
475 memcpy(recvbuf, sendbuf, sendcounts[0] *
sizeof(
double));
485#if DBM_ALLOC_MPI && defined(__parallel)
486 CHECK(MPI_Alloc_mem((MPI_Aint)size, MPI_INFO_NULL, &result));
487#elif DBM_ALLOC_OPENMP && (201811 <= _OPENMP)
488 result = omp_alloc(size, omp_null_allocator);
490 result = malloc(size);
500#if DBM_ALLOC_MPI && defined(__parallel)
501 CHECK(MPI_Free_mem(mem));
502#elif DBM_ALLOC_OPENMP && (201811 <= _OPENMP)
503 omp_free(mem, omp_null_allocator);
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.
void dbm_mpi_free_mem(void *mem)
Wrapper around MPI_Free_mem.
void * dbm_mpi_alloc_mem(size_t size)
Wrapper around MPI_Alloc_mem.
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.
void dbm_mpi_max_uint64(uint64_t *values, const int count, const dbm_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_UINT64_T.
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