Loading...
Searching...
No Matches
OracleBatch.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 "OracleBatch.h"
6
7#include <algorithm>
8#include <chrono>
9#include <cstdint>
10#include <cstring>
11#include <limits>
12#include <stdexcept>
13
14namespace pot {
15
16namespace {
17
18bool mpiRunning() noexcept
19{
20#ifdef GPR_HAVE_MPI
21 int up = 0;
22 MPI_Initialized(&up);
23 if (!up) return false;
24 int down = 0;
25 MPI_Finalized(&down);
26 return !down;
27#else
28 return false;
29#endif
30}
31
32} // namespace
33
35{
36#ifdef GPR_HAVE_MPI
37 return MPI_COMM_WORLD;
38#else
39 return 0;
40#endif
41}
42
44{
45 OracleBatchLayout layout;
46 layout.chunk = chunk < 0 ? 0 : chunk;
48#ifdef GPR_HAVE_MPI
49 if (mpiRunning()) {
50 MPI_Comm_rank(layout.comm, &layout.rank);
51 MPI_Comm_size(layout.comm, &layout.nprocs);
52 }
53#endif
54 return layout;
55}
56
57int oracleBatchOwner(Eigen::Index point, Eigen::Index count,
58 const OracleBatchLayout& layout) noexcept
59{
60 if (layout.nprocs <= 1 || count <= 1 || point < 0) return 0;
61 const Eigen::Index unit = layout.chunk > 0 ? point / layout.chunk : point;
62 const long long offset =
63 ((layout.owner_offset % layout.nprocs) + layout.nprocs) % layout.nprocs;
64 return static_cast<int>((unit + offset) % layout.nprocs);
65}
66
67std::vector<std::vector<Eigen::Index>> ownedOracleGroups(
68 Eigen::Index count, const OracleBatchLayout& layout)
69{
70 std::vector<std::vector<Eigen::Index>> groups;
71 if (count <= 0) return groups;
72 if (layout.chunk <= 0) {
73 std::vector<Eigen::Index> owned;
74 for (Eigen::Index i = 0; i < count; ++i) {
75 if (oracleBatchOwner(i, count, layout) == layout.rank)
76 owned.push_back(i);
77 }
78 if (!owned.empty()) groups.push_back(std::move(owned));
79 return groups;
80 }
81 for (Eigen::Index first = 0; first < count; first += layout.chunk) {
82 if (oracleBatchOwner(first, count, layout) != layout.rank) continue;
83 const Eigen::Index last = std::min(count, first + layout.chunk);
84 std::vector<Eigen::Index> chunk;
85 chunk.reserve(static_cast<std::size_t>(last - first));
86 for (Eigen::Index i = first; i < last; ++i)
87 chunk.push_back(i);
88 groups.push_back(std::move(chunk));
89 }
90 return groups;
91}
92
94 const std::string& local_message,
95 const OracleBatchLayout& layout)
96{
97 OracleBatchOutcome outcome;
98 outcome.status = local_status;
99 outcome.message = local_message;
100 outcome.owner = local_status == OracleBatchStatus::Ok ? -1 : layout.rank;
101#ifdef GPR_HAVE_MPI
102 if (layout.nprocs > 1 && mpiRunning()) {
103 // One reduction carries both decisions: the highest status and
104 // the lowest failing rank (encoded so that MPI_MAX picks the
105 // lowest rank among the highest status).
106 const int local_code =
107 static_cast<int>(local_status) * (layout.nprocs + 1) +
108 (local_status == OracleBatchStatus::Ok
109 ? 0
110 : layout.nprocs - layout.rank);
111 int code = 0;
112 MPI_Allreduce(&local_code, &code, 1, MPI_INT, MPI_MAX, layout.comm);
113 outcome.status =
114 static_cast<OracleBatchStatus>(code / (layout.nprocs + 1));
115 const int tag = code % (layout.nprocs + 1);
116 outcome.owner = tag == 0 ? -1 : layout.nprocs - tag;
117 if (outcome.status == OracleBatchStatus::Ok) {
118 outcome.message.clear();
119 return outcome;
120 }
121 std::string message =
122 layout.rank == outcome.owner ? local_message : std::string();
123 if (message.size() > 4096) message.resize(4096);
124 int length = static_cast<int>(message.size());
125 MPI_Bcast(&length, 1, MPI_INT, outcome.owner, layout.comm);
126 message.resize(static_cast<std::size_t>(length));
127 if (length > 0)
128 MPI_Bcast(message.data(), length, MPI_CHAR, outcome.owner,
129 layout.comm);
130 outcome.message = std::move(message);
131 return outcome;
132 }
133#endif
134 if (outcome.status == OracleBatchStatus::Ok) outcome.message.clear();
135 return outcome;
136}
137
138void allreduceOwnedOracleValues(std::vector<double>& energies,
139 Eigen::MatrixXd& gradients,
140 const OracleBatchLayout& layout,
141 double* communication_seconds)
142{
143 if (static_cast<Eigen::Index>(energies.size()) != gradients.rows())
144 throw std::invalid_argument(
145 "oracle batch: energies and gradient rows differ");
146#ifdef GPR_HAVE_MPI
147 if (layout.nprocs <= 1 || !mpiRunning()) return;
148 const auto start = std::chrono::steady_clock::now();
149 static_assert(sizeof(double) == sizeof(std::uint64_t) &&
150 std::numeric_limits<double>::is_iec559);
151 const auto gradient_count = static_cast<std::size_t>(gradients.size());
152 std::vector<std::uint64_t> bits(energies.size() + gradient_count);
153 if (!energies.empty())
154 std::memcpy(bits.data(), energies.data(),
155 energies.size() * sizeof(double));
156 if (gradient_count > 0)
157 std::memcpy(bits.data() + energies.size(), gradients.data(),
158 gradient_count * sizeof(double));
159 // Each entry has one owner. Positive zero contributes no set bits, so
160 // integer OR preserves the owner's signed zero and subnormal values.
161 // Energies and gradients share a collective; no floating sum is needed.
162 for (std::size_t offset = 0; offset < bits.size();) {
163 const int count = static_cast<int>(std::min(
164 bits.size() - offset,
165 static_cast<std::size_t>(std::numeric_limits<int>::max())));
166 MPI_Allreduce(MPI_IN_PLACE, bits.data() + offset, count, MPI_UINT64_T,
167 MPI_BOR, layout.comm);
168 offset += static_cast<std::size_t>(count);
169 }
170 if (!energies.empty())
171 std::memcpy(energies.data(), bits.data(),
172 energies.size() * sizeof(double));
173 if (gradient_count > 0)
174 std::memcpy(gradients.data(), bits.data() + energies.size(),
175 gradient_count * sizeof(double));
176 if (communication_seconds) {
177 *communication_seconds += std::chrono::duration<double>(
178 std::chrono::steady_clock::now() - start)
179 .count();
180 }
181#else
182 (void)layout;
183 (void)communication_seconds;
184#endif
185}
186
187void allreduceOwnedOracleMask(std::vector<char>& accepted,
188 const OracleBatchLayout& layout)
189{
190#ifdef GPR_HAVE_MPI
191 if (layout.nprocs <= 1 || !mpiRunning() || accepted.empty()) return;
192 std::vector<unsigned char> bits(accepted.begin(), accepted.end());
193 for (std::size_t offset = 0; offset < bits.size();) {
194 const int count = static_cast<int>(std::min(
195 bits.size() - offset,
196 static_cast<std::size_t>(std::numeric_limits<int>::max())));
197 MPI_Allreduce(MPI_IN_PLACE, bits.data() + offset, count,
198 MPI_UNSIGNED_CHAR, MPI_BOR, layout.comm);
199 offset += static_cast<std::size_t>(count);
200 }
201 for (std::size_t i = 0; i < bits.size(); ++i)
202 accepted[i] = static_cast<char>(bits[i] ? 1 : 0);
203#else
204 (void)accepted;
205 (void)layout;
206#endif
207}
208
209} // namespace pot
One step's oracle points shared out over the ranks of a communicator, with a layout-independent owner...
Potential wrappers and the batched oracle interface.
void allreduceOwnedOracleMask(std::vector< char > &accepted, const OracleBatchLayout &layout)
OracleBatchStatus
What one rank reports about its groups before the value collectives.
Definition OracleBatch.h:78
int OracleBatchComm
Definition OracleBatch.h:39
OracleBatchComm oracleBatchWorld() noexcept
The communicator of the whole run, or the no-MPI placeholder.
OracleBatchOutcome agreeOracleBatchStatus(OracleBatchStatus local_status, const std::string &local_message, const OracleBatchLayout &layout)
std::vector< std::vector< Eigen::Index > > ownedOracleGroups(Eigen::Index count, const OracleBatchLayout &layout)
The groups this rank evaluates, each one batch call, in point order.
int oracleBatchOwner(Eigen::Index point, Eigen::Index count, const OracleBatchLayout &layout) noexcept
Owner rank of one point of a batch of count points.
void allreduceOwnedOracleValues(std::vector< double > &energies, Eigen::MatrixXd &gradients, const OracleBatchLayout &layout, double *communication_seconds)
How a step's oracle points are shared out.
Definition OracleBatch.h:46
OracleBatchComm comm
Definition OracleBatch.h:62
static OracleBatchLayout world(Eigen::Index chunk=0, int owner_offset=0)
The layout of the running communicator (rank 0 of 1 without MPI).
std::string message
The failing owner's message, identical on every rank.
Definition OracleBatch.h:91
int owner
The lowest rank that failed, or -1.
Definition OracleBatch.h:93
OracleBatchStatus status
Definition OracleBatch.h:89