51#include <boost/test/unit_test.hpp>
55#include <condition_variable>
86 std::unique_lock<std::mutex> lk(m_mtx);
87 const int gen =
m_gen;
96 m_cv.wait(lk, [&] {
return gen !=
m_gen; });
111 explicit InMemorySwitch(
int np)
116 void Exchange(
int rank, std::vector<RoutedMessage> &outbound,
117 std::vector<RoutedMessage> &inbound)
128 for (
int r = 0; r <
m_np; ++r)
135 d.bytes = std::move(m.bytes);
136 m_inbox[m.dest].push_back(std::move(
d));
142 inbound = std::move(m_inbox[
rank]);
150 if (!std::is_sorted(inbound.begin(), inbound.end(),
152 return a.src < b.src;
170 bool InboundWasSorted()
const
179 std::vector<std::vector<RoutedMessage>>
m_inbox;
190 void Exchange(std::vector<RoutedMessage> &outbound,
191 std::vector<RoutedMessage> &inbound)
override
193 m_sw.Exchange(m_rank, outbound, inbound);
195 int Rank()
const override
199 int Size()
const override
228Workload MakeWorkload(
int np, std::mt19937 &rng)
231 w.registrations.resize(np);
234 w.registrations[
rank].emplace_back(
id, order);
235 auto it =
w.maxOrder.find(
id);
237 (it ==
w.maxOrder.end()) ? order : std::max(it->second, order);
241 std::uniform_int_distribution<int> orderDist(1, 15);
245 for (
int r = 0; r < np; ++r)
248 addReg(r, edge, orderDist(rng));
249 addReg((r + 1) % np, edge, orderDist(rng));
255 const int nCorner = std::min(np, 8);
256 for (
int r = 0; r < nCorner; ++r)
258 addReg(r, corner, orderDist(rng));
263 const int64_t periodic = 900000;
264 addReg(0, periodic, orderDist(rng));
267 addReg(np - 1, periodic, orderDist(rng));
271 for (
int r = 0; r < np; ++r)
273 addReg(r, 2000000 + r, orderDist(rng));
277 addReg(0, 3000000, orderDist(rng));
278 addReg(0, 3000000, orderDist(rng));
285 std::sort(kv.second.begin(), kv.second.end());
286 kv.second.erase(std::unique(kv.second.begin(), kv.second.end()),
302 std::map<int64_t, double>
sum;
303 std::map<int64_t, std::map<int, int>>
mult;
307PlanOracle MakePlanOracle(
const Workload &
w)
310 for (
size_t r = 0; r <
w.registrations.size(); ++r)
312 for (
auto &pr :
w.registrations[r])
314 o.sum[pr.first] += pr.second;
315 o.mult[pr.first][
static_cast<int>(r)]++;
316 o.anyHigh[pr.first] = o.anyHigh[pr.first] || (pr.second > 7);
332std::vector<int64_t> DistinctIds(
const Workload &
w,
int rank)
334 std::vector<int64_t> ids;
335 for (
auto &pr :
w.registrations[
rank])
337 ids.push_back(pr.first);
339 std::sort(ids.begin(), ids.end());
340 ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
353void CheckOneShotRank(
int rank,
const Workload &
w,
const PlanOracle &o,
354 InMemorySwitch &sw, Failure &out)
356 auto fail = [&](
const std::string &msg) {
360 out.message =
"rank " + std::to_string(
rank) +
": " + msg;
364 auto transport = std::make_shared<InMemoryTransport>(sw,
rank);
365 const auto ®s =
w.registrations[
rank];
366 const std::vector<int64_t> ids = DistinctIds(
w,
rank);
370 cr.Reserve(ids.size());
371 cr.RegisterKeys(ids);
376 sp.Reserve(regs.size());
377 for (
size_t i = 0; i < regs.size(); ++i)
379 sp.Register(regs[i].first,
380 Tag{regs[i].first,
rank,
static_cast<int>(i)});
387 sb.Reserve(regs.size());
388 for (
auto &pr : regs)
390 sb.Register(pr.first, pr.second > 7);
396 if (cr.NumResolved() != ids.size())
398 fail(
"ConnectivityResolver resolved " +
399 std::to_string(cr.NumResolved()) +
" ids, registered " +
400 std::to_string(ids.size()));
403 for (
size_t u = 0; u < cr.NumResolved(); ++u)
405 const int64_t id = cr.ResolvedId(u);
408 fail(
"ConnectivityResolver: ids not returned ascending");
414 const std::vector<int> got(sh.
begin(), sh.
end());
415 if (!std::is_sorted(got.begin(), got.end()) ||
416 std::adjacent_find(got.begin(), got.end()) != got.end())
418 fail(
"id " + std::to_string(
id) +
" sharers not sorted/unique");
421 if (got !=
w.sharers.at(
id))
423 fail(
"id " + std::to_string(
id) +
" sharer mismatch");
426 if (cr.SharersOf(
id).begin() != sh.
begin())
428 fail(
"SharersOf disagrees with SharersAt for id " +
436 if (!sp.Knows(
id) || !sb.Knows(
id))
438 fail(
"missing id " + std::to_string(
id));
443 std::map<int, int> count;
445 for (
auto &entry : sp.GetSharedPayloads(
id))
447 if (entry.second.id !=
id || entry.second.rank != entry.first)
449 fail(
"misrouted payload for id " + std::to_string(
id));
452 count[entry.first]++;
454 if (count != o.mult.at(
id))
456 fail(
"wrong payload multiplicities for id " + std::to_string(
id));
459 for (
auto &entry : sb.GetSharedPayloads(
id))
463 if (
anyHigh != o.anyHigh.at(
id))
465 fail(
"SharedPayloadResolver<bool> wrong for id " +
470 std::vector<int> ranks = sp.GetSharers(
id);
471 std::sort(ranks.begin(), ranks.end());
472 ranks.erase(std::unique(ranks.begin(), ranks.end()), ranks.end());
473 if (ranks !=
w.sharers.at(
id))
475 fail(
"payload sharer mismatch for id " + std::to_string(
id));
485void TrialForNp(
int np, std::mt19937 &rng)
487 const Workload
w = MakeWorkload(np, rng);
488 const PlanOracle o = MakePlanOracle(
w);
489 InMemorySwitch sw(np);
491 std::vector<Failure> failures(np);
493 std::vector<std::thread> threads;
495 for (
int r = 0; r < np; ++r)
497 threads.emplace_back(
498 [&, r] { CheckOneShotRank(r,
w, o, sw, failures[r]); });
500 for (
auto &t : threads)
505 BOOST_REQUIRE_MESSAGE(sw.InboundWasSorted(),
506 "InMemorySwitch: inbound not sorted by src");
508 for (
auto &f : failures)
510 BOOST_REQUIRE_MESSAGE(!f.failed, f.message);
525 std::mt19937 rng(424242);
527 const std::vector<int> npList = {1, 2, 3, 4, 5, 6, 7, 8, 9,
528 13, 16, 17, 31, 32, 33, 47, 64, 65};
530 for (
int np : npList)
532 BOOST_TEST_CONTEXT(
"np = " << np)
534 for (
int trial = 0; trial < 8; ++trial)
554std::vector<int64_t> PlanSlots(
const Workload &
w,
int rank,
555 std::vector<int> ®OfSlot)
557 std::vector<int64_t> ids;
559 const auto ®s =
w.registrations[
rank];
560 for (
size_t i = 0; i < regs.size(); ++i)
562 ids.push_back(regs[i].first);
563 regOfSlot.push_back(
static_cast<int>(i));
567 regOfSlot.push_back(-1);
582 std::map<int64_t, std::vector<std::pair<int, int>>>
payloads;
585void CheckPlanRank(
int rank,
const Workload &
w,
const PlanOracle &o,
586 InMemorySwitch &sw, PlanRecord &rec)
588 auto fail = [&](
const std::string &msg) {
589 if (!rec.failure.failed)
591 rec.failure.failed =
true;
592 rec.failure.message =
"rank " + std::to_string(
rank) +
": " + msg;
596 auto transport = std::make_shared<InMemoryTransport>(sw,
rank);
597 const auto ®s =
w.registrations[
rank];
598 std::vector<int> regOfSlot;
599 const std::vector<int64_t> ids = PlanSlots(
w,
rank, regOfSlot);
601 SharedIdPlan plan(transport, ids, ExchangeBackend::eTransport);
602 rec.fastPath = plan.UsesSingleSlotFastPath();
606 for (
int round = 0; round < 2; ++round)
608 std::vector<double> vals(ids.size(), -1.0);
609 for (
size_t s = 0; s < ids.size(); ++s)
611 if (regOfSlot[s] >= 0)
613 vals[s] = regs[regOfSlot[s]].second;
616 plan.Gather(vals.data(), std::plus<double>{});
617 for (
size_t s = 0; s < ids.size(); ++s)
619 const double expect = regOfSlot[s] < 0 ? -1.0 : o.sum.at(ids[s]);
620 if (vals[s] != expect)
622 fail(
"Gather: slot " + std::to_string(s) +
" got " +
623 std::to_string(vals[s]) +
", expected " +
624 std::to_string(expect));
632 const std::vector<int> &s2u = plan.SlotToUnique();
633 for (
size_t s = plan.ActiveSlotBound(); s < ids.size(); ++s)
635 const int u = s2u[s];
637 (plan.IsShared(u) || std::count(s2u.begin(), s2u.end(), u) > 1))
639 fail(
"slot " + std::to_string(s) +
640 " lies past ActiveSlotBound() but is shared or repeated");
646 std::vector<int> ord(ids.size(), 0);
647 for (
size_t s = 0; s < ids.size(); ++s)
649 if (regOfSlot[s] >= 0)
651 ord[s] = regs[regOfSlot[s]].second;
654 const std::vector<int> ordBefore = ord;
655 std::vector<int> uniq(plan.NumUnique());
656 plan.Reduce(ord.data(), uniq.data(),
657 [](
int a,
int b) { return std::max(a, b); });
658 if (ord != ordBefore)
660 fail(
"Reduce modified its input");
663 for (
size_t u = 0; u < plan.NumUnique(); ++u)
665 const int64_t id = plan.UniqueIds()[u];
666 if (uniq[u] !=
w.maxOrder.at(
id))
668 fail(
"Reduce: id " + std::to_string(
id) +
" wrong max");
671 if (plan.GetSharers(u) !=
w.sharers.at(
id))
673 fail(
"plan sharers wrong for id " + std::to_string(
id));
679 const std::vector<int> mask = plan.UniqueMask();
680 for (
size_t s = 0; s < ids.size(); ++s)
684 if (regOfSlot[s] < 0)
686 fail(
"UniqueMask set on an ignored slot");
689 rec.maskedIds.push_back(ids[s]);
694 plan.PreparePayloadExchange();
695 std::vector<Tag> tags(ids.size());
696 for (
size_t s = 0; s < ids.size(); ++s)
698 tags[s] = Tag{ids[s],
rank,
static_cast<int>(s)};
700 std::vector<Tag>
out;
701 plan.ExchangePayloads(tags.data(),
out);
702 for (
size_t u = 0; u < plan.NumUnique(); ++u)
704 const int64_t id = plan.UniqueIds()[u];
705 std::map<int, int> count;
707 for (
size_t e = plan.PayloadBegin(u);
e < plan.PayloadEnd(u); ++
e)
709 if (out[e].
id !=
id || out[e].
rank != plan.PayloadRank(e) ||
710 plan.PayloadRank(e) < prev)
712 fail(
"ExchangePayloads: bad entry for id " +
716 prev = plan.PayloadRank(e);
717 count[
out[
e].rank]++;
718 rec.payloads[
id].emplace_back(out[e].
rank, out[e].
slot);
720 if (count != o.mult.at(
id))
722 fail(
"ExchangePayloads: wrong multiplicities for id " +
732void CheckPersistentResolver(
int rank,
const Workload &
w,
const PlanOracle &o,
733 InMemorySwitch &sw, Failure &f)
735 auto fail = [&](
const std::string &msg) {
739 f.message =
"rank " + std::to_string(
rank) +
": " + msg;
743 auto transport = std::make_shared<InMemoryTransport>(sw,
rank);
744 const auto ®s =
w.registrations[
rank];
747 for (
size_t i = 0; i < regs.size(); ++i)
749 sp.Register(regs[i].first, Tag{regs[i].first,
rank, 0});
751 sp.Setup(ExchangeBackend::eTransport);
752 for (
int round = 0; round < 2; ++round)
754 for (
size_t i = 0; i < regs.size(); ++i)
756 sp.SetPayload(i, Tag{regs[i].first,
rank, round});
759 for (
auto &pr : regs)
761 std::map<int, int> count;
762 for (
auto &entry : sp.GetSharedPayloads(pr.first))
764 if (entry.second.id != pr.first ||
765 entry.second.rank != entry.first ||
766 entry.second.slot != round)
768 fail(
"persistent SharedPayloadResolver: stale or "
769 "misrouted payload for id " +
770 std::to_string(pr.first));
773 count[entry.first]++;
775 if (count != o.mult.at(pr.first))
777 fail(
"persistent SharedPayloadResolver: wrong "
778 "multiplicities for id " +
779 std::to_string(pr.first));
786void PlanTrialForNp(
int np, std::mt19937 &rng)
788 const Workload
w = MakeWorkload(np, rng);
789 const PlanOracle o = MakePlanOracle(
w);
790 InMemorySwitch sw(np);
791 std::vector<PlanRecord> recs(np);
792 std::vector<Failure> persistent(np);
794 auto runRank = [&](
int rank) {
795 CheckPlanRank(
rank,
w, o, sw, recs[
rank]);
796 CheckPersistentResolver(
rank,
w, o, sw, persistent[
rank]);
799 std::vector<std::thread> threads;
801 for (
int r = 0; r < np; ++r)
803 threads.emplace_back(runRank, r);
805 for (
auto &t : threads)
816 for (
int r = 0; r < np; ++r)
818 nFast += recs[r].fastPath ? 1 : 0;
820 BOOST_REQUIRE_MESSAGE(nFast > 0 && nFast < np,
821 "expected both SharedIdPlan fold paths to be "
823 std::to_string(nFast) +
" fast of " +
827 for (
int r = 0; r < np; ++r)
829 BOOST_REQUIRE_MESSAGE(!recs[r].
failure.failed, recs[r].failure.message);
830 BOOST_REQUIRE_MESSAGE(!persistent[r].
failed, persistent[r].
message);
834 std::map<int64_t, int> kept;
835 for (
auto &rec : recs)
842 for (
auto &kv : o.
sum)
844 BOOST_REQUIRE_MESSAGE(kept[kv.first] == 1,
"UniqueMask kept id "
853 const auto &ref = recs[kv.second.front()].payloads.at(kv.first);
854 for (
int r : kv.second)
856 BOOST_REQUIRE_MESSAGE(recs[r].
payloads.at(kv.first) == ref,
857 "payload lists differ between sharers of id "
872 std::mt19937 rng(171717);
874 const std::vector<int> npList = {1, 2, 3, 4, 5, 7, 8, 13, 16, 17, 33};
876 for (
int np : npList)
878 BOOST_TEST_CONTEXT(
"np = " << np)
880 for (
int trial = 0; trial < 4; ++trial)
882 PlanTrialForNp(np, rng);
Id-keyed exchange of every sharer's raw payload for a shared entity.
std::map< int64_t, std::vector< int > > sharers
std::map< int64_t, std::map< int, int > > mult
std::map< int64_t, std::vector< std::pair< int, int > > > payloads
Per id, the (rank, slot) list ExchangePayloads() delivered.
std::vector< std::vector< RoutedMessage > > m_outbox
bool fastPath
Whether this rank's plan took the one-slot-per-id fast path.
std::vector< std::vector< RoutedMessage > > m_inbox
std::atomic< bool > m_sorted
std::vector< int64_t > maskedIds
Ids whose UniqueMask() bit is set on this rank.
std::map< int64_t, int > maxOrder
std::map< int64_t, bool > anyHigh
std::vector< std::vector< std::pair< int64_t, int > > > registrations
std::condition_variable m_cv
std::map< int64_t, double > sum
Base communications class.
Rendezvous discovery on its own, for the common case where you only need to find which ranks share ea...
Abstract interface for a personalised all-to-all exchange of opaque byte blobs.
A persistent gather-scatter plan over shared global ids: discovery is paid once, then a fixed neighbo...
static constexpr int64_t kIgnore
Slot id meaning "take no part", the role id 0 plays for gslib. Mesh entity ids use 0 legitimately,...
Discovery-then-direct-exchange resolver: for every registered (entity id, payload) pair,...
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
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::vector< double > w(NPUPPER)
std::vector< double > d(NPUPPER *NPUPPER)
BOOST_AUTO_TEST_CASE(TestCanGetRawPtr)
A resolved id's sharer ranks, as a range over the resolver's own storage – ascending,...
const int * begin() const
An opaque byte payload plus routing metadata.