Loading...
Searching...
No Matches
gpr::linalg::ProbeExecutor Class Reference

#include <gpr/linalg/ProbeExecutor.h>

Public Member Functions

 ProbeExecutor ()=default
Eigen::VectorXd run (int n_probes, const std::function< double(int)> &probe_fn, KernelInverse::Kind kind, const MpiContext &ctx) const

Detailed Description

Definition at line 50 of file ProbeExecutor.h.

Constructor & Destructor Documentation

◆ ProbeExecutor()

gpr::linalg::ProbeExecutor::ProbeExecutor ( )
default

Member Function Documentation

◆ run()

Eigen::VectorXd gpr::linalg::ProbeExecutor::run ( int n_probes,
const std::function< double(int)> & probe_fn,
KernelInverse::Kind kind,
const MpiContext & ctx ) const
nodiscard

Run n_probes probe evaluations under the dispatch policy implied by kind, against the MPI context ctx, and return the global result vector. The result vector has length n_probes on every rank.

Parameters
n_probesTotal number of probes to evaluate. Must be >= 0.
probe_fnCallable with signature double(int) returning the scalar contribution of probe idx (in the range [0, n_probes)). May return NaN to signal failure; the executor will then return a vector of NaN on every rank.
kindDispatch policy. RankLocal triggers round-robin partitioning + Allreduce-SUM. Distributed runs every probe on every rank with no Allreduce.
ctxMPI context. Use MpiContext::local() for the single-process path (always available) or MpiContext::world() under the ScaLAPACK build.
Returns
The n_probes -element vector of probe values, identical on every rank.

Performance contract: the std::function<double(int)> parameter is type-erased; lambdas larger than the SBO threshold (~24-32 bytes on libstdc++) heap-allocate on construction. Callers should construct the std::function ONCE outside the loop and reuse it, not re-bind on every probe. The probe lambda itself is invoked n_probes / nprocs times under RankLocal kind, n_probes times under Distributed kind.

Definition at line 46 of file ProbeExecutor.cpp.

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.
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}
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out

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