GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
ProbeExecutor.cpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) gpridge developers
3
// See LICENSE for details.
4
5
#include "
ProbeExecutor.h
"
6
7
#include <cmath>
8
#include <limits>
9
#include <stdexcept>
10
11
#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
12
#include <mpi.h>
13
#endif
14
15
namespace
gpr
{
16
namespace
linalg
{
17
18
namespace
{
19
20
constexpr
double
kNaN = std::numeric_limits<double>::quiet_NaN();
21
22
// Single-rank sequential dispatch. Every probe evaluated, NaN propagates
23
// to every output entry. No collective. Used both by the local() context
24
// path and by the Distributed kind under nprocs == 1.
25
Eigen::VectorXd run_sequential(
int
n_probes,
26
const
std::function<
double
(
int
)>& probe_fn)
27
{
28
Eigen::VectorXd
out
= Eigen::VectorXd::Zero(n_probes);
29
bool
failed =
false
;
30
for
(
int
idx = 0; idx < n_probes; ++idx) {
31
const
double
v = probe_fn(idx);
32
if
(std::isnan(v)) {
33
failed =
true
;
34
break
;
35
}
36
out
(idx) = v;
37
}
38
if
(failed) {
39
return
Eigen::VectorXd::Constant(n_probes, kNaN);
40
}
41
return
out
;
42
}
43
44
}
// namespace
45
46
Eigen::VectorXd
ProbeExecutor::run
(
int
n_probes,
47
const
std::function<
double
(
int
)>& probe_fn,
48
KernelInverse::Kind
kind,
49
const
MpiContext
& ctx)
const
50
{
51
if
(n_probes <= 0)
return
Eigen::VectorXd();
52
53
// Single-process fast path. nprocs == 1 means there is nobody to
54
// Allreduce with, and Distributed degenerates to "every rank runs
55
// every probe" which on a single rank is just the sequential loop.
56
if
(!ctx.
is_distributed
()) {
57
return
run_sequential(n_probes, probe_fn);
58
}
59
60
#if defined(GPR_HAVE_MPI) || defined(GPR_LINALG_SCALAPACK)
61
// Distributed kind: the operator's solve / solve_mat is itself a
62
// collective. Every rank must enter every probe together; partitioning
63
// would deadlock at the next pdtrsm. No Allreduce on the result --
64
// the operator's collective solve already produced the same answer
65
// on every rank.
66
if
(kind ==
KernelInverse::Kind::Distributed
) {
67
// run_sequential stops at the first NaN. probe_fn here is an MPI
68
// collective, so a rank that stops early leaves the others waiting in
69
// the next solve. Run every probe on every rank, then agree on the
70
// failure afterwards.
71
Eigen::VectorXd
out
= Eigen::VectorXd::Zero(n_probes);
72
bool
local_failed =
false
;
73
for
(
int
idx = 0; idx < n_probes; ++idx) {
74
const
double
v = probe_fn(idx);
75
if
(std::isnan(v)) {
76
local_failed =
true
;
77
continue
;
78
}
79
out
(idx) = v;
80
}
81
bool
global_failed =
false
;
82
MPI_Allreduce(&local_failed, &global_failed, 1, MPI_C_BOOL, MPI_LOR,
83
ctx.
comm
);
84
if
(global_failed)
return
Eigen::VectorXd::Constant(n_probes, kNaN);
85
return
out
;
86
}
87
88
// RankLocal kind: round-robin partition. Rank r evaluates probe idx
89
// iff idx % nprocs == r. Other ranks fill 0.0 in their local result
90
// vector. A SUM-Allreduce reconstitutes the full result on every
91
// rank since each entry is owned by exactly one rank.
92
Eigen::VectorXd local = Eigen::VectorXd::Zero(n_probes);
93
bool
local_failed =
false
;
94
for
(
int
idx = 0; idx < n_probes; ++idx) {
95
if
(idx % ctx.
nprocs
!= ctx.
rank
)
continue
;
96
const
double
v = probe_fn(idx);
97
if
(std::isnan(v)) {
98
local_failed =
true
;
99
// Keep iterating only through this rank's owned probes? No --
100
// bail out. Other ranks may have already reduced or be ahead;
101
// they will hit the OR-flag Allreduce below regardless of
102
// when this rank stops. Skipping further work on the failed
103
// rank avoids burning compute that will be discarded.
104
break
;
105
}
106
local(idx) = v;
107
}
108
109
// Allreduce-OR the failure flag first. Every rank must enter this
110
// collective even on the failed-rank early-break path above (the
111
// break left `local_failed` set, which propagates here).
112
// Scalar bool + MPI_C_BOOL is the conventional pairing for a single-flag
113
// OR-reduce: MPI_LOR operates on truth value, not bit pattern, so the
114
// implementation-defined bool/_Bool size does not matter here. Do NOT
115
// extend this pairing to bool* buffers; std::vector<bool> is bit-packed
116
// and has the wrong storage class for MPI buffer semantics.
117
bool
global_failed =
false
;
118
MPI_Allreduce(&local_failed, &global_failed, 1, MPI_C_BOOL, MPI_LOR,
119
ctx.
comm
);
120
121
if
(global_failed) {
122
// Bypass the result Allreduce on global failure: every rank
123
// returns NaN-vector immediately. Skipping the second Allreduce
124
// is safe because it is the only collective remaining and every
125
// rank takes the same branch (driven by the global OR flag, not
126
// by per-rank state). No deadlock risk.
127
return
Eigen::VectorXd::Constant(n_probes, kNaN);
128
}
129
130
Eigen::VectorXd global = Eigen::VectorXd::Zero(n_probes);
131
MPI_Allreduce(local.data(), global.data(),
static_cast<
int
>
(local.size()),
132
MPI_DOUBLE, MPI_SUM, ctx.
comm
);
133
return
global;
134
#else
135
// Non-SCALAPACK build with nprocs > 1 cannot happen on the supported
136
// build matrix (MpiContext::local() is the only constructor available
137
// and it pins nprocs to 1). The sequential fall-through is correct
138
// either way.
139
// The non-MPI build only supports MpiContext::local() (nprocs == 1).
140
// A user who hand-constructs `MpiContext{0, 4, 0}` would get silently
141
// partitioned probes without a reduce, returning per-rank-incorrect
142
// results. assert() is compiled out under -Db_ndebug=true, which is
143
// exactly the configuration that would ship the silent wrong answer, so
144
// throw instead.
145
if
(ctx.
nprocs
!= 1) {
146
throw
std::logic_error(
147
"ProbeExecutor: nprocs > 1 requires the SCALAPACK build"
);
148
}
149
(void)kind;
150
return
run_sequential(n_probes, probe_fn);
151
#endif
152
}
153
154
}
// namespace linalg
155
}
// namespace gpr
ProbeExecutor.h
MPI-aware dispatcher for the Hutchinson-style probe loop used by the Laplace Hessian assembler and th...
gpr::linalg::KernelInverse::Kind
Kind
Definition
KernelInverse.h:44
gpr::linalg::KernelInverse::Kind::Distributed
@ Distributed
Definition
KernelInverse.h:53
gpr::linalg::ProbeExecutor::run
Eigen::VectorXd run(int n_probes, const std::function< double(int)> &probe_fn, KernelInverse::Kind kind, const MpiContext &ctx) const
Definition
ProbeExecutor.cpp:46
out
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Definition
eon_relax_engine.h:240
gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Definition
ConditionedKernel.h:14
gpr
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition
Macros.h:16
gpr::linalg::MpiContext
Definition
MpiContext.h:59
gpr::linalg::MpiContext::rank
int rank
Definition
MpiContext.h:60
gpr::linalg::MpiContext::is_distributed
bool is_distributed() const noexcept
Definition
MpiContext.h:67
gpr::linalg::MpiContext::comm
MpiCommType comm
Definition
MpiContext.h:62
gpr::linalg::MpiContext::nprocs
int nprocs
Definition
MpiContext.h:61
gpr
linalg
ProbeExecutor.cpp
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf