Loading...
Searching...
No Matches
MpiContext.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
30
31#ifndef GPR_LINALG_MPI_CONTEXT_H_
32#define GPR_LINALG_MPI_CONTEXT_H_
33
34#include <stdexcept>
35
36#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
37#include <mpi.h>
38#endif
39
40namespace gpr {
41namespace linalg {
42
43#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
44using MpiCommType = MPI_Comm;
45// MPI_COMM_NULL on OpenMPI expands to a runtime void*-cast that is not a
46// C++17 constant expression, so a constexpr alias would not compile under
47// the scalapack build matrix. Inline non-constexpr keeps the same
48// header-only ODR semantics without the constexpr restriction.
49inline const MpiCommType kMpiCommNull = MPI_COMM_NULL;
50#else
51// Stand-in opaque communicator type for non-MPI builds. ProbeExecutor only
52// inspects nprocs (always 1 here) and never dereferences the communicator,
53// so an int placeholder is safe and avoids leaking <mpi.h> into translation
54// units that compile without MPI.
55using MpiCommType = int;
56inline constexpr MpiCommType kMpiCommNull = 0;
57#endif
58
59struct MpiContext {
60 int rank{0};
61 int nprocs{1};
63
67 [[nodiscard]] bool is_distributed() const noexcept
68 {
69 return nprocs > 1;
70 }
71
75 [[nodiscard]] static MpiContext local() noexcept
76 {
77 return MpiContext{0, 1, kMpiCommNull};
78 }
79
80#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
83 [[nodiscard]] static MpiContext fromCommunicator(MPI_Comm communicator)
84 {
85 if (communicator == MPI_COMM_NULL) return local();
86 int initialized = 0;
87 int finalized = 0;
88 MPI_Initialized(&initialized);
89 MPI_Finalized(&finalized);
90 if (!initialized || finalized)
91 throw std::logic_error(
92 "MPI context requires an active MPI runtime");
93 int intercommunicator = 0;
94 if (MPI_Comm_test_inter(communicator, &intercommunicator) !=
95 MPI_SUCCESS)
96 throw std::runtime_error("could not inspect MPI communicator");
97 if (intercommunicator)
98 throw std::invalid_argument(
99 "MPI context requires an intracommunicator");
100 MpiContext ctx;
101 ctx.comm = communicator;
102 if (MPI_Comm_rank(communicator, &ctx.rank) != MPI_SUCCESS ||
103 MPI_Comm_size(communicator, &ctx.nprocs) != MPI_SUCCESS)
104 throw std::runtime_error("could not inspect MPI process group");
105 return ctx;
106 }
107
111 [[nodiscard]] static MpiContext world()
112 {
113 return fromCommunicator(MPI_COMM_WORLD);
114 }
115#endif
116};
117
118} // namespace linalg
119} // namespace gpr
120
121#endif // GPR_LINALG_MPI_CONTEXT_H_
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
int MpiCommType
Definition MpiContext.h:55
constexpr MpiCommType kMpiCommNull
Definition MpiContext.h:56
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
bool is_distributed() const noexcept
Definition MpiContext.h:67
static MpiContext local() noexcept
Definition MpiContext.h:75