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

ITransport implementation built on MPI_Alltoall (of counts) followed by MPI_Alltoallv (of data). More...

#include <Transport.hpp>

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

Public Member Functions

 AlltoallvTransport (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 MPI_Alltoall and MPI_Alltoallv.
 
- Public Member Functions inherited from Nektar::LibUtilities::ITransport
virtual ~ITransport ()=default
 

Private Attributes

CommSharedPtr m_comm
 
int m_nproc = 0
 
int m_rank = 0
 

Detailed Description

ITransport implementation built on MPI_Alltoall (of counts) followed by MPI_Alltoallv (of data).

Simple and robust, but the count exchange is \( O(P) \) memory per rank (see the note in Exchange()), so this transport mechanism is suitable for below a few thousand ranks, and can be used as the correctness reference for #CrystalRouterTransport.

Definition at line 437 of file Transport.hpp.

Constructor & Destructor Documentation

◆ AlltoallvTransport()

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

Construct a transport bound to comm.

Parameters
commCommunicator whose ranks participate in each Exchange().

Definition at line 445 of file Transport.hpp.

Member Function Documentation

◆ Comm()

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

Implements Nektar::LibUtilities::ITransport.

Definition at line 459 of file Transport.hpp.

460 {
461 return m_comm;
462 }

References m_comm.

◆ Exchange()

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

Personalised all-to-all via MPI_Alltoall and MPI_Alltoallv.

Parameters
[in]outboundMessages to send, at most one per destination rank; multiple messages to the same destination are concatenated. Left empty on return (see ITransport::Exchange).
[out]inboundOne message per rank that sent to this rank.
Note
The sendCounts/recvCounts/sdispls/rdispls arrays below are sized O(P) – this is the one place in the module where memory scales with rank count rather than local entity count. It is bounded by the MPI_Alltoall of counts, not by the (much larger) data exchange, and is the documented trade-off of this transport; use CrystalRouterTransport instead once P becomes large.

Implements Nektar::LibUtilities::ITransport.

Definition at line 478 of file Transport.hpp.

480 {
481 const int np = m_nproc;
482 std::vector<int> sendCounts(np, 0), recvCounts(np, 0);
483 for (auto &m : outbound)
484 {
485 ASSERTL1(m.dest >= 0 && m.dest < np,
486 "AlltoallvTransport::Exchange: message has an "
487 "out-of-range dest rank");
488 sendCounts[m.dest] += static_cast<int>(m.bytes.size());
489 }
490
491 m_comm->AlltoAll(sendCounts, recvCounts);
492
493 std::vector<int> sdispls(np, 0), rdispls(np, 0);
494 for (int p = 1; p < np; ++p)
495 {
496 sdispls[p] = sdispls[p - 1] + sendCounts[p - 1];
497 rdispls[p] = rdispls[p - 1] + recvCounts[p - 1];
498 }
499 const int totSend = sdispls[np - 1] + sendCounts[np - 1];
500 const int totRecv = rdispls[np - 1] + recvCounts[np - 1];
501
502 // sendCounts / sdispls / totSend are `int` because MPI's count
503 // arguments are `int`; a rank whose aggregate outbound volume
504 // exceeds INT_MAX silently wraps in the accumulation above rather
505 // than failing loudly, corrupting data instead of crashing.
506 // ASSERTL0 (checked in release too) since this is exactly the kind
507 // of wall people hit only once meshes get large, and the whole
508 // point is to fail loudly rather than ship a wrapped count.
509 {
510 int64_t check = 0;
511 for (auto &m : outbound)
512 {
513 check += static_cast<int64_t>(m.bytes.size());
514 }
515 ASSERTL0(check == static_cast<int64_t>(totSend),
516 "AlltoallvTransport::Exchange: outbound byte volume "
517 "overflowed the MPI int count limit (2^31-1 bytes); "
518 "switch to CrystalRouterTransport or shard the "
519 "exchange.");
520 }
521
522 // Pad to one element: a rank with no traffic at all in a round is
523 // routine (the rendezvous buckets are sparse), but Nektar's
524 // CommDataTypeTraits::GetPointer asserts on an empty vector, so a
525 // zero-length buffer would trip an ASSERTL1 in debug and take the
526 // address of element 0 of an empty vector in release. The counts stay
527 // zero either way, so nothing is actually sent or received.
528 std::vector<std::byte> sbuf(std::max(totSend, 1)),
529 rbuf(std::max(totRecv, 1));
530
531 // Copy each message into its destination's slot at an
532 // independently-advancing cursor, not always at sdispls[dest]: two
533 // messages to the same destination previously both landed at the
534 // start of that destination's slot, so the second silently
535 // overwrote the first and the unwritten tail of the slot shipped as
536 // zero bytes -- wrong data with no exception, since
537 // DrainBucketsToMessages() (the only caller today) happens to key
538 // by dest and so never triggers this. CrystalRouterTransport was
539 // never affected, since it serialises whole messages rather than
540 // concatenating by destination.
541 std::vector<int> cursor = sdispls;
542 for (auto &m : outbound)
543 {
544 std::copy(m.bytes.begin(), m.bytes.end(),
545 sbuf.begin() + cursor[m.dest]);
546 cursor[m.dest] += static_cast<int>(m.bytes.size());
547 }
548
549 m_comm->AlltoAllv(sbuf, sendCounts, sdispls, rbuf, recvCounts, rdispls);
550
551 // Unpack received buffer back into per-source messages.
552 inbound.clear();
553 for (int p = 0; p < np; ++p)
554 {
555 if (recvCounts[p] == 0)
556 {
557 continue;
558 }
559 RoutedMessage m;
560 m.src = p;
561 m.dest = m_rank;
562 m.bytes.assign(rbuf.begin() + rdispls[p],
563 rbuf.begin() + rdispls[p] + recvCounts[p]);
564 inbound.push_back(std::move(m));
565 }
566
567 // Guarantee the same postcondition CrystalRouterTransport gives
568 // (outbound left empty), rather than leaving it fully intact as
569 // before -- see ITransport::Exchange.
570 outbound.clear();
571 }
#define ASSERTL0(condition, msg)
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
std::vector< double > p(NPUPPER)
std::int64_t int64_t

References ASSERTL0, ASSERTL1, Nektar::LibUtilities::RoutedMessage::bytes, Nektar::LibUtilities::RoutedMessage::dest, m_comm, m_nproc, m_rank, and Nektar::LibUtilities::RoutedMessage::src.

◆ Rank()

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

Implements Nektar::LibUtilities::ITransport.

Definition at line 451 of file Transport.hpp.

452 {
453 return m_rank;
454 }

References m_rank.

◆ Size()

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

Implements Nektar::LibUtilities::ITransport.

Definition at line 455 of file Transport.hpp.

456 {
457 return m_nproc;
458 }

References m_nproc.

Member Data Documentation

◆ m_comm

CommSharedPtr Nektar::LibUtilities::AlltoallvTransport::m_comm
private

Definition at line 574 of file Transport.hpp.

Referenced by Comm(), and Exchange().

◆ m_nproc

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

Definition at line 575 of file Transport.hpp.

Referenced by Exchange(), and Size().

◆ m_rank

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

Definition at line 576 of file Transport.hpp.

Referenced by Exchange(), and Rank().