Block-Structured AMR Software Framework
AMReX_ParallelContext.H
Go to the documentation of this file.
1 #ifndef AMREX_PARALLELCONTEXT_H
2 #define AMREX_PARALLELCONTEXT_H
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_Extension.H>
6 #include <AMReX_Vector.H>
7 #include <AMReX_ccse-mpi.H>
8 
9 #include <iosfwd>
10 #include <memory>
11 
12 namespace amrex::ParallelContext {
13 
14 class Frame
15 {
16 public:
17 
18  Frame (MPI_Comm c, int id, int io_rank);
19  explicit Frame (MPI_Comm c);
20  Frame (Frame const&) = delete;
21  Frame& operator= (Frame const&) = delete;
22  Frame& operator= (Frame &&) = delete;
23  Frame (Frame && rhs) noexcept;
24  ~Frame ();
25 
26  [[nodiscard]] int MyID () const noexcept { return m_id; }
27  [[nodiscard]] int MyProc () const noexcept { return m_rank_me; }
28  [[nodiscard]] int NProcs () const noexcept { return m_nranks; }
29  [[nodiscard]] int IOProc () const noexcept { return m_io_rank; }
30  static int local_to_global_rank (int lrank);
31  static void local_to_global_rank (int* global, const int* local, int n);
32  static void global_to_local_rank (int* local, const int* global, int n);
33  static int global_to_local_rank (int grank);
34  int get_inc_mpi_tag ();
35  void set_ofs_name (std::string filename);
36  std::ofstream * get_ofs_ptr ();
37 
41 
42 private:
43  int m_id = -1;
44  int m_rank_me = 0;
45  int m_nranks = 1;
46  int m_mpi_tag = -1;
47  int m_io_rank = -1;
48  std::string m_out_filename;
49  std::unique_ptr<std::ofstream> m_out;
50 };
51 
53 
55 inline MPI_Comm CommunicatorAll () noexcept { return frames[0].comm; }
57 inline MPI_Group GroupAll () noexcept { return frames[0].group; }
59 inline int NProcsAll () noexcept { return frames[0].NProcs(); }
61 inline int MyProcAll () noexcept { return frames[0].MyProc(); }
63 inline int IOProcessorNumberAll () noexcept { return frames[0].IOProc(); }
65 inline bool IOProcessorAll () noexcept { return MyProcAll() == IOProcessorNumberAll(); }
67 inline std::ofstream * OFSPtrAll () noexcept { return frames[0].get_ofs_ptr(); }
68 
70 inline MPI_Comm CommunicatorSub () noexcept { return frames.back().comm; }
72 inline MPI_Group GroupSub () noexcept { return frames.back().group; }
74 inline int NProcsSub () noexcept { return frames.back().NProcs(); }
76 inline int MyProcSub () noexcept { return frames.back().MyProc(); }
78 inline int IOProcessorNumberSub () noexcept { return frames.back().IOProc(); }
80 inline bool IOProcessorSub () noexcept { return MyProcSub() == IOProcessorNumberSub(); }
82 inline std::ofstream * OFSPtrSub () noexcept { return frames.back().get_ofs_ptr(); }
83 
84 #ifdef BL_USE_MPI
85 inline void BarrierSub () noexcept { MPI_Barrier(CommunicatorSub()); }
86 inline void BarrierAll () noexcept { MPI_Barrier(CommunicatorAll()); }
87 #else
88 inline void BarrierSub () noexcept { }
89 inline void BarrierAll () noexcept { }
90 #endif
91 
93 inline int get_inc_mpi_tag () noexcept { return frames.back().get_inc_mpi_tag(); }
95 inline int local_to_global_rank (int rank) noexcept { return Frame::local_to_global_rank(rank); }
96 inline void local_to_global_rank (int* global, const int* local, int n) noexcept
97  { Frame::local_to_global_rank(global, local, n); }
98 inline int global_to_local_rank (int rank) noexcept { return Frame::global_to_local_rank(rank); }
99 inline void global_to_local_rank (int* local, const int* global, int n) noexcept
100  { Frame::global_to_local_rank(local, global, n); }
101 
102 inline void push (MPI_Comm c) { frames.emplace_back(c); }
103 inline void push (MPI_Comm c, int id, int io_rank) { frames.emplace_back(c, id, io_rank); }
104 inline void set_last_frame_ofs (const std::string & filename) {
105  frames.back().set_ofs_name(filename);
106 }
108 inline void pop () { frames.pop_back(); }
109 
110 }
111 
112 #endif // AMREX_PARALLELCONTEXT_H
#define AMREX_EXPORT
Definition: AMReX_Extension.H:191
static constexpr int MPI_GROUP_NULL
Definition: AMReX_ccse-mpi.H:56
int MPI_Comm
Definition: AMReX_ccse-mpi.H:47
int MPI_Group
Definition: AMReX_ccse-mpi.H:48
static constexpr int MPI_COMM_NULL
Definition: AMReX_ccse-mpi.H:55
Definition: AMReX_ParallelContext.H:15
MPI_Comm comm
sub-communicator associated with frame
Definition: AMReX_ParallelContext.H:39
static int local_to_global_rank(int lrank)
Definition: AMReX_ParallelContext.cpp:51
int m_mpi_tag
Definition: AMReX_ParallelContext.H:46
void set_ofs_name(std::string filename)
Definition: AMReX_ParallelContext.cpp:112
int IOProc() const noexcept
Definition: AMReX_ParallelContext.H:29
int m_io_rank
Definition: AMReX_ParallelContext.H:47
MPI_Group group
to avoid repeatedly creating groups in rank translation
Definition: AMReX_ParallelContext.H:40
int MyProc() const noexcept
Definition: AMReX_ParallelContext.H:27
int m_nranks
local # of ranks
Definition: AMReX_ParallelContext.H:45
std::string m_out_filename
Definition: AMReX_ParallelContext.H:48
Frame & operator=(Frame const &)=delete
int m_id
frame ID
Definition: AMReX_ParallelContext.H:43
~Frame()
Definition: AMReX_ParallelContext.cpp:41
Frame(Frame const &)=delete
std::unique_ptr< std::ofstream > m_out
Definition: AMReX_ParallelContext.H:49
int NProcs() const noexcept
Definition: AMReX_ParallelContext.H:28
static void global_to_local_rank(int *local, const int *global, int n)
Definition: AMReX_ParallelContext.cpp:85
int m_rank_me
local rank
Definition: AMReX_ParallelContext.H:44
std::ofstream * get_ofs_ptr()
Definition: AMReX_ParallelContext.cpp:119
int get_inc_mpi_tag()
Definition: AMReX_ParallelContext.cpp:103
Frame(MPI_Comm c, int id, int io_rank)
Definition: AMReX_ParallelContext.cpp:11
int MyID() const noexcept
Definition: AMReX_ParallelContext.H:26
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition: AMReX_Vector.H:27
Definition: AMReX_ParallelContext.cpp:7
void push(MPI_Comm c)
Definition: AMReX_ParallelContext.H:102
void BarrierSub() noexcept
Definition: AMReX_ParallelContext.H:88
MPI_Group GroupAll() noexcept
world group
Definition: AMReX_ParallelContext.H:57
int MyProcAll() noexcept
my rank in world communicator
Definition: AMReX_ParallelContext.H:61
MPI_Comm CommunicatorSub() noexcept
sub-communicator for current frame
Definition: AMReX_ParallelContext.H:70
int get_inc_mpi_tag() noexcept
get and increment mpi tag in current frame
Definition: AMReX_ParallelContext.H:93
bool IOProcessorAll() noexcept
Am IO processor for world communicator?
Definition: AMReX_ParallelContext.H:65
int MyProcSub() noexcept
my sub-rank in current frame
Definition: AMReX_ParallelContext.H:76
int NProcsAll() noexcept
number of ranks in world communicator
Definition: AMReX_ParallelContext.H:59
void pop()
Note that it's the user's responsibility to free the MPI_Comm.
Definition: AMReX_ParallelContext.H:108
int local_to_global_rank(int rank) noexcept
translate between local rank and global rank
Definition: AMReX_ParallelContext.H:95
Vector< Frame > frames
stack of communicator frames
Definition: AMReX_ParallelContext.cpp:9
int global_to_local_rank(int rank) noexcept
Definition: AMReX_ParallelContext.H:98
int NProcsSub() noexcept
number of ranks in current frame
Definition: AMReX_ParallelContext.H:74
std::ofstream * OFSPtrSub() noexcept
Pointer to ofstream.
Definition: AMReX_ParallelContext.H:82
void BarrierAll() noexcept
Definition: AMReX_ParallelContext.H:89
MPI_Group GroupSub() noexcept
sub-group for current frame
Definition: AMReX_ParallelContext.H:72
int IOProcessorNumberSub() noexcept
IO sub-rank in current frame.
Definition: AMReX_ParallelContext.H:78
std::ofstream * OFSPtrAll() noexcept
Pointer to ofstream.
Definition: AMReX_ParallelContext.H:67
int IOProcessorNumberAll() noexcept
IO rank in world communicator.
Definition: AMReX_ParallelContext.H:63
MPI_Comm CommunicatorAll() noexcept
world communicator
Definition: AMReX_ParallelContext.H:55
void set_last_frame_ofs(const std::string &filename)
Definition: AMReX_ParallelContext.H:104
bool IOProcessorSub() noexcept
Am IO processor for current frame?
Definition: AMReX_ParallelContext.H:80