(git:5a3db73)
Loading...
Searching...
No Matches
cp_mpi.h
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#ifndef CP_MPI_H
8#define CP_MPI_H
9
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13
14#if defined(__parallel)
15#include <mpi.h>
16typedef MPI_Comm cp_mpi_comm_t;
17#else
18typedef int cp_mpi_comm_t;
19#endif
20
21/*******************************************************************************
22 * \brief Initialize MPI with MPI_THREAD_MULTIPLE or attach to an active MPI.
23 *
24 * Must be called outside OpenMP parallel regions and paired with
25 * cp_mpi_finalize.
26 * \author Ole Schuett
27 ******************************************************************************/
28void cp_mpi_init(int *argc, char ***argv);
29
30/*******************************************************************************
31 * \brief Detach from MPI and finalize it only if cp_mpi_init initialized it.
32 *
33 * Must be called outside OpenMP parallel regions.
34 * \author Ole Schuett
35 ******************************************************************************/
36void cp_mpi_finalize(void);
37
38/*******************************************************************************
39 * \brief Returns MPI_COMM_WORLD.
40 * \author Ole Schuett
41 ******************************************************************************/
43
44/*******************************************************************************
45 * \brief Returns MPI_COMM_WORLD.
46 * \author Ole Schuett
47 ******************************************************************************/
49
50/*******************************************************************************
51 * \brief Wrapper around MPI_Comm_f2c.
52 * \author Ole Schuett
53 ******************************************************************************/
54cp_mpi_comm_t cp_mpi_comm_f2c(const int fortran_comm);
55
56/*******************************************************************************
57 * \brief Wrapper around MPI_Comm_c2f.
58 * \author Ole Schuett
59 ******************************************************************************/
60int cp_mpi_comm_c2f(const cp_mpi_comm_t comm);
61
62/*******************************************************************************
63 * \brief Wrapper around MPI_Comm_rank.
64 * \author Ole Schuett
65 ******************************************************************************/
66int cp_mpi_comm_rank(const cp_mpi_comm_t comm);
67
68/*******************************************************************************
69 * \brief Wrapper around MPI_Comm_size.
70 * \author Ole Schuett
71 ******************************************************************************/
72int cp_mpi_comm_size(const cp_mpi_comm_t comm);
73
74/*******************************************************************************
75 * \brief Wrapper around MPI_Barrier; a null communicator is a no-op.
76 ******************************************************************************/
77void cp_mpi_barrier(const cp_mpi_comm_t comm);
78
79/*******************************************************************************
80 * \brief Wrapper around MPI_Dims_create.
81 * \author Ole Schuett
82 ******************************************************************************/
83void cp_mpi_dims_create(const int nnodes, const int ndims, int dims[]);
84
85/*******************************************************************************
86 * \brief Wrapper around MPI_Cart_create.
87 * \author Ole Schuett
88 ******************************************************************************/
89cp_mpi_comm_t cp_mpi_cart_create(const cp_mpi_comm_t comm_old, const int ndims,
90 const int dims[], const int periods[],
91 const int reorder);
92
93/*******************************************************************************
94 * \brief Wrapper around MPI_Cart_get.
95 * \author Ole Schuett
96 ******************************************************************************/
97void cp_mpi_cart_get(const cp_mpi_comm_t comm, int maxdims, int dims[],
98 int periods[], int coords[]);
99
100/*******************************************************************************
101 * \brief Wrapper around MPI_Cart_rank.
102 * \author Ole Schuett
103 ******************************************************************************/
104int cp_mpi_cart_rank(const cp_mpi_comm_t comm, const int coords[]);
105
106/*******************************************************************************
107 * \brief Wrapper around MPI_Cart_sub.
108 * \author Ole Schuett
109 ******************************************************************************/
111 const int remain_dims[]);
112
113/*******************************************************************************
114 * \brief Wrapper around MPI_Comm_free.
115 * \author Ole Schuett
116 ******************************************************************************/
118
119/*******************************************************************************
120 * \brief Wrapper around MPI_Comm_compare.
121 * \author Ole Schuett
122 ******************************************************************************/
124 const cp_mpi_comm_t comm2);
125
126/*******************************************************************************
127 * \brief Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_INT.
128 * \author Ole Schuett
129 ******************************************************************************/
130void cp_mpi_max_int(int *values, const int count, const cp_mpi_comm_t comm);
131
132/*******************************************************************************
133 * \brief Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_UINT64_T.
134 * \author Hans Pabst
135 ******************************************************************************/
136void cp_mpi_max_uint64(uint64_t *values, const int count,
137 const cp_mpi_comm_t comm);
138
139/*******************************************************************************
140 * \brief Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_DOUBLE.
141 * \author Ole Schuett
142 ******************************************************************************/
143void cp_mpi_max_double(double *values, const int count,
144 const cp_mpi_comm_t comm);
145
146/*******************************************************************************
147 * \brief Wrapper around MPI_Reduce for op MPI_MAX and datatype MPI_DOUBLE.
148 * \author Frederick Stein
149 ******************************************************************************/
150void cp_mpi_max_root_double(double *values, const int count, const int root,
151 const cp_mpi_comm_t comm);
152
153/*******************************************************************************
154 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT.
155 * \author Ole Schuett
156 ******************************************************************************/
157void cp_mpi_sum_int(int *values, const int count, const cp_mpi_comm_t comm);
158
159/*******************************************************************************
160 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_LONG.
161 * \author Hans Pabst
162 ******************************************************************************/
163void cp_mpi_sum_long(long *values, const int count, const cp_mpi_comm_t comm);
164
165/*******************************************************************************
166 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT64_T.
167 * \author Ole Schuett
168 ******************************************************************************/
169void cp_mpi_sum_int64(int64_t *values, const int count,
170 const cp_mpi_comm_t comm);
171
172/*******************************************************************************
173 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_DOUBLE.
174 * \author Ole Schuett
175 ******************************************************************************/
176void cp_mpi_sum_double(double *values, const int count,
177 const cp_mpi_comm_t comm);
178
179/*******************************************************************************
180 * \brief Wrapper around MPI_Reduce for op MPI_SUM and datatype MPI_DOUBLE.
181 * \author Frederick Stein
182 ******************************************************************************/
183void cp_mpi_sum_root_double(double *values, const int count, const int root,
184 const cp_mpi_comm_t comm);
185
186/*******************************************************************************
187 * \brief Wrapper around MPI_Sendrecv for datatype MPI_BYTE.
188 * \author Ole Schuett
189 ******************************************************************************/
190int cp_mpi_sendrecv_byte(const void *sendbuf, const int sendcount,
191 const int dest, const int sendtag, void *recvbuf,
192 const int recvcount, const int source,
193 const int recvtag, const cp_mpi_comm_t comm);
194
195/*******************************************************************************
196 * \brief Wrapper around MPI_Sendrecv for datatype MPI_DOUBLE.
197 * \author Ole Schuett
198 ******************************************************************************/
199int cp_mpi_sendrecv_double(const double *sendbuf, const int sendcount,
200 const int dest, const int sendtag, double *recvbuf,
201 const int recvcount, const int source,
202 const int recvtag, const cp_mpi_comm_t comm);
203
204/*******************************************************************************
205 * \brief Wrapper around MPI_Alltoall for datatype MPI_INT.
206 * \author Ole Schuett
207 ******************************************************************************/
208void cp_mpi_alltoall_int(const int *sendbuf, const int sendcount, int *recvbuf,
209 const int recvcount, const cp_mpi_comm_t comm);
210
211/*******************************************************************************
212 * \brief Wrapper around MPI_Alltoallv for datatype MPI_BYTE.
213 * \author Ole Schuett
214 ******************************************************************************/
215void cp_mpi_alltoallv_byte(const void *sendbuf, const int *sendcounts,
216 const int *sdispls, void *recvbuf,
217 const int *recvcounts, const int *rdispls,
218 const cp_mpi_comm_t comm);
219
220/*******************************************************************************
221 * \brief Wrapper around MPI_Alltoallv for datatype MPI_DOUBLE.
222 * \author Ole Schuett
223 ******************************************************************************/
224void cp_mpi_alltoallv_double(const double *sendbuf, const int *sendcounts,
225 const int *sdispls, double *recvbuf,
226 const int *recvcounts, const int *rdispls,
227 const cp_mpi_comm_t comm);
228
229/*******************************************************************************
230 * \brief Wrapper around MPI_Bcast for datatype MPI_INT.
231 * \author Frederick Stein
232 ******************************************************************************/
233void cp_mpi_bcast_int(int *buf, const int count, const int root,
234 const cp_mpi_comm_t comm);
235
236/*******************************************************************************
237 * \brief Wrapper around MPI_Bcast for datatype MPI_CHAR.
238 * \author Frederick Stein
239 ******************************************************************************/
240void cp_mpi_bcast_char(char *buf, const int count, const int root,
241 const cp_mpi_comm_t comm);
242
243/*******************************************************************************
244 * \brief Wrapper around MPI_Alloc_mem.
245 * \author Hans Pabst
246 ******************************************************************************/
247void *cp_mpi_alloc_mem(size_t size);
248
249/*******************************************************************************
250 * \brief Wrapper around MPI_Free_mem.
251 * \author Hans Pabst
252 ******************************************************************************/
253void cp_mpi_free_mem(void *ptr);
254
255#endif
256
257// EOF
int cp_mpi_comm_size(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_size.
Definition cp_mpi.c:208
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
int cp_mpi_comm_t
Definition cp_mpi.h:18
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_free_mem(void *ptr)
Wrapper around MPI_Free_mem.
Definition cp_mpi.c:734
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