Nektar++
Loading...
Searching...
No Matches
SharedIdResolversMPITest.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: SharedIdResolversMPITest.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Real-MPI integration test for the shared-id resolvers over
32// both shipped transports.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36// Real-MPI integration test, driven by the Tester through
37// Tests/SharedIdResolversMPI.tst at several rank counts. For the current
38// communicator it:
39//
40// 1. Builds a workload with ring edges, a multi-rank corner vertex, a
41// periodic pair, purely local entities, and a duplicate registration.
42// 2. Discovers it with BOTH transports and asserts they agree
43// (the cross-transport validation).
44// 3. Asserts the reduced max-orders and sharer sets against an analytic
45// oracle computed the same way on every rank.
46// 4. Replays each plan over every exchange backend, with values that change
47// between rounds, so a stale plan shows up as the previous answer.
48//
49// The resolver logic itself, and the crystal router's routing decisions, are
50// unit tested serially in TestSharedIdResolvers.cpp and
51// TestCrystalRouter.cpp;
52// this test is what exercises them over real MPI.
53//
54// A single line "np=<n> PASS" is printed on rank 0 and matched by the .tst
55// regex metric; the exit code is 0 only if every rank passed.
56
57#include <algorithm>
58#include <cstdint>
59#include <functional>
60#include <iostream>
61#include <map>
62#include <memory>
63#include <string>
64#include <vector>
65
67
68using namespace Nektar::LibUtilities;
69
70namespace
71{
72
73struct Workload
74{
75 std::vector<std::pair<int64_t, int>> myRegs; // this rank's (id, order)
76 std::map<int64_t, int> oracleMax; // id -> max order
77 std::map<int64_t, long> oracleSum; // id -> sum of orders
78 std::map<int64_t, std::vector<int>>
79 oracleShare; // id -> sorted, unique ranks
80};
81
82// Deterministic per-(rank,id) order so every rank can compute the oracle
83// without communication. Mixes rank and id.
84int OrderFor(int rank, int64_t id)
85{
86 uint64_t h = static_cast<uint64_t>(id) * 1315423911u + rank * 2654435761u;
87 return 1 + static_cast<int>(h % 15u);
88}
89
90Workload BuildWorkload(int rank, int np)
91{
92 Workload w;
93
94 // Helper records both "my registrations" and the global oracle. Because
95 // OrderFor is a pure function of (rank, id), each rank can reconstruct the
96 // full oracle for every id it cares about.
97 auto contributor = [&](int64_t id, int who) {
98 w.oracleMax[id] = w.oracleMax.count(id)
99 ? std::max(w.oracleMax[id], OrderFor(who, id))
100 : OrderFor(who, id);
101 w.oracleShare[id].push_back(who);
102 w.oracleSum[id] += OrderFor(who, id);
103 if (who == rank)
104 {
105 w.myRegs.emplace_back(id, OrderFor(rank, id));
106 }
107 };
108
109 // 1. Ring edges: edge (1000 + r) shared by r and (r+1)%np.
110 for (int r = 0; r < np; ++r)
111 {
112 const int64_t edge = 1000 + r;
113 contributor(edge, r);
114 contributor(edge, (r + 1) % np);
115 }
116
117 // 2. Corner vertex shared by first min(np, 8) ranks.
118 const int64_t corner = 500000;
119 for (int r = 0; r < std::min(np, 8); ++r)
120 {
121 contributor(corner, r);
122 }
123
124 // 3. Periodic pair canonicalised to one id, on rank 0 and rank np-1.
125 const int64_t periodic = 900000;
126 contributor(periodic, 0);
127 if (np > 1)
128 {
129 contributor(periodic, np - 1);
130 }
131
132 // 4. Purely local entity per rank.
133 for (int r = 0; r < np; ++r)
134 {
135 contributor(2000000 + r, r);
136 }
137
138 // 5. Duplicate registration on rank 0.
139 contributor(3000000, 0);
140 contributor(3000000, 0); // second contribution, same rank
141
142 for (auto &kv : w.oracleShare)
143 {
144 // GetSharers() returns ascending, duplicate-free ranks: a rank that
145 // registers the same id twice (id 3000000 above, and edge 1000 when
146 // np == 1) appears once. The oracle must match.
147 std::sort(kv.second.begin(), kv.second.end());
148 kv.second.erase(std::unique(kv.second.begin(), kv.second.end()),
149 kv.second.end());
150 }
151 return w;
152}
153
154// This rank's slot ids and values, one entry per registration (so rank 0's
155// duplicate id appears twice, as it would in a real assembly map).
156void SlotArrays(const Workload &w, std::vector<int64_t> &ids,
157 std::vector<int> &vals)
158{
159 ids.clear();
160 vals.clear();
161 for (auto &pr : w.myRegs)
162 {
163 ids.push_back(pr.first);
164 vals.push_back(pr.second);
165 }
166}
167
168// Discover the workload with a given transport; return id -> (value, sharers).
169struct Resolved
170{
171 std::map<int64_t, int> value;
172 std::map<int64_t, std::vector<int>> sharers;
173};
174
175Resolved RunWith(std::shared_ptr<ITransport> transport, const Workload &w)
176{
177 std::vector<int64_t> ids;
178 std::vector<int> vals;
179 SlotArrays(w, ids, vals);
180
181 SharedIdPlan plan(transport, ids, ExchangeBackend::ePairwise);
182 std::vector<int> uniq(plan.NumUnique());
183 plan.Reduce(vals.data(), uniq.data(),
184 [](int a, int b) { return std::max(a, b); });
185
186 Resolved out;
187 for (size_t u = 0; u < plan.NumUnique(); ++u)
188 {
189 const int64_t id = plan.UniqueIds()[u];
190 out.value[id] = uniq[u];
191 out.sharers[id] = plan.GetSharers(u);
192 }
193 return out;
194}
195
196// Persistent layer over real MPI: for every discovery transport and every
197// exchange backend, a SharedIdPlan must reproduce the oracle and keep doing so
198// over repeated calls with changing values -- which is the point.
199void CheckPlans(const std::vector<std::shared_ptr<ITransport>> &transports,
200 const Workload &w,
201 const std::function<void(const std::string &)> &fail)
202{
203 const ExchangeBackend backends[] = {ExchangeBackend::ePairwise,
204 ExchangeBackend::eNeighbourCollective,
205 ExchangeBackend::eTransport};
206
207 std::vector<int64_t> ids;
208 std::vector<int> orders;
209 SlotArrays(w, ids, orders);
210 std::vector<long> vals(orders.begin(), orders.end());
211
212 for (size_t t = 0; t < transports.size(); ++t)
213 {
214 for (ExchangeBackend b : backends)
215 {
216 const std::string where =
217 "transport " + std::to_string(t) + ", backend " +
218 std::to_string(static_cast<int>(b)) + ": ";
219
220 SharedIdPlan plan(transports[t], ids, b);
221
222 // Gather(+): every slot ends up holding the global sum. Values
223 // change every round, so a plan that quietly kept the previous
224 // round's data would show up here.
225 for (int round = 0; round < 3; ++round)
226 {
227 // Scaling every contribution scales the sum, whatever an
228 // id's multiplicity across ranks.
229 std::vector<long> v(vals.size());
230 for (size_t s = 0; s < vals.size(); ++s)
231 {
232 v[s] = vals[s] * (round + 1);
233 }
234 plan.Gather(v.data(), std::plus<long>{});
235 for (size_t s = 0; s < ids.size(); ++s)
236 {
237 if (v[s] != w.oracleSum.at(ids[s]) * (round + 1))
238 {
239 fail(where + "Gather sum wrong for id " +
240 std::to_string(ids[s]));
241 break;
242 }
243 }
244 }
245
246 // Reduce(max) into per-unique results, with the sharer sets the
247 // plan discovered.
248 for (int round = 0; round < 3; ++round)
249 {
250 std::vector<int> v(orders.size());
251 for (size_t s = 0; s < orders.size(); ++s)
252 {
253 v[s] = orders[s] + 100 * round;
254 }
255 std::vector<int> uniq(plan.NumUnique());
256 plan.Reduce(v.data(), uniq.data(),
257 [](int x, int y) { return std::max(x, y); });
258 for (size_t u = 0; u < plan.NumUnique(); ++u)
259 {
260 const int64_t id = plan.UniqueIds()[u];
261 if (uniq[u] != w.oracleMax.at(id) + 100 * round ||
262 plan.GetSharers(u) != w.oracleShare.at(id))
263 {
264 fail(where + "persistent plan wrong for id " +
265 std::to_string(id));
266 break;
267 }
268 }
269 }
270 }
271 }
272}
273
274} // namespace
275
276int main(int argc, char **argv)
277{
278 CommSharedPtr comm =
279 GetCommFactory().CreateInstance("ParallelMPI", argc, argv);
280
281 int rank = comm->GetRank(), np = comm->GetSize();
282
283 Workload w = BuildWorkload(rank, np);
284
285 auto alltoallv = std::make_shared<AlltoallvTransport>(comm);
286 auto crystal = std::make_shared<CrystalRouterTransport>(comm);
287
288 Resolved rA = RunWith(alltoallv, w);
289 Resolved rC = RunWith(crystal, w);
290
291 int localOk = 1;
292 auto fail = [&](const std::string &msg) {
293 std::cerr << "[rank " << rank << ", np " << np << "] " << msg << "\n";
294 localOk = 0;
295 };
296
297 // Cross-transport agreement.
298 if (rA.value != rC.value)
299 {
300 fail("transports disagree on resolved values");
301 }
302 if (rA.sharers != rC.sharers)
303 {
304 fail("transports disagree on sharer sets");
305 }
306
307 // Oracle agreement (check against rA; rC already shown equal).
308 for (auto &pr : w.myRegs)
309 {
310 const int64_t id = pr.first;
311 if (rA.value[id] != w.oracleMax.at(id))
312 {
313 fail("value mismatch for id " + std::to_string(id));
314 }
315 if (rA.sharers[id] != w.oracleShare.at(id))
316 {
317 fail("sharer mismatch for id " + std::to_string(id));
318 }
319 }
320
321 CheckPlans({alltoallv, crystal}, w, fail);
322
323 // Reduce pass/fail across all ranks.
324 comm->AllReduce(localOk, ReduceMin);
325
326 if (rank == 0)
327 {
328 std::cout << "np=" << np << (localOk ? " PASS" : " FAIL")
329 << std::endl;
330 }
331
332 comm->Finalise();
333
334 return localOk ? 0 : 1;
335}
Id-keyed exchange of every sharer's raw payload for a shared entity.
std::map< int64_t, std::vector< int > > sharers
int64_t id
tBasePtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
A persistent gather-scatter plan over shared global ids: discovery is paid once, then a fixed neighbo...
ExchangeBackend
How a #SharedIdPlan moves data on each execute call.
CommFactory & GetCommFactory()
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition Comm.h:62
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT TData *NEK_RESTRICT out
std::int64_t int64_t
std::uint64_t uint64_t
Definition main.py:1