Loading...
Searching...
No Matches
gpr::MpiLifetime Class Reference

A native entry point finalizes only the MPI world it initializes. More...

#include <managers/MpiLifetime.h>

Public Member Functions

 MpiLifetime (int &argc, char **&argv)
 MpiLifetime (const MpiLifetime &)=delete
MpiLifetime & operator= (const MpiLifetime &)=delete
 ~MpiLifetime ()
void finalize () noexcept
bool borrowed () const noexcept

Static Public Member Functions

static bool & ownedWorldPending () noexcept
 Set when an owned world was left open by an escaping exception.

Detailed Description

A native entry point finalizes only the MPI world it initializes.

Definition at line 49 of file MpiLifetime.h.

Constructor & Destructor Documentation

◆ MpiLifetime() [1/2]

gpr::MpiLifetime::MpiLifetime ( int & argc,
char **& argv )
inline

Definition at line 51 of file MpiLifetime.h.

52 {
53#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
54 int finalized = 0;
55 MPI_Finalized(&finalized);
56 if (finalized)
57 throw std::runtime_error("cannot enter a finalized MPI world");
58 int initialized = 0;
59 MPI_Initialized(&initialized);
60 borrowed_ = initialized != 0;
61 int provided = MPI_THREAD_SINGLE;
62 if (!borrowed_) {
64 if (MPI_Init_thread(&argc, &argv,
65 linalg::requiredMpiThreadSupport(),
66 &provided) != MPI_SUCCESS)
67 throw std::runtime_error("could not initialize MPI");
68 owns_ = true;
69 } else if (MPI_Query_thread(&provided) != MPI_SUCCESS) {
70 throw std::runtime_error("could not inspect MPI thread support");
71 }
72 if (provided < linalg::requiredMpiThreadSupport()) {
73 finalize();
74 throw std::runtime_error(linalg::mpiThreadSupportError());
75 }
76#else
77 (void)argc;
78 (void)argv;
79#endif
80 }
void finalize() noexcept
void excludeLibfabricBtlOnOneNode() noexcept
Definition MpiLifetime.h:42

◆ MpiLifetime() [2/2]

gpr::MpiLifetime::MpiLifetime ( const MpiLifetime & )
delete

◆ ~MpiLifetime()

gpr::MpiLifetime::~MpiLifetime ( )
inline

Definition at line 85 of file MpiLifetime.h.

86 {
87 // While an exception unwinds the driver, MPI_Finalize would wait for
88 // ranks that may sit in another collective. The world stays open and
89 // reportFatal ends it once the error is printed.
90 if (std::uncaught_exceptions() > unwinding_) {
91 if (owns_) ownedWorldPending() = true;
92 return;
93 }
94 finalize();
95 }
static bool & ownedWorldPending() noexcept
Set when an owned world was left open by an escaping exception.
Definition MpiLifetime.h:98

Member Function Documentation

◆ borrowed()

bool gpr::MpiLifetime::borrowed ( ) const
inlinenodiscardnoexcept

Definition at line 124 of file MpiLifetime.h.

125 {
126 return borrowed_;
127 }

◆ finalize()

void gpr::MpiLifetime::finalize ( )
inlinenoexcept

Definition at line 104 of file MpiLifetime.h.

105 {
106#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
107 if (!owns_) return;
108 int finalized = 0;
109 MPI_Finalized(&finalized);
110 if (!finalized) {
111 // Every rank publishes buffered output before a peer can
112 // finalize and return a nonzero status to the launcher.
113 std::cout.flush();
114 std::cerr.flush();
115 std::clog.flush();
116 std::fflush(stdout);
117 std::fflush(stderr);
118 MPI_Finalize();
119 }
120 owns_ = false;
121#endif
122 }

◆ operator=()

MpiLifetime & gpr::MpiLifetime::operator= ( const MpiLifetime & )
delete

◆ ownedWorldPending()

bool & gpr::MpiLifetime::ownedWorldPending ( )
inlinestaticnoexcept

Set when an owned world was left open by an escaping exception.

Definition at line 98 of file MpiLifetime.h.

99 {
100 static bool pending = false;
101 return pending;
102 }

The documentation for this class was generated from the following file: