Debian Codegraph @

dune/uggrid/parallel/ppif/ppifcontext.hh

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3 #ifndef DUNE_UGGRID_PARALLEL_PPIF_PPIFCONTEXT_HH
4 #define DUNE_UGGRID_PARALLEL_PPIF_PPIFCONTEXT_HH 1
5
6 #include <memory>
7 #include <array>
8
9 #if ModelP
10 # include <mpi.h>
11 #endif
12
13 #include <dune/uggrid/parallel/ppif/ppiftypes.hh>
14
15 namespace PPIF {
16
17 /**
18 * context object for low-level parallel communication
19 */
20 class PPIFContext
21 {
22 public:
23 /**
24 * constructor.
25 *
26 * By default `MPI_COMM_WORLD` is used for communication.
27 *
28 * \note This is a MPI collective operation (invokes `MPI_Comm_dup`)
29 */
30 PPIFContext();
31
32 /**
33 * destructor
34 *
35 * \note This is a MPI collective operation (invokes `MPI_Comm_free`)
36 */
37 ~PPIFContext();
38
39 /**
40 * our rank; in the interval [0, procs)
41 */
42 int me() const
43 { return me_; }
44
45 /**
46 * rank of the master process (usually 0)
47 */
48 int master() const
49 { return master_; }
50
51 /**
52 * returns `true` if local process is master
53 */
54 bool isMaster() const
55 { return me() == master(); }
56
57 /**
58 * number of processes
59 */
60 int procs() const
61 { return procs_; }
62
63 protected:
64 int me_ = 0;
65 int master_ = 0;
66 int procs_ = 1;
67
68 #if ModelP || DOXYGEN
69 public:
70 /**
71 * maximum number of downtree nodes max
72 * log2(P)
73 */
74 static const int MAXT = 15;
75
76 /**
77 * constructor.
78 *
79 * \note This is a MPI collective operation (invokes `MPI_Comm_dup`)
80 */
81 explicit PPIFContext(const MPI_Comm& comm);
82
83 /**
84 * MPI communicator
85 */
86 MPI_Comm comm() const
87 { return comm_; }
88
89 int dimX() const
90 { return dims_[0]; }
91
92 int dimY() const
93 { return dims_[1]; }
94
95 int dimZ() const
96 { return dims_[2]; }
97
98 /**
99 * degree of downtree nodes
100 */
101 int degree() const
102 { return degree_; }
103
104 /**
105 * channel uptree
106 */
107 VChannelPtr uptree() const
108 { return uptree_; }
109
110 /**
111 * channels downtree (may be empty)
112 */
113 const std::array<VChannelPtr, MAXT>& downtree() const
114 { return downtree_; }
115
116 /**
117 * number of processors in subtree
118 */
119 const std::array<int, MAXT>& slvcnt() const
120 { return slvcnt_; }
121
122 protected:
123 MPI_Comm comm_ = MPI_COMM_NULL;
124
125 std::array<int, 3> dims_ = {{1, 1, 1}};
126 int degree_ = 0;
127 VChannelPtr uptree_ = nullptr;
128 std::array<VChannelPtr, MAXT> downtree_ = {};
129 std::array<int, MAXT> slvcnt_ = {};
130
131 friend void InitPPIF(PPIFContext&);
132 friend void ExitPPIF(PPIFContext&);
133 #endif
134 };
135
136 } /* namespace PPIF */
137
138 #endif