Loading...
Searching...
No Matches
MpiLifetime.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5#include <cstdio>
6#include <cstdlib>
7#include <cstring>
8#include <exception>
9#include <iostream>
10#include <stdexcept>
11
12#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
13#include <mpi.h>
14
16#endif
17
18namespace gpr {
19
24inline bool launchedOnOneNode() noexcept
25{
26 const char* world = std::getenv("OMPI_COMM_WORLD_SIZE");
27 const char* local = std::getenv("OMPI_COMM_WORLD_LOCAL_SIZE");
28 if (world != nullptr && local != nullptr)
29 return std::strcmp(world, local) == 0;
30 if (const char* nodes = std::getenv("SLURM_STEP_NUM_NODES"))
31 return std::strcmp(nodes, "1") == 0;
32 return world == nullptr && std::getenv("PMIX_RANK") == nullptr &&
33 std::getenv("PMI_RANK") == nullptr;
34}
35
42inline void excludeLibfabricBtlOnOneNode() noexcept
43{
44 if (std::getenv("OMPI_MCA_btl") != nullptr || !launchedOnOneNode()) return;
45 setenv("OMPI_MCA_btl", "^ofi", /*overwrite=*/0);
46}
47
50public:
51 MpiLifetime(int& argc, char**& argv)
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 }
81
82 MpiLifetime(const MpiLifetime&) = delete;
84
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 }
96
98 static bool& ownedWorldPending() noexcept
99 {
100 static bool pending = false;
101 return pending;
102 }
103
104 void finalize() noexcept
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 }
123
124 [[nodiscard]] bool borrowed() const noexcept
125 {
126 return borrowed_;
127 }
128
129private:
130 bool owns_ = false;
131 bool borrowed_ = false;
132 int unwinding_ = std::uncaught_exceptions();
133};
134
143inline int reportFatal(const char* program, const char* what) noexcept
144{
145 std::fprintf(stderr, "%s: error: %s\n", program, what);
146 std::fflush(stderr);
147#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
148 int initialized = 0, finalized = 0;
149 MPI_Initialized(&initialized);
150 MPI_Finalized(&finalized);
151 if (initialized && !finalized) {
152 int size = 1;
153 MPI_Comm_size(MPI_COMM_WORLD, &size);
154 if (size > 1) MPI_Abort(MPI_COMM_WORLD, 1);
155 if (MpiLifetime::ownedWorldPending()) MPI_Finalize();
156 }
157#endif
158 return 1;
159}
160
161} // namespace gpr
static bool & ownedWorldPending() noexcept
Set when an owned world was left open by an escaping exception.
Definition MpiLifetime.h:98
bool borrowed() const noexcept
MpiLifetime & operator=(const MpiLifetime &)=delete
void finalize() noexcept
MpiLifetime(const MpiLifetime &)=delete
MpiLifetime(int &argc, char **&argv)
Definition MpiLifetime.h:51
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
void excludeLibfabricBtlOnOneNode() noexcept
Definition MpiLifetime.h:42
bool launchedOnOneNode() noexcept
Definition MpiLifetime.h:24
int reportFatal(const char *program, const char *what) noexcept