15#if defined(__parallel)
16static bool mpi_initialized_by_cp2k =
false;
17static bool mpi_attached_by_cp2k =
false;
25 const int error = (CMD); \
26 if (MPI_SUCCESS != error) { \
27 fprintf(stderr, "MPI error #%i in %s:%i\n", error, __FILE__, __LINE__); \
28 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE); \
35static void assert_outside_parallel(
const char *routine_name) {
36 if (omp_in_parallel()) {
37 fprintf(stderr,
"%s must be called outside an OpenMP parallel region.\n",
46static const char *thread_level_name(
const int level) {
48 case MPI_THREAD_SINGLE:
49 return "MPI_THREAD_SINGLE";
50 case MPI_THREAD_FUNNELED:
51 return "MPI_THREAD_FUNNELED";
52 case MPI_THREAD_SERIALIZED:
53 return "MPI_THREAD_SERIALIZED";
54 case MPI_THREAD_MULTIPLE:
55 return "MPI_THREAD_MULTIPLE";
64static void require_thread_multiple(
const int provided) {
65 if (provided < MPI_THREAD_MULTIPLE) {
66 fprintf(stderr,
"CP2K requires MPI_THREAD_MULTIPLE, but MPI provides %s.\n",
67 thread_level_name(provided));
68 MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
79#if defined(__parallel)
80 assert_outside_parallel(
"cp_mpi_init");
81 if (mpi_attached_by_cp2k) {
82 fprintf(stderr,
"cp_mpi_init must only be called once per lifecycle.\n");
86 int finalized, initialized, provided;
87 CHECK(MPI_Finalized(&finalized));
89 fprintf(stderr,
"cp_mpi_init cannot be called after MPI_Finalize.\n");
93 CHECK(MPI_Initialized(&initialized));
95 CHECK(MPI_Query_thread(&provided));
97 CHECK(MPI_Init_thread(argc, argv, MPI_THREAD_MULTIPLE, &provided));
98 mpi_initialized_by_cp2k =
true;
100 require_thread_multiple(provided);
101 mpi_attached_by_cp2k =
true;
113#if defined(__parallel)
114 assert_outside_parallel(
"cp_mpi_finalize");
115 if (!mpi_attached_by_cp2k) {
116 fprintf(stderr,
"cp_mpi_finalize called without a matching cp_mpi_init.\n");
121 CHECK(MPI_Finalized(&finalized));
123 fprintf(stderr,
"MPI was finalized before cp_mpi_finalize.\n");
126 if (mpi_initialized_by_cp2k) {
127 CHECK(MPI_Finalize());
128 mpi_initialized_by_cp2k =
false;
130 mpi_attached_by_cp2k =
false;
139#if defined(__parallel)
140 return MPI_COMM_WORLD;
151#if defined(__parallel)
152 return MPI_COMM_NULL;
163#if defined(__parallel)
166 ? MPI_Comm_f2c(fortran_comm)
179#if defined(__parallel)
180 return MPI_Comm_c2f(comm);
192#if defined(__parallel)
194 if (MPI_COMM_NULL != comm) {
195 CHECK(MPI_Comm_rank(comm, &rank));
209#if defined(__parallel)
211 if (MPI_COMM_NULL != comm) {
212 CHECK(MPI_Comm_size(comm, &nranks));
225#if defined(__parallel)
226 if (MPI_COMM_NULL != comm) {
227 CHECK(MPI_Barrier(comm));
239#if defined(__parallel)
240 CHECK(MPI_Dims_create(nnodes, ndims, dims));
243 for (
int i = 1;
i < ndims;
i++) {
254 const int dims[],
const int periods[],
256#if defined(__parallel)
257 assert_outside_parallel(
"cp_mpi_cart_create");
259 CHECK(MPI_Cart_create(comm_old, ndims, dims, periods, reorder, &comm_cart));
276 int periods[],
int coords[]) {
277#if defined(__parallel)
278 CHECK(MPI_Cart_get(comm, maxdims, dims, periods, coords));
281 for (
int i = 0;
i < maxdims;
i++) {
294#if defined(__parallel)
296 CHECK(MPI_Cart_rank(comm, coords, &rank));
310 const int remain_dims[]) {
311#if defined(__parallel)
312 assert_outside_parallel(
"cp_mpi_cart_sub");
314 CHECK(MPI_Cart_sub(comm, remain_dims, &newcomm));
328#if defined(__parallel)
329 assert_outside_parallel(
"cp_mpi_comm_free");
330 CHECK(MPI_Comm_free(comm));
342#if defined(__parallel)
344 CHECK(MPI_Comm_compare(comm1, comm2, &res));
345 return res == MPI_IDENT || res == MPI_CONGRUENT || res == MPI_SIMILAR;
358#if defined(__parallel)
359 if (MPI_COMM_NULL != comm) {
363 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT, MPI_MAX, comm));
364 memcpy(values, recvbuf, count *
sizeof(
int));
382#if defined(__parallel)
383 if (MPI_COMM_NULL != comm) {
387 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_UINT64_T, MPI_MAX, comm));
388 memcpy(values, recvbuf, count *
sizeof(uint64_t));
406#if defined(__parallel)
407 if (MPI_COMM_NULL != comm) {
411 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_DOUBLE, MPI_MAX, comm));
412 memcpy(values, recvbuf, count *
sizeof(
double));
430#if defined(__parallel)
431 if (MPI_COMM_NULL != comm) {
435 CHECK(MPI_Reduce(values, recvbuf, count, MPI_DOUBLE, MPI_MAX, root, comm));
436 memcpy(values, recvbuf, count *
sizeof(
double));
454#if defined(__parallel)
455 if (MPI_COMM_NULL != comm) {
459 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT, MPI_SUM, comm));
460 memcpy(values, recvbuf, count *
sizeof(
int));
477#if defined(__parallel)
478 if (MPI_COMM_NULL != comm) {
482 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_LONG, MPI_SUM, comm));
483 memcpy(values, recvbuf, count *
sizeof(
long));
501#if defined(__parallel)
502 if (MPI_COMM_NULL != comm) {
506 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT64_T, MPI_SUM, comm));
507 memcpy(values, recvbuf, count *
sizeof(int64_t));
525#if defined(__parallel)
526 if (MPI_COMM_NULL != comm) {
530 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_DOUBLE, MPI_SUM, comm));
531 memcpy(values, recvbuf, count *
sizeof(
double));
549#if defined(__parallel)
550 if (MPI_COMM_NULL != comm) {
554 CHECK(MPI_Reduce(values, recvbuf, count, MPI_DOUBLE, MPI_SUM, root, comm));
555 memcpy(values, recvbuf, count *
sizeof(
double));
573 const int dest,
const int sendtag,
void *recvbuf,
574 const int recvcount,
const int source,
576#if defined(__parallel)
578 CHECK(MPI_Sendrecv(sendbuf, sendcount, MPI_BYTE, dest, sendtag, recvbuf,
579 recvcount, MPI_BYTE, source, recvtag, comm, &status));
580 int count_received = 0;
581 CHECK(MPI_Get_count(&status, MPI_BYTE, &count_received));
582 return count_received;
593 fprintf(stderr,
"Error: cp_mpi_sendrecv_byte not available without MPI\n");
603 const int dest,
const int sendtag,
double *recvbuf,
604 const int recvcount,
const int source,
606#if defined(__parallel)
608 CHECK(MPI_Sendrecv(sendbuf, sendcount, MPI_DOUBLE, dest, sendtag, recvbuf,
609 recvcount, MPI_DOUBLE, source, recvtag, comm, &status));
611 CHECK(MPI_Get_count(&status, MPI_DOUBLE, &count_received));
612 return count_received;
623 fprintf(stderr,
"Error: cp_mpi_sendrecv_double not available without MPI\n");
634#if defined(__parallel)
635 CHECK(MPI_Alltoall(sendbuf, sendcount, MPI_INT, recvbuf, recvcount, MPI_INT,
640 memcpy(recvbuf, sendbuf, sendcount *
sizeof(
int));
649 const int *sdispls,
void *recvbuf,
650 const int *recvcounts,
const int *rdispls,
652#if defined(__parallel)
653 CHECK(MPI_Alltoallv(sendbuf, sendcounts, sdispls, MPI_BYTE, recvbuf,
654 recvcounts, rdispls, MPI_BYTE, comm));
657 cp_assert(sendcounts[0] == recvcounts[0]);
658 cp_assert(sdispls[0] == 0 && rdispls[0] == 0);
659 memcpy(recvbuf, sendbuf, sendcounts[0]);
668 const int *sdispls,
double *recvbuf,
669 const int *recvcounts,
const int *rdispls,
671#if defined(__parallel)
672 CHECK(MPI_Alltoallv(sendbuf, sendcounts, sdispls, MPI_DOUBLE, recvbuf,
673 recvcounts, rdispls, MPI_DOUBLE, comm));
676 cp_assert(sendcounts[0] == recvcounts[0]);
677 cp_assert(sdispls[0] == 0 && rdispls[0] == 0);
678 memcpy(recvbuf, sendbuf, sendcounts[0] *
sizeof(
double));
688#if defined(__parallel)
689 CHECK(MPI_Bcast(buf, count, MPI_INT, root, comm));
704#if defined(__parallel)
705 CHECK(MPI_Bcast(buf, count, MPI_CHAR, root, comm));
720#if DBM_ALLOC_MPI && defined(__parallel)
721 CHECK(MPI_Alloc_mem((MPI_Aint)size, MPI_INFO_NULL, &result));
722#elif DBM_ALLOC_OPENMP && (201811 <= _OPENMP)
723 result = omp_alloc(size, omp_null_allocator);
725 result = malloc(size);
735#if DBM_ALLOC_MPI && defined(__parallel)
736 CHECK(MPI_Free_mem(mem));
737#elif DBM_ALLOC_OPENMP && (201811 <= _OPENMP)
738 omp_free(mem, omp_null_allocator);
#define cp_assert(condition)
int cp_mpi_comm_size(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_size.
void cp_mpi_free_mem(void *mem)
Wrapper around MPI_Free_mem.
void cp_mpi_max_int(int *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_INT.
void cp_mpi_sum_double(double *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_DOUBLE.
cp_mpi_comm_t cp_mpi_cart_create(const cp_mpi_comm_t comm_old, const int ndims, const int dims[], const int periods[], const int reorder)
Wrapper around MPI_Cart_create.
int cp_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 cp_mpi_comm_t comm)
Wrapper around MPI_Sendrecv for datatype MPI_BYTE.
void cp_mpi_alltoallv_double(const double *sendbuf, const int *sendcounts, const int *sdispls, double *recvbuf, const int *recvcounts, const int *rdispls, const cp_mpi_comm_t comm)
Wrapper around MPI_Alltoallv for datatype MPI_DOUBLE.
int cp_mpi_cart_rank(const cp_mpi_comm_t comm, const int coords[])
Wrapper around MPI_Cart_rank.
void cp_mpi_init(int *argc, char ***argv)
Initialize MPI with MPI_THREAD_MULTIPLE or attach to an active MPI.
int cp_mpi_comm_c2f(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_c2f.
void cp_mpi_bcast_char(char *buf, const int count, const int root, const cp_mpi_comm_t comm)
Wrapper around MPI_Bcast for datatype MPI_CHAR.
void * cp_mpi_alloc_mem(size_t size)
Wrapper around MPI_Alloc_mem.
void cp_mpi_barrier(const cp_mpi_comm_t comm)
Wrapper around MPI_Barrier; a null communicator is a no-op.
cp_mpi_comm_t cp_mpi_get_comm_null(void)
Returns MPI_COMM_WORLD.
void cp_mpi_max_uint64(uint64_t *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_UINT64_T.
void cp_mpi_max_root_double(double *values, const int count, const int root, const cp_mpi_comm_t comm)
Wrapper around MPI_Reduce for op MPI_MAX and datatype MPI_DOUBLE.
cp_mpi_comm_t cp_mpi_get_comm_world(void)
Returns MPI_COMM_WORLD.
void cp_mpi_cart_get(const cp_mpi_comm_t comm, int maxdims, int dims[], int periods[], int coords[])
Wrapper around MPI_Cart_get.
void cp_mpi_max_double(double *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_DOUBLE.
void cp_mpi_sum_int64(int64_t *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT64_T.
void cp_mpi_comm_free(cp_mpi_comm_t *comm)
Wrapper around MPI_Comm_free.
void cp_mpi_alltoall_int(const int *sendbuf, const int sendcount, int *recvbuf, const int recvcount, const cp_mpi_comm_t comm)
Wrapper around MPI_Alltoall for datatype MPI_INT.
void cp_mpi_dims_create(const int nnodes, const int ndims, int dims[])
Wrapper around MPI_Dims_create.
bool cp_mpi_comms_are_similar(const cp_mpi_comm_t comm1, const cp_mpi_comm_t comm2)
Wrapper around MPI_Comm_compare.
void cp_mpi_sum_int(int *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT.
void cp_mpi_sum_long(long *values, const int count, const cp_mpi_comm_t comm)
Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_LONG.
void cp_mpi_sum_root_double(double *values, const int count, const int root, const cp_mpi_comm_t comm)
Wrapper around MPI_Reduce for op MPI_SUM and datatype MPI_DOUBLE.
void cp_mpi_alltoallv_byte(const void *sendbuf, const int *sendcounts, const int *sdispls, void *recvbuf, const int *recvcounts, const int *rdispls, const cp_mpi_comm_t comm)
Wrapper around MPI_Alltoallv for datatype MPI_BYTE.
void cp_mpi_finalize(void)
Detach from MPI and finalize it only if cp_mpi_init initialized it.
cp_mpi_comm_t cp_mpi_comm_f2c(const int fortran_comm)
Wrapper around MPI_Comm_f2c.
cp_mpi_comm_t cp_mpi_cart_sub(const cp_mpi_comm_t comm, const int remain_dims[])
Wrapper around MPI_Cart_sub.
void cp_mpi_bcast_int(int *buf, const int count, const int root, const cp_mpi_comm_t comm)
Wrapper around MPI_Bcast for datatype MPI_INT.
int cp_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 cp_mpi_comm_t comm)
Wrapper around MPI_Sendrecv for datatype MPI_DOUBLE.
int cp_mpi_comm_rank(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
static void const int const int i