(git:5a3db73)
Loading...
Searching...
No Matches
cp_mpi.c
Go to the documentation of this file.
1/*----------------------------------------------------------------------------*/
2/* CP2K: A general program to perform molecular dynamics simulations */
3/* Copyright 2000-2026 CP2K developers group <https://cp2k.org> */
4/* */
5/* SPDX-License-Identifier: BSD-3-Clause */
6/*----------------------------------------------------------------------------*/
7#include "cp_mpi.h"
8
9#include "../base/cp_assert.h"
10#include <omp.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15#if defined(__parallel)
16static bool mpi_initialized_by_cp2k = false;
17static bool mpi_attached_by_cp2k = false;
18
19/*******************************************************************************
20 * \brief Check given MPI status and upon failure abort with a nice message.
21 * \author Ole Schuett
22 ******************************************************************************/
23#define CHECK(CMD) \
24 do { \
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); \
29 } \
30 } while (0)
31
32/*******************************************************************************
33 * \brief Abort if an MPI lifecycle operation is called from a parallel region.
34 ******************************************************************************/
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",
38 routine_name);
39 abort();
40 }
41}
42
43/*******************************************************************************
44 * \brief Return a human-readable MPI thread-support level.
45 ******************************************************************************/
46static const char *thread_level_name(const int level) {
47 switch (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";
56 default:
57 return "unknown";
58 }
59}
60
61/*******************************************************************************
62 * \brief Abort unless MPI provides MPI_THREAD_MULTIPLE support.
63 ******************************************************************************/
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);
69 abort();
70 }
71}
72#endif
73
74/*******************************************************************************
75 * \brief Initialize MPI with MPI_THREAD_MULTIPLE or attach to an active MPI.
76 * \author Ole Schuett
77 ******************************************************************************/
78void cp_mpi_init(int *argc, char ***argv) {
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");
83 abort();
84 }
85
86 int finalized, initialized, provided;
87 CHECK(MPI_Finalized(&finalized));
88 if (finalized) {
89 fprintf(stderr, "cp_mpi_init cannot be called after MPI_Finalize.\n");
90 abort();
91 }
92
93 CHECK(MPI_Initialized(&initialized));
94 if (initialized) {
95 CHECK(MPI_Query_thread(&provided));
96 } else {
97 CHECK(MPI_Init_thread(argc, argv, MPI_THREAD_MULTIPLE, &provided));
98 mpi_initialized_by_cp2k = true;
99 }
100 require_thread_multiple(provided);
101 mpi_attached_by_cp2k = true;
102#else
103 (void)argc; // mark used
104 (void)argv;
105#endif
106}
107
108/*******************************************************************************
109 * \brief Detach from MPI and finalize it only if cp_mpi_init initialized it.
110 * \author Ole Schuett
111 ******************************************************************************/
112void cp_mpi_finalize(void) {
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");
117 abort();
118 }
119
120 int finalized;
121 CHECK(MPI_Finalized(&finalized));
122 if (finalized) {
123 fprintf(stderr, "MPI was finalized before cp_mpi_finalize.\n");
124 abort();
125 }
126 if (mpi_initialized_by_cp2k) {
127 CHECK(MPI_Finalize());
128 mpi_initialized_by_cp2k = false;
129 }
130 mpi_attached_by_cp2k = false;
131#endif
132}
133
134/*******************************************************************************
135 * \brief Returns MPI_COMM_WORLD.
136 * \author Ole Schuett
137 ******************************************************************************/
139#if defined(__parallel)
140 return MPI_COMM_WORLD;
141#else
142 return -1;
143#endif
144}
145
146/*******************************************************************************
147 * \brief Returns MPI_COMM_WORLD.
148 * \author Ole Schuett
149 ******************************************************************************/
151#if defined(__parallel)
152 return MPI_COMM_NULL;
153#else
154 return -2;
155#endif
156}
157
158/*******************************************************************************
159 * \brief Wrapper around MPI_Comm_f2c.
160 * \author Ole Schuett
161 ******************************************************************************/
162cp_mpi_comm_t cp_mpi_comm_f2c(const int fortran_comm) {
163#if defined(__parallel)
164 // Attempt to support "no MPI" if __parallel is defined (0 -> MPI_COMM_NULL).
165 return MPI_Comm_c2f(cp_mpi_get_comm_null()) != fortran_comm
166 ? MPI_Comm_f2c(fortran_comm)
168#else
169 (void)fortran_comm; // mark used
170 return -1;
171#endif
172}
173
174/*******************************************************************************
175 * \brief Wrapper around MPI_Comm_c2f.
176 * \author Ole Schuett
177 ******************************************************************************/
179#if defined(__parallel)
180 return MPI_Comm_c2f(comm);
181#else
182 (void)comm; // mark used
183 return -1;
184#endif
185}
186
187/*******************************************************************************
188 * \brief Wrapper around MPI_Comm_rank.
189 * \author Ole Schuett
190 ******************************************************************************/
192#if defined(__parallel)
193 int rank = 0;
194 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
195 CHECK(MPI_Comm_rank(comm, &rank));
196 }
197 return rank;
198#else
199 (void)comm; // mark used
200 return 0;
201#endif
202}
203
204/*******************************************************************************
205 * \brief Wrapper around MPI_Comm_size.
206 * \author Ole Schuett
207 ******************************************************************************/
209#if defined(__parallel)
210 int nranks = 1;
211 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
212 CHECK(MPI_Comm_size(comm, &nranks));
213 }
214 return nranks;
215#else
216 (void)comm; // mark used
217 return 1;
218#endif
219}
220
221/*******************************************************************************
222 * \brief Wrapper around MPI_Barrier; a null communicator is a no-op.
223 ******************************************************************************/
225#if defined(__parallel)
226 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
227 CHECK(MPI_Barrier(comm));
228 }
229#else
230 (void)comm; // mark used
231#endif
232}
233
234/*******************************************************************************
235 * \brief Wrapper around MPI_Dims_create.
236 * \author Ole Schuett
237 ******************************************************************************/
238void cp_mpi_dims_create(const int nnodes, const int ndims, int dims[]) {
239#if defined(__parallel)
240 CHECK(MPI_Dims_create(nnodes, ndims, dims));
241#else
242 dims[0] = nnodes;
243 for (int i = 1; i < ndims; i++) {
244 dims[i] = 1;
245 }
246#endif
247}
248
249/*******************************************************************************
250 * \brief Wrapper around MPI_Cart_create.
251 * \author Ole Schuett
252 ******************************************************************************/
253cp_mpi_comm_t cp_mpi_cart_create(const cp_mpi_comm_t comm_old, const int ndims,
254 const int dims[], const int periods[],
255 const int reorder) {
256#if defined(__parallel)
257 assert_outside_parallel("cp_mpi_cart_create");
258 cp_mpi_comm_t comm_cart;
259 CHECK(MPI_Cart_create(comm_old, ndims, dims, periods, reorder, &comm_cart));
260 return comm_cart;
261#else
262 (void)comm_old; // mark used
263 (void)ndims;
264 (void)dims;
265 (void)periods;
266 (void)reorder;
267 return -1;
268#endif
269}
270
271/*******************************************************************************
272 * \brief Wrapper around MPI_Cart_get.
273 * \author Ole Schuett
274 ******************************************************************************/
275void cp_mpi_cart_get(const cp_mpi_comm_t comm, int maxdims, int dims[],
276 int periods[], int coords[]) {
277#if defined(__parallel)
278 CHECK(MPI_Cart_get(comm, maxdims, dims, periods, coords));
279#else
280 (void)comm; // mark used
281 for (int i = 0; i < maxdims; i++) {
282 dims[i] = 1;
283 periods[i] = 1;
284 coords[i] = 0;
285 }
286#endif
287}
288
289/*******************************************************************************
290 * \brief Wrapper around MPI_Cart_rank.
291 * \author Ole Schuett
292 ******************************************************************************/
293int cp_mpi_cart_rank(const cp_mpi_comm_t comm, const int coords[]) {
294#if defined(__parallel)
295 int rank;
296 CHECK(MPI_Cart_rank(comm, coords, &rank));
297 return rank;
298#else
299 (void)comm; // mark used
300 (void)coords;
301 return 0;
302#endif
303}
304
305/*******************************************************************************
306 * \brief Wrapper around MPI_Cart_sub.
307 * \author Ole Schuett
308 ******************************************************************************/
310 const int remain_dims[]) {
311#if defined(__parallel)
312 assert_outside_parallel("cp_mpi_cart_sub");
313 cp_mpi_comm_t newcomm;
314 CHECK(MPI_Cart_sub(comm, remain_dims, &newcomm));
315 return newcomm;
316#else
317 (void)comm; // mark used
318 (void)remain_dims;
319 return -1;
320#endif
321}
322
323/*******************************************************************************
324 * \brief Wrapper around MPI_Comm_free.
325 * \author Ole Schuett
326 ******************************************************************************/
328#if defined(__parallel)
329 assert_outside_parallel("cp_mpi_comm_free");
330 CHECK(MPI_Comm_free(comm));
331#else
332 (void)comm; // mark used
333#endif
334}
335
336/*******************************************************************************
337 * \brief Wrapper around MPI_Comm_compare.
338 * \author Ole Schuett
339 ******************************************************************************/
341 const cp_mpi_comm_t comm2) {
342#if defined(__parallel)
343 int res;
344 CHECK(MPI_Comm_compare(comm1, comm2, &res));
345 return res == MPI_IDENT || res == MPI_CONGRUENT || res == MPI_SIMILAR;
346#else
347 (void)comm1; // mark used
348 (void)comm2;
349 return true;
350#endif
351}
352
353/*******************************************************************************
354 * \brief Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_INT.
355 * \author Ole Schuett
356 ******************************************************************************/
357void cp_mpi_max_int(int *values, const int count, const cp_mpi_comm_t comm) {
358#if defined(__parallel)
359 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
360 int value = 0;
361 void *recvbuf =
362 (1 < count ? cp_mpi_alloc_mem(count * sizeof(int)) : &value);
363 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT, MPI_MAX, comm));
364 memcpy(values, recvbuf, count * sizeof(int));
365 if (1 < count) {
366 cp_mpi_free_mem(recvbuf);
367 }
368 }
369#else
370 (void)comm; // mark used
371 (void)values;
372 (void)count;
373#endif
374}
375
376/*******************************************************************************
377 * \brief Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_UINT64_T.
378 * \author Ole Schuett
379 ******************************************************************************/
380void cp_mpi_max_uint64(uint64_t *values, const int count,
381 const cp_mpi_comm_t comm) {
382#if defined(__parallel)
383 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
384 uint64_t value = 0;
385 void *recvbuf =
386 (1 < count ? cp_mpi_alloc_mem(count * sizeof(uint64_t)) : &value);
387 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_UINT64_T, MPI_MAX, comm));
388 memcpy(values, recvbuf, count * sizeof(uint64_t));
389 if (1 < count) {
390 cp_mpi_free_mem(recvbuf);
391 }
392 }
393#else
394 (void)comm; // mark used
395 (void)values;
396 (void)count;
397#endif
398}
399
400/*******************************************************************************
401 * \brief Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_DOUBLE.
402 * \author Ole Schuett
403 ******************************************************************************/
404void cp_mpi_max_double(double *values, const int count,
405 const cp_mpi_comm_t comm) {
406#if defined(__parallel)
407 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
408 double value = 0;
409 void *recvbuf =
410 (1 < count ? cp_mpi_alloc_mem(count * sizeof(double)) : &value);
411 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_DOUBLE, MPI_MAX, comm));
412 memcpy(values, recvbuf, count * sizeof(double));
413 if (1 < count) {
414 cp_mpi_free_mem(recvbuf);
415 }
416 }
417#else
418 (void)comm; // mark used
419 (void)values;
420 (void)count;
421#endif
422}
423
424/*******************************************************************************
425 * \brief Wrapper around MPI_Reduce for op MPI_MAX and datatype MPI_DOUBLE.
426 * \author Frederick Stein
427 ******************************************************************************/
428void cp_mpi_max_root_double(double *values, const int count, const int root,
429 const cp_mpi_comm_t comm) {
430#if defined(__parallel)
431 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
432 double value = 0;
433 void *recvbuf =
434 (1 < count ? cp_mpi_alloc_mem(count * sizeof(double)) : &value);
435 CHECK(MPI_Reduce(values, recvbuf, count, MPI_DOUBLE, MPI_MAX, root, comm));
436 memcpy(values, recvbuf, count * sizeof(double));
437 if (1 < count) {
438 cp_mpi_free_mem(recvbuf);
439 }
440 }
441#else
442 (void)comm; // mark used
443 (void)values;
444 (void)count;
445 (void)root;
446#endif
447}
448
449/*******************************************************************************
450 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT.
451 * \author Ole Schuett
452 ******************************************************************************/
453void cp_mpi_sum_int(int *values, const int count, const cp_mpi_comm_t comm) {
454#if defined(__parallel)
455 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
456 int value = 0;
457 void *recvbuf =
458 (1 < count ? cp_mpi_alloc_mem(count * sizeof(int)) : &value);
459 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT, MPI_SUM, comm));
460 memcpy(values, recvbuf, count * sizeof(int));
461 if (1 < count) {
462 cp_mpi_free_mem(recvbuf);
463 }
464 }
465#else
466 (void)comm; // mark used
467 (void)values;
468 (void)count;
469#endif
470}
471
472/*******************************************************************************
473 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_LONG.
474 * \author Hans Pabst
475 ******************************************************************************/
476void cp_mpi_sum_long(long *values, const int count, const cp_mpi_comm_t comm) {
477#if defined(__parallel)
478 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
479 long value = 0;
480 void *recvbuf =
481 (1 < count ? cp_mpi_alloc_mem(count * sizeof(long)) : &value);
482 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_LONG, MPI_SUM, comm));
483 memcpy(values, recvbuf, count * sizeof(long));
484 if (1 < count) {
485 cp_mpi_free_mem(recvbuf);
486 }
487 }
488#else
489 (void)comm; // mark used
490 (void)values;
491 (void)count;
492#endif
493}
494
495/*******************************************************************************
496 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT64_T.
497 * \author Ole Schuett
498 ******************************************************************************/
499void cp_mpi_sum_int64(int64_t *values, const int count,
500 const cp_mpi_comm_t comm) {
501#if defined(__parallel)
502 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
503 int64_t value = 0;
504 void *recvbuf =
505 (1 < count ? cp_mpi_alloc_mem(count * sizeof(int64_t)) : &value);
506 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_INT64_T, MPI_SUM, comm));
507 memcpy(values, recvbuf, count * sizeof(int64_t));
508 if (1 < count) {
509 cp_mpi_free_mem(recvbuf);
510 }
511 }
512#else
513 (void)comm; // mark used
514 (void)values;
515 (void)count;
516#endif
517}
518
519/*******************************************************************************
520 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_DOUBLE.
521 * \author Ole Schuett
522 ******************************************************************************/
523void cp_mpi_sum_double(double *values, const int count,
524 const cp_mpi_comm_t comm) {
525#if defined(__parallel)
526 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
527 double value = 0;
528 void *recvbuf =
529 (1 < count ? cp_mpi_alloc_mem(count * sizeof(double)) : &value);
530 CHECK(MPI_Allreduce(values, recvbuf, count, MPI_DOUBLE, MPI_SUM, comm));
531 memcpy(values, recvbuf, count * sizeof(double));
532 if (1 < count) {
533 cp_mpi_free_mem(recvbuf);
534 }
535 }
536#else
537 (void)comm; // mark used
538 (void)values;
539 (void)count;
540#endif
541}
542
543/*******************************************************************************
544 * \brief Wrapper around MPI_Reduce for op MPI_SUM and datatype MPI_DOUBLE.
545 * \author Frederick Stein
546 ******************************************************************************/
547void cp_mpi_sum_root_double(double *values, const int count, const int root,
548 const cp_mpi_comm_t comm) {
549#if defined(__parallel)
550 if (MPI_COMM_NULL != comm) { // !MPI_Comm_compare
551 double value = 0;
552 void *recvbuf =
553 (1 < count ? cp_mpi_alloc_mem(count * sizeof(double)) : &value);
554 CHECK(MPI_Reduce(values, recvbuf, count, MPI_DOUBLE, MPI_SUM, root, comm));
555 memcpy(values, recvbuf, count * sizeof(double));
556 if (1 < count) {
557 cp_mpi_free_mem(recvbuf);
558 }
559 }
560#else
561 (void)comm; // mark used
562 (void)values;
563 (void)count;
564 (void)root;
565#endif
566}
567
568/*******************************************************************************
569 * \brief Wrapper around MPI_Sendrecv for datatype MPI_BYTE.
570 * \author Ole Schuett
571 ******************************************************************************/
572int cp_mpi_sendrecv_byte(const void *sendbuf, const int sendcount,
573 const int dest, const int sendtag, void *recvbuf,
574 const int recvcount, const int source,
575 const int recvtag, const cp_mpi_comm_t comm) {
576#if defined(__parallel)
577 MPI_Status status;
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;
583#else
584 (void)sendbuf; // mark used
585 (void)sendcount;
586 (void)dest;
587 (void)sendtag;
588 (void)recvbuf;
589 (void)recvcount;
590 (void)source;
591 (void)recvtag;
592 (void)comm;
593 fprintf(stderr, "Error: cp_mpi_sendrecv_byte not available without MPI\n");
594 abort();
595#endif
596}
597
598/*******************************************************************************
599 * \brief Wrapper around MPI_Sendrecv for datatype MPI_DOUBLE.
600 * \author Ole Schuett
601 ******************************************************************************/
602int cp_mpi_sendrecv_double(const double *sendbuf, const int sendcount,
603 const int dest, const int sendtag, double *recvbuf,
604 const int recvcount, const int source,
605 const int recvtag, const cp_mpi_comm_t comm) {
606#if defined(__parallel)
607 MPI_Status status;
608 CHECK(MPI_Sendrecv(sendbuf, sendcount, MPI_DOUBLE, dest, sendtag, recvbuf,
609 recvcount, MPI_DOUBLE, source, recvtag, comm, &status));
610 int count_received;
611 CHECK(MPI_Get_count(&status, MPI_DOUBLE, &count_received));
612 return count_received;
613#else
614 (void)sendbuf; // mark used
615 (void)sendcount;
616 (void)dest;
617 (void)sendtag;
618 (void)recvbuf;
619 (void)recvcount;
620 (void)source;
621 (void)recvtag;
622 (void)comm;
623 fprintf(stderr, "Error: cp_mpi_sendrecv_double not available without MPI\n");
624 abort();
625#endif
626}
627
628/*******************************************************************************
629 * \brief Wrapper around MPI_Alltoall for datatype MPI_INT.
630 * \author Ole Schuett
631 ******************************************************************************/
632void cp_mpi_alltoall_int(const int *sendbuf, const int sendcount, int *recvbuf,
633 const int recvcount, const cp_mpi_comm_t comm) {
634#if defined(__parallel)
635 CHECK(MPI_Alltoall(sendbuf, sendcount, MPI_INT, recvbuf, recvcount, MPI_INT,
636 comm));
637#else
638 (void)comm; // mark used
639 cp_assert(sendcount == recvcount);
640 memcpy(recvbuf, sendbuf, sendcount * sizeof(int));
641#endif
642}
643
644/*******************************************************************************
645 * \brief Wrapper around MPI_Alltoallv for datatype MPI_BYTE.
646 * \author Ole Schuett
647 ******************************************************************************/
648void cp_mpi_alltoallv_byte(const void *sendbuf, const int *sendcounts,
649 const int *sdispls, void *recvbuf,
650 const int *recvcounts, const int *rdispls,
651 const cp_mpi_comm_t comm) {
652#if defined(__parallel)
653 CHECK(MPI_Alltoallv(sendbuf, sendcounts, sdispls, MPI_BYTE, recvbuf,
654 recvcounts, rdispls, MPI_BYTE, comm));
655#else
656 (void)comm; // mark used
657 cp_assert(sendcounts[0] == recvcounts[0]);
658 cp_assert(sdispls[0] == 0 && rdispls[0] == 0);
659 memcpy(recvbuf, sendbuf, sendcounts[0]);
660#endif
661}
662
663/*******************************************************************************
664 * \brief Wrapper around MPI_Alltoallv for datatype MPI_DOUBLE.
665 * \author Ole Schuett
666 ******************************************************************************/
667void cp_mpi_alltoallv_double(const double *sendbuf, const int *sendcounts,
668 const int *sdispls, double *recvbuf,
669 const int *recvcounts, const int *rdispls,
670 const cp_mpi_comm_t comm) {
671#if defined(__parallel)
672 CHECK(MPI_Alltoallv(sendbuf, sendcounts, sdispls, MPI_DOUBLE, recvbuf,
673 recvcounts, rdispls, MPI_DOUBLE, comm));
674#else
675 (void)comm; // mark used
676 cp_assert(sendcounts[0] == recvcounts[0]);
677 cp_assert(sdispls[0] == 0 && rdispls[0] == 0);
678 memcpy(recvbuf, sendbuf, sendcounts[0] * sizeof(double));
679#endif
680}
681
682/*******************************************************************************
683 * \brief Wrapper around MPI_Bcast for datatype MPI_INT.
684 * \author Frederick Stein
685 ******************************************************************************/
686void cp_mpi_bcast_int(int *buf, const int count, const int root,
687 const cp_mpi_comm_t comm) {
688#if defined(__parallel)
689 CHECK(MPI_Bcast(buf, count, MPI_INT, root, comm));
690#else
691 (void)comm; // mark used
692 (void)count;
693 (void)root;
694 (void)buf;
695#endif
696}
697
698/*******************************************************************************
699 * \brief Wrapper around MPI_Bcast for datatype MPI_CHAR.
700 * \author Frederick Stein
701 ******************************************************************************/
702void cp_mpi_bcast_char(char *buf, const int count, const int root,
703 const cp_mpi_comm_t comm) {
704#if defined(__parallel)
705 CHECK(MPI_Bcast(buf, count, MPI_CHAR, root, comm));
706#else
707 (void)comm; // mark used
708 (void)count;
709 (void)root;
710 (void)buf;
711#endif
712}
713
714/*******************************************************************************
715 * \brief Wrapper around MPI_Alloc_mem.
716 * \author Hans Pabst
717 ******************************************************************************/
718void *cp_mpi_alloc_mem(size_t size) {
719 void *result = NULL;
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 /*v5.0*/ <= _OPENMP)
723 result = omp_alloc(size, omp_null_allocator);
724#else
725 result = malloc(size);
726#endif
727 return result;
728}
729
730/*******************************************************************************
731 * \brief Wrapper around MPI_Free_mem.
732 * \author Hans Pabst
733 ******************************************************************************/
734void cp_mpi_free_mem(void *mem) {
735#if DBM_ALLOC_MPI && defined(__parallel)
736 CHECK(MPI_Free_mem(mem));
737#elif DBM_ALLOC_OPENMP && (201811 /*v5.0*/ <= _OPENMP)
738 omp_free(mem, omp_null_allocator);
739#else
740 free(mem);
741#endif
742}
743
744// EOF
#define cp_assert(condition)
Definition cp_assert.h:22
int cp_mpi_comm_size(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_size.
Definition cp_mpi.c:208
void cp_mpi_free_mem(void *mem)
Wrapper around MPI_Free_mem.
Definition cp_mpi.c:734
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.
Definition cp_mpi.c:357
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.
Definition cp_mpi.c:523
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.
Definition cp_mpi.c:253
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.
Definition cp_mpi.c:572
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.
Definition cp_mpi.c:667
int cp_mpi_cart_rank(const cp_mpi_comm_t comm, const int coords[])
Wrapper around MPI_Cart_rank.
Definition cp_mpi.c:293
void cp_mpi_init(int *argc, char ***argv)
Initialize MPI with MPI_THREAD_MULTIPLE or attach to an active MPI.
Definition cp_mpi.c:78
int cp_mpi_comm_c2f(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_c2f.
Definition cp_mpi.c:178
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.
Definition cp_mpi.c:702
void * cp_mpi_alloc_mem(size_t size)
Wrapper around MPI_Alloc_mem.
Definition cp_mpi.c:718
void cp_mpi_barrier(const cp_mpi_comm_t comm)
Wrapper around MPI_Barrier; a null communicator is a no-op.
Definition cp_mpi.c:224
cp_mpi_comm_t cp_mpi_get_comm_null(void)
Returns MPI_COMM_WORLD.
Definition cp_mpi.c:150
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.
Definition cp_mpi.c:380
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.
Definition cp_mpi.c:428
cp_mpi_comm_t cp_mpi_get_comm_world(void)
Returns MPI_COMM_WORLD.
Definition cp_mpi.c:138
void cp_mpi_cart_get(const cp_mpi_comm_t comm, int maxdims, int dims[], int periods[], int coords[])
Wrapper around MPI_Cart_get.
Definition cp_mpi.c:275
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.
Definition cp_mpi.c:404
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.
Definition cp_mpi.c:499
void cp_mpi_comm_free(cp_mpi_comm_t *comm)
Wrapper around MPI_Comm_free.
Definition cp_mpi.c:327
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.
Definition cp_mpi.c:632
void cp_mpi_dims_create(const int nnodes, const int ndims, int dims[])
Wrapper around MPI_Dims_create.
Definition cp_mpi.c:238
bool cp_mpi_comms_are_similar(const cp_mpi_comm_t comm1, const cp_mpi_comm_t comm2)
Wrapper around MPI_Comm_compare.
Definition cp_mpi.c:340
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.
Definition cp_mpi.c:453
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.
Definition cp_mpi.c:476
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.
Definition cp_mpi.c:547
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.
Definition cp_mpi.c:648
void cp_mpi_finalize(void)
Detach from MPI and finalize it only if cp_mpi_init initialized it.
Definition cp_mpi.c:112
cp_mpi_comm_t cp_mpi_comm_f2c(const int fortran_comm)
Wrapper around MPI_Comm_f2c.
Definition cp_mpi.c:162
cp_mpi_comm_t cp_mpi_cart_sub(const cp_mpi_comm_t comm, const int remain_dims[])
Wrapper around MPI_Cart_sub.
Definition cp_mpi.c:309
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.
Definition cp_mpi.c:686
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.
Definition cp_mpi.c:602
int cp_mpi_comm_rank(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
Definition cp_mpi.c:191
int cp_mpi_comm_t
Definition cp_mpi.h:18
static void const int const int i