Nektar++
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
Nektar::LibUtilities::CrystalRouterTransport Class Reference

ITransport implementation performing a personalised all-to-all on a (bisected) hypercube (a Fox/gslib-style crystal router). More...

#include <Transport.hpp>

Inheritance diagram for Nektar::LibUtilities::CrystalRouterTransport:
[legend]

Classes

struct  Record
 One message in flight, as an index into m_pool. More...
 

Public Member Functions

 CrystalRouterTransport (CommSharedPtr comm)
 Construct a transport bound to comm.
 
int Rank () const override
 
int Size () const override
 
CommSharedPtr Comm () const override
 
void Exchange (std::vector< RoutedMessage > &outbound, std::vector< RoutedMessage > &inbound) override
 Personalised all-to-all via recursive-bisection hypercube routing.
 
- Public Member Functions inherited from Nektar::LibUtilities::ITransport
virtual ~ITransport ()=default
 

Private Member Functions

void Flatten (const std::vector< RoutedMessage > &outbound)
 Copy outbound into the flat (m_recs, m_pool) form routing works on. The only place a caller's per-message buffer is read.
 
void Rebuild (std::vector< RoutedMessage > &inbound)
 Hand the routed records back as RoutedMessage, the one allocation per message the interface obliges us to make.
 
void PackSend (const std::vector< Record > &send)
 Write the records selected by send into m_sendBuf, in wire format: a record count, then per record its header and payload.
 
void UnpackInto (const std::vector< std::byte > &buf, size_t &liveBytes)
 Append the records in buf (wire format, from PackSend()) to m_recs, their payloads going on the end of m_pool.
 
void ExchangeWith (int partner)
 Symmetric size-then-data exchange of m_sendBuf with partner, leaving the reply in m_recvBuf.
 
void SendOneWay (int partner)
 One-way forward of m_sendBuf from an unpaired rank in the larger half to partner, which has no direct partner this round.
 
void RecvOneWay (int partner)
 Receive the mirror side of a SendOneWay() into m_recvBuf.
 
void Route ()
 Drive the recursive-bisection hypercube routing loop until every record in m_recs has reached its destination rank.
 
void Compact ()
 Rebuild the pool from the live records, dropping departed payloads.
 

Private Attributes

std::vector< Record > m_recs
 Records in flight, and the contiguous pool their payloads live in.
 
std::vector< std::byte > m_pool
 
std::vector< Record > m_keep
 Scratch for one round, kept as members so the buffers are grown once and then reused for the life of the transport.
 
std::vector< Record > m_send
 
std::vector< std::byte > m_next
 
std::vector< std::byte > m_sendBuf
 
std::vector< std::byte > m_recvBuf
 
CommSharedPtr m_comm
 
int m_nproc = 0
 
int m_rank = 0
 

Static Private Attributes

static constexpr size_t kHeaderBytes = 3 * sizeof(int32_t)
 Bytes of a record's wire header: dest, src, len.
 
static constexpr size_t kCompactSlack = 64 * 1024
 Slack below which reclaiming departed payloads is not worth a pass.
 

Detailed Description

ITransport implementation performing a personalised all-to-all on a (bisected) hypercube (a Fox/gslib-style crystal router).

O(log P) communication rounds; each message is forwarded at most log2(P) hops, with no O(P) arrays. This is the variant to use at very large rank counts.

The recursive-bisection formulation in Route() handles non-power-of-two rank counts directly: at each step the current group [lo, hi] is split into [lo, mid] and [mid+1, hi]. Equal halves pair up directly; an odd split leaves one unpaired rank in the larger half, which forwards its cross-group messages one-way to a designated rank in the other half.

Definition at line 594 of file Transport.hpp.

Constructor & Destructor Documentation

◆ CrystalRouterTransport()

Nektar::LibUtilities::CrystalRouterTransport::CrystalRouterTransport ( CommSharedPtr  comm)
inlineexplicit

Construct a transport bound to comm.

Parameters
commCommunicator whose ranks participate in each Exchange().

Definition at line 599 of file Transport.hpp.

Member Function Documentation

◆ Comm()

CommSharedPtr Nektar::LibUtilities::CrystalRouterTransport::Comm ( ) const
inlineoverridevirtual
Returns
The underlying communicator this transport operates over.

Implements Nektar::LibUtilities::ITransport.

Definition at line 613 of file Transport.hpp.

614 {
615 return m_comm;
616 }

References m_comm.

◆ Compact()

void Nektar::LibUtilities::CrystalRouterTransport::Compact ( )
inlineprivate

Rebuild the pool from the live records, dropping departed payloads.

Definition at line 915 of file Transport.hpp.

916 {
917 m_next.clear();
918 for (Record &r : m_recs)
919 {
920 const auto off = static_cast<uint32_t>(m_next.size());
921 m_next.insert(m_next.end(), m_pool.begin() + r.off,
922 m_pool.begin() + r.off + r.len);
923 r.off = off;
924 }
925 m_pool.swap(m_next);
926 }
std::vector< Record > m_recs
Records in flight, and the contiguous pool their payloads live in.
std::uint32_t uint32_t

References m_next, m_pool, and m_recs.

Referenced by Route().

◆ Exchange()

void Nektar::LibUtilities::CrystalRouterTransport::Exchange ( std::vector< RoutedMessage > &  outbound,
std::vector< RoutedMessage > &  inbound 
)
inlineoverridevirtual

Personalised all-to-all via recursive-bisection hypercube routing.

Parameters
[in,out]outboundMessages to send, at most one per destination rank; moved from on entry.
[out]inboundFilled with every message addressed to this rank once routing completes (i.e. once dest == Rank() for all of them).

Implements Nektar::LibUtilities::ITransport.

Definition at line 626 of file Transport.hpp.

628 {
629 Flatten(outbound);
630 // Standard guarantees a moved-from vector is left in a valid but
631 // unspecified state, not necessarily empty -- clear explicitly so
632 // the "outbound left empty" postcondition (see ITransport::Exchange)
633 // doesn't depend on stdlib implementation details.
634 outbound.clear();
635 Route();
636 // After routing every record has dest == m_rank.
637 Rebuild(inbound);
638 }
void Route()
Drive the recursive-bisection hypercube routing loop until every record in m_recs has reached its des...
void Rebuild(std::vector< RoutedMessage > &inbound)
Hand the routed records back as RoutedMessage, the one allocation per message the interface obliges u...
void Flatten(const std::vector< RoutedMessage > &outbound)
Copy outbound into the flat (m_recs, m_pool) form routing works on. The only place a caller's per-mes...

References Flatten(), Rebuild(), and Route().

◆ ExchangeWith()

void Nektar::LibUtilities::CrystalRouterTransport::ExchangeWith ( int  partner)
inlineprivate

Symmetric size-then-data exchange of m_sendBuf with partner, leaving the reply in m_recvBuf.

Definition at line 795 of file Transport.hpp.

796 {
797 int64_t sn = static_cast<int64_t>(m_sendBuf.size());
798 int64_t rn = 0;
799
800 m_comm->SendRecv(partner, sn, partner, rn);
801
802 m_recvBuf.resize(static_cast<size_t>(rn));
803 m_comm->SendRecv(partner, m_sendBuf, partner, m_recvBuf);
804 }
std::int64_t int64_t

References m_comm, m_recvBuf, and m_sendBuf.

Referenced by Route().

◆ Flatten()

void Nektar::LibUtilities::CrystalRouterTransport::Flatten ( const std::vector< RoutedMessage > &  outbound)
inlineprivate

Copy outbound into the flat (m_recs, m_pool) form routing works on. The only place a caller's per-message buffer is read.

Definition at line 668 of file Transport.hpp.

669 {
670 size_t total = 0;
671 for (const auto &m : outbound)
672 {
673 total += m.bytes.size();
674 }
675
676 m_recs.clear();
677 m_recs.reserve(outbound.size());
678 m_pool.clear();
679 m_pool.resize(total);
680
681 size_t off = 0;
682 for (const auto &m : outbound)
683 {
684 const size_t n = m.bytes.size();
685 if (n > 0)
686 {
687 std::memcpy(m_pool.data() + off, m.bytes.data(), n);
688 }
689 m_recs.push_back(Record{
690 static_cast<int32_t>(m.dest), static_cast<int32_t>(m.src),
691 static_cast<uint32_t>(off), static_cast<uint32_t>(n)});
692 off += n;
693 }
694 }
std::int32_t int32_t

References m_pool, and m_recs.

Referenced by Exchange().

◆ PackSend()

void Nektar::LibUtilities::CrystalRouterTransport::PackSend ( const std::vector< Record > &  send)
inlineprivate

Write the records selected by send into m_sendBuf, in wire format: a record count, then per record its header and payload.

The count is always written, so the buffer is never empty and never reaches Comm's GetPointer(), which asserts on an empty container.

Definition at line 722 of file Transport.hpp.

723 {
724 size_t bytes = sizeof(int32_t);
725 for (const Record &r : send)
726 {
727 bytes += kHeaderBytes + r.len;
728 }
729 m_sendBuf.resize(bytes);
730
731 std::byte *cur = m_sendBuf.data();
732 const int32_t nSend = static_cast<int32_t>(send.size());
733 std::memcpy(cur, &nSend, sizeof(int32_t));
734 cur += sizeof(int32_t);
735
736 for (const Record &r : send)
737 {
738 const int32_t hdr[3] = {r.dest, r.src, static_cast<int32_t>(r.len)};
739 std::memcpy(cur, hdr, kHeaderBytes);
740 cur += kHeaderBytes;
741 if (r.len > 0)
742 {
743 std::memcpy(cur, m_pool.data() + r.off, r.len);
744 cur += r.len;
745 }
746 }
747 }
static constexpr size_t kHeaderBytes
Bytes of a record's wire header: dest, src, len.

References kHeaderBytes, m_pool, and m_sendBuf.

Referenced by Route().

◆ Rank()

int Nektar::LibUtilities::CrystalRouterTransport::Rank ( ) const
inlineoverridevirtual
Returns
This rank's index within Comm().

Implements Nektar::LibUtilities::ITransport.

Definition at line 605 of file Transport.hpp.

606 {
607 return m_rank;
608 }

References m_rank.

◆ Rebuild()

void Nektar::LibUtilities::CrystalRouterTransport::Rebuild ( std::vector< RoutedMessage > &  inbound)
inlineprivate

Hand the routed records back as RoutedMessage, the one allocation per message the interface obliges us to make.

Definition at line 700 of file Transport.hpp.

701 {
702 inbound.clear();
703 inbound.reserve(m_recs.size());
704 for (const Record &r : m_recs)
705 {
706 RoutedMessage m;
707 m.dest = r.dest;
708 m.src = r.src;
709 m.bytes.assign(m_pool.begin() + r.off,
710 m_pool.begin() + r.off + r.len);
711 inbound.push_back(std::move(m));
712 }
713 }

References Nektar::LibUtilities::RoutedMessage::bytes, Nektar::LibUtilities::RoutedMessage::dest, m_pool, m_recs, and Nektar::LibUtilities::RoutedMessage::src.

Referenced by Exchange().

◆ RecvOneWay()

void Nektar::LibUtilities::CrystalRouterTransport::RecvOneWay ( int  partner)
inlineprivate

Receive the mirror side of a SendOneWay() into m_recvBuf.

Definition at line 818 of file Transport.hpp.

819 {
820 int64_t rn = 0;
821 m_comm->Recv(partner, rn);
822
823 m_recvBuf.resize(static_cast<size_t>(rn));
824 m_comm->Recv(partner, m_recvBuf);
825 }

References m_comm, and m_recvBuf.

Referenced by Route().

◆ Route()

void Nektar::LibUtilities::CrystalRouterTransport::Route ( )
inlineprivate

Drive the recursive-bisection hypercube routing loop until every record in m_recs has reached its destination rank.

Each iteration narrows the active group [lo, hi] by half (via detail::CrystalPlanStep) and moves any record whose destination is no longer in this rank's half across to a partner (or forwards it one-way if unpaired), so after ceil(log2(nproc)) rounds every record has arrived. Records that stay are compacted into m_next alongside the ones that arrive, so the pool never accumulates the payloads of records that have left.

Definition at line 839 of file Transport.hpp.

840 {
841 int lo = 0;
842 int hi = m_nproc - 1;
843
844 while (lo < hi)
845 {
846 const detail::CrystalStep s =
848
849 // Split: records whose destination has left this half are sent,
850 // the rest stay exactly where they are. Keeping them in place is
851 // what makes a hop cheap -- the pool only ever grows within a
852 // call, so a kept record's offset stays valid and no payload is
853 // copied to hold on to it.
854 m_keep.clear();
855 m_send.clear();
856 size_t liveBytes = 0;
857 for (const Record &r : m_recs)
858 {
859 if (detail::CrystalDestInMyHalf(s, r.dest))
860 {
861 m_keep.push_back(r);
862 liveBytes += r.len;
863 }
864 else
865 {
866 m_send.push_back(r);
867 }
868 }
869
870 // Reads m_pool, so it must precede the appends below.
872 m_recs.swap(m_keep);
873
874 if (s.partner >= 0)
875 {
876 ExchangeWith(s.partner);
877 UnpackInto(m_recvBuf, liveBytes);
878 }
879 else
880 {
881 // Unpaired rank in the larger half: forward one-way into the
882 // other half.
883 SendOneWay(s.forwardTo);
884 }
885
886 // If we are the designated receiver of a one-way forward, post
887 // it.
888 if (s.recvForwardFrom >= 0)
889 {
890 RecvOneWay(s.recvForwardFrom);
891 UnpackInto(m_recvBuf, liveBytes);
892 }
893
894 // The payloads of records that have left are still sitting in
895 // the pool. Reclaim them only once they dominate it, so the
896 // common hop pays no copy at all and the pool stays inside a
897 // constant factor of what is live.
898 if (m_pool.size() > 2 * liveBytes + kCompactSlack)
899 {
900 Compact();
901 }
902
903 if (s.inLow)
904 {
905 hi = s.mid;
906 }
907 else
908 {
909 lo = s.mid + 1;
910 }
911 }
912 }
void PackSend(const std::vector< Record > &send)
Write the records selected by send into m_sendBuf, in wire format: a record count,...
void SendOneWay(int partner)
One-way forward of m_sendBuf from an unpaired rank in the larger half to partner, which has no direct...
std::vector< Record > m_keep
Scratch for one round, kept as members so the buffers are grown once and then reused for the life of ...
static constexpr size_t kCompactSlack
Slack below which reclaiming departed payloads is not worth a pass.
void RecvOneWay(int partner)
Receive the mirror side of a SendOneWay() into m_recvBuf.
void ExchangeWith(int partner)
Symmetric size-then-data exchange of m_sendBuf with partner, leaving the reply in m_recvBuf.
void Compact()
Rebuild the pool from the live records, dropping departed payloads.
void UnpackInto(const std::vector< std::byte > &buf, size_t &liveBytes)
Append the records in buf (wire format, from PackSend()) to m_recs, their payloads going on the end o...
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...
CrystalStep CrystalPlanStep(int lo, int hi, int rank)
Compute this rank's routing decision for one bisection step of the crystal router.

References Compact(), Nektar::LibUtilities::detail::CrystalDestInMyHalf(), Nektar::LibUtilities::detail::CrystalPlanStep(), ExchangeWith(), Nektar::LibUtilities::detail::CrystalStep::forwardTo, hi, Nektar::LibUtilities::detail::CrystalStep::inLow, kCompactSlack, lo, m_keep, m_nproc, m_pool, m_rank, m_recs, m_recvBuf, m_send, Nektar::LibUtilities::detail::CrystalStep::mid, PackSend(), Nektar::LibUtilities::detail::CrystalStep::partner, Nektar::LibUtilities::detail::CrystalStep::recvForwardFrom, RecvOneWay(), SendOneWay(), and UnpackInto().

Referenced by Exchange().

◆ SendOneWay()

void Nektar::LibUtilities::CrystalRouterTransport::SendOneWay ( int  partner)
inlineprivate

One-way forward of m_sendBuf from an unpaired rank in the larger half to partner, which has no direct partner this round.

Definition at line 810 of file Transport.hpp.

811 {
812 int64_t sn = static_cast<int64_t>(m_sendBuf.size());
813 m_comm->Send(partner, sn);
814 m_comm->Send(partner, m_sendBuf);
815 }

References m_comm, and m_sendBuf.

Referenced by Route().

◆ Size()

int Nektar::LibUtilities::CrystalRouterTransport::Size ( ) const
inlineoverridevirtual
Returns
The number of ranks in Comm().

Implements Nektar::LibUtilities::ITransport.

Definition at line 609 of file Transport.hpp.

610 {
611 return m_nproc;
612 }

References m_nproc.

◆ UnpackInto()

void Nektar::LibUtilities::CrystalRouterTransport::UnpackInto ( const std::vector< std::byte > &  buf,
size_t &  liveBytes 
)
inlineprivate

Append the records in buf (wire format, from PackSend()) to m_recs, their payloads going on the end of m_pool.

Definition at line 753 of file Transport.hpp.

754 {
755 if (buf.empty())
756 {
757 return;
758 }
759
760 const std::byte *cur = buf.data();
761 [[maybe_unused]] const std::byte *end = buf.data() + buf.size();
762
763 int32_t n = 0;
764 std::memcpy(&n, cur, sizeof(int32_t));
765 cur += sizeof(int32_t);
766
767 m_recs.reserve(m_recs.size() + static_cast<size_t>(n));
768 // No reserve() on the pool: an exact-size reserve on every round
769 // would defeat the containers geometric growth and reallocate each
770 // time. insert() already grows it amortised-constant.
771 for (int32_t i = 0; i < n; ++i)
772 {
773 int32_t hdr[3];
774 std::memcpy(hdr, cur, kHeaderBytes);
775 cur += kHeaderBytes;
776
777 const auto len = static_cast<uint32_t>(hdr[2]);
778 const auto off = static_cast<uint32_t>(m_pool.size());
779 m_pool.insert(m_pool.end(), cur, cur + len);
780 cur += len;
781
782 m_recs.push_back(Record{hdr[0], hdr[1], off, len});
783 liveBytes += len;
784 }
785
786 ASSERTL1(cur == end,
787 "CrystalRouterTransport: malformed buffer (record lengths "
788 "didn't sum to the buffer size)");
789 }
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....

References ASSERTL1, kHeaderBytes, m_pool, and m_recs.

Referenced by Route().

Member Data Documentation

◆ kCompactSlack

constexpr size_t Nektar::LibUtilities::CrystalRouterTransport::kCompactSlack = 64 * 1024
staticconstexprprivate

Slack below which reclaiming departed payloads is not worth a pass.

Definition at line 662 of file Transport.hpp.

Referenced by Route().

◆ kHeaderBytes

constexpr size_t Nektar::LibUtilities::CrystalRouterTransport::kHeaderBytes = 3 * sizeof(int32_t)
staticconstexprprivate

Bytes of a record's wire header: dest, src, len.

Definition at line 659 of file Transport.hpp.

Referenced by PackSend(), and UnpackInto().

◆ m_comm

CommSharedPtr Nektar::LibUtilities::CrystalRouterTransport::m_comm
private

Definition at line 936 of file Transport.hpp.

Referenced by Comm(), ExchangeWith(), RecvOneWay(), and SendOneWay().

◆ m_keep

std::vector<Record> Nektar::LibUtilities::CrystalRouterTransport::m_keep
private

Scratch for one round, kept as members so the buffers are grown once and then reused for the life of the transport.

Definition at line 933 of file Transport.hpp.

Referenced by Route().

◆ m_next

std::vector<std::byte> Nektar::LibUtilities::CrystalRouterTransport::m_next
private

Definition at line 934 of file Transport.hpp.

Referenced by Compact().

◆ m_nproc

int Nektar::LibUtilities::CrystalRouterTransport::m_nproc = 0
private

Definition at line 937 of file Transport.hpp.

Referenced by Route(), and Size().

◆ m_pool

std::vector<std::byte> Nektar::LibUtilities::CrystalRouterTransport::m_pool
private

Definition at line 930 of file Transport.hpp.

Referenced by Compact(), Flatten(), PackSend(), Rebuild(), Route(), and UnpackInto().

◆ m_rank

int Nektar::LibUtilities::CrystalRouterTransport::m_rank = 0
private

Definition at line 938 of file Transport.hpp.

Referenced by Rank(), and Route().

◆ m_recs

std::vector<Record> Nektar::LibUtilities::CrystalRouterTransport::m_recs
private

Records in flight, and the contiguous pool their payloads live in.

Definition at line 929 of file Transport.hpp.

Referenced by Compact(), Flatten(), Rebuild(), Route(), and UnpackInto().

◆ m_recvBuf

std::vector<std::byte> Nektar::LibUtilities::CrystalRouterTransport::m_recvBuf
private

Definition at line 934 of file Transport.hpp.

Referenced by ExchangeWith(), RecvOneWay(), and Route().

◆ m_send

std::vector<Record> Nektar::LibUtilities::CrystalRouterTransport::m_send
private

Definition at line 933 of file Transport.hpp.

Referenced by Route().

◆ m_sendBuf

std::vector<std::byte> Nektar::LibUtilities::CrystalRouterTransport::m_sendBuf
private

Definition at line 934 of file Transport.hpp.

Referenced by ExchangeWith(), PackSend(), and SendOneWay().