Block-Structured AMR Software Framework
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
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
12namespace amrex::ParallelContext {
13
14class Frame
15{
16public:
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
42private:
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
55inline MPI_Comm CommunicatorAll () noexcept { return frames[0].comm; }
57inline MPI_Group GroupAll () noexcept { return frames[0].group; }
59inline int NProcsAll () noexcept { return frames[0].NProcs(); }
61inline int MyProcAll () noexcept { return frames[0].MyProc(); }
63inline int IOProcessorNumberAll () noexcept { return frames[0].IOProc(); }
65inline bool IOProcessorAll () noexcept { return MyProcAll() == IOProcessorNumberAll(); }
67inline std::ofstream * OFSPtrAll () noexcept { return frames[0].get_ofs_ptr(); }
68
70inline MPI_Comm CommunicatorSub () noexcept { return frames.back().comm; }
72inline MPI_Group GroupSub () noexcept { return frames.back().group; }
74inline int NProcsSub () noexcept { return frames.back().NProcs(); }
76inline int MyProcSub () noexcept { return frames.back().MyProc(); }
78inline int IOProcessorNumberSub () noexcept { return frames.back().IOProc(); }
80inline bool IOProcessorSub () noexcept { return MyProcSub() == IOProcessorNumberSub(); }
82inline std::ofstream * OFSPtrSub () noexcept { return frames.back().get_ofs_ptr(); }
83
84#ifdef BL_USE_MPI
85inline void BarrierSub () noexcept { MPI_Barrier(CommunicatorSub()); }
86inline void BarrierAll () noexcept { MPI_Barrier(CommunicatorAll()); }
87#else
88inline void BarrierSub () noexcept { }
89inline void BarrierAll () noexcept { }
90#endif
91
93inline int get_inc_mpi_tag () noexcept { return frames.back().get_inc_mpi_tag(); }
95inline int local_to_global_rank (int rank) noexcept { return Frame::local_to_global_rank(rank); }
96inline void local_to_global_rank (int* global, const int* local, int n) noexcept
97 { Frame::local_to_global_rank(global, local, n); }
98inline int global_to_local_rank (int rank) noexcept { return Frame::global_to_local_rank(rank); }
99inline void global_to_local_rank (int* local, const int* global, int n) noexcept
100 { Frame::global_to_local_rank(local, global, n); }
101
102inline void push (MPI_Comm c) { frames.emplace_back(c); }
103inline void push (MPI_Comm c, int id, int io_rank) { frames.emplace_back(c, id, io_rank); }
104inline void set_last_frame_ofs (const std::string & filename) {
105 frames.back().set_ofs_name(filename);
106}
108inline 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
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
std::ofstream * OFSPtrAll() noexcept
Pointer to ofstream.
Definition AMReX_ParallelContext.H:67
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
std::ofstream * OFSPtrSub() noexcept
Pointer to ofstream.
Definition AMReX_ParallelContext.H:82
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
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
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