Nektar++
Loading...
Searching...
No Matches
Classes | Functions
Nektar::LibUtilities::detail Namespace Reference

Classes

struct  CrystalStep
 Crystal-router routing decision for one bisection step. More...
 
class  IFixedExchange
 A fixed-pattern exchange over a known neighbour set. More...
 
class  NeighbourCollectiveFixedExchange
 ExchangeBackend::eNeighbourCollective: one MPI_Neighbor_alltoallv over a distributed-graph communicator built at construction. More...
 
class  PairwiseFixedExchange
 ExchangeBackend::ePairwise: one non-blocking send and receive per neighbour. More...
 
class  TransportFixedExchange
 ExchangeBackend::eTransport: replay the fixed pattern through an arbitrary ITransport, one RoutedMessage per neighbour. More...
 

Functions

template<class T >
T EpsilonDifference (T lhs, T rhs)
 
template<typename T >
T LoadAt (const std::vector< std::byte > &buf, size_t i)
 Read element i of a trivially-copyable T from a byte buffer.
 
template<typename T >
void StoreAt (std::vector< std::byte > &buf, size_t i, const T &v)
 Write element i of a trivially-copyable T into a byte buffer.
 
ExchangeBackend ResolveBackend (ExchangeBackend backend, const CommSharedPtr &comm)
 The backend a plan can actually use, given what comm supports.
 
std::unique_ptr< IFixedExchange > MakeFixedExchange (ExchangeBackend backend, const std::shared_ptr< ITransport > &transport, const std::vector< int > &nbrs)
 Construct the IFixedExchange for backend over nbrs.
 
template<typename T >
void PackPod (std::vector< std::byte > &buf, const T &v)
 Append a trivially-copyable POD value to a byte buffer.
 
template<typename T >
T UnpackPod (const std::byte *&cur)
 Read a trivially-copyable POD value from a cursor and advance it.
 
CrystalStep CrystalPlanStep (int lo, int hi, int rank)
 Compute this rank's routing decision for one bisection step of the crystal router.
 
bool CrystalDestInMyHalf (const CrystalStep &s, int dest)
 Test whether a message destined for dest belongs in the querying rank's half of the current bisection.
 
int RendezvousRank (int64_t id, int nproc)
 Bit-mixing hash (splitmix64 finaliser) mapping an entity id to its rendezvous rank, so sequential IDs spread across rendezvous ranks and don't create hot spots.
 
std::vector< RoutedMessage > DrainBucketsToMessages (std::unordered_map< int, std::vector< std::byte > > &buckets, int selfRank)
 Move sparse per-destination byte buckets into transport messages, clearing the buckets.
 
CommSharedPtr BuildNeighbourComm (const CommSharedPtr &base, int selfRank, const std::unordered_map< int64_t, std::vector< int > > &sharers)
 Build a persistent distributed-graph communicator over the discovered topological neighbours (self excluded).
 

Function Documentation

◆ BuildNeighbourComm()

CommSharedPtr Nektar::LibUtilities::detail::BuildNeighbourComm ( const CommSharedPtr &  base,
int  selfRank,
const std::unordered_map< int64_t, std::vector< int > > &  sharers 
)
inline

Build a persistent distributed-graph communicator over the discovered topological neighbours (self excluded).

Parameters
baseBase communicator to derive the graph communicator from.
selfRankThis rank's index; excluded from its own neighbour list.
sharersResolved sharer lists, keyed by entity id (as populated by ConnectivityResolver::Resolve()).
Returns
A distributed-graph communicator (via DistGraphCreateAdjacent) whose neighbours are the union of all ranks appearing in sharers, excluding selfRank.

Definition at line 998 of file Transport.hpp.

1001{
1002 std::set<int> nbrs;
1003 for (const auto &kv : sharers)
1004 {
1005 for (int r : kv.second)
1006 {
1007 if (r != selfRank)
1008 {
1009 nbrs.insert(r);
1010 }
1011 }
1012 }
1013 std::vector<int> ranks(nbrs.begin(), nbrs.end());
1014 std::vector<int> weights;
1015
1016 return base->DistGraphCreateAdjacent(ranks, weights, 0);
1017}
std::map< int64_t, std::vector< int > > sharers

References sharers.

◆ CrystalDestInMyHalf()

bool Nektar::LibUtilities::detail::CrystalDestInMyHalf ( const CrystalStep &  s,
int  dest 
)
inline

Test whether a message destined for dest belongs in the querying rank's half of the current bisection.

Parameters
sRouting decision for the current step, from CrystalPlanStep().
destDestination rank of the message being tested.
Returns
true if dest is in the same half as the querying rank (so the message should be kept for a later round), false if it must be sent across to the other half this round.

Definition at line 358 of file Transport.hpp.

359{
360 const bool destLow = (dest <= s.mid);
361 return destLow == s.inLow;
362}
bool inLow
Whether the querying rank is in the low half.
int mid
Split point: low half is [lo, mid].

References CrystalDestInMyHalf(), Nektar::LibUtilities::detail::CrystalStep::inLow, and Nektar::LibUtilities::detail::CrystalStep::mid.

Referenced by CrystalDestInMyHalf(), and Nektar::LibUtilities::CrystalRouterTransport::Route().

◆ CrystalPlanStep()

CrystalStep Nektar::LibUtilities::detail::CrystalPlanStep ( int  lo,
int  hi,
int  rank 
)
inline

Compute this rank's routing decision for one bisection step of the crystal router.

Parameters
loLower bound (inclusive) of the current group of ranks.
hiUpper bound (inclusive) of the current group of ranks.
rankThe rank computing its own routing decision (normally the calling rank's own rank).
Returns
A #CrystalStep describing which half rank falls in and who it should exchange (or one-way forward) messages with this round.

Definition at line 274 of file Transport.hpp.

275{
276 CrystalStep s;
277
278 // Compute step parameters according to comments in CrystalStep struct.
279 s.lo = lo;
280 s.hi = hi;
281 s.mid = lo + (hi - lo) / 2;
282 s.lowSize = s.mid - lo + 1;
283 s.highSize = hi - s.mid;
284 s.inLow = (rank <= s.mid);
285 s.partner = -1;
286 s.forwardTo = -1;
287 s.recvForwardFrom = -1;
288
289 // Invariant of `mid = lo + (hi - lo) / 2` (floor division): the low
290 // half is never smaller than the high half -- lowSize is always either
291 // highSize or highSize + 1, never less. Consequently the "unpaired
292 // extra in the larger *high* half" branch below, and the
293 // `highSize == lowSize + 1` mirror case, can never trigger for this
294 // split rule; they exist only so behaviour stays correct (rather than
295 // silently wrong) if the split rule above is ever changed to something
296 // that can produce a larger high half. Asserted here, once, rather than
297 // deleting the "dead" branches, so that change would fail loudly here
298 // instead of routing messages incorrectly somewhere in route().
299 ASSERTL1(s.lowSize == s.highSize || s.lowSize == s.highSize + 1,
300 "CrystalPlanStep: split invariant violated (lowSize must be "
301 "highSize or highSize + 1) -- if the split rule above changed, "
302 "the branches guarded by this invariant need revisiting too");
303
304 if (s.inLow)
305 {
306 const int idx = rank - lo;
307 if (idx < s.highSize)
308 {
309 // pair with matching rank in high half
310 s.partner = s.mid + 1 + idx;
311 }
312 else
313 {
314 // unpaired extra in the larger low half
315 s.forwardTo = s.mid + 1;
316 }
317 }
318 else
319 {
320 const int idx = rank - (s.mid + 1);
321 if (idx < s.lowSize)
322 {
323 // pair with matching rank in low half
324 s.partner = lo + idx;
325 }
326 else
327 {
328 // unpaired extra in the larger high half
329 s.forwardTo = lo;
330 }
331 }
332
333 // Mirror of another rank's one-way forward: exactly the first rank of the
334 // smaller half receives it. Derived from group sizes so both sides agree.
335 if (s.lowSize == s.highSize + 1 && rank == s.mid + 1)
336 {
337 s.recvForwardFrom = s.mid;
338 }
339 else if (s.highSize == s.lowSize + 1 && rank == lo)
340 {
341 s.recvForwardFrom = s.hi;
342 }
343
344 return s;
345}
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Crystal-router routing decision for one bisection step.
int partner
Rank to exchange with, or -1 if unpaired extra.
int hi
Upper bound (inclusive) of the current group.
int forwardTo
One-way forward target (valid iff partner == -1).
int highSize
Number of ranks in the high half, hi - mid.
int lo
Lower bound (inclusive) of the current group.
int lowSize
Number of ranks in the low half, mid - lo + 1.
int recvForwardFrom
Rank we must receive a one-way forward from, or -1.

References ASSERTL1, CrystalPlanStep(), Nektar::LibUtilities::detail::CrystalStep::forwardTo, Nektar::LibUtilities::detail::CrystalStep::hi, hi, Nektar::LibUtilities::detail::CrystalStep::highSize, Nektar::LibUtilities::detail::CrystalStep::inLow, Nektar::LibUtilities::detail::CrystalStep::lo, lo, Nektar::LibUtilities::detail::CrystalStep::lowSize, Nektar::LibUtilities::detail::CrystalStep::mid, Nektar::LibUtilities::detail::CrystalStep::partner, rank, and Nektar::LibUtilities::detail::CrystalStep::recvForwardFrom.

Referenced by CrystalPlanStep(), and Nektar::LibUtilities::CrystalRouterTransport::Route().

◆ DrainBucketsToMessages()

std::vector< RoutedMessage > Nektar::LibUtilities::detail::DrainBucketsToMessages ( std::unordered_map< int, std::vector< std::byte > > &  buckets,
int  selfRank 
)
inline

Move sparse per-destination byte buckets into transport messages, clearing the buckets.

Parameters
[in,out]bucketsPer-destination-rank byte buffers, keyed only by ranks this rank actually has data for (i.e. sparse, not O(nproc)); cleared on return.
selfRankThis rank's index, stamped into each produced message's src field.
Returns
One RoutedMessage per non-empty bucket. Only non-empty destinations are present, so this is O(local).

Definition at line 974 of file Transport.hpp.

976{
977 std::vector<RoutedMessage> msgs;
978 msgs.reserve(buckets.size());
979 for (auto &kv : buckets)
980 {
981 msgs.push_back(RoutedMessage{kv.first, selfRank, std::move(kv.second)});
982 }
983 buckets.clear();
984 return msgs;
985}
std::vector< RoutedMessage > msgs
An opaque byte payload plus routing metadata.

References msgs.

Referenced by Nektar::LibUtilities::ConnectivityResolver::Resolve(), and Nektar::LibUtilities::SharedPayloadResolver< PayloadT >::Resolve().

◆ EpsilonDifference()

template<class T >
T Nektar::LibUtilities::detail::EpsilonDifference ( T  lhs,
T  rhs 
)
inline

Definition at line 49 of file RealComparison.hpp.

50{
51 if (lhs == rhs)
52 {
53 return T(0);
54 }
55
56 const T diff = std::abs(lhs - rhs);
57 const T scale = (std::min)(std::abs(lhs), std::abs(rhs));
58 const T eps = std::numeric_limits<T>::epsilon();
59
60 if (scale == T(0))
61 {
62 return diff / eps;
63 }
64
65 return diff / (scale * eps);
66}

Referenced by Nektar::LibUtilities::IsRealEqual().

◆ LoadAt()

template<typename T >
T Nektar::LibUtilities::detail::LoadAt ( const std::vector< std::byte > &  buf,
size_t  i 
)
inline

Read element i of a trivially-copyable T from a byte buffer.

Definition at line 121 of file SharedIdPlan.hpp.

122{
123 T v;
124 std::memcpy(static_cast<void *>(&v), buf.data() + i * sizeof(T), sizeof(T));
125 return v;
126}

◆ MakeFixedExchange()

std::unique_ptr< IFixedExchange > Nektar::LibUtilities::detail::MakeFixedExchange ( ExchangeBackend  backend,
const std::shared_ptr< ITransport > &  transport,
const std::vector< int > &  nbrs 
)
inline

Construct the IFixedExchange for backend over nbrs.

Definition at line 376 of file SharedIdPlan.hpp.

379{
380 if (backend == ExchangeBackend::eTransport)
381 {
382 return std::make_unique<TransportFixedExchange>(transport, nbrs);
383 }
384
385 CommSharedPtr comm = transport->Comm();
386 ASSERTL0(comm, "Every ExchangeBackend but eTransport needs a transport "
387 "with an underlying Comm; use ExchangeBackend::eTransport "
388 "otherwise.");
389 if (backend == ExchangeBackend::eCrystalRouter)
390 {
391 // Its own router, so that the cost of an execute never depends on
392 // which transport discovery happened to run over.
393 return std::make_unique<TransportFixedExchange>(
394 std::make_shared<CrystalRouterTransport>(comm), nbrs);
395 }
396 if (backend == ExchangeBackend::ePairwise)
397 {
398 return std::make_unique<PairwiseFixedExchange>(comm, nbrs);
399 }
400 return std::make_unique<NeighbourCollectiveFixedExchange>(comm, nbrs);
401}
#define ASSERTL0(condition, msg)
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition Comm.h:62

References ASSERTL0, Nektar::LibUtilities::eCrystalRouter, Nektar::LibUtilities::ePairwise, and Nektar::LibUtilities::eTransport.

Referenced by Nektar::LibUtilities::SharedIdPlan::Build().

◆ PackPod()

template<typename T >
void Nektar::LibUtilities::detail::PackPod ( std::vector< std::byte > &  buf,
const T &  v 
)
inline

Append a trivially-copyable POD value to a byte buffer.

Template Parameters
TTrivially-copyable value type.
Parameters
bufBuffer to append to; grown by sizeof(T) bytes.
vValue to serialise.

Definition at line 200 of file Transport.hpp.

201{
202 static_assert(std::is_trivially_copyable_v<T>,
203 "PackPod requires a trivially-copyable type");
204 const auto *p = reinterpret_cast<const std::byte *>(&v);
205 buf.insert(buf.end(), p, p + sizeof(T));
206}

◆ RendezvousRank()

int Nektar::LibUtilities::detail::RendezvousRank ( int64_t  id,
int  nproc 
)
inline

Bit-mixing hash (splitmix64 finaliser) mapping an entity id to its rendezvous rank, so sequential IDs spread across rendezvous ranks and don't create hot spots.

Parameters
idGlobal entity id.
nprocNumber of ranks to hash into, i.e. Comm()->GetSize().
Returns
The rendezvous rank for id, in [0, nproc).

Definition at line 952 of file Transport.hpp.

953{
954 uint64_t h = static_cast<uint64_t>(id);
955 h ^= h >> 30;
956 h *= 0xbf58476d1ce4e5b9ULL;
957 h ^= h >> 27;
958 h *= 0x94d049bb133111ebULL;
959 h ^= h >> 31;
960 return static_cast<int>(h % static_cast<uint64_t>(nproc));
961}
int64_t id
std::uint64_t uint64_t

References id.

Referenced by Nektar::LibUtilities::ConnectivityResolver::Register().

◆ ResolveBackend()

ExchangeBackend Nektar::LibUtilities::detail::ResolveBackend ( ExchangeBackend  backend,
const CommSharedPtr &  comm 
)
inline

The backend a plan can actually use, given what comm supports.

ExchangeBackend::eNeighbourCollective needs MPI_Dist_graph_create_adjacent, which arrived in MPI-3. Some MPI libraries still in service predate it – Microsoft MPI among them – so there is nothing to build the neighbourhood communicator with. Fall back to ExchangeBackend::ePairwise, which moves exactly the same data with one send and receive per neighbour; results are identical either way, only the MPI calls differ. MultiRegions::AssemblyCommDG drops its own neighbourhood method on the same test.

Parameters
backendBackend the caller asked for.
commCommunicator the plan will run over; null for a transport with no underlying Comm, where nothing is downgraded.
Returns
backend, or ePairwise if it cannot be honoured.

Definition at line 364 of file SharedIdPlan.hpp.

366{
367 if (backend == ExchangeBackend::eNeighbourCollective && comm &&
368 std::get<0>(comm->GetVersion()) < 3)
369 {
370 return ExchangeBackend::ePairwise;
371 }
372 return backend;
373}

References Nektar::LibUtilities::eNeighbourCollective, and Nektar::LibUtilities::ePairwise.

◆ StoreAt()

template<typename T >
void Nektar::LibUtilities::detail::StoreAt ( std::vector< std::byte > &  buf,
size_t  i,
const T &  v 
)
inline

Write element i of a trivially-copyable T into a byte buffer.

Definition at line 130 of file SharedIdPlan.hpp.

131{
132 std::memcpy(buf.data() + i * sizeof(T), &v, sizeof(T));
133}

◆ UnpackPod()

template<typename T >
T Nektar::LibUtilities::detail::UnpackPod ( const std::byte *&  cur)
inline

Read a trivially-copyable POD value from a cursor and advance it.

Template Parameters
TTrivially-copyable value type.
Parameters
curCursor into a byte buffer, positioned at the start of a value of type T; advanced by sizeof(T) bytes on return.
Returns
The unpacked value.

Definition at line 217 of file Transport.hpp.

218{
219 static_assert(std::is_trivially_copyable_v<T> &&
220 std::is_default_constructible_v<T>,
221 "UnpackPod requires a trivially-copyable, "
222 "default-constructible type (T v; default-constructs "
223 "before the memcpy below)");
224 T v;
225 std::memcpy(static_cast<void *>(&v), cur, sizeof(T));
226 cur += sizeof(T);
227 return v;
228}