Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
wrapper.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
16
20#include "shambase/time.hpp"
24#include "shamcomm/wrapper.hpp"
25#include <unordered_map>
26#include <array>
27
28namespace {
29
30 std::unordered_map<std::string, f64> mpi_timers;
31
32} // namespace
33
34namespace shamcomm::mpi {
35 void register_time(std::string timername, f64 time) {
36 mpi_timers[timername] += time;
37 mpi_timers["total"] += time;
38
40 auto wtime = shambase::details::get_wtime();
41 shambase::profiling::register_counter_val(timername, wtime, mpi_timers[timername]);
42 shambase::profiling::register_counter_val("total MPi time", wtime, mpi_timers["total"]);
43 }
44 }
45
46 f64 get_timer(std::string timername) { return mpi_timers[timername]; }
47
48 const std::unordered_map<std::string, f64> &get_timers() { return mpi_timers; }
49
50 std::vector<std::string> possible_keys{
51 "total", "MPI_Isend", "MPI_Irecv",
52 "MPI_Allreduce", "MPI_Allgather", "MPI_Allgatherv",
53 "MPI_Exscan", "MPI_Wait", "MPI_Waitall",
54 "MPI_Barrier", "MPI_Probe", "MPI_Recv",
55 "MPI_Get_count", "MPI_Send", "MPI_File_set_view",
56 "MPI_Type_size", "MPI_File_write_all", "MPI_File_write",
57 "MPI_File_read", "MPI_File_write_at", "MPI_File_read_at",
58 "MPI_File_close", "MPI_File_open", "MPI_Test",
59 "MPI_Gather", "MPI_Gatherv",
60 };
61
62 const std::vector<std::string> &get_possible_keys() { return possible_keys; }
63
64} // namespace shamcomm::mpi
65
66namespace {
67
68 template<class Func>
69 inline void wrap_profiling(std::string timername, Func &&f) {
70 f64 tstart;
72 f();
74 }
75
76} // namespace
77
78namespace shamcomm::mpi {
79
80 void check_tag_value(i32 tag) {
81 if (tag > mpi_max_tag_value()) {
83 "mpi_max_tag_value ({}) exceeded with tag {}", mpi_max_tag_value(), tag));
84 }
85 }
86
87 void Isend(
88 const void *buf,
89 int count,
90 MPI_Datatype datatype,
91 int dest,
92 int tag,
93 MPI_Comm comm,
94 MPI_Request *request) {
95 StackEntry stack_loc{};
96
97 check_tag_value(tag);
98
99 wrap_profiling("MPI_Isend", [&]() {
100 MPICHECK(MPI_Isend(buf, count, datatype, dest, tag, comm, request));
101 });
102 }
103
104 void Irecv(
105 void *buf,
106 int count,
107 MPI_Datatype datatype,
108 int source,
109 int tag,
110 MPI_Comm comm,
111 MPI_Request *request) {
112 StackEntry stack_loc{};
113
114 check_tag_value(tag);
115
116 wrap_profiling("MPI_Irecv", [&]() {
117 MPICHECK(MPI_Irecv(buf, count, datatype, source, tag, comm, request));
118 });
119 }
120
122 const void *sendbuf,
123 void *recvbuf,
124 int count,
125 MPI_Datatype datatype,
126 MPI_Op op,
127 MPI_Comm comm) {
128 StackEntry stack_loc{};
129
130 wrap_profiling("MPI_Allreduce", [&]() {
131 MPICHECK(MPI_Allreduce(sendbuf, recvbuf, count, datatype, op, comm));
132 });
133 }
134
136 const void *sendbuf,
137 int sendcount,
138 MPI_Datatype sendtype,
139 void *recvbuf,
140 int recvcount,
141 MPI_Datatype recvtype,
142 MPI_Comm comm) {
143 StackEntry stack_loc{};
144
145 wrap_profiling("MPI_Allgather", [&]() {
146 MPICHECK(
147 MPI_Allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm));
148 });
149 }
150
152 const void *sendbuf,
153 int sendcount,
154 MPI_Datatype sendtype,
155 void *recvbuf,
156 const int recvcounts[],
157 const int displs[],
158 MPI_Datatype recvtype,
159 MPI_Comm comm) {
160 StackEntry stack_loc{};
161
162 wrap_profiling("MPI_Allgatherv", [&]() {
163 MPICHECK(MPI_Allgatherv(
164 sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm));
165 });
166 }
167
168 void Exscan(
169 const void *sendbuf,
170 void *recvbuf,
171 int count,
172 MPI_Datatype datatype,
173 MPI_Op op,
174 MPI_Comm comm) {
175 StackEntry stack_loc{};
176
177 wrap_profiling("MPI_Exscan", [&]() {
178 MPICHECK(MPI_Exscan(sendbuf, recvbuf, count, datatype, op, comm));
179 });
180 }
181
182 void Wait(MPI_Request *request, MPI_Status *status) {
183 StackEntry stack_loc{};
184 wrap_profiling("MPI_Wait", [&]() {
185 MPICHECK(MPI_Wait(request, status));
186 });
187 }
188
189 void Waitall(int count, MPI_Request array_of_requests[], MPI_Status *array_of_statuses) {
190 StackEntry stack_loc{};
191 wrap_profiling("MPI_Waitall", [&]() {
192 MPICHECK(MPI_Waitall(count, array_of_requests, array_of_statuses));
193 });
194 }
195
196 void Barrier(MPI_Comm comm) {
197 StackEntry stack_loc{};
198 wrap_profiling("MPI_Barrier", [&]() {
199 MPICHECK(MPI_Barrier(comm));
200 });
201 }
202
203 void Probe(int source, int tag, MPI_Comm comm, MPI_Status *status) {
204 StackEntry stack_loc{};
205 wrap_profiling("MPI_Probe", [&]() {
206 MPICHECK(MPI_Probe(source, tag, comm, status));
207 });
208 }
209
210 void Recv(
211 void *buf,
212 int count,
213 MPI_Datatype datatype,
214 int source,
215 int tag,
216 MPI_Comm comm,
217 MPI_Status *status) {
218 StackEntry stack_loc{};
219 wrap_profiling("MPI_Recv", [&]() {
220 MPICHECK(MPI_Recv(buf, count, datatype, source, tag, comm, status));
221 });
222 }
223
224 void Get_count(const MPI_Status *status, MPI_Datatype datatype, int *count) {
225 StackEntry stack_loc{};
226 wrap_profiling("MPI_Get_count", [&]() {
227 MPICHECK(MPI_Get_count(status, datatype, count));
228 });
229 }
230
231 void Send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) {
232 StackEntry stack_loc{};
233 wrap_profiling("MPI_Send", [&]() {
234 MPICHECK(MPI_Send(buf, count, datatype, dest, tag, comm));
235 });
236 }
237
239 MPI_File fh,
240 MPI_Offset disp,
241 MPI_Datatype etype,
242 MPI_Datatype filetype,
243 const char *datarep,
244 MPI_Info info) {
245 StackEntry stack_loc{};
246 wrap_profiling("MPI_File_set_view", [&]() {
247 MPICHECK(MPI_File_set_view(fh, disp, etype, filetype, datarep, info));
248 });
249 }
250
251 void Type_size(MPI_Datatype type, int *size) {
252 StackEntry stack_loc{};
253 wrap_profiling("MPI_Type_size", [&]() {
254 MPICHECK(MPI_Type_size(type, size));
255 });
256 }
257
259 MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status) {
260 StackEntry stack_loc{};
261 wrap_profiling("MPI_File_write_all", [&]() {
262 MPICHECK(MPI_File_write_all(fh, buf, count, datatype, status));
263 });
264 }
265
267 MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status) {
268 StackEntry stack_loc{};
269 wrap_profiling("MPI_File_write", [&]() {
270 MPICHECK(MPI_File_write(fh, buf, count, datatype, status));
271 });
272 }
273
274 void File_read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status) {
275 StackEntry stack_loc{};
276 wrap_profiling("MPI_File_read", [&]() {
277 MPICHECK(MPI_File_read(fh, buf, count, datatype, status));
278 });
279 }
280
282 MPI_File fh,
283 MPI_Offset offset,
284 const void *buf,
285 int count,
286 MPI_Datatype datatype,
287 MPI_Status *status) {
288 StackEntry stack_loc{};
289 wrap_profiling("MPI_File_write_at", [&]() {
290 MPICHECK(MPI_File_write_at(fh, offset, buf, count, datatype, status));
291 });
292 }
293
295 MPI_File fh,
296 MPI_Offset offset,
297 void *buf,
298 int count,
299 MPI_Datatype datatype,
300 MPI_Status *status) {
301 StackEntry stack_loc{};
302 wrap_profiling("MPI_File_read_at", [&]() {
303 MPICHECK(MPI_File_read_at(fh, offset, buf, count, datatype, status));
304 });
305 }
306
307 void File_close(MPI_File *fh) {
308 StackEntry stack_loc{};
309 wrap_profiling("MPI_File_close", [&]() {
310 MPICHECK(MPI_File_close(fh));
311 });
312 }
313
314 void File_open(MPI_Comm comm, const char *filename, int amode, MPI_Info info, MPI_File *fh) {
315 StackEntry stack_loc{};
316 wrap_profiling("MPI_File_open", [&]() {
317 MPICHECK(MPI_File_open(comm, filename, amode, info, fh));
318 });
319 }
320
321 void Test(MPI_Request *request, int *flag, MPI_Status *status) {
322 StackEntry stack_loc{};
323 wrap_profiling("MPI_Test", [&]() {
324 MPICHECK(MPI_Test(request, flag, status));
325 });
326 }
327
328 void Gather(
329 const void *sendbuf,
330 int sendcount,
331 MPI_Datatype sendtype,
332 void *recvbuf,
333 int recvcount,
334 MPI_Datatype recvtype,
335 int root,
336 MPI_Comm comm) {
337 StackEntry stack_loc{};
338 wrap_profiling("MPI_Gather", [&]() {
339 MPICHECK(
340 MPI_Gather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm));
341 });
342 }
343
345 const void *sendbuf,
346 int sendcount,
347 MPI_Datatype sendtype,
348 void *recvbuf,
349 const int recvcounts[],
350 const int displs[],
351 MPI_Datatype recvtype,
352 int root,
353 MPI_Comm comm) {
354 StackEntry stack_loc{};
355 wrap_profiling("MPI_Gatherv", [&]() {
356 MPICHECK(MPI_Gatherv(
357 sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm));
358 });
359 }
360
361} // namespace shamcomm::mpi
double f64
Alias for double.
std::int32_t i32
32 bit integer
This header file contains utility functions related to exception handling in the code.
Core formatting functions: format, vformat, and format_printf.
Utility functions for MPI error checking.
#define MPICHECK(mpicall)
Shortcut macro to check MPI return codes.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
i32 mpi_max_tag_value()
Gets the maximum value of the MPI tag.
Definition worldInfo.cpp:37
bool is_profiling_enabled()
Check if profiling is enabled.
Definition profiling.cpp:94
void register_counter_val(const std::string &name, f64 time, f64 val)
Register a counter value.
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
f64 get_wtime()
Returns the current wall clock time in seconds.
Functions related to the MPI communicator.
void File_set_view(MPI_File fh, MPI_Offset disp, MPI_Datatype etype, MPI_Datatype filetype, const char *datarep, MPI_Info info)
MPI wrapper for MPI_File_set_view.
Definition wrapper.cpp:238
void Get_count(const MPI_Status *status, MPI_Datatype datatype, int *count)
MPI wrapper for MPI_Get_count.
Definition wrapper.cpp:224
void Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status)
MPI wrapper for MPI_Recv.
Definition wrapper.cpp:210
void Irecv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Request *request)
MPI wrapper for MPI_Irecv.
Definition wrapper.cpp:104
void Exscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
MPI wrapper for MPI_Exscan.
Definition wrapper.cpp:168
void Gatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int recvcounts[], const int displs[], MPI_Datatype recvtype, int root, MPI_Comm comm)
MPI wrapper for MPI_Gatherv.
Definition wrapper.cpp:344
void Probe(int source, int tag, MPI_Comm comm, MPI_Status *status)
MPI wrapper for MPI_Probe.
Definition wrapper.cpp:203
void Barrier(MPI_Comm comm)
MPI wrapper for MPI_Barrier.
Definition wrapper.cpp:196
void File_close(MPI_File *fh)
MPI wrapper for MPI_File_close.
Definition wrapper.cpp:307
void File_read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
MPI wrapper for MPI_File_read.
Definition wrapper.cpp:274
void Allgatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int recvcounts[], const int displs[], MPI_Datatype recvtype, MPI_Comm comm)
MPI wrapper for MPI_Allgatherv.
Definition wrapper.cpp:151
const std::vector< std::string > & get_possible_keys()
return all possible keys for the internal timers
Definition wrapper.cpp:62
void register_time(std::string timername, f64 time)
Register a timer value.
Definition wrapper.cpp:35
f64 get_timer(std::string timername)
get a timer value
Definition wrapper.cpp:46
void File_write_at(MPI_File fh, MPI_Offset offset, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
MPI wrapper for MPI_File_write_at.
Definition wrapper.cpp:281
void File_open(MPI_Comm comm, const char *filename, int amode, MPI_Info info, MPI_File *fh)
MPI wrapper for MPI_File_open.
Definition wrapper.cpp:314
void Allreduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
MPI wrapper for MPI_Allreduce.
Definition wrapper.cpp:121
void Waitall(int count, MPI_Request array_of_requests[], MPI_Status *array_of_statuses)
MPI wrapper for MPI_Waitall.
Definition wrapper.cpp:189
void Wait(MPI_Request *request, MPI_Status *status)
MPI wrapper for MPI_Wait.
Definition wrapper.cpp:182
void Type_size(MPI_Datatype type, int *size)
MPI wrapper for MPI_Type_size.
Definition wrapper.cpp:251
const std::unordered_map< std::string, f64 > & get_timers()
return all internal timers
Definition wrapper.cpp:48
void Gather(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
MPI wrapper for MPI_Gather.
Definition wrapper.cpp:328
void File_write(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
MPI wrapper for MPI_File_write.
Definition wrapper.cpp:266
void Send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)
MPI wrapper for MPI_Send.
Definition wrapper.cpp:231
void Test(MPI_Request *request, int *flag, MPI_Status *status)
MPI wrapper for MPI_Test.
Definition wrapper.cpp:321
void Allgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
MPI wrapper for MPI_Allgather.
Definition wrapper.cpp:135
void Isend(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request *request)
MPI wrapper for MPI_Isend.
Definition wrapper.cpp:87
void File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
MPI wrapper for MPI_File_read_at.
Definition wrapper.cpp:294
void File_write_all(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
MPI wrapper for MPI_File_write_all.
Definition wrapper.cpp:258