(git:9ccafda)
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 Wrapper around MPI_Comm_f2c.
46 * \author Ole Schuett
47 ******************************************************************************/
48cp_mpi_comm_t cp_mpi_comm_f2c(const int fortran_comm);
49
50/*******************************************************************************
51 * \brief Wrapper around MPI_Comm_c2f.
52 * \author Ole Schuett
53 ******************************************************************************/
54int cp_mpi_comm_c2f(const cp_mpi_comm_t comm);
55
56/*******************************************************************************
57 * \brief Wrapper around MPI_Comm_rank.
58 * \author Ole Schuett
59 ******************************************************************************/
60int cp_mpi_comm_rank(const cp_mpi_comm_t comm);
61
62/*******************************************************************************
63 * \brief Wrapper around MPI_Comm_size.
64 * \author Ole Schuett
65 ******************************************************************************/
66int cp_mpi_comm_size(const cp_mpi_comm_t comm);
67
68/*******************************************************************************
69 * \brief Wrapper around MPI_Barrier; a null communicator is a no-op.
70 ******************************************************************************/
71void cp_mpi_barrier(const cp_mpi_comm_t comm);
72
73/*******************************************************************************
74 * \brief Wrapper around MPI_Dims_create.
75 * \author Ole Schuett
76 ******************************************************************************/
77void cp_mpi_dims_create(const int nnodes, const int ndims, int dims[]);
78
79/*******************************************************************************
80 * \brief Wrapper around MPI_Cart_create.
81 * \author Ole Schuett
82 ******************************************************************************/
83cp_mpi_comm_t cp_mpi_cart_create(const cp_mpi_comm_t comm_old, const int ndims,
84 const int dims[], const int periods[],
85 const int reorder);
86
87/*******************************************************************************
88 * \brief Wrapper around MPI_Cart_get.
89 * \author Ole Schuett
90 ******************************************************************************/
91void cp_mpi_cart_get(const cp_mpi_comm_t comm, int maxdims, int dims[],
92 int periods[], int coords[]);
93
94/*******************************************************************************
95 * \brief Wrapper around MPI_Cart_rank.
96 * \author Ole Schuett
97 ******************************************************************************/
98int cp_mpi_cart_rank(const cp_mpi_comm_t comm, const int coords[]);
99
100/*******************************************************************************
101 * \brief Wrapper around MPI_Cart_sub.
102 * \author Ole Schuett
103 ******************************************************************************/
105 const int remain_dims[]);
106
107/*******************************************************************************
108 * \brief Wrapper around MPI_Comm_free.
109 * \author Ole Schuett
110 ******************************************************************************/
112
113/*******************************************************************************
114 * \brief Wrapper around MPI_Comm_compare.
115 * \author Ole Schuett
116 ******************************************************************************/
118 const cp_mpi_comm_t comm2);
119
120/*******************************************************************************
121 * \brief Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_INT.
122 * \author Ole Schuett
123 ******************************************************************************/
124void cp_mpi_max_int(int *values, const int count, const cp_mpi_comm_t comm);
125
126/*******************************************************************************
127 * \brief Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_UINT64_T.
128 * \author Hans Pabst
129 ******************************************************************************/
130void cp_mpi_max_uint64(uint64_t *values, const int count,
131 const cp_mpi_comm_t comm);
132
133/*******************************************************************************
134 * \brief Wrapper around MPI_Allreduce for op MPI_MAX and datatype MPI_DOUBLE.
135 * \author Ole Schuett
136 ******************************************************************************/
137void cp_mpi_max_double(double *values, const int count,
138 const cp_mpi_comm_t comm);
139
140/*******************************************************************************
141 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT.
142 * \author Ole Schuett
143 ******************************************************************************/
144void cp_mpi_sum_int(int *values, const int count, const cp_mpi_comm_t comm);
145
146/*******************************************************************************
147 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_LONG.
148 * \author Hans Pabst
149 ******************************************************************************/
150void cp_mpi_sum_long(long *values, const int count, const cp_mpi_comm_t comm);
151
152/*******************************************************************************
153 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_INT64_T.
154 * \author Ole Schuett
155 ******************************************************************************/
156void cp_mpi_sum_int64(int64_t *values, const int count,
157 const cp_mpi_comm_t comm);
158
159/*******************************************************************************
160 * \brief Wrapper around MPI_Allreduce for op MPI_SUM and datatype MPI_DOUBLE.
161 * \author Ole Schuett
162 ******************************************************************************/
163void cp_mpi_sum_double(double *values, const int count,
164 const cp_mpi_comm_t comm);
165
166/*******************************************************************************
167 * \brief Wrapper around MPI_Sendrecv for datatype MPI_BYTE.
168 * \author Ole Schuett
169 ******************************************************************************/
170int cp_mpi_sendrecv_byte(const void *sendbuf, const int sendcount,
171 const int dest, const int sendtag, void *recvbuf,
172 const int recvcount, const int source,
173 const int recvtag, const cp_mpi_comm_t comm);
174
175/*******************************************************************************
176 * \brief Wrapper around MPI_Sendrecv for datatype MPI_DOUBLE.
177 * \author Ole Schuett
178 ******************************************************************************/
179int cp_mpi_sendrecv_double(const double *sendbuf, const int sendcount,
180 const int dest, const int sendtag, double *recvbuf,
181 const int recvcount, const int source,
182 const int recvtag, const cp_mpi_comm_t comm);
183
184/*******************************************************************************
185 * \brief Wrapper around MPI_Alltoall for datatype MPI_INT.
186 * \author Ole Schuett
187 ******************************************************************************/
188void cp_mpi_alltoall_int(const int *sendbuf, const int sendcount, int *recvbuf,
189 const int recvcount, const cp_mpi_comm_t comm);
190
191/*******************************************************************************
192 * \brief Wrapper around MPI_Alltoallv for datatype MPI_BYTE.
193 * \author Ole Schuett
194 ******************************************************************************/
195void cp_mpi_alltoallv_byte(const void *sendbuf, const int *sendcounts,
196 const int *sdispls, void *recvbuf,
197 const int *recvcounts, const int *rdispls,
198 const cp_mpi_comm_t comm);
199
200/*******************************************************************************
201 * \brief Wrapper around MPI_Alltoallv for datatype MPI_DOUBLE.
202 * \author Ole Schuett
203 ******************************************************************************/
204void cp_mpi_alltoallv_double(const double *sendbuf, const int *sendcounts,
205 const int *sdispls, double *recvbuf,
206 const int *recvcounts, const int *rdispls,
207 const cp_mpi_comm_t comm);
208
209/*******************************************************************************
210 * \brief Wrapper around MPI_Alloc_mem.
211 * \author Hans Pabst
212 ******************************************************************************/
213void *cp_mpi_alloc_mem(size_t size);
214
215/*******************************************************************************
216 * \brief Wrapper around MPI_Free_mem.
217 * \author Hans Pabst
218 ******************************************************************************/
219void cp_mpi_free_mem(void *ptr);
220
221#endif
222
223// EOF
int cp_mpi_comm_size(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_size.
Definition cp_mpi.c:194
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:343
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:484
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:239
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:508
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:603
int cp_mpi_cart_rank(const cp_mpi_comm_t comm, const int coords[])
Wrapper around MPI_Cart_rank.
Definition cp_mpi.c:279
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:164
void * cp_mpi_alloc_mem(size_t size)
Wrapper around MPI_Alloc_mem.
Definition cp_mpi.c:622
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:210
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:366
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:261
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:390
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:460
void cp_mpi_comm_free(cp_mpi_comm_t *comm)
Wrapper around MPI_Comm_free.
Definition cp_mpi.c:313
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:568
void cp_mpi_dims_create(const int nnodes, const int ndims, int dims[])
Wrapper around MPI_Dims_create.
Definition cp_mpi.c:224
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:326
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:414
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:437
void cp_mpi_free_mem(void *ptr)
Wrapper around MPI_Free_mem.
Definition cp_mpi.c:638
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:584
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:150
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:295
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:538
int cp_mpi_comm_rank(const cp_mpi_comm_t comm)
Wrapper around MPI_Comm_rank.
Definition cp_mpi.c:177