-
Notifications
You must be signed in to change notification settings - Fork 339
/
AMReX_ParallelDescriptor.H
1484 lines (1271 loc) · 45.2 KB
/
AMReX_ParallelDescriptor.H
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef BL_PARALLELDESCRIPTOR_H
#define BL_PARALLELDESCRIPTOR_H
#include <AMReX_Config.H>
#include <AMReX_ccse-mpi.H>
#include <AMReX_ParallelContext.H>
#include <AMReX_BLBackTrace.H>
#include <AMReX_BLProfiler.H>
#include <AMReX_BLassert.H>
#include <AMReX_Extension.H>
#include <AMReX_INT.H>
#include <AMReX_REAL.H>
#include <AMReX_Array.H>
#include <AMReX_Vector.H>
#ifndef BL_AMRPROF
#include <AMReX_Box.H>
#endif
#ifdef BL_LAZY
#include <AMReX_Lazy.H>
#endif
#ifdef AMREX_PMI
#include <pmi.h>
#endif
#ifdef AMREX_USE_OMP
#include <omp.h>
#endif
#include <algorithm>
#include <atomic>
#include <csignal>
#include <functional>
#include <limits>
#include <numeric>
#include <string>
#include <typeinfo>
#include <vector>
namespace amrex {
template <typename T> class LayoutData;
//! Parallel frontend that abstracts functionalities needed to spawn processes and handle communication
namespace ParallelDescriptor
{
//
//! Used as default argument to ParallelDescriptor::Barrier().
const std::string Unnamed("Unnamed");
//! Hold the description and status of communication data
class Message
{
public:
Message () :
m_finished(true),
m_type(MPI_DATATYPE_NULL),
m_req(MPI_REQUEST_NULL) {}
Message (MPI_Request req_, MPI_Datatype type_) :
m_finished(false),
m_type(type_),
m_req(req_) {}
Message (MPI_Status stat_, MPI_Datatype type_) :
m_finished(true),
m_type(type_),
m_req(MPI_REQUEST_NULL), m_stat(stat_) {}
void wait ();
bool test ();
size_t count () const;
int tag () const;
int pid () const;
MPI_Datatype type () const { return m_type; }
MPI_Request req () const { return m_req; }
MPI_Status stat () const { return m_stat; }
private:
bool m_finished;
MPI_Datatype m_type;
MPI_Request m_req;
mutable MPI_Status m_stat;
};
#ifdef BL_USE_MPI
void MPI_Error(const char* file, int line, const char* msg, int rc);
#define BL_MPI_REQUIRE(x) \
do \
{ \
if ( int l_status_ = (x) ) \
{ \
amrex::ParallelDescriptor::MPI_Error(__FILE__,__LINE__,#x, l_status_); \
} \
} \
while ( false )
#endif
/**
* \brief Perform any needed parallel initialization. This MUST be the
* first routine in this class called from within a program.
*/
void StartParallel (int* argc = 0,
char*** argv = 0,
MPI_Comm mpi_comm = MPI_COMM_WORLD);
void Initialize ();
void Finalize ();
extern AMREX_EXPORT int use_gpu_aware_mpi;
inline bool UseGpuAwareMpi () { return use_gpu_aware_mpi; }
//! Split the process pool into teams
void StartTeams ();
void EndTeams ();
/**
* \brief Perform any needed parallel finalization. This MUST be the
* last routine in this class called from within a program.
*/
void EndParallel ();
//! return the rank number local to the current Parallel Context
inline int
MyProc () noexcept
{
return ParallelContext::MyProcAll();
}
inline int
MyProc (MPI_Comm comm) noexcept
{
#ifdef BL_USE_MPI
int r;
MPI_Comm_rank(comm,&r);
return r;
#else
amrex::ignore_unused(comm);
return 0;
#endif
}
//! Provide functionalities needed to construct a team of processes to perform a particular job
struct ProcessTeam
{
typedef MPI_Comm team_t;
//! synchronize processes within the team
void Barrier () const {
if (m_size > 1) {
#if defined(BL_USE_MPI3)
MPI_Barrier(m_team_comm);
#endif
}
}
//! memory fence
void MemoryBarrier () const {
if (m_size > 1) {
#ifdef AMREX_USE_OMP
if (omp_in_parallel()) {
#pragma omp barrier
}
#pragma omp master
#endif
{
#if defined(BL_USE_MPI3)
std::atomic_thread_fence(std::memory_order_release);
MPI_Barrier(m_team_comm);
std::atomic_thread_fence(std::memory_order_acquire);
#endif
}
}
}
//! free a communicator
void clear () {
#if defined(BL_USE_MPI3)
if (m_size > 1) {
MPI_Comm_free(&m_team_comm);
if (m_rankInTeam==0) MPI_Comm_free(&m_lead_comm);
}
#endif
}
const team_t& get() const {
return m_team_comm;
}
//! return the communicator
const MPI_Comm& get_team_comm() const { return m_team_comm; }
const MPI_Comm& get_lead_comm() const { return m_lead_comm; }
int m_numTeams;
int m_size;
int m_color;
int m_lead;
int m_rankInTeam;
int m_do_team_reduce;
MPI_Comm m_team_comm;
MPI_Comm m_lead_comm;
};
extern AMREX_EXPORT ProcessTeam m_Team;
extern AMREX_EXPORT int m_MinTag, m_MaxTag;
inline int MinTag () noexcept { return m_MinTag; }
inline int MaxTag () noexcept { return m_MaxTag; }
extern AMREX_EXPORT MPI_Comm m_comm;
inline MPI_Comm Communicator () noexcept { return m_comm; }
//! return the number of MPI ranks local to the current Parallel Context
inline int
NProcs () noexcept
{
return ParallelContext::NProcsAll();
}
inline int
NProcs (MPI_Comm comm) noexcept
{
#ifdef BL_USE_MPI
int s;
BL_MPI_REQUIRE(MPI_Comm_size(comm, &s));
return s;
#else
amrex::ignore_unused(comm);
return 1;
#endif
}
/**
* \brief The MPI rank number of the I/O Processor (probably rank 0).
* This rank is usually used to write to stdout.
*/
extern AMREX_EXPORT const int ioProcessor;
inline int
IOProcessorNumber () noexcept
{
return ioProcessor;
}
/**
* \brief Is this CPU the I/O Processor?
* To get the rank number, call IOProcessorNumber()
*/
inline bool
IOProcessor () noexcept
{
return MyProc() == IOProcessorNumber();
}
inline int
IOProcessorNumber (MPI_Comm comm) noexcept
{
return (comm == ParallelDescriptor::Communicator()) ? ioProcessor : 0;
}
inline bool
IOProcessor (MPI_Comm comm) noexcept
{
return MyProc(comm) == IOProcessorNumber(comm);
}
//
inline int
TeamSize () noexcept
{
return m_Team.m_size;
}
inline int
NTeams () noexcept
{
return m_Team.m_numTeams;
}
inline int
MyTeamColor () noexcept
{
return m_Team.m_color;
}
inline int
MyTeamLead () noexcept
{
return m_Team.m_lead;
}
inline int
MyRankInTeam () noexcept
{
return m_Team.m_rankInTeam;
}
inline int
TeamLead (int rank) noexcept
{
return (rank >= 0) ? (rank - rank % m_Team.m_size) : MPI_PROC_NULL;
}
inline bool
isTeamLead () noexcept
{
return MyRankInTeam() == 0;
}
inline bool
sameTeam (int rank) noexcept
{
return MyTeamLead() == TeamLead(rank);
}
inline bool
sameTeam (int rankA, int rankB) noexcept
{
return TeamLead(rankA) == TeamLead(rankB);
}
inline int
RankInLeadComm (int rank) noexcept
{
return (rank >= 0) ? (rank / m_Team.m_size) : MPI_PROC_NULL;
}
inline bool
doTeamReduce () noexcept
{
return m_Team.m_do_team_reduce;
}
inline const ProcessTeam&
MyTeam () noexcept
{
return m_Team;
}
inline std::pair<int,int>
team_range (int begin, int end, int rit = -1, int nworkers = 0) noexcept
{
int rb, re;
{
if (rit < 0) rit = ParallelDescriptor::MyRankInTeam();
if (nworkers == 0) nworkers = ParallelDescriptor::TeamSize();
BL_ASSERT(rit<nworkers);
if (nworkers == 1) {
rb = begin;
re = end;
} else {
int ntot = end - begin;
int nr = ntot / nworkers;
int nlft = ntot - nr * nworkers;
if (rit < nlft) { // get nr+1 items
rb = begin + rit * (nr + 1);
re = rb + nr + 1;
} else { // get nr items
rb = begin + rit * nr + nlft;
re = rb + nr;
}
}
}
#ifdef AMREX_USE_OMP
int nthreads = omp_get_num_threads();
if (nthreads > 1) {
int tid = omp_get_thread_num();
int ntot = re - rb;
int nr = ntot / nthreads;
int nlft = ntot - nr * nthreads;
if (tid < nlft) { // get nr+1 items
rb += tid * (nr + 1);
re = rb + nr + 1;
} else { // get nr items
rb += tid * nr + nlft;
re = rb + nr;
}
}
#endif
return std::make_pair(rb,re);
}
template <typename F>
void team_for (int begin, int end, const F& f)
{
const auto& range = team_range(begin, end);
for (int i = range.first; i < range.second; ++i) {
f(i);
}
}
template <typename F> // rit: rank in team
void team_for (int begin, int end, int rit, const F& f)
{
const auto& range = team_range(begin, end, rit);
for (int i = range.first; i < range.second; ++i) {
f(i);
}
}
template <typename F> // rit: rank in team
void team_for (int begin, int end, int rit, int nworkers, const F& f)
{
const auto& range = team_range(begin, end, rit, nworkers);
for (int i = range.first; i < range.second; ++i) {
f(i);
}
}
void Barrier (const std::string& message = Unnamed);
void Barrier (const MPI_Comm &comm, const std::string& message = Unnamed);
Message Abarrier ();
Message Abarrier (const MPI_Comm &comm);
void Test (MPI_Request& request, int& flag, MPI_Status& status);
void Test (Vector<MPI_Request>& request, int& flag, Vector<MPI_Status>& status);
void Comm_dup (MPI_Comm comm, MPI_Comm& newcomm);
//! Abort with specified error code.
void Abort (int errorcode = SIGABRT, bool backtrace = true);
//! ErrorString return string associated with error internal error condition
const char* ErrorString (int errcode);
//! Returns wall-clock seconds since start of execution.
double second () noexcept;
//! And-wise boolean reduction.
void ReduceBoolAnd (bool& rvar);
//! And-wise boolean reduction to specified cpu.
void ReduceBoolAnd (bool& rvar, int cpu);
//! Or-wise boolean reduction.
void ReduceBoolOr (bool& rvar);
//! Or-wise boolean reduction to specified cpu.
void ReduceBoolOr (bool& rvar, int cpu);
//! Real sum reduction.
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealSum (T& rvar);
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealSum (T* rvar, int cnt);
// Having this for backward compatibility
void ReduceRealSum (Vector<std::reference_wrapper<Real> >&& rvar);
//
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealSum (Vector<std::reference_wrapper<T> >&& rvar);
//! Real sum reduction to specified cpu.
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealSum (T& rvar, int cpu);
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealSum (T* rvar, int cnt, int cpu);
// Having this for backward compatibility
void ReduceRealSum (Vector<std::reference_wrapper<Real> >&& rvar, int cpu);
//
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealSum (Vector<std::reference_wrapper<T> >&& rvar, int cpu);
//! Real max reduction.
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMax (T& rvar);
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMax (T* rvar, int cnt);
// Having this for backward compatibility
void ReduceRealMax (Vector<std::reference_wrapper<Real> >&& rvar);
//
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMax (Vector<std::reference_wrapper<T> >&& rvar);
//! Real max reduction to specified cpu.
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMax (T& rvar, int cpu);
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMax (T* rvar, int cnt, int cpu);
// Having this for backward compatibility
void ReduceRealMax (Vector<std::reference_wrapper<Real> >&& rvar, int cpu);
//
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMax (Vector<std::reference_wrapper<T> >&& rvar, int cpu);
//! Real min reduction.
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMin (T& rvar);
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMin (T* rvar, int cnt);
// Having this for backward compatibility
void ReduceRealMin (Vector<std::reference_wrapper<Real> >&& rvar);
//
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMin (Vector<std::reference_wrapper<T> >&& rvar);
//! Real min reduction to specified cpu.
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMin (T& rvar, int cpu);
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMin (T* rvar, int cnt, int cpu);
// Having this for backward compatibility
void ReduceRealMin (Vector<std::reference_wrapper<Real> >&& rvar, int cpu);
//
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value>::type
ReduceRealMin (Vector<std::reference_wrapper<T> >&& rvar, int cpu);
//! Integer sum reduction.
void ReduceIntSum (int& rvar);
void ReduceIntSum (int* rvar, int cnt);
void ReduceIntSum (Vector<std::reference_wrapper<int> >&& rvar);
//! Integer sum reduction to specified cpu.
void ReduceIntSum (int& rvar, int cpu);
void ReduceIntSum (int* rvar, int cnt, int cpu);
void ReduceIntSum (Vector<std::reference_wrapper<int> >&& rvar, int cpu);
//! Integer max reduction.
void ReduceIntMax (int& rvar);
void ReduceIntMax (int* rvar, int cnt);
void ReduceIntMax (Vector<std::reference_wrapper<int> >&& rvar);
//! Integer max reduction to specified cpu.
void ReduceIntMax (int& rvar, int cpu);
void ReduceIntMax (int* rvar, int cnt, int cpu);
void ReduceIntMax (Vector<std::reference_wrapper<int> >&& rvar, int cpu);
//! Integer min reduction.
void ReduceIntMin (int& rvar);
void ReduceIntMin (int* rvar, int cnt);
void ReduceIntMin (Vector<std::reference_wrapper<int> >&& rvar);
//! Integer min reduction to specified cpu.
void ReduceIntMin (int& rvar, int cpu);
void ReduceIntMin (int* rvar, int cnt, int cpu);
void ReduceIntMin (Vector<std::reference_wrapper<int> >&& rvar, int cpu);
//! Long sum reduction.
void ReduceLongSum (Long& rvar);
void ReduceLongSum (Long* rvar, int cnt);
void ReduceLongSum (Vector<std::reference_wrapper<Long> >&& rvar);
//! Long sum reduction to specified cpu.
void ReduceLongSum (Long& rvar, int cpu);
void ReduceLongSum (Long* rvar, int cnt, int cpu);
void ReduceLongSum (Vector<std::reference_wrapper<Long> >&& rvar, int cpu);
//! Long max reduction.
void ReduceLongMax (Long& rvar);
void ReduceLongMax (Long* rvar, int cnt);
void ReduceLongMax (Vector<std::reference_wrapper<Long> >&& rvar);
//! Long max reduction to specified cpu.
void ReduceLongMax (Long& rvar, int cpu);
void ReduceLongMax (Long* rvar, int cnt, int cpu);
void ReduceLongMax (Vector<std::reference_wrapper<Long> >&& rvar, int cpu);
//! Long min reduction.
void ReduceLongMin (Long& rvar);
void ReduceLongMin (Long* rvar, int cnt);
void ReduceLongMin (Vector<std::reference_wrapper<Long> >&& rvar);
//! Long min reduction to specified cpu.
void ReduceLongMin (Long& rvar, int cpu);
void ReduceLongMin (Long* rvar, int cnt, int cpu);
void ReduceLongMin (Vector<std::reference_wrapper<Long> >&& rvar, int cpu);
//! Long and-wise reduction.
void ReduceLongAnd (Long& rvar);
void ReduceLongAnd (Long* rvar, int cnt);
void ReduceLongAnd (Vector<std::reference_wrapper<Long> >&& rvar);
//! Long and-wise reduction to specified cpu.
void ReduceLongAnd (Long& rvar, int cpu);
void ReduceLongAnd (Long* rvar, int cnt, int cpu);
void ReduceLongAnd (Vector<std::reference_wrapper<Long> >&& rvar, int cpu);
//! Parallel gather.
void Gather (Real* sendbuf,
int sendcount,
Real* recvbuf,
int root);
/**
* \brief Returns sequential message sequence numbers, usually used as
* tags for send/recv.
*/
inline int SeqNum () noexcept { return ParallelContext::get_inc_mpi_tag(); }
template <class T> Message Asend(const T*, size_t n, int pid, int tag);
template <class T> Message Asend(const T*, size_t n, int pid, int tag, MPI_Comm comm);
template <class T> Message Asend(const std::vector<T>& buf, int pid, int tag);
template <class T> Message Arecv(T*, size_t n, int pid, int tag);
template <class T> Message Arecv(T*, size_t n, int pid, int tag, MPI_Comm comm);
template <class T> Message Arecv(std::vector<T>& buf, int pid, int tag);
template <class T> Message Send(const T* buf, size_t n, int dst_pid, int tag);
template <class T> Message Send(const T* buf, size_t n, int dst_pid, int tag, MPI_Comm comm);
template <class T> Message Send(const std::vector<T>& buf, int dst_pid, int tag);
template <class T> Message Recv(T*, size_t n, int pid, int tag);
template <class T> Message Recv(T*, size_t n, int pid, int tag, MPI_Comm comm);
template <class T> Message Recv(std::vector<T>& t, int pid, int tag);
template <class T> void Bcast(T*, size_t n, int root = 0);
template <class T> void Bcast(T*, size_t n, int root, const MPI_Comm &comm);
void Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm);
template <class T, class T1> void Scatter(T*, size_t n, const T1*, size_t n1, int root);
template <class T, class T1> void Gather(const T*, size_t n, T1*, size_t n1, int root);
template <class T> std::vector<T> Gather(const T&, int root);
template <class T> void Gatherv (const T* send, int sc,
T* recv, const std::vector<int>& rc, const std::vector<int>& disp,
int root);
//! Gather LayoutData values to a vector on root
template <class T> void GatherLayoutDataToVector (const LayoutData<T>& sendbuf,
Vector<T>& recvbuf,
int root);
void Wait (MPI_Request& req, MPI_Status& status);
void Waitall (Vector<MPI_Request>& reqs, Vector<MPI_Status>& status);
void Waitany (Vector<MPI_Request>& reqs, int &index, MPI_Status& status);
void Waitsome (Vector<MPI_Request>&, int&, Vector<int>&, Vector<MPI_Status>&);
void ReadAndBcastFile(const std::string &filename, Vector<char> &charBuf,
bool bExitOnError = true,
const MPI_Comm &comm = Communicator() );
void IProbe(int src_pid, int tag, int &mflag, MPI_Status &status);
void IProbe(int src_pid, int tag, MPI_Comm comm, int &mflag, MPI_Status &status);
/** Convert an MPI_THREAD_<X> level to string
*
* @param mtlev MPI_THREAD_<X> level
* @return string representation of the equivalent MPI macro name
*/
std::string
mpi_level_to_string (int mtlev);
// PMI = Process Management Interface, available on Crays. Provides API to
// query topology of the job.
#ifdef AMREX_PMI
void PMI_Initialize();
void PMI_PrintMeshcoords(const pmi_mesh_coord_t *pmi_mesh_coord);
#endif
#ifdef BL_USE_MPI
int select_comm_data_type (std::size_t nbytes);
std::size_t alignof_comm_data (std::size_t nbytes);
#endif
}
}
namespace amrex {
#ifdef BL_USE_MPI
template <class T>
ParallelDescriptor::Message
ParallelDescriptor::Asend (const T* buf, size_t n, int dst_pid, int tag)
{
return Asend(buf, n, dst_pid, tag, Communicator());
}
namespace ParallelDescriptor { // Have to use namespace here to work around a gcc bug
template <>
Message
Asend<char> (const char* buf, size_t n, int dst_pid, int tag, MPI_Comm comm);
template <class T>
Message
Asend (const T* buf, size_t n, int dst_pid, int tag, MPI_Comm comm)
{
static_assert(!std::is_same<char,T>::value, "Asend: char version has been specialized");
BL_PROFILE_T_S("ParallelDescriptor::Asend(TsiiM)", T);
BL_COMM_PROFILE(BLProfiler::AsendTsiiM, n * sizeof(T), dst_pid, tag);
MPI_Request req;
BL_MPI_REQUIRE( MPI_Isend(const_cast<T*>(buf),
n,
Mpi_typemap<T>::type(),
dst_pid,
tag,
comm,
&req) );
BL_COMM_PROFILE(BLProfiler::AsendTsiiM, BLProfiler::AfterCall(), dst_pid, tag);
return Message(req, Mpi_typemap<T>::type());
}
}
template <class T>
ParallelDescriptor::Message
ParallelDescriptor::Asend (const std::vector<T>& buf, int dst_pid, int tag)
{
return Asend(buf.data(), buf.size(), dst_pid, tag, Communicator());
}
template <class T>
ParallelDescriptor::Message
ParallelDescriptor::Send (const T* buf, size_t n, int dst_pid, int tag)
{
return Send(buf, n, dst_pid, tag, Communicator());
}
namespace ParallelDescriptor { // Have to use namespace here to work around a gcc bug
template <>
Message
Send<char> (const char* buf, size_t n, int dst_pid, int tag, MPI_Comm comm);
template <class T>
Message
Send (const T* buf, size_t n, int dst_pid, int tag, MPI_Comm comm)
{
static_assert(!std::is_same<char,T>::value, "Send: char version has been specialized");
BL_PROFILE_T_S("ParallelDescriptor::Send(Tsii)", T);
#ifdef BL_COMM_PROFILING
int dst_pid_world(-1);
MPI_Group groupComm, groupWorld;
BL_MPI_REQUIRE( MPI_Comm_group(comm, &groupComm) );
BL_MPI_REQUIRE( MPI_Comm_group(Communicator(), &groupWorld) );
BL_MPI_REQUIRE( MPI_Group_translate_ranks(groupComm, 1, &dst_pid, groupWorld, &dst_pid_world) );
BL_COMM_PROFILE(BLProfiler::SendTsii, n * sizeof(T), dst_pid_world, tag);
#endif
BL_MPI_REQUIRE( MPI_Send(const_cast<T*>(buf),
n,
Mpi_typemap<T>::type(),
dst_pid,
tag,
comm) );
BL_COMM_PROFILE(BLProfiler::SendTsii, BLProfiler::AfterCall(), dst_pid, tag);
return Message();
}
}
template <class T>
ParallelDescriptor::Message
ParallelDescriptor::Send (const std::vector<T>& buf, int dst_pid, int tag)
{
return Send(buf.data(), buf.size(), dst_pid, tag, Communicator());
}
template <class T>
ParallelDescriptor::Message
ParallelDescriptor::Arecv (T* buf, size_t n, int src_pid, int tag)
{
return Arecv(buf, n, src_pid, tag, Communicator());
}
namespace ParallelDescriptor { // Have to use namespace here to work around a gcc bug
template <>
Message
Arecv<char> (char* buf, size_t n, int src_pid, int tag, MPI_Comm comm);
template <class T>
Message
Arecv (T* buf, size_t n, int src_pid, int tag, MPI_Comm comm)
{
static_assert(!std::is_same<char,T>::value, "Arecv: char version has been specialized");
BL_PROFILE_T_S("ParallelDescriptor::Arecv(TsiiM)", T);
BL_COMM_PROFILE(BLProfiler::ArecvTsiiM, n * sizeof(T), src_pid, tag);
MPI_Request req;
BL_MPI_REQUIRE( MPI_Irecv(buf,
n,
Mpi_typemap<T>::type(),
src_pid,
tag,
comm,
&req) );
BL_COMM_PROFILE(BLProfiler::ArecvTsiiM, BLProfiler::AfterCall(), src_pid, tag);
return Message(req, Mpi_typemap<T>::type());
}
}
template <class T>
ParallelDescriptor::Message
ParallelDescriptor::Arecv (std::vector<T>& buf, int src_pid, int tag)
{
return Arecv(buf.data(), buf.size(), src_pid, tag, Communicator());
}
template <class T>
ParallelDescriptor::Message
ParallelDescriptor::Recv (T* buf, size_t n, int src_pid, int tag)
{
return Recv(buf, n, src_pid, tag, Communicator());
}
namespace ParallelDescriptor { // Have to use namespace here to work around a gcc bug
template <>
Message
Recv<char> (char* buf, size_t n, int src_pid, int tag, MPI_Comm comm);
template <class T>
Message
Recv (T* buf, size_t n, int src_pid, int tag, MPI_Comm comm)
{
static_assert(!std::is_same<char,T>::value, "Recv: char version has been specialized");
BL_PROFILE_T_S("ParallelDescriptor::Recv(Tsii)", T);
BL_COMM_PROFILE(BLProfiler::RecvTsii, BLProfiler::BeforeCall(), src_pid, tag);
MPI_Status stat;
BL_MPI_REQUIRE( MPI_Recv(buf,
n,
Mpi_typemap<T>::type(),
src_pid,
tag,
comm,
&stat) );
#ifdef BL_COMM_PROFILING
int src_pid_comm(stat.MPI_SOURCE);
int src_pid_world(stat.MPI_SOURCE);
if(src_pid_comm != MPI_ANY_SOURCE) {
MPI_Group groupComm, groupWorld;
BL_MPI_REQUIRE( MPI_Comm_group(comm, &groupComm) );
BL_MPI_REQUIRE( MPI_Comm_group(Communicator(), &groupWorld) );
BL_MPI_REQUIRE( MPI_Group_translate_ranks(groupComm, 1, &src_pid_comm, groupWorld, &src_pid_world) );
}
BL_COMM_PROFILE(BLProfiler::RecvTsii, n * sizeof(T), src_pid_world, stat.MPI_TAG);
#endif
return Message(stat, Mpi_typemap<T>::type());
}
}
template <class T>
ParallelDescriptor::Message
ParallelDescriptor::Recv (std::vector<T>& buf, int src_pid, int tag)
{
return Recv(buf.data(), buf.size(), src_pid, tag, Communicator());
}
template <class T>
void
ParallelDescriptor::Bcast (T* t,
size_t n,
int root)
{
#ifdef BL_LAZY
Lazy::EvalReduction();
#endif
BL_ASSERT(n < static_cast<size_t>(std::numeric_limits<int>::max()));
BL_PROFILE_T_S("ParallelDescriptor::Bcast(Tsi)", T);
BL_COMM_PROFILE(BLProfiler::BCastTsi, BLProfiler::BeforeCall(), root, BLProfiler::NoTag());
BL_MPI_REQUIRE( MPI_Bcast(t,
n,
Mpi_typemap<T>::type(),
root,
Communicator()) );
BL_COMM_PROFILE(BLProfiler::BCastTsi, n * sizeof(T), root, BLProfiler::NoTag());
}
template <class T>
void
ParallelDescriptor::Bcast (T* t,
size_t n,
int root,
const MPI_Comm &comm)
{
#ifdef BL_LAZY
int r;
MPI_Comm_compare(comm, Communicator(), &r);
if (r == MPI_IDENT)
Lazy::EvalReduction();
#endif
BL_ASSERT(n < static_cast<size_t>(std::numeric_limits<int>::max()));
BL_PROFILE_T_S("ParallelDescriptor::Bcast(Tsi)", T);
BL_COMM_PROFILE(BLProfiler::BCastTsi, BLProfiler::BeforeCall(), root, BLProfiler::NoTag());
BL_MPI_REQUIRE( MPI_Bcast(t,
n,
Mpi_typemap<T>::type(),
root,
comm) );
BL_COMM_PROFILE(BLProfiler::BCastTsi, n * sizeof(T), root, BLProfiler::NoTag());
}
template <class T, class T1>
void
ParallelDescriptor::Gather (const T* t,
size_t n,
T1* t1,
size_t n1,
int root)
{
BL_PROFILE_T_S("ParallelDescriptor::Gather(TsT1si)", T);
BL_COMM_PROFILE(BLProfiler::GatherTsT1Si, BLProfiler::BeforeCall(), root, BLProfiler::NoTag());
BL_ASSERT(n < static_cast<size_t>(std::numeric_limits<int>::max()));
BL_ASSERT(n1 < static_cast<size_t>(std::numeric_limits<int>::max()));
BL_MPI_REQUIRE( MPI_Gather(const_cast<T*>(t),
n,
Mpi_typemap<T>::type(),
t1,
n1,
Mpi_typemap<T1>::type(),
root,
Communicator()) );
BL_COMM_PROFILE(BLProfiler::GatherTsT1Si, n * sizeof(T), root, BLProfiler::NoTag());
}
template <class T>
std::vector<T>
ParallelDescriptor::Gather (const T& t, int root)
{
BL_PROFILE_T_S("ParallelDescriptor::Gather(Ti)", T);
BL_COMM_PROFILE(BLProfiler::GatherTi, BLProfiler::BeforeCall(), root, BLProfiler::NoTag());
std::vector<T> resl;
if ( root == MyProc() ) resl.resize(NProcs());
BL_MPI_REQUIRE( MPI_Gather(const_cast<T*>(&t),
1,
Mpi_typemap<T>::type(),
resl.data(),
1,
Mpi_typemap<T>::type(),
root,
Communicator()) );
BL_COMM_PROFILE(BLProfiler::GatherTi, sizeof(T), root, BLProfiler::NoTag());
return resl;
}
template <class T>
void
ParallelDescriptor::Gatherv (const T* send, int sc,
T* recv, const std::vector<int>& rc, const std::vector<int>& disp,
int root)
{
BL_PROFILE_T_S("ParallelDescriptor::Gatherv(Ti)", T);
BL_COMM_PROFILE(BLProfiler::Gatherv, BLProfiler::BeforeCall(), root, BLProfiler::NoTag());
MPI_Gatherv(send, sc, ParallelDescriptor::Mpi_typemap<T>::type(),
recv, &rc[0], &disp[0], ParallelDescriptor::Mpi_typemap<T>::type(),
root, Communicator());
BL_COMM_PROFILE(BLProfiler::Gatherv, std::accumulate(rc.begin(),rc.end(),0)*sizeof(T), root, BLProfiler::NoTag());
}
template <class T>
void
ParallelDescriptor::GatherLayoutDataToVector (const LayoutData<T>& sendbuf,
Vector<T>& recvbuf, int root)
{
BL_PROFILE_T_S("ParallelDescriptor::GatherLayoutData(Ti)", T);
// Gather prelims
Vector<int> index_to_send = sendbuf.IndexArray();
Vector<T> T_to_send;
T_to_send.reserve(sendbuf.local_size());
for (int i : sendbuf.IndexArray())
{
T_to_send.push_back(sendbuf[i]);
}
int nprocs = ParallelContext::NProcsSub();
Vector<int> recvcount(nprocs, 0);
recvbuf.resize(sendbuf.size());
const Vector<int>& old_pmap = sendbuf.DistributionMap().ProcessorMap();
for (int i=0; i<old_pmap.size(); ++i)
{
++recvcount[old_pmap[i]];
}
// Make a map from post-gather to pre-gather index
Vector<Vector<int>> new_ind_to_old_ind(nprocs);
for (int i=0; i<nprocs; ++i)
{
new_ind_to_old_ind[i].reserve(recvcount[i]);
}
for (int i=0; i<old_pmap.size(); ++i)
{
new_ind_to_old_ind[old_pmap[i]].push_back(i);
}
// Flatten
Vector<int> new_index_to_old_index;
new_index_to_old_index.reserve(old_pmap.size());
for (const Vector<int>& v : new_ind_to_old_ind)
{
if (v.size()>0)
{
for (int el : v)
{