97#ifndef __ADS_MPIMailbox
98 #define __ADS_MPIMailbox
101#if defined( ADS_WITH_MPI )
117#include <type_traits>
144 static void encode(
const std::string & x , std::vector< std::byte > & out ) {
145 const auto * b =
reinterpret_cast< const std::byte *
>( x.data() );
146 out.insert( out.end() , b , b + x.size() );
149 static std::string
decode( std::span< const std::byte > b ) {
151 std::string(
reinterpret_cast< const char *
>( b.data() ) , b.size() ) );
157 requires std::is_trivially_copyable_v< T >
161 static void encode(
const std::vector< T > & x ,
162 std::vector< std::byte > & out ) {
163 const std::size_t at = out.size();
164 out.resize( at + x.size() *
sizeof( T ) );
166 std::memcpy( out.data() + at , x.data() , x.size() *
sizeof( T ) );
169 static std::vector< T >
decode( std::span< const std::byte > b ) {
170 std::vector< T > x( b.size() /
sizeof( T ) );
172 std::memcpy( x.data() , b.data() , x.size() *
sizeof( T ) );
195 MPIMailboxCore(
const MPIMailboxCore & ) =
delete;
197 MPIMailboxCore & operator=(
const MPIMailboxCore & ) =
delete;
200 [[nodiscard]] std::size_t rank(
void )
const {
201 return( std::size_t( f_rank ) );
205 [[nodiscard]] std::size_t size(
void )
const {
206 return( std::size_t( f_size ) );
211 [[nodiscard]]
bool terminated(
void )
const {
return( f_done ); }
215 [[nodiscard]]
bool stopped(
void )
const {
return( f_stopped ); }
225 if( f_done || f_stopped )
228 for(
int q = 0 ; q < f_size ; ++q )
230 f_buf.emplace_back();
231 f_req.push_back( MPI_REQUEST_NULL );
233 MPI_Isend(
nullptr , 0 , MPI_BYTE , q , kTag , f_comm , &f_req.back() ) ,
235 ++f_to[ 2 * std::size_t( q ) + 1 ];
240 [[nodiscard]] std::uint64_t sent(
void )
const {
return( f_sent ); }
243 [[nodiscard]] std::uint64_t batches(
void )
const {
return( f_batches ); }
246 [[nodiscard]] std::uint64_t received(
void )
const {
return( f_received ); }
249 [[nodiscard]] std::uint64_t waves(
void )
const {
return( f_waves ); }
252 [[nodiscard]] MPI_Comm comm(
void )
const {
return( f_comm ); }
257 static constexpr int kTag = 1;
260 explicit MPIMailboxCore( MPI_Comm comm ) {
261 detail::mpiCheck( MPI_Comm_dup( comm , &f_comm ) ,
"MPI_Comm_dup" );
262 detail::mpiCheck( MPI_Comm_rank( f_comm , &f_rank ) ,
"MPI_Comm_rank" );
263 detail::mpiCheck( MPI_Comm_size( f_comm , &f_size ) ,
"MPI_Comm_size" );
264 f_to.assign( 2 * std::size_t( f_size ) , 0 );
270 if( ! f_req.empty() )
271 MPI_Waitall(
int( f_req.size() ) , f_req.data() , MPI_STATUSES_IGNORE );
273 MPI_Wait( &f_wave , MPI_STATUS_IGNORE );
274 MPI_Comm_free( &f_comm );
279 void isend( std::vector< B > && b , std::size_t to , std::uint64_t k ) {
280 f_buf.push_back( std::move( b ) );
281 const auto & x = f_buf.back();
282 f_req.push_back( MPI_REQUEST_NULL );
283 detail::mpiCheck( MPI_Isend( x.data() ,
int( x.size() *
sizeof( B ) ) ,
284 MPI_BYTE ,
int( to ) , kTag , f_comm ,
294 std::size_t receive( T && take ) {
296 for(
int batches = 0 ; batches < 64 ; ++batches ) {
299 detail::mpiCheck( MPI_Iprobe( MPI_ANY_SOURCE , kTag , f_comm , &flag ,
304 const std::size_t k = receiveOne( st , take );
316 void progress(
void ) {
320 f_index.resize( f_req.size() );
321 detail::mpiCheck( MPI_Testsome(
int( f_req.size() ) , f_req.data() , &done ,
322 f_index.data() , MPI_STATUSES_IGNORE ) ,
324 if( ( done == MPI_UNDEFINED ) || ( done == 0 ) )
327 std::size_t keep = 0;
328 for( std::size_t i = 0 ; i < f_req.size() ; ++i )
329 if( f_req[ i ] != MPI_REQUEST_NULL ) {
331 f_req[ keep ] = f_req[ i ];
332 f_buf[ keep ] = std::move( f_buf[ i ] );
336 f_req.resize( keep );
337 f_buf.resize( keep );
348 detail::mpiCheck( MPI_Test( &f_wave , &flag , MPI_STATUS_IGNORE ) ,
354 if( f_sums[ 2 ] == std::uint64_t( f_size ) )
355 return( f_done =
true );
356 if( f_waves > 1 && ( f_sums[ 0 ] == f_lastR ) )
357 return( f_done =
true );
358 f_lastR = f_sums[ 1 ];
360 f_mine[ 0 ] = f_sent;
361 f_mine[ 1 ] = f_received;
362 f_mine[ 2 ] = f_stopped ? 1 : 0;
363 detail::mpiCheck( MPI_Iallreduce( f_mine , f_sums , 3 , MPI_UINT64_T ,
376 std::size_t close( T && take ) {
380 std::uint64_t in[ 2 ] = { 0 , 0 };
381 detail::mpiCheck( MPI_Reduce_scatter_block( f_to.data() , in , 2 ,
384 "MPI_Reduce_scatter_block" );
386 while( ( f_inMsgs < in[ 0 ] ) || ( f_inNotes < in[ 1 ] ) ) {
388 detail::mpiCheck( MPI_Probe( MPI_ANY_SOURCE , kTag , f_comm , &st ) ,
390 n += receiveOne( st , take );
392 if( ! f_req.empty() )
394 MPI_Waitall(
int( f_req.size() ) , f_req.data() , MPI_STATUSES_IGNORE ) ,
402 [[nodiscard]]
bool closed(
void )
const {
return( f_closed ); }
405 void countSent( std::uint64_t k ) { f_sent += k; }
408 void countSelf( std::uint64_t k ) { f_received += k; }
415 std::size_t receiveOne(
const MPI_Status & st , T & take ) {
417 detail::mpiCheck( MPI_Get_count( &st , MPI_BYTE , &bytes ) ,
419 f_in.resize( std::size_t( bytes ) /
sizeof( B ) );
420 detail::mpiCheck( MPI_Recv( f_in.data() , bytes , MPI_BYTE , st.MPI_SOURCE ,
421 kTag , f_comm , MPI_STATUS_IGNORE ) ,
427 const std::size_t k = take( std::span< const B >( f_in ) );
432 MPI_Comm f_comm = MPI_COMM_NULL;
435 std::vector< B > f_in;
436 std::vector< MPI_Request > f_req;
437 std::vector< std::vector< B > > f_buf;
438 std::vector< int > f_index;
440 std::vector< std::uint64_t > f_to;
441 std::uint64_t f_inMsgs = 0;
442 std::uint64_t f_inNotes = 0;
443 std::uint64_t f_sent = 0;
444 std::uint64_t f_batches = 0;
445 std::uint64_t f_received = 0;
446 MPI_Request f_wave = MPI_REQUEST_NULL;
447 bool f_waving =
false;
448 std::uint64_t f_mine[ 3 ] = { 0 , 0 , 0 };
449 std::uint64_t f_sums[ 3 ] = { 0 , 0 , 0 };
450 std::uint64_t f_lastR = 0;
451 std::uint64_t f_waves = 0;
452 bool f_stopped =
false;
454 bool f_closed =
false;
468 using Core = detail::MPIMailboxCore< std::byte >;
476 std::size_t
batch = 65536 )
477 : Core( comm ) , f_batch(
batch ) {
479 f_out.resize( size() );
480 f_count.assign( size() , 0 );
486 if( terminated() && ! closed() )
488 close( []( std::span< const std::byte > b ) {
return( records( b ) ); } );
509 void send( std::size_t to , std::uint32_t t ,
510 std::span< const std::byte > b ) {
511 ADS_PRE( ( to < size() ) && ( b.size() <= 0xFFFFFFFFull ) );
512 auto & o = f_out[ to ];
513 const std::uint32_t head[ 2 ] = { t , std::uint32_t( b.size() ) };
514 const std::size_t at = o.size();
515 o.resize( at +
sizeof( head ) + padded( b.size() ) );
516 std::memcpy( o.data() + at , head ,
sizeof( head ) );
518 std::memcpy( o.data() + at +
sizeof( head ) , b.data() , b.size() );
520 if( ( o.size() >= f_batch ) && ! stopped() )
528 for( std::size_t to = 0 ; to < f_out.size() ; ++to )
529 if( ! f_out[ to ].empty() )
563 if( ! f_self.empty() ) {
564 std::swap( f_self , f_selfTaken );
565 n = forEach( f_selfTaken , f );
569 return( n + receive( [ & ]( std::span< const std::byte > b ) {
570 return( forEach( b , f ) );
592 std::size_t n = close(
593 [ & ]( std::span< const std::byte > b ) {
return( forEach( b , f ) ); } );
594 n += forEach( f_self , f );
596 for(
auto & o : f_out ) {
597 n += forEach( o , f );
606 static std::size_t padded( std::size_t k ) {
return( ( k + 7 ) & ~7ull ); }
610 static std::size_t forEach( std::span< const std::byte > x , F & f ) {
612 for( std::size_t at = 0 ; at < x.size() ; ++n ) {
613 std::uint32_t head[ 2 ];
614 std::memcpy( head , x.data() + at ,
sizeof( head ) );
615 at +=
sizeof( head );
616 f( head[ 0 ] , x.subspan( at , head[ 1 ] ) );
617 at += padded( head[ 1 ] );
623 static std::size_t records( std::span< const std::byte > x ) {
624 auto none = []( std::uint32_t , std::span< const std::byte > ) { };
625 return( forEach( x ,
none ) );
629 void post( std::size_t to ) {
630 auto & o = f_out[ to ];
631 countSent( f_count[ to ] );
633 f_self.insert( f_self.end() , o.begin() , o.end() );
635 std::vector< std::byte > b;
636 b.reserve( f_batch + 64 );
638 isend( std::move( b ) , to , f_count[ to ] );
645 std::vector< std::vector< std::byte > > f_out;
646 std::vector< std::uint64_t > f_count;
647 std::vector< std::byte > f_self;
648 std::vector< std::byte > f_selfTaken;
660template<
class Msg ,
bool = std::is_trivially_copyable_v< Msg > >
664 using Core = detail::MPIMailboxCore< Msg >;
674 std::size_t
batch = 256 )
675 : Core( comm ) , f_batch(
batch ) {
677 f_out.resize( this->size() );
683 if( this->terminated() && ! this->closed() )
685 this->close( []( std::span< const Msg > b ) {
return( b.size() ); } );
704 void send( std::size_t to ,
const Msg & x ) {
706 auto & o = f_out[ to ];
708 if( ( o.size() >= f_batch ) && ! this->stopped() )
732 if( this->stopped() )
734 for( std::size_t to = 0 ; to < f_out.size() ; ++to )
735 if( ! f_out[ to ].empty() )
775 if( this->stopped() )
779 if( ! f_self.empty() ) {
780 std::swap( f_self , f_selfTaken );
781 for(
const auto & x : f_selfTaken )
783 n += f_selfTaken.size();
785 this->countSelf( n );
787 return( n + this->receive( [ & ]( std::span< const Msg > b ) {
788 for(
const auto & x : b )
819 if( this->terminated() )
822 return( this->wave() );
851 std::size_t n = this->close( [ & ]( std::span< const Msg > b ) {
852 for(
const auto & x : b )
856 for(
auto * v : { &f_self , &f_selfTaken } ) {
857 for(
const auto & x : *v )
862 for(
auto & o : f_out ) {
863 for(
const auto & x : o )
874 void post( std::size_t to ) {
875 auto & o = f_out[ to ];
876 this->countSent( o.size() );
877 if( to == this->rank() ) {
878 f_self.insert( f_self.end() , o.begin() , o.end() );
882 const std::uint64_t k = o.size();
883 std::vector< Msg > b;
884 b.reserve( f_batch );
886 this->isend( std::move( b ) , to , k );
890 std::vector< std::vector< Msg > > f_out;
891 std::vector< Msg > f_self;
892 std::vector< Msg > f_selfTaken;
908 requires(
const Msg & x , std::vector< std::byte > & out ,
909 std::span< const std::byte > b ) {
913 "a message that is not trivially copyable needs MPICodec< Msg >" );
923 std::size_t
batch = 65536 )
927 [[nodiscard]] std::size_t
rank(
void )
const {
return( f_box.rank() ); }
929 [[nodiscard]] std::size_t
size(
void )
const {
return( f_box.size() ); }
934 void send( std::size_t to ,
const Msg & x ) {
937 f_box.send( to , 0 , f_bytes );
941 void flush(
void ) { f_box.flush(); }
946 return( f_box.poll( [ & ]( std::uint32_t , std::span< const std::byte > b ) {
947 f( MPICodec< Msg >::decode( b ) );
959 f_box.leftovers( [ & ]( std::uint32_t , std::span< const std::byte > b ) {
960 f( MPICodec< Msg >::decode( b ) );
965 void stop(
void ) { f_box.stop(); }
967 [[nodiscard]]
bool stopped(
void )
const {
return( f_box.stopped() ); }
969 [[nodiscard]]
bool terminated(
void )
const {
return( f_box.terminated() ); }
971 [[nodiscard]] std::uint64_t
sent(
void )
const {
return( f_box.sent() ); }
973 [[nodiscard]] std::uint64_t
batches(
void )
const {
974 return( f_box.batches() );
977 [[nodiscard]] std::uint64_t
received(
void )
const {
978 return( f_box.received() );
981 [[nodiscard]] std::uint64_t
waves(
void )
const {
return( f_box.waves() ); }
983 [[nodiscard]] MPI_Comm
comm(
void )
const {
return( f_box.comm() ); }
988 std::vector< std::byte > f_bytes;
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The processes of MPI as the workers of a distributed computation, beyond the book,...
The mailbox of the long-lived workers of an executor [see Executor.h], beyond the book,...
the mailbox of the processes of a communicator for messages of bytes, each with a tag of its own,...
Definition MPIMailbox.h:466
std::size_t leftovers(F &&f)
the calls f( t , b ) on the messages left to this process, those on their way to it,...
Definition MPIMailbox.h:590
std::size_t poll(F &&f)
the calls f( t , b ) on the messages received, of tag and bytes (valid during the call,...
Definition MPIMailbox.h:558
~MPIByteMailbox()
the closing, if leftovers() did not do it, its messages dropped; a collective operation
Definition MPIMailbox.h:485
bool quiescent(void)
true if the computation has ended, or its stop; called with no work left, it sends the outboxes (unle...
Definition MPIMailbox.h:576
MPIByteMailbox(MPI_Comm comm=MPI_COMM_WORLD, std::size_t batch=65536)
the mailbox of the processes of comm, which sends an outbox when it holds batch bytes; a collective o...
Definition MPIMailbox.h:475
void send(std::size_t to, std::uint32_t t, std::span< const std::byte > b)
the message of the bytes and of the tag appended to the outbox for to, which is sent when it holds ...
Definition MPIMailbox.h:509
void flush(void)
the outboxes sent, unless stopped
Definition MPIMailbox.h:525
bool quiescent(void)
as MPIByteMailbox::quiescent()
Definition MPIMailbox.h:952
MPI_Comm comm(void) const
the communicator of the mailbox
Definition MPIMailbox.h:983
std::size_t rank(void) const
the rank of this process
Definition MPIMailbox.h:927
Msg Message
the type of the messages
Definition MPIMailbox.h:917
void send(std::size_t to, const Msg &x)
the bytes of appended to the outbox for to, as MPIByteMailbox::send()
Definition MPIMailbox.h:934
void stop(void)
as MPIByteMailbox::stop()
Definition MPIMailbox.h:965
bool terminated(void) const
true once quiescent() has seen the end
Definition MPIMailbox.h:969
std::size_t leftovers(F &&f)
as MPIByteMailbox::leftovers(), the messages rebuilt
Definition MPIMailbox.h:957
std::uint64_t waves(void) const
the waves completed so far
Definition MPIMailbox.h:981
std::uint64_t sent(void) const
the messages sent by this process so far
Definition MPIMailbox.h:971
MPIMailbox(MPI_Comm comm=MPI_COMM_WORLD, std::size_t batch=65536)
the mailbox of the processes of comm, which sends an outbox when it holds batch bytes; a collective o...
Definition MPIMailbox.h:922
std::size_t poll(F &&f)
the calls f( x ) on the messages received, rebuilt, and their number
Definition MPIMailbox.h:945
void flush(void)
the outboxes sent, unless stopped
Definition MPIMailbox.h:941
std::size_t size(void) const
the number of the processes
Definition MPIMailbox.h:929
bool stopped(void) const
true once this process has stopped
Definition MPIMailbox.h:967
std::uint64_t batches(void) const
the batches sent by MPI so far
Definition MPIMailbox.h:973
std::uint64_t received(void) const
the messages received by this process so far
Definition MPIMailbox.h:977
std::size_t poll(F &&f)
the calls f( x ) on the messages received, and their number; nothing after the stop
Definition MPIMailbox.h:774
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 ...
Definition MPIMailbox.h:849
bool quiescent(void)
true if the computation has ended, or its stop; called with no work left, it sends the outboxes (unle...
Definition MPIMailbox.h:818
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
Definition MPIMailbox.h:673
void send(std::size_t to, const Msg &x)
appended to the outbox for to, which is sent when it holds a batch; never waits, and after the stop o...
Definition MPIMailbox.h:704
void flush(void)
the outboxes sent, unless stopped
Definition MPIMailbox.h:731
Msg Message
the type of the messages
Definition MPIMailbox.h:668
~MPIMailbox()
the closing, if leftovers() did not do it, its messages dropped; a collective operation
Definition MPIMailbox.h:682
@ none
Definition Capabilities.h:553
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Batch< S > batch(S &s)
a batch on the structure , open while it lives: its events are collected, and the outermost batch sen...
Definition Modification.h:2150
static void encode(const std::string &x, std::vector< std::byte > &out)
the characters appended to out
Definition MPIMailbox.h:144
static std::string decode(std::span< const std::byte > b)
the string of the bytes
Definition MPIMailbox.h:149
static std::vector< T > decode(std::span< const std::byte > b)
the vector of the bytes
Definition MPIMailbox.h:169
static void encode(const std::vector< T > &x, std::vector< std::byte > &out)
the bytes of the items appended to out
Definition MPIMailbox.h:161
the customization point of the messages that are not trivially copyable for MPIMailbox: a specializat...
Definition MPIMailbox.h:137