Skip to content

Commit db6f15b

Browse files
committed
Restructure monitor class and add trailing _ for private members
1 parent b596dcd commit db6f15b

File tree

1 file changed

+65
-64
lines changed

1 file changed

+65
-64
lines changed

c++/mpi/monitor.hpp

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -60,33 +60,6 @@ namespace mpi {
6060
int event = 0;
6161
};
6262

63-
// MPI communicator.
64-
mpi::communicator comm;
65-
66-
// Future objects stored on the root process for local events on non-root processes.
67-
std::vector<future> root_futures;
68-
69-
// MPI request for the broadcasting done on the root process in case an event has occurred on any rank.
70-
MPI_Request req_ibcast_any{};
71-
72-
// MPI request for the broadcasting done on the root process in case an event has occurred on all ranks.
73-
MPI_Request req_ibcast_all{};
74-
75-
// MPI request for the sending done on non-root processes.
76-
MPI_Request req_isent{};
77-
78-
// Set to 1, if a local event has occurred on this process.
79-
int local_event = 0;
80-
81-
// Set to 1, if an event has occurred on any process.
82-
int any_event = 0;
83-
84-
// Set to 1, if an event has occurred on all processes.
85-
int all_events = 0;
86-
87-
// Set to true, if finialize_communications() has been called.
88-
bool finalized = false;
89-
9063
public:
9164
/**
9265
* @brief Construct a monitor on top of a given mpi::communicator.
@@ -101,16 +74,16 @@ namespace mpi {
10174
*
10275
* @param c mpi::communicator.
10376
*/
104-
monitor(mpi::communicator c) : comm(c.duplicate()) {
105-
if (comm.rank() == 0) {
106-
root_futures.resize(c.size() - 1);
77+
monitor(mpi::communicator c) : comm_(c.duplicate()) {
78+
if (comm_.rank() == 0) {
79+
root_futures_.resize(c.size() - 1);
10780
for (int rank = 1; rank < c.size(); ++rank) {
108-
check_mpi_call(MPI_Irecv(&(root_futures[rank - 1].event), 1, MPI_INT, rank, rank, comm.get(), &(root_futures[rank - 1].request)),
81+
check_mpi_call(MPI_Irecv(&(root_futures_[rank - 1].event), 1, MPI_INT, rank, rank, comm_.get(), &(root_futures_[rank - 1].request)),
10982
"MPI_Irecv");
11083
}
11184
} else {
112-
check_mpi_call(MPI_Ibcast(&any_event, 1, MPI_INT, 0, comm.get(), &req_ibcast_any), "MPI_Ibcast");
113-
check_mpi_call(MPI_Ibcast(&all_events, 1, MPI_INT, 0, comm.get(), &req_ibcast_all), "MPI_Ibcast");
85+
check_mpi_call(MPI_Ibcast(&any_event_, 1, MPI_INT, 0, comm_.get(), &req_ibcast_any_), "MPI_Ibcast");
86+
check_mpi_call(MPI_Ibcast(&all_events_, 1, MPI_INT, 0, comm_.get(), &req_ibcast_all_), "MPI_Ibcast");
11487
}
11588
}
11689

@@ -136,16 +109,16 @@ namespace mpi {
136109
*/
137110
void report_local_event() {
138111
// prevent sending multiple signals
139-
if (local_event or finalized) { return; }
112+
if (local_event_ or finalized_) { return; }
140113

141114
// a local event has occurred
142-
local_event = 1;
143-
if (comm.rank() == 0) {
115+
local_event_ = 1;
116+
if (comm_.rank() == 0) {
144117
// on root process, check all other nodes and perform necessary broadcasts
145118
root_check_nodes_and_bcast();
146119
} else {
147120
// on non-root processes, let the root process know about the local event
148-
check_mpi_call(MPI_Isend(&local_event, 1, MPI_INT, 0, comm.rank(), comm.get(), &req_isent), "MPI_Isend");
121+
check_mpi_call(MPI_Isend(&local_event_, 1, MPI_INT, 0, comm_.rank(), comm_.get(), &req_isent_), "MPI_Isend");
149122
}
150123
}
151124

@@ -166,24 +139,24 @@ namespace mpi {
166139
*/
167140
[[nodiscard]] bool event_on_any_rank() {
168141
// if final_communications() has already been called, any_event == 0 if no event has occurred, otherwise it is 1
169-
if (finalized) return any_event;
142+
if (finalized_) return any_event_;
170143

171144
// if a local event has occurred, we return true
172-
if (local_event) return true;
145+
if (local_event_) return true;
173146

174147
// on the root process, we first check the status of all non-root processes, perform the necessary broadcasts and
175148
// return true if an event has occurred
176-
if (comm.rank() == 0) {
149+
if (comm_.rank() == 0) {
177150
root_check_nodes_and_bcast();
178-
return any_event;
151+
return any_event_;
179152
}
180153

181154
// on non-root processes, we check the status of the corresponding broadcast and return true if an event has
182155
// occurred
183156
MPI_Status status;
184157
int has_received = 0;
185-
check_mpi_call(MPI_Test(&req_ibcast_any, &has_received, &status), "MPI_Test");
186-
return has_received and any_event;
158+
check_mpi_call(MPI_Test(&req_ibcast_any_, &has_received, &status), "MPI_Test");
159+
return has_received and any_event_;
187160
}
188161

189162
/**
@@ -202,21 +175,21 @@ namespace mpi {
202175
[[nodiscard]] bool event_on_all_ranks() {
203176
// if final_communications() has already been called, all_events == 0 if an event has not occurred on every
204177
// process, otherwise it is 1
205-
if (finalized) return all_events;
178+
if (finalized_) return all_events_;
206179

207180
// on the root process, we first check the status of all non-root processes, perform the necessary broadcasts and
208181
// return true if an event has occurred on all of them
209-
if (comm.rank() == 0) {
182+
if (comm_.rank() == 0) {
210183
root_check_nodes_and_bcast();
211-
return all_events;
184+
return all_events_;
212185
}
213186

214187
// on non-root processes, we check the status of the broadcast and return true if an event has occurred on all
215188
// processes
216189
MPI_Status status;
217190
int has_received = 0;
218-
check_mpi_call(MPI_Test(&req_ibcast_all, &has_received, &status), "MPI_Test");
219-
return has_received and all_events;
191+
check_mpi_call(MPI_Test(&req_ibcast_all_, &has_received, &status), "MPI_Test");
192+
return has_received and all_events_;
220193
}
221194

222195
/**
@@ -229,29 +202,29 @@ namespace mpi {
229202
*/
230203
void finalize_communications() {
231204
// prevent multiple calls
232-
if (finalized) return;
205+
if (finalized_) return;
233206

234-
if (comm.rank() == 0) {
207+
if (comm_.rank() == 0) {
235208
// on root process, wait for all non-root processes to finish their MPI_Isend calls
236209
while (root_check_nodes_and_bcast()) {
237210
usleep(100); // 100 us (micro seconds)
238211
}
239212
// and perform broadcasts in case they have not been done yet
240-
if (not any_event) { check_mpi_call(MPI_Ibcast(&any_event, 1, MPI_INT, 0, comm.get(), &req_ibcast_any), "MPI_Ibcast"); }
241-
if (not all_events) { check_mpi_call(MPI_Ibcast(&all_events, 1, MPI_INT, 0, comm.get(), &req_ibcast_all), "MPI_Ibcast"); }
213+
if (not any_event_) { check_mpi_call(MPI_Ibcast(&any_event_, 1, MPI_INT, 0, comm_.get(), &req_ibcast_any_), "MPI_Ibcast"); }
214+
if (not all_events_) { check_mpi_call(MPI_Ibcast(&all_events_, 1, MPI_INT, 0, comm_.get(), &req_ibcast_all_), "MPI_Ibcast"); }
242215
} else {
243216
// on non-root processes, perform MPI_Isend call in case it has not been done yet
244-
if (not local_event) { check_mpi_call(MPI_Isend(&local_event, 1, MPI_INT, 0, comm.rank(), comm.get(), &req_isent), "MPI_Isend"); }
217+
if (not local_event_) { check_mpi_call(MPI_Isend(&local_event_, 1, MPI_INT, 0, comm_.rank(), comm_.get(), &req_isent_), "MPI_Isend"); }
245218
}
246219

247220
// all nodes wait for the broadcasts to be completed
248221
MPI_Status status_any, status_all;
249-
check_mpi_call(MPI_Wait(&req_ibcast_any, &status_any), "MPI_Wait");
250-
check_mpi_call(MPI_Wait(&req_ibcast_all, &status_all), "MPI_Wait");
222+
check_mpi_call(MPI_Wait(&req_ibcast_any_, &status_any), "MPI_Wait");
223+
check_mpi_call(MPI_Wait(&req_ibcast_all_, &status_all), "MPI_Wait");
251224

252225
// free the communicator
253-
comm.free();
254-
finalized = true;
226+
comm_.free();
227+
finalized_ = true;
255228
}
256229

257230
private:
@@ -263,24 +236,52 @@ namespace mpi {
263236
bool any = false;
264237
bool all = true;
265238
bool finished = true;
266-
for (auto &[request, rank_event] : root_futures) {
239+
for (auto &[request, rank_event] : root_futures_) {
267240
MPI_Status status;
268241
int rank_received = 0;
269242
check_mpi_call(MPI_Test(&request, &rank_received, &status), "MPI_Test");
270243
any |= (rank_received and rank_event);
271244
all &= (rank_received and rank_event);
272245
finished &= rank_received;
273246
}
274-
if (not any_event and (any or local_event)) {
275-
any_event = 1;
276-
check_mpi_call(MPI_Ibcast(&any_event, 1, MPI_INT, 0, comm.get(), &req_ibcast_any), "MPI_Ibcast");
247+
if (not any_event_ and (any or local_event_)) {
248+
any_event_ = 1;
249+
check_mpi_call(MPI_Ibcast(&any_event_, 1, MPI_INT, 0, comm_.get(), &req_ibcast_any_), "MPI_Ibcast");
277250
}
278-
if (not all_events and all and local_event) {
279-
all_events = 1;
280-
check_mpi_call(MPI_Ibcast(&all_events, 1, MPI_INT, 0, comm.get(), &req_ibcast_all), "MPI_Ibcast");
251+
if (not all_events_ and all and local_event_) {
252+
all_events_ = 1;
253+
check_mpi_call(MPI_Ibcast(&all_events_, 1, MPI_INT, 0, comm_.get(), &req_ibcast_all_), "MPI_Ibcast");
281254
}
282255
return not finished;
283256
}
257+
258+
private:
259+
// MPI communicator.
260+
mpi::communicator comm_;
261+
262+
// Future objects stored on the root process for local events on non-root processes.
263+
std::vector<future> root_futures_;
264+
265+
// MPI request for the broadcasting done on the root process in case an event has occurred on any rank.
266+
MPI_Request req_ibcast_any_{};
267+
268+
// MPI request for the broadcasting done on the root process in case an event has occurred on all ranks.
269+
MPI_Request req_ibcast_all_{};
270+
271+
// MPI request for the sending done on non-root processes.
272+
MPI_Request req_isent_{};
273+
274+
// Set to 1, if a local event has occurred on this process.
275+
int local_event_ = 0;
276+
277+
// Set to 1, if an event has occurred on any process.
278+
int any_event_ = 0;
279+
280+
// Set to 1, if an event has occurred on all processes.
281+
int all_events_ = 0;
282+
283+
// Set to true, if finialize_communications() has been called.
284+
bool finalized_ = false;
284285
};
285286

286287
} // namespace mpi

0 commit comments

Comments
 (0)