hops
MpiInitializerFinalizer.hpp
Go to the documentation of this file.
1 #ifndef HOPS_MPIINITIALIZERFINALIZER_HPP
2 #define HOPS_MPIINITIALIZERFINALIZER_HPP
3 
4 #include <mpi.h>
5 
6 namespace hops {
8  public:
9  MpiInitializerFinalizer() = delete;
10 
12  int isMpiInitialized;
13  MPI_Initialized(&isMpiInitialized);
14  if (!isMpiInitialized) {
15  MPI_Init(NULL, NULL);
16  std::atexit(finalize);
17  }
18  }
19 
20  static void finalize() {
21  int isMpiFinalized;
22  MPI_Finalized(&isMpiFinalized);
23  if (!isMpiFinalized) {
24  int isFinalizeSuccessful = !MPI_Finalize();
25  if (!isFinalizeSuccessful) {
26  throw std::runtime_error("MPI failed to finalize.");
27  }
28  }
29  }
30 
31  static const int &getInternalMpiTag() {
32  return INTERNAL_MPI_TAG;
33  }
34 
35  private:
36  constexpr static int INTERNAL_MPI_TAG = 137;
37  };
38 }
39 
40 #endif //HOPS_MPIINITIALIZERFINALIZER_HPP
hops::MpiInitializerFinalizer::getInternalMpiTag
static const int & getInternalMpiTag()
Definition: MpiInitializerFinalizer.hpp:31
hops
Definition: CsvReader.hpp:8
hops::MpiInitializerFinalizer
Definition: MpiInitializerFinalizer.hpp:7
hops::MpiInitializerFinalizer::initializeAndQueueFinalizeAtExit
static void initializeAndQueueFinalizeAtExit()
Definition: MpiInitializerFinalizer.hpp:11
hops::MpiInitializerFinalizer::MpiInitializerFinalizer
MpiInitializerFinalizer()=delete
hops::MpiInitializerFinalizer::finalize
static void finalize()
Definition: MpiInitializerFinalizer.hpp:20