ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::MPIMailbox< Msg, bool > Class Template Reference

the mailbox of the processes of a communicator, with outboxes of batches and the detection of the termination by the four counters of Mattern: for a trivially copyable Msg, the batches are arrays of messages, copied as bytes; otherwise [see the specialization below] each message is a record of the bytes that MPICodec< Msg > gives More...

#include <ADS/distributed/MPIMailbox.h>

Inheritance diagram for ADS::MPIMailbox< Msg, bool >:

Public types

using Message = Msg
 the type of the messages

Public member functions

 MPIMailbox (MPI_Comm comm=MPI_COMM_WORLD, std::size_t batch=256)
 the mailbox of the processes of comm, which sends batches of batch messages; a collective operation
 ~MPIMailbox ()
 the closing, if leftovers() did not do it, its messages dropped; a collective operation
void send (std::size_t to, const Msg &x)
 \( x \) appended to the outbox for to, which is sent when it holds a batch; never waits, and after the stop only appends
void flush (void)
 the outboxes sent, unless stopped
template<class F>
std::size_t poll (F &&f)
 the calls f( x ) on the messages received, and their number; nothing after the stop
bool quiescent (void)
 true if the computation has ended, or its stop; called with no work left, it sends the outboxes (unless stopped) and joins the waves
template<class F>
std::size_t leftovers (F &&f)
 the calls f( x ) on the messages left to this process, those on their way to it, those to itself and those of its outboxes, and their number: a collective operation after quiescent() has returned true, after which every message sent has been handed to a function once, by a poll() or here (none after a termination without stop)

Detailed description

template<class Msg, bool = std::is_trivially_copyable_v< Msg >>
class ADS::MPIMailbox< Msg, bool >

the mailbox of the processes of a communicator, with outboxes of batches and the detection of the termination by the four counters of Mattern: for a trivially copyable Msg, the batches are arrays of messages, copied as bytes; otherwise [see the specialization below] each message is a record of the bytes that MPICodec< Msg > gives

Member typedef documentation

◆ Message

template<class Msg, bool = std::is_trivially_copyable_v< Msg >>
using ADS::MPIMailbox< Msg, bool >::Message = Msg

the type of the messages

Constructor and destructor documentation

◆ MPIMailbox()

template<class Msg, bool = std::is_trivially_copyable_v< Msg >>
ADS::MPIMailbox< Msg, bool >::MPIMailbox ( MPI_Comm comm = MPI_COMM_WORLD,
std::size_t batch = 256 )
inlineexplicit

the mailbox of the processes of comm, which sends batches of batch messages; a collective operation

Precondition
MPI is initialized, \( \mathit{batch} \ge 1 \)

◆ ~MPIMailbox()

template<class Msg, bool = std::is_trivially_copyable_v< Msg >>
ADS::MPIMailbox< Msg, bool >::~MPIMailbox ( )
inline

the closing, if leftovers() did not do it, its messages dropped; a collective operation

Member function documentation

◆ flush()

template<class Msg, bool = std::is_trivially_copyable_v< Msg >>
void ADS::MPIMailbox< Msg, bool >::flush ( void )
inline

the outboxes sent, unless stopped

In pseudocode (beyond the book), \( s \) the messages sent:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{flush}() \cr 2 & \quad \textbf{if}\ \textbf{not}\ \text{stopped}\ \textbf{then} \ \textbf{foreach}\ \mathit{to} \text{ with } O_{\mathit{to}} \ne \emptyset\ \textbf{do}\ \mathrm{post}( \mathit{to} ) \cr 3 & \textbf{procedure}\ \mathrm{post}( \mathit{to} ) \cr 4 & \quad s \gets s + |O_{\mathit{to}}| \cr 5 & \quad \textbf{if}\ \mathit{to} \text{ is this process} \ \textbf{then}\ \text{move } O_{\mathit{to}} \text{ to the } \text{messages to itself} \cr 6 & \quad \textbf{else}\ \mathrm{MPI\_Isend}( O_{\mathit{to}} , \mathit{to} ) \text{, the buffer kept with its request until it } \text{completes} \end{array}} \]

◆ leftovers()

template<class Msg, bool = std::is_trivially_copyable_v< Msg >>
template<class F>
std::size_t ADS::MPIMailbox< Msg, bool >::leftovers ( F && f)
inline

the calls f( x ) on the messages left to this process, those on their way to it, those to itself and those of its outboxes, and their number: a collective operation after quiescent() has returned true, after which every message sent has been handed to a function once, by a poll() or here (none after a termination without stop)

In pseudocode (beyond the book), \( m_{q , i} \) the messages and \( n_{q , i} \) the notes sent by MPI by \( q \) to \( i \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{leftovers}( f ) \cr 2 & \quad ( M , N ) \gets \mathrm{MPI\_Reduce\_scatter\_block}( ( m_{ i , q } , n_{ i , q } )_q , + )\quad \triangleright\ \text{those to} \text{ this process} \cr 3 & \quad \textbf{while}\ \text{fewer than } M \text{ messages or } N \text{ notes were received}\ \textbf{do} \cr 4 & \qquad \mathrm{MPI\_Recv}( b ) \text{ from any source}; \ \textbf{foreach}\ x \in b\ \textbf{do}\ f( x ) \cr 5 & \quad \text{wait for its sends};\ \textbf{foreach}\ x \text{ to } \text{itself or in an outbox}\ \textbf{do}\ f( x ) \end{array}} \]

Precondition
terminated()

◆ poll()

template<class Msg, bool = std::is_trivially_copyable_v< Msg >>
template<class F>
std::size_t ADS::MPIMailbox< Msg, bool >::poll ( F && f)
inline

the calls f( x ) on the messages received, and their number; nothing after the stop

Each message is passed as const Msg &, where Mailbox::poll() of the workers in the shared memory [see Mailbox.h] passes Msg &&: there a message is an object that the channel moves out of its queue, which may own memory (a vector, the state of a node), and \( f \) takes it over with no copy; here the messages of a batch are the bytes that MPI_Recv wrote into a vector of Msg of the mailbox (and those to itself are in another one), read in place: a move of a trivially copyable message would only copy it, and the vector is used again by the next batch, hence \( f \) copies what it keeps. The mailbox of the messages that are not trivially copyable passes instead the message rebuilt by MPICodec< Msg >::decode(), a temporary that \( f \) may take by value or as Msg &&.

In pseudocode (beyond the book), \( r \) the messages received:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{poll}( f ) \cr 2 & \quad \textbf{if}\ \text{stopped}\ \textbf{then return}\ 0 \cr 3 & \quad \text{free the buffers of the sends that } \mathrm{MPI\_Testsome} \text{ finds completed} \cr 4 & \quad n \gets 0;\ \textbf{foreach}\ x \text{ among the messages to } \text{itself}\ \textbf{do}\ f( x );\ n \gets n + 1 \cr 5 & \quad \textbf{while}\ \mathrm{MPI\_Iprobe}( \text{any source} ) \text{ finds a batch } b\ \textbf{do} \cr 6 & \qquad \textbf{if}\ b = \emptyset\ \textbf{then}\ \text{stopped}; \ \textbf{break}\quad \triangleright\ \text{the note of a stop} \cr 7 & \qquad \mathrm{MPI\_Recv}( b );\ \textbf{foreach}\ x \in b \ \textbf{do}\ f( x );\ n \gets n + |b| \cr 8 & \quad r \gets r + n;\ \textbf{return}\ n \end{array}} \]

◆ quiescent()

template<class Msg, bool = std::is_trivially_copyable_v< Msg >>
bool ADS::MPIMailbox< Msg, bool >::quiescent ( void )
inline

true if the computation has ended, or its stop; called with no work left, it sends the outboxes (unless stopped) and joins the waves

In pseudocode (Mattern's four counters on reductions that do not block, beyond the book), \( R' \) the receipts of the previous wave, \( \sigma \) the processes stopped:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{quiescent}() \cr 2 & \quad \mathrm{flush}() \cr 3 & \quad \textbf{if}\ \text{a wave is in flight}\ \textbf{then} \cr 4 & \qquad \textbf{if}\ \text{it has not completed}\ \textbf{then} \ \textbf{return false} \cr 5 & \qquad ( S , R , \sigma ) \gets \text{its sums};\ \textbf{if} \ \sigma = P\ \textbf{then return true} \cr 6 & \qquad \textbf{if}\ \text{a previous wave exists }\textbf{and} \ S = R'\ \textbf{then}\ \textbf{return true} \cr 7 & \qquad R' \gets R \cr 8 & \quad \mathrm{MPI\_Iallreduce}( ( s , r , \text{stopped} ) , + ) \quad \triangleright\ \text{the next wave, from a passive process} \cr 9 & \quad \textbf{return false} \end{array}} \]

◆ send()

template<class Msg, bool = std::is_trivially_copyable_v< Msg >>
void ADS::MPIMailbox< Msg, bool >::send ( std::size_t to,
const Msg & x )
inline

\( x \) appended to the outbox for to, which is sent when it holds a batch; never waits, and after the stop only appends

In pseudocode (beyond the book), \( O_{\mathit{to}} \) the outbox:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{send}( \mathit{to} , x ) \cr 2 & \quad \text{append } x \text{ to } O_{\mathit{to}} \cr 3 & \quad \textbf{if}\ |O_{\mathit{to}}| \ge \mathit{batch} \ \textbf{and not stopped then}\ \mathrm{post}( \mathit{to} ) \end{array}} \]

Precondition
to is a rank

The documentation for this class was generated from the following file: