core 1.5.0
Shared MPI block decomposition, asynchronous ghost-layer exchange, SDF geometry, load balancing (header-only C++20)
Loading...
Searching...
No Matches
mpi_stub.hpp
Go to the documentation of this file.
1// core -- single-rank no-MPI stub. When the library is built without MPI (PECLET_CORE_NO_MPI), the
2// grid solver runs as ONE block: every ghost cell is a periodic self-copy onto the same block, so
3// there are no remote neighbours. The only collectives ever EXECUTED at size 1 are the trivial ones
4// -- rank=0, size=1, Allreduce/Bcast are local identities. The point-to-point and
5// neighbourhood-collective calls exist only so the single code path compiles and links; they are
6// never reached at size 1.
7//
8// This keeps "one code": the same GridHaloTopology / GridHalo / NbxEngine run, with MPI replaced by
9// these stubs. Build WITH MPI (the default) and this header is never included -- see
10// peclet/core/common/mpi.hpp.
11#pragma once
12
13#include <cstdlib>
14#include <cstring>
15#include <map>
16
17using MPI_Comm = int;
18using MPI_Request = int;
19using MPI_Datatype = int; // value doubles as the element byte size (so Allreduce can memcpy)
20using MPI_Op = int;
21using MPI_Info = int;
22struct MPI_Status {
25};
26
27#define MPI_COMM_WORLD 0
28#define MPI_COMM_NULL (-1)
29#define MPI_REQUEST_NULL 0
30#define MPI_INFO_NULL 0
31#define MPI_STATUS_IGNORE (static_cast<MPI_Status*>(nullptr))
32#define MPI_STATUSES_IGNORE (static_cast<MPI_Status*>(nullptr))
33#define MPI_ANY_SOURCE (-1)
34#define MPI_UNDEFINED (-32766)
35#define MPI_UNWEIGHTED (static_cast<int*>(nullptr))
36#define MPI_SUCCESS 0
37// datatypes carry their byte size (MPI_Allreduce copies count * datatype bytes)
38#define MPI_BYTE 1
39#define MPI_INT 4
40#define MPI_LONG 8
41#define MPI_DOUBLE 8
42// reduction ops are ignored: at size 1 the local value IS the global value
43#define MPI_SUM 0
44#define MPI_MAX 1
45#define MPI_MIN 2
46#define MPI_LAND 3
47
48inline int MPI_Init(int*, char***) {
49 return MPI_SUCCESS;
50}
51inline int MPI_Initialized(int* f) {
52 *f = 1;
53 return MPI_SUCCESS;
54} // pretend initialised (no real MPI)
55inline int MPI_Finalize() {
56 return MPI_SUCCESS;
57}
58inline int MPI_Finalized(int* f) {
59 *f = 0;
60 return MPI_SUCCESS;
61}
62inline int MPI_Abort(MPI_Comm, int code) {
63 std::abort();
64 return code;
65}
66inline int MPI_Comm_rank(MPI_Comm, int* r) {
67 *r = 0;
68 return MPI_SUCCESS;
69}
70inline int MPI_Comm_size(MPI_Comm, int* s) {
71 *s = 1;
72 return MPI_SUCCESS;
73}
74// Communicator attributes (NbxEngine's per-communicator round counter). Keyed by (comm, keyval).
75using MPI_Comm_copy_attr_function = int(MPI_Comm, int, void*, void*, void*, int*);
76using MPI_Comm_delete_attr_function = int(MPI_Comm, int, void*, void*);
77#define MPI_COMM_NULL_COPY_FN (static_cast<MPI_Comm_copy_attr_function*>(nullptr))
78#define MPI_COMM_NULL_DELETE_FN (static_cast<MPI_Comm_delete_attr_function*>(nullptr))
79#define MPI_KEYVAL_INVALID (-1)
81inline std::map<std::pair<int, int>, void*>& attrs() {
82 static std::map<std::pair<int, int>, void*> m;
83 return m;
84}
85inline int& nextKeyval() {
86 static int k = 100;
87 return k;
88}
89} // namespace peclet::core::mpi_stub
91 int* keyval, void*) {
93 return 0;
94}
95inline int MPI_Comm_get_attr(MPI_Comm c, int keyval, void* val, int* flag) {
97 auto it = m.find({c, keyval});
98 *flag = it != m.end();
99 if (*flag)
100 *static_cast<void**>(val) = it->second;
101 return 0;
102}
103inline int MPI_Comm_set_attr(MPI_Comm c, int keyval, void* val) {
104 peclet::core::mpi_stub::attrs()[{c, keyval}] = val;
105 return 0;
106}
108 return MPI_SUCCESS;
109}
111 return MPI_SUCCESS;
112}
113// single rank: the local contribution is already the global result.
114inline int MPI_Allreduce(const void* sbuf, void* rbuf, int count, MPI_Datatype dt, MPI_Op,
115 MPI_Comm) {
116 if (sbuf != rbuf)
117 std::memcpy(rbuf, sbuf, static_cast<size_t>(count) * static_cast<size_t>(dt));
118 return MPI_SUCCESS;
119}
120inline int MPI_Bcast(void*, int, MPI_Datatype, int, MPI_Comm) {
121 return MPI_SUCCESS;
122} // 1 rank: no-op
123
124// --- never reached at size 1 (no remote neighbours); present only to compile/link the one code
125// path ---
126inline int MPI_Send(const void*, int, MPI_Datatype, int, int, MPI_Comm) {
127 return MPI_SUCCESS;
128}
129inline int MPI_Recv(void*, int, MPI_Datatype, int, int, MPI_Comm, MPI_Status*) {
130 return MPI_SUCCESS;
131}
132inline int MPI_Isend(const void*, int, MPI_Datatype, int, int, MPI_Comm, MPI_Request*) {
133 return MPI_SUCCESS;
134}
135inline int MPI_Irecv(void*, int, MPI_Datatype, int, int, MPI_Comm, MPI_Request*) {
136 return MPI_SUCCESS;
137}
138inline int MPI_Issend(const void*, int, MPI_Datatype, int, int, MPI_Comm, MPI_Request*) {
139 return MPI_SUCCESS;
140}
141inline int MPI_Iprobe(int, int, MPI_Comm, int* flag, MPI_Status*) {
142 *flag = 0;
143 return MPI_SUCCESS;
144}
145inline int MPI_Get_count(const MPI_Status*, MPI_Datatype, int* c) {
146 *c = 0;
147 return MPI_SUCCESS;
148}
149inline int MPI_Test(MPI_Request*, int* flag, MPI_Status*) {
150 *flag = 1;
151 return MPI_SUCCESS;
152}
153inline int MPI_Testall(int, MPI_Request*, int* flag, MPI_Status*) {
154 *flag = 1;
155 return MPI_SUCCESS;
156}
158 return MPI_SUCCESS;
159}
160inline int MPI_Waitall(int, MPI_Request*, MPI_Status*) {
161 return MPI_SUCCESS;
162}
163inline int MPI_Waitany(int, MPI_Request*, int* idx, MPI_Status*) {
164 *idx = MPI_UNDEFINED;
165 return MPI_SUCCESS;
166}
168 return MPI_SUCCESS;
169}
170inline int MPI_Dist_graph_create_adjacent(MPI_Comm c, int, const int*, const int*, int, const int*,
171 const int*, MPI_Info, int, MPI_Comm* out) {
172 *out = c;
173 return MPI_SUCCESS;
174}
175inline int MPI_Neighbor_alltoallv(const void*, const int*, const int*, MPI_Datatype, void*,
176 const int*, const int*, MPI_Datatype, MPI_Comm) {
177 return MPI_SUCCESS;
178}
#define MPI_SUCCESS
Definition mpi_stub.hpp:36
int MPI_Comm_size(MPI_Comm, int *s)
Definition mpi_stub.hpp:70
int MPI_Bcast(void *, int, MPI_Datatype, int, MPI_Comm)
Definition mpi_stub.hpp:120
int MPI_Comm_get_attr(MPI_Comm c, int keyval, void *val, int *flag)
Definition mpi_stub.hpp:95
int MPI_Testall(int, MPI_Request *, int *flag, MPI_Status *)
Definition mpi_stub.hpp:153
int MPI_Allreduce(const void *sbuf, void *rbuf, int count, MPI_Datatype dt, MPI_Op, MPI_Comm)
Definition mpi_stub.hpp:114
int MPI_Comm
Definition mpi_stub.hpp:17
int MPI_Request
Definition mpi_stub.hpp:18
int MPI_Waitall(int, MPI_Request *, MPI_Status *)
Definition mpi_stub.hpp:160
int MPI_Finalize()
Definition mpi_stub.hpp:55
int(MPI_Comm, int, void *, void *, void *, int *) MPI_Comm_copy_attr_function
Definition mpi_stub.hpp:75
int MPI_Comm_set_attr(MPI_Comm c, int keyval, void *val)
Definition mpi_stub.hpp:103
int MPI_Comm_free(MPI_Comm *)
Definition mpi_stub.hpp:107
int MPI_Op
Definition mpi_stub.hpp:20
int MPI_Iprobe(int, int, MPI_Comm, int *flag, MPI_Status *)
Definition mpi_stub.hpp:141
int MPI_Recv(void *, int, MPI_Datatype, int, int, MPI_Comm, MPI_Status *)
Definition mpi_stub.hpp:129
int MPI_Waitany(int, MPI_Request *, int *idx, MPI_Status *)
Definition mpi_stub.hpp:163
int MPI_Ibarrier(MPI_Comm, MPI_Request *)
Definition mpi_stub.hpp:167
int MPI_Isend(const void *, int, MPI_Datatype, int, int, MPI_Comm, MPI_Request *)
Definition mpi_stub.hpp:132
int MPI_Get_count(const MPI_Status *, MPI_Datatype, int *c)
Definition mpi_stub.hpp:145
int MPI_Finalized(int *f)
Definition mpi_stub.hpp:58
int MPI_Wait(MPI_Request *, MPI_Status *)
Definition mpi_stub.hpp:157
int MPI_Abort(MPI_Comm, int code)
Definition mpi_stub.hpp:62
int MPI_Issend(const void *, int, MPI_Datatype, int, int, MPI_Comm, MPI_Request *)
Definition mpi_stub.hpp:138
int MPI_Comm_rank(MPI_Comm, int *r)
Definition mpi_stub.hpp:66
int MPI_Initialized(int *f)
Definition mpi_stub.hpp:51
int MPI_Send(const void *, int, MPI_Datatype, int, int, MPI_Comm)
Definition mpi_stub.hpp:126
int MPI_Irecv(void *, int, MPI_Datatype, int, int, MPI_Comm, MPI_Request *)
Definition mpi_stub.hpp:135
int MPI_Barrier(MPI_Comm)
Definition mpi_stub.hpp:110
int MPI_Comm_create_keyval(MPI_Comm_copy_attr_function *, MPI_Comm_delete_attr_function *, int *keyval, void *)
Definition mpi_stub.hpp:90
int(MPI_Comm, int, void *, void *) MPI_Comm_delete_attr_function
Definition mpi_stub.hpp:76
int MPI_Datatype
Definition mpi_stub.hpp:19
int MPI_Init(int *, char ***)
Definition mpi_stub.hpp:48
int MPI_Info
Definition mpi_stub.hpp:21
int MPI_Neighbor_alltoallv(const void *, const int *, const int *, MPI_Datatype, void *, const int *, const int *, MPI_Datatype, MPI_Comm)
Definition mpi_stub.hpp:175
int MPI_Test(MPI_Request *, int *flag, MPI_Status *)
Definition mpi_stub.hpp:149
int MPI_Dist_graph_create_adjacent(MPI_Comm c, int, const int *, const int *, int, const int *, const int *, MPI_Info, int, MPI_Comm *out)
Definition mpi_stub.hpp:170
#define MPI_UNDEFINED
Definition mpi_stub.hpp:34
std::map< std::pair< int, int >, void * > & attrs()
Definition mpi_stub.hpp:81
int MPI_SOURCE
Definition mpi_stub.hpp:23