-
Notifications
You must be signed in to change notification settings - Fork 442
Expand file tree
/
Copy pathAMReX_NFiles.cpp
More file actions
570 lines (469 loc) · 16.6 KB
/
AMReX_NFiles.cpp
File metadata and controls
570 lines (469 loc) · 16.6 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include <AMReX_NFiles.H>
#include <deque>
#include <iterator>
#include <set>
#include <utility>
namespace amrex {
int NFilesIter::currentDeciderIndex(-1);
int NFilesIter::minDigits(5);
NFilesIter::NFilesIter(int noutfiles, std::string fileprefix,
bool groupsets, bool setBuf)
: myProc (ParallelDescriptor::MyProc()),
nProcs (ParallelDescriptor::NProcs()),
nOutFiles (ActualNFiles(noutfiles)),
nSets (LengthOfSet(nProcs, nOutFiles)),
groupSets (groupsets),
mySetPosition (WhichSetPosition(myProc, nProcs, nOutFiles, groupSets)),
fileNumber (FileNumber(nOutFiles, myProc, groupSets)),
filePrefix (std::move(fileprefix)),
fullFileName (FileName(fileNumber, filePrefix)),
coordinatorProc (ParallelDescriptor::IOProcessorNumber()),
stWriteTag (ParallelDescriptor::SeqNum()),
stReadTag (ParallelDescriptor::SeqNum())
{
if(setBuf) {
io_buffer.resize(VisMFBuffer::GetIOBufferSize());
fileStream.rdbuf()->pubsetbuf(io_buffer.dataPtr(), io_buffer.size());
}
if(myProc == coordinatorProc) {
// ---- make a static order
fileNumbersWriteOrder.resize(nOutFiles);
for(int i(0); i < nProcs; ++i) {
fileNumbersWriteOrder[FileNumber(nOutFiles, i, groupSets)].push_back(i);
}
}
availableDeciders.resize(0);
availableDeciders.reserve(nProcs);
setZeroProcs.reserve(nOutFiles);
for(int i(0); i < nProcs; ++i) {
// ---- count zero set positions and find an alternate decider
if(NFilesIter::WhichSetPosition(i, nProcs, nOutFiles, groupSets) == 0) {
setZeroProcs.push_back(i);
} else {
availableDeciders.push_back(i);
}
}
if(currentDeciderIndex < 0) {
currentDeciderIndex = nSets / 2;
if(currentDeciderIndex >= availableDeciders.size()) {
currentDeciderIndex = 0;
}
}
#if 0
bool checkNFiles(false);
if(checkNFiles) {
CheckNFiles(nProcs, nOutFiles, groupSets);
}
#endif
}
void NFilesIter::SetDynamic(int deciderproc)
{
deciderProc = deciderproc;
// ---- we have to check currentDeciderIndex here also in case of
// ---- different nfiles for plots and checkpoints
if(currentDeciderIndex >= availableDeciders.size() || currentDeciderIndex < 0) {
currentDeciderIndex = 0;
}
if( ! availableDeciders.empty()) {
if(deciderProc < 0 || deciderProc >= nProcs) {
deciderProc = availableDeciders[currentDeciderIndex];
}
if(NFilesIter::WhichSetPosition(deciderProc, nProcs, nOutFiles, groupSets) == 0) {
// ---- the decider cannot have set position zero
deciderProc = availableDeciders[currentDeciderIndex];
}
}
currentDeciderIndex += nSets - 1;
if(currentDeciderIndex >= availableDeciders.size() || currentDeciderIndex < 0) {
currentDeciderIndex = 0;
}
#if 0
// The following has no effect because WhichSetPostion is a pure function and
// its return type is not used. So not sure why this is here in the first place.
if(myProc == deciderProc) {
NFilesIter::WhichSetPosition(myProc, nProcs, nOutFiles, groupSets);
}
#endif
deciderTag = ParallelDescriptor::SeqNum();
coordinatorTag = ParallelDescriptor::SeqNum();
doneTag = ParallelDescriptor::SeqNum();
writeTag = ParallelDescriptor::SeqNum();
remainingWriters = nProcs;
useStaticSetSelection = false;
if(nOutFiles == nProcs) {
useStaticSetSelection = true;
coordinatorProc = ParallelDescriptor::IOProcessorNumber();
} else {
fileNumbersWriteOrder.clear();
fileNumbersWriteOrder.resize(nOutFiles);
}
}
void NFilesIter::SetSparseFPP(const Vector<int> &ranksToWrite)
{
if(ranksToWrite.empty()) {
return;
}
if(ranksToWrite.size() > nProcs) {
amrex::Abort("**** Error in NFilesIter::SetSparseFPP: ranksToWrite.size() > nProcs.");
}
sparseWritingRanks = ranksToWrite;
// ---- do more error checking here
// ---- ranks in range, is dynamic on already
mySparseFileNumber = -1;
for(int r : ranksToWrite) {
if(r < 0 || r >= nProcs) {
amrex::Abort("**** Error in NFilesIter::SetSparseFPP: rank out of range.");
}
if(r == myProc) {
if(mySparseFileNumber == -1) {
mySparseFileNumber = myProc;
} else {
amrex::Abort("**** Error in NFilesIter::SetSparseFPP: ranksToWrite not unique.");
}
}
}
nOutFiles = static_cast<int>(ranksToWrite.size());
if(myProc == coordinatorProc) {
// ---- get the write order from ranksToWrite
fileNumbersWriteOrder.clear();
fileNumbersWriteOrder.resize(nOutFiles);
for(int i(0); i < fileNumbersWriteOrder.size(); ++i) {
fileNumbersWriteOrder[i].push_back(ranksToWrite[i]);
}
}
if(mySparseFileNumber != -1) {
fileNumber = mySparseFileNumber;
fullFileName = FileName(fileNumber, filePrefix);
} else {
fullFileName = "fullFileNameUndefined";
}
useSparseFPP = true;
useStaticSetSelection = true;
}
NFilesIter::NFilesIter(std::string filename,
Vector<int> readranks,
bool setBuf)
: myProc (ParallelDescriptor::MyProc()),
nProcs (ParallelDescriptor::NProcs()),
fullFileName (std::move(filename)),
isReading (true),
readRanks (std::move(readranks)),
myReadIndex (indexUndefined)
{
for(int i(0); i < readRanks.size(); ++i) {
if(myProc == readRanks[i]) {
if(myReadIndex != indexUndefined) {
amrex::Abort("**** Error in NFilesIter: readRanks not unique.");
}
myReadIndex = i;
}
}
if(myReadIndex == indexUndefined) { // ---- nothing to read
finishedReading = true;
return;
} else {
finishedReading = false;
}
if(setBuf) {
io_buffer.resize(VisMFBuffer::GetIOBufferSize());
fileStream.rdbuf()->pubsetbuf(io_buffer.dataPtr(), io_buffer.size());
}
}
NFilesIter::~NFilesIter() {
if( ! useStaticSetSelection) {
CleanUpMessages();
}
}
bool NFilesIter::ReadyToWrite(bool appendFirst) {
#ifdef BL_USE_MPI
if(finishedWriting) {
return false;
}
if(useStaticSetSelection) {
if(useSparseFPP) {
if(mySparseFileNumber != -1) {
if( ! appendFirst) {
fileStream.open(fullFileName.c_str(),
std::ios::out | std::ios::trunc | std::ios::binary);
} else {
fileStream.open(fullFileName.c_str(),
std::ios::out | std::ios::app | std::ios::binary);
}
if( ! fileStream.good()) {
amrex::FileOpenFailed(fullFileName);
}
return true;
} else {
return false;
}
} else { // ---- the general static set selection
for(int iSet(0); iSet < nSets; ++iSet) {
if(mySetPosition == iSet) {
if(iSet == 0 && ! appendFirst) { // ---- first set
fileStream.open(fullFileName.c_str(),
std::ios::out | std::ios::trunc | std::ios::binary);
} else {
fileStream.open(fullFileName.c_str(),
std::ios::out | std::ios::app | std::ios::binary);
}
if( ! fileStream.good()) {
amrex::FileOpenFailed(fullFileName);
}
return true;
}
if(mySetPosition == (iSet + 1)) { // ---- next set waits
int iBuff, waitForPID(-1);
if(groupSets) {
waitForPID = (myProc - nOutFiles);
} else {
waitForPID = (myProc - 1);
}
ParallelDescriptor::Recv(&iBuff, 1, waitForPID, stWriteTag);
}
}
}
} else { // ---- use dynamic set selection
if(mySetPosition == 0) { // ---- return true, ready to write data
fullFileName = amrex::Concatenate(filePrefix, fileNumber, minDigits);
if(appendFirst) {
fileStream.open(fullFileName.c_str(),
std::ios::out | std::ios::app | std::ios::binary);
} else {
fileStream.open(fullFileName.c_str(),
std::ios::out | std::ios::trunc | std::ios::binary);
}
if( ! fileStream.good()) {
amrex::FileOpenFailed(fullFileName);
}
return true;
} else if(myProc == deciderProc) { // ---- this proc decides who decides
BL_PROFILE("NFI::ReadyToWrite:decider");
// ---- the first message received is the coordinator
ParallelDescriptor::Recv(&coordinatorProc, 1, MPI_ANY_SOURCE, deciderTag);
for(int setZeroProc : setZeroProcs) { // ---- tell the set zero ranks who is coordinating
ParallelDescriptor::Send(&coordinatorProc, 1, setZeroProc, coordinatorTag);
}
unreadMessages.push_back(std::make_pair(deciderTag, setZeroProcs.size() - 1));
}
// ---- these are the rest of the procs who need to write
if( ! finishedWriting) { // ---- the deciderProc drops through to here
// ---- wait for signal to start writing
ParallelDescriptor::Message rmess =
ParallelDescriptor::Recv(&fileNumber, 1, MPI_ANY_SOURCE, writeTag);
coordinatorProc = rmess.pid();
fullFileName = amrex::Concatenate(filePrefix, fileNumber, minDigits);
fileStream.open(fullFileName.c_str(),
std::ios::out | std::ios::app | std::ios::binary);
if( ! fileStream.good()) {
amrex::FileOpenFailed(fullFileName);
}
return true;
}
}
return false;
#else
amrex::ignore_unused(appendFirst);
if(finishedWriting) {
return false;
}
fileStream.open(fullFileName.c_str(),
std::ios::out | std::ios::trunc | std::ios::binary);
if( ! fileStream.good()) {
amrex::FileOpenFailed(fullFileName);
}
return true;
#endif
}
bool NFilesIter::ReadyToRead() {
if(finishedReading) {
return false;
}
if(myReadIndex != 0) { // ---- wait for rank myReadIndex - 1
int iBuff(-1), waitForPID(readRanks[myReadIndex - 1]);
ParallelDescriptor::Recv(&iBuff, 1, waitForPID, stReadTag);
}
fileStream.open(fullFileName.c_str(),
std::ios::in | std::ios::binary);
if( ! fileStream.good()) {
amrex::FileOpenFailed(fullFileName);
}
return true;
}
NFilesIter &NFilesIter::operator++() {
#ifdef BL_USE_MPI
if(isReading) {
fileStream.close();
if(myReadIndex < readRanks.size() - 1) {
int iBuff(0), wakeUpPID(readRanks[myReadIndex + 1]);
ParallelDescriptor::Send(&iBuff, 1, wakeUpPID, stReadTag);
}
finishedReading = true;
} else { // ---- writing
if(useStaticSetSelection) {
if(useSparseFPP) {
if(mySparseFileNumber != -1) {
fileStream.flush();
fileStream.close();
}
finishedWriting = true;
} else { // ---- the general static set selection
fileStream.flush();
fileStream.close();
int iBuff(0), wakeUpPID(-1);
if(groupSets) {
wakeUpPID = (myProc + nOutFiles);
} else {
wakeUpPID = (myProc + 1);
}
if(wakeUpPID < nProcs) {
int nextSP = WhichSetPosition(wakeUpPID, nProcs, nOutFiles, groupSets);
if(nextSP > mySetPosition) {
ParallelDescriptor::Send(&iBuff, 1, wakeUpPID, stWriteTag);
}
}
finishedWriting = true;
}
} else { // ---- use dynamic set selection
if(mySetPosition == 0) { // ---- write data
fileStream.flush();
fileStream.close();
finishedWriting = true;
// ---- tell the decider we are done
ParallelDescriptor::Send(&myProc, 1, deciderProc, deciderTag);
// ---- wait to find out who will coordinate
ParallelDescriptor::Recv(&coordinatorProc, 1, deciderProc, coordinatorTag);
if(myProc == coordinatorProc) {
Vector<std::deque<int> > procsToWrite(nOutFiles); // ---- [fileNumber](procsToWriteToFileNumber)
// ---- populate with the static nfiles sets
for(int i(0); i < nProcs; ++i) {
int procSet(WhichSetPosition(i, nProcs, nOutFiles, groupSets));
int whichFileNumber(NFilesIter::FileNumber(nOutFiles, i, groupSets));
// ---- procSet == 0 have already written their data
if(procSet == 0) {
fileNumbersWriteOrder[whichFileNumber].push_back(i);
--remainingWriters;
}
if(procSet != 0) {
procsToWrite[whichFileNumber].push_back(i);
}
}
// ---- signal each remaining processor when to write and to which file
std::set<int> availableFileNumbers;
availableFileNumbers.insert(fileNumber); // ---- the coordinators file number
// ---- recv incoming available files
while(remainingWriters > 0) {
int nextProcToWrite(-1), nextFileNumberToWrite, nextFileNumberAvailable;
auto ait = availableFileNumbers.begin();
nextFileNumberToWrite = *ait;
availableFileNumbers.erase(nextFileNumberToWrite);
for(int nfn(0); nfn < procsToWrite.size(); ++nfn) {
// ---- start with the current next file number
// ---- get a proc from another file number if the queue is empty
auto tempNFN = static_cast<int>((nextFileNumberToWrite + nfn) % procsToWrite.size());
if(!procsToWrite[tempNFN].empty()) {
nextProcToWrite = procsToWrite[tempNFN].front();
procsToWrite[tempNFN].pop_front();
break; // ---- found one
}
}
if(nextProcToWrite == -1) {
--remainingWriters;
// amrex::Print() << myProc << "::IOIOIOIO: nptw == -1 rW = " << remainingWriters << '\n';
} else {
fileNumbersWriteOrder[nextFileNumberToWrite].push_back(nextProcToWrite);
ParallelDescriptor::Send(&nextFileNumberToWrite, 1, nextProcToWrite, writeTag);
ParallelDescriptor::Recv(&nextFileNumberAvailable, 1, MPI_ANY_SOURCE, doneTag);
availableFileNumbers.insert(nextFileNumberAvailable);
--remainingWriters;
}
}
unreadMessages.push_back(std::make_pair(doneTag, setZeroProcs.size() - 1));
} else { // ---- tell the coordinatorProc we are done writing
ParallelDescriptor::Send(&fileNumber, 1, coordinatorProc, doneTag);
}
}
if( ! finishedWriting) { // ---- the deciderProc drops through to here
fileStream.flush();
fileStream.close();
finishedWriting = true;
// ---- signal we are finished
ParallelDescriptor::Send(&fileNumber, 1, coordinatorProc, doneTag);
}
}
}
#else
if(isReading) {
fileStream.close();
finishedReading = true;
} else { // ---- writing
fileStream.flush();
fileStream.close();
finishedWriting = true;
}
#endif
return *this;
}
std::streampos NFilesIter::SeekPos() {
return fileStream.tellp();
}
bool NFilesIter::CheckNFiles(int nProcs, int nOutFiles, bool groupSets)
{
if(ParallelDescriptor::IOProcessor()) {
std::set<int> fileNumbers;
for(int i(0); i < nProcs; ++i) {
fileNumbers.insert(FileNumber(nOutFiles, i, groupSets));
}
// amrex::Print() << "nOutFiles fileNumbers.size() = " << nOutFiles
// << " " << fileNumbers.size() << '\n';
if(nOutFiles != std::ssize(fileNumbers)) {
// amrex::Print() << "**** Different number of files." << '\n';
return false;
}
}
return true;
}
Vector<int> NFilesIter::FileNumbersWritten()
{
Vector<int> fileNumbersWritten(nProcs, -1);
if(myProc == coordinatorProc) {
#if 0
int total(0);
std::set<int> procSet;
for(int f(0); f < fileNumbersWriteOrder.size(); ++f) {
total += fileNumbersWriteOrder[f].size();
for(int r(0); r < fileNumbersWriteOrder[f].size(); ++r) {
procSet.insert(fileNumbersWriteOrder[f][r]);
}
}
if(total != nProcs || std::ssize(procSet) != nProcs) {
amrex::AllPrint() << "**** Error in NFilesIter::FileNumbersWritten(): "
<< " coordinatorProc nProcs total procSet.size() = "
<< coordinatorProc << " " << nProcs << " "
<< total << " " << procSet.size() << '\n';
}
#endif
for(int f(0); f < fileNumbersWriteOrder.size(); ++f) {
for(int r(0); r < fileNumbersWriteOrder[f].size(); ++r) {
fileNumbersWritten[fileNumbersWriteOrder[f][r]] = f;
}
}
}
return fileNumbersWritten;
}
void NFilesIter::CleanUpMessages() {
#ifdef BL_USE_MPI
BL_PROFILE("NFI::CleanUpMessages");
for(auto & pii : unreadMessages) {
int fromProc, tag(pii.first), nMessages(pii.second);
#if 0
amrex::AllPrint() << ParallelDescriptor::MyProc() << ":: cleaning up " << nMessages
<< " messages for tag " << tag << '\n';
#endif
for(int n(0); n < nMessages; ++n) {
ParallelDescriptor::Recv(&fromProc, 1, MPI_ANY_SOURCE, tag);
}
}
unreadMessages.clear();
#endif
}
}