In this example, we show how to use the mpi::monitor class to communicate and process errors across a communicator.
#include <iostream>
int main(int argc, char *argv[]) {
auto stop = [&monitor, world](int i) {
bool res = false;
if (monitor.event_on_any_rank()) {
std::cerr <<
"Processor " << world.
rank() <<
": After " << i <<
" steps an event has been communicated.\n";
res = true;
}
return res;
};
int event_rank = 3;
for (int i = 0; i < 1000000; ++i) {
if (world.
rank() == event_rank) {
std::cerr << "Processor " << event_rank << ": Local event reported.\n";
monitor.report_local_event();
}
if (stop(i)) break;
}
if (monitor.event_on_any_rank()) {
std::cout << "Oh no! An event occurred somewhere and the loop has not been finished on all processes.\n";
} else {
std::cout << "No worries, all processes have finished the loop.\n";
}
}
}
C++ wrapper around MPI_Comm providing various convenience functions.
int rank() const
Get the rank of the calling process in the communicator.
Constructed on top of an MPI communicator, this class helps to monitor and communicate events across ...
Provides a class for monitoring and communicating events across multiple processes.
Includes all relevant mpi headers.
RAII class to initialize and finalize MPI.
Output (running with -n 12
):
Processor 3: Local event reported.
Processor 3: After 0 steps an event has been communicated.
Processor 4: After 8428 steps an event has been communicated.
Processor 0: After 0 steps an event has been communicated.
Processor 8: After 10723 steps an event has been communicated.
Processor 5: After 10426 steps an event has been communicated.
Processor 6: After 12172 steps an event has been communicated.
Processor 7: After 9014 steps an event has been communicated.
Processor 1: After 400 steps an event has been communicated.
Processor 2: After 1646 steps an event has been communicated.
Processor 11: After 12637 steps an event has been communicated.
Processor 10: After 9120 steps an event has been communicated.
Processor 9: After 1 steps an event has been communicated.
Oh no! An event occurred somewhere and the loop has not been finished on all processes.
Output (running with -n 3
):
No worries, all processes have finished the loop.