-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathAMReX_MPMD.cpp
More file actions
381 lines (331 loc) · 10.7 KB
/
AMReX_MPMD.cpp
File metadata and controls
381 lines (331 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <AMReX_MPMD.H>
#include <AMReX_ParallelDescriptor.H>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
#ifdef AMREX_USE_MPI
namespace amrex::MPMD {
namespace {
bool initialized = false;
bool mpi_initialized_by_us = false;
MPI_Comm app_comm = MPI_COMM_NULL;
int myproc;
int nprocs;
int appnum;
}
namespace {
template <typename T>
int num_unique_elements (std::vector<T>& v)
{
std::ranges::sort(v);
auto last = std::ranges::unique(v);
return static_cast<int>(last.begin() - v.begin());
}
}
/*
Initialize_without_split function assigns and checks the required
AMReX_MPMD variables. This function is internally leveraged by
Initialize function.
This function needs to be used EXPLICITLY ONLY with pyAMReX (python)
so that the communication split can be performed using a python
library, for example, mpi4py.
*/
void Initialize_without_split (int argc, char* argv[])
{
initialized = true;
int flag;
MPI_Initialized(&flag);
if (!flag) {
MPI_Init(&argc, &argv);
mpi_initialized_by_us = true;
}
MPI_Comm_rank(MPI_COMM_WORLD, &myproc);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
int* p;
MPI_Comm_get_attr(MPI_COMM_WORLD, MPI_APPNUM, &p, &flag);
appnum = *p;
std::vector<int> all_appnum(nprocs);
MPI_Allgather(&appnum, 1, MPI_INT, all_appnum.data(), 1, MPI_INT, MPI_COMM_WORLD);
int napps = num_unique_elements(all_appnum);
// MPI_APPNUM does not appear to work with slurm on some systems.
if (napps != 2) {
std::vector<int> all_argc(nprocs);
MPI_Allgather(&argc, 1, MPI_INT, all_argc.data(), 1, MPI_INT, MPI_COMM_WORLD);
napps = num_unique_elements(all_argc);
if (napps == 2) {
appnum = static_cast<int>(argc != all_argc[0]);
}
}
if (napps != 2) {
std::string exename;
if (argc > 0) {
exename = std::string(argv[0]);
}
unsigned long long hexe = std::hash<std::string>{}(exename);
std::vector<unsigned long long> all_hexe(nprocs);
MPI_Allgather(&hexe, 1, MPI_UNSIGNED_LONG_LONG,
all_hexe.data(), 1, MPI_UNSIGNED_LONG_LONG, MPI_COMM_WORLD);
napps = num_unique_elements(all_hexe);
if (napps == 2) {
appnum = static_cast<int>(hexe != all_hexe[0]);
}
}
if (napps != 2) {
std::cout << "amrex::MPMD only supports two programs." << '\n';
MPI_Abort(MPI_COMM_WORLD, 1);
}
}
MPI_Comm Initialize (int argc, char* argv[])
{
Initialize_without_split(argc,argv);
MPI_Comm_split(MPI_COMM_WORLD, appnum, myproc, &app_comm);
return app_comm;
}
void Finalize ()
{
MPI_Comm_free(&app_comm);
if (mpi_initialized_by_us) {
MPI_Finalize();
mpi_initialized_by_us = false;
}
initialized = false;
}
bool Initialized () { return initialized; }
int MyProc ()
{
return myproc;
}
int NProcs ()
{
return nprocs;
}
/*
AppNum function is provided so that appnum (color)
can be passed to python library (mpi4py) to perform
a pythonic version of MPI_Comm_split.
*/
int AppNum ()
{
return appnum;
}
int MyProgId ()
{
return (myproc == ParallelDescriptor::MyProc()) ? 0 : 1;
}
Copier::Copier (BoxArray const& ba, DistributionMapping const& dm,
bool send_ba)
: m_send_id(FabArrayBase::getNextCommMetaDataId()),
m_recv_id(FabArrayBase::getNextCommMetaDataId()),
m_ba(ba), m_dm(dm)
{
int rank_offset = myproc - ParallelDescriptor::MyProc();
int this_root, other_root;
if (rank_offset == 0) { // First program
this_root = 0;
other_root = ParallelDescriptor::NProcs();
} else {
this_root = rank_offset;
other_root = 0;
}
Vector<Box> bv = ba.boxList().data();
int this_nboxes = static_cast<int>(ba.size());
Vector<int> procs = dm.ProcessorMap();
if (rank_offset != 0) {
for (int i = 0; i < this_nboxes; ++i) {
procs[i] += rank_offset;
}
}
Vector<Box> obv;
Vector<int> oprocs;
int other_nboxes = this_nboxes;
if (myproc == this_root) {
if (rank_offset == 0) // the first program
{
MPI_Send(&this_nboxes, 1, MPI_INT, other_root, 0, MPI_COMM_WORLD);
if (!send_ba)
{
MPI_Recv(&other_nboxes, 1, MPI_INT, other_root, 1, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
obv.resize(other_nboxes);
}
MPI_Send(bv.data(), this_nboxes,
ParallelDescriptor::Mpi_typemap<Box>::type(),
other_root, 2, MPI_COMM_WORLD);
if (!send_ba)
{
MPI_Recv(obv.data(), other_nboxes,
ParallelDescriptor::Mpi_typemap<Box>::type(),
other_root, 3, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
MPI_Send(procs.data(), this_nboxes, MPI_INT, other_root, 4, MPI_COMM_WORLD);
oprocs.resize(other_nboxes);
MPI_Recv(oprocs.data(), other_nboxes, MPI_INT, other_root, 5, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
}
else // the second program
{
if (!send_ba)
{
MPI_Recv(&other_nboxes, 1, MPI_INT, other_root, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
obv.resize(other_nboxes);
}
MPI_Send(&this_nboxes, 1, MPI_INT, other_root, 1, MPI_COMM_WORLD);
if (!send_ba)
{
MPI_Recv(obv.data(), other_nboxes,
ParallelDescriptor::Mpi_typemap<Box>::type(),
other_root, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
MPI_Send(bv.data(), this_nboxes,
ParallelDescriptor::Mpi_typemap<Box>::type(),
other_root, 3, MPI_COMM_WORLD);
oprocs.resize(other_nboxes);
MPI_Recv(oprocs.data(), other_nboxes, MPI_INT, other_root, 4, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
MPI_Send(procs.data(), this_nboxes, MPI_INT, other_root, 5, MPI_COMM_WORLD);
}
}
if (!send_ba) {
ParallelDescriptor::Bcast(&other_nboxes, 1);
if (obv.empty()){
obv.resize(other_nboxes);
}
ParallelDescriptor::Bcast(obv.data(), obv.size());
}
if (oprocs.empty()) {
oprocs.resize(other_nboxes);
}
ParallelDescriptor::Bcast(oprocs.data(), oprocs.size());
BoxArray oba;
if (!obv.empty()) {
oba.define(BoxList(std::move(obv)));
}
// At this point, ba and bv hold our boxes, and oba holds the other
// program's boxes. procs holds mpi ranks of our boxes, and oprocs holds
// mpi ranks of the other program's boxes. All mpi ranks are in
// MPI_COMM_WORLD.
// Build communication meta-data
if (!send_ba){
AMREX_ALWAYS_ASSERT(ba.ixType() == oba.ixType());
m_is_thread_safe = ba.ixType().cellCentered();
}else{
m_is_thread_safe = true;
}
std::vector<std::pair<int,Box> > isects;
for (int i = 0; i < this_nboxes; ++i) {
if (procs[i] == myproc) {
if (!send_ba){
oba.intersections(bv[i], isects);
}
else{
isects.resize(0);
isects.emplace_back(i,bv[i]);
}
for (auto const& isec : isects) {
const int oi = isec.first;
const Box& bx = isec.second;
const int orank = oprocs[oi];
m_SndTags[orank].emplace_back(bx, bx, oi, i);
m_RcvTags[orank].emplace_back(bx, bx, i, oi);
}
}
}
if (!send_ba){
for (auto& kv : m_SndTags) {
std::sort(kv.second.begin(), kv.second.end());
}
for (auto& kv : m_RcvTags) {
std::sort(kv.second.begin(), kv.second.end());
}
}
}
Copier::Copier (bool)
: m_send_id(FabArrayBase::getNextCommMetaDataId()),
m_recv_id(FabArrayBase::getNextCommMetaDataId()),
m_is_thread_safe(true)
{
int rank_offset = myproc - ParallelDescriptor::MyProc();
int this_root, other_root;
if (rank_offset == 0) { // First program
this_root = 0;
other_root = ParallelDescriptor::NProcs();
} else {
this_root = rank_offset;
other_root = 0;
}
Vector<Box> bv;
int this_nboxes;
if (myproc == this_root) {
int tags[2];
if (rank_offset == 0) // the first program
{
tags[0] = 1;
tags[1] = 3;
}
else // the second program
{
tags[0] = 0;
tags[1] = 2;
}
MPI_Recv(&this_nboxes, 1, MPI_INT, other_root, tags[0], MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
bv.resize(this_nboxes);
MPI_Recv(bv.data(), this_nboxes,
ParallelDescriptor::Mpi_typemap<Box>::type(),
other_root, tags[1], MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
ParallelDescriptor::Bcast(&this_nboxes, 1);
if (bv.empty()) {
bv.resize(this_nboxes);
}
ParallelDescriptor::Bcast(bv.data(), bv.size());
m_ba.define(BoxList(std::move(bv)));
m_dm.define(m_ba);
Vector<int> procs = m_dm.ProcessorMap();
if (rank_offset != 0) {
for (int i = 0; i < this_nboxes; ++i) {
procs[i] += rank_offset;
}
}
Vector<int> oprocs(this_nboxes);
if (myproc == this_root) {
if (rank_offset == 0) // the first program
{
MPI_Send(procs.data(), this_nboxes, MPI_INT, other_root, 4, MPI_COMM_WORLD);
MPI_Recv(oprocs.data(), this_nboxes, MPI_INT, other_root, 5, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
}
else // the second program
{
MPI_Recv(oprocs.data(), this_nboxes, MPI_INT, other_root, 4, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
MPI_Send(procs.data(), this_nboxes, MPI_INT, other_root, 5, MPI_COMM_WORLD);
}
}
ParallelDescriptor::Bcast(oprocs.data(), oprocs.size());
// procs holds mpi ranks of our boxes, and oprocs holds
// mpi ranks of the other program's boxes. All mpi ranks are in
// MPI_COMM_WORLD.
// Build communication meta-data
for (int i = 0; i < this_nboxes; ++i) {
if (procs[i] == myproc) {
const Box& bx = m_ba[i];
const int orank = oprocs[i];
m_SndTags[orank].emplace_back(bx, bx, i, i);
m_RcvTags[orank].emplace_back(bx, bx, i, i);
}
}
}
BoxArray const& Copier::boxArray () const
{
return m_ba;
}
DistributionMapping const& Copier::DistributionMap () const
{
return m_dm;
}
}
#endif