140 #define __ADS_Mailbox
158#include <type_traits>
159#include <unordered_map>
173template<
class K ,
class H = std::hash< K > >
174[[nodiscard]] std::size_t
ownerOf(
const K & k , std::size_t p ,
178 return( std::size_t( h( k ) ) % p );
196template<
class K ,
class M =
void >
210 static constexpr bool on =
false;
214template<
class K ,
class M >
215struct CoalescePolicy< CoalesceBy< K , M > >
217 static constexpr bool on =
true;
222template<
class C ,
class Msg >
225 static constexpr bool on =
false;
232template<
class K ,
class M ,
class Msg >
233struct Coalescing< CoalesceBy< K , M > , Msg >
235 static constexpr bool on =
true;
238 std::remove_cvref_t< std::invoke_result_t< const K & , const Msg & > >;
239 using Map = std::conditional_t< std::is_void_v< M > ,
240 std::unordered_map< Key , std::size_t > , M >;
241 using Slots = std::vector< Map >;
248[[nodiscard]]
constexpr auto & mappedOf(
const I & it )
250 if constexpr( std::is_pointer_v< I > )
253 return( it->second );
262template<
class C ,
class Msg >
264 std::same_as< C , NoCoalescing > ||
265 ( detail::CoalescePolicy< C >::on &&
266 std::default_initializable<
267 typename detail::CoalescePolicy< C >::KeyOf > &&
268 std::invocable< const typename detail::CoalescePolicy< C >::KeyOf & ,
270 requires(
typename detail::Coalescing< C , Msg >::Map & m ,
271 const typename detail::Coalescing< C , Msg >::Key & k ,
275 detail::mappedOf( m.try_emplace( k , i ).first )
276 } -> std::same_as< std::size_t & >;
277 { m.try_emplace( k , i ).second } -> std::convertible_to< bool >;
279 { m.empty() } -> std::convertible_to< bool >;
291template<
class Msg , ChannelFamily Ch = MeshChannels< Msg > ,
292 class Co = NoCoalescing >
297 "the policy of the coalescing is NoCoalescing or a "
298 "CoalesceBy with a key and a map" );
301 using Coalesce = detail::Coalescing< Co , Msg >;
304 static constexpr bool coalescing = Coalesce::on;
308 static constexpr bool merges = std::invocable< F & , Msg & , const Msg & >;
319 template<
class... A >
320 explicit Mailbox( std::size_t p , std::size_t
batch = 64 , A &&... args )
321 : f_ch( p , std::forward< A >( args )... ) , f_worker( p ) ,
324 for(
auto & w : f_worker ) {
325 w = std::make_unique< Worker >();
327 w->next.assign( p ,
batch );
328 w->listed.assign( p , 0 );
329 if constexpr( coalescing )
330 w->slots.resize( p );
332 f_count.value.store( std::int64_t( p ) , std::memory_order_relaxed );
336 [[nodiscard]] std::size_t
size(
void )
const {
return( f_worker.size() ); }
360 void send( std::size_t me , std::size_t to , Msg x ) {
362 Worker & w = *f_worker[ me ];
364 auto & o = w.out[ to ];
365 if( ! w.listed[ to ] ) {
367 w.dirty.push_back( to );
369 o.push_back( std::move( x ) );
370 if constexpr( coalescing )
373 .try_emplace(
typename Coalesce::KeyOf()( o.back() ) , kClosed )
375 if( ( o.size() >= w.next[ to ] ) && !
stopped() )
411 requires( coalescing && merges< F > )
412 void send( std::size_t me , std::size_t to , Msg x , F &&
merge ) {
414 Worker & w = *f_worker[ me ];
416 auto & o = w.out[ to ];
417 if( ! w.listed[ to ] ) {
419 w.dirty.push_back( to );
421 o.push_back( std::move( x ) );
422 const std::size_t i = o.size() - 1;
423 auto [ it , fresh ] =
424 w.slots[ to ].try_emplace(
typename Coalesce::KeyOf()( o.back() ) , i );
426 const std::size_t j = detail::mappedOf( it );
428 if( ( j != kClosed ) && absorb(
merge , o[ j ] , o.back() ) ) {
433 detail::mappedOf( it ) = i;
435 if( ( o.size() >= w.next[ to ] ) && !
stopped() )
466 Worker & w = *f_worker[ me ];
468 std::size_t keep = 0;
469 for( std::size_t i = 0 ; i < w.dirty.size() ; ++i ) {
470 const std::size_t to = w.dirty[ i ];
473 if( w.out[ to ].empty() )
476 w.dirty[ keep++ ] = to;
478 w.dirty.resize( keep );
501 std::size_t
poll( std::size_t me , F && f ) {
505 Worker & w = *f_worker[ me ];
506 const std::size_t k = f_ch.drain( me , [ & ]( Msg && x ) {
508 f_count.value.fetch_add( 1 , std::memory_order_acq_rel );
515 f_count.value.fetch_sub( std::int64_t( k ) , std::memory_order_acq_rel );
543 Worker & w = *f_worker[ me ];
548 if( f_count.value.fetch_sub( 1 , std::memory_order_acq_rel ) == 1 )
551 return( f_count.value.load( std::memory_order_acquire ) == 0 );
558 ( f_count.value.load( std::memory_order_acquire ) == 0 ) );
567 void stop(
void ) { f_stop.value.store(
true , std::memory_order_release ); }
571 return( f_stop.value.load( std::memory_order_acquire ) );
595 requires std::predicate< F & , std::size_t >
600 for( std::size_t me = 0 ; me <
size() ; ++me ) {
601 Worker & w = *f_worker[ me ];
602 w.active = bool( active( me ) );
605 f_count.value.store( n , std::memory_order_release );
606 f_stop.value.store(
false , std::memory_order_release );
612 rearm( []( std::size_t ) {
return(
true ); } );
634 requires( std::predicate< P & , std::size_t > || std::predicate< P & > )
635 void audit( [[maybe_unused]] P && pred ) {
639 if constexpr( std::predicate< P & , std::size_t > ) {
640 for( std::size_t me = 0 ; me <
size() ; ++me )
671 Worker & w = *f_worker[ me ];
673 for(
const std::size_t to : w.dirty ) {
674 auto & o = w.out[ to ];
679 if constexpr( coalescing )
680 w.slots[ to ].clear();
682 w.next[ to ] = f_batch;
685 return( n + f_ch.drain( me , [ & ]( Msg && x ) { f( std::move( x ) ); } ) );
694 for( std::size_t me = 0 ; me <
size() ; ++me )
700 [[nodiscard]] std::uint64_t
sent( std::size_t me )
const {
702 return( f_worker[ me ]->
sent );
707 [[nodiscard]] std::uint64_t
batches( std::size_t me )
const {
709 return( f_worker[ me ]->
batches );
714 [[nodiscard]] std::uint64_t
merged( std::size_t me )
const {
716 return( f_worker[ me ]->
merged );
720 [[nodiscard]] std::uint64_t
received( std::size_t me )
const {
726 [[nodiscard]] Ch &
channels(
void ) {
return( f_ch ); }
731 static constexpr std::size_t kClosed = std::size_t( -1 );
734 struct alignas( 128 ) Worker
736 std::vector< std::vector< Msg > > out;
737 std::vector< std::size_t > next;
738 std::vector< char > listed;
739 std::vector< std::size_t > dirty;
741 std::uint64_t sent = 0;
742 std::uint64_t batches = 0;
743 std::uint64_t received = 0;
744 std::uint64_t merged = 0;
751 struct alignas( 128 ) Counter
753 std::atomic< std::int64_t > value{ 0 };
757 struct alignas( 128 ) Flag
759 std::atomic< bool > value{
false };
764 [[nodiscard]]
bool rearmable(
void )
const {
765 for(
const auto & w : f_worker )
766 for(
const std::size_t to : w->dirty )
767 if( ! w->out[ to ].empty() )
770 ( f_count.value.load( std::memory_order_acquire ) == 0 ) );
775 [[nodiscard]]
bool drained(
void ) {
777 for( std::size_t me = 0 ; me <
size() ; ++me )
778 n += f_ch.drain( me , []( Msg && ) { } );
785 static bool absorb( F &
merge , Msg & p ,
const Msg & x ) {
786 if constexpr( std::is_void_v<
787 std::invoke_result_t< F & , Msg & , const Msg & > > ) {
792 return(
bool(
merge( p , x ) ) );
797 void push( std::size_t me , std::size_t to ) {
798 Worker & w = *f_worker[ me ];
799 auto & o = w.out[ to ];
800 const std::size_t k = o.size();
803 f_count.value.fetch_add( std::int64_t( k ) , std::memory_order_acq_rel );
804 const std::size_t j = f_ch.tryPush( me , to , std::span< const Msg >( o ) );
806 f_count.value.fetch_sub( std::int64_t( k - j ) ,
807 std::memory_order_acq_rel );
811 o.erase( o.begin() , o.begin() + std::ptrdiff_t( j ) );
812 if constexpr( coalescing )
813 if( ! w.slots[ to ].empty() )
814 w.slots[ to ].clear();
816 w.next[ to ] = o.size() + f_batch;
820 std::vector< std::unique_ptr< Worker > > f_worker;
The channels between the long-lived workers of an executor [see Executor.h], beyond the book: worker...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
std::size_t poll(std::size_t me, F &&f)
the calls f( x ) on the messages of the inbox of me, and their number; a passive worker that finds me...
Definition Mailbox.h:501
Co Coalescing
< the family of the channels
Definition Mailbox.h:314
std::uint64_t sent(std::size_t me) const
the messages that me has pushed so far
Definition Mailbox.h:700
bool terminated(void) const
true once the computation has ended or the mailbox has stopped (any thread may ask)
Definition Mailbox.h:556
Ch & channels(void)
the channels underneath
Definition Mailbox.h:726
void rearm(void)
rearm() above with all the workers active, as at the construction
Definition Mailbox.h:611
std::size_t size(void) const
the number of the workers
Definition Mailbox.h:336
Mailbox(std::size_t p, std::size_t batch=64, A &&... args)
< the policy of the coalescing
Definition Mailbox.h:320
std::uint64_t batches(std::size_t me) const
the batches that me has pushed so far, i.e., the pushes that the channels took at least one message o...
Definition Mailbox.h:707
void audit(P &&pred)
at the level of the audits, the predicate of the consumer checked at a quiescent point: pred( me ) fo...
Definition Mailbox.h:635
std::uint64_t received(std::size_t me) const
the messages that me has drained so far by poll()
Definition Mailbox.h:720
std::uint64_t merged(std::size_t me) const
the messages that me has merged so far into one pending, by send() with a merge; 0 with NoCoalescing
Definition Mailbox.h:714
Msg Message
Definition Mailbox.h:312
bool stopped(void) const
true once stop() has been called
Definition Mailbox.h:570
bool quiescent(std::size_t me)
true if the computation has ended or the mailbox has stopped; called by me with no work left,...
Definition Mailbox.h:539
void send(std::size_t me, std::size_t to, Msg x, F &&merge)
send( me , to , x ) with the coalescing of the policy: merged into the last message of its key in th...
Definition Mailbox.h:412
void send(std::size_t me, std::size_t to, Msg x)
appended to the outbox of me for to, which is pushed when it holds a batch more than a push left in i...
Definition Mailbox.h:360
void stop(void)
ends the computation at once, whatever the messages on their way: from then on quiescent() and termin...
Definition Mailbox.h:567
std::size_t leftovers(std::size_t me, F &&f)
the calls f( x ) on the messages left to me: those of its outboxes, for each destination in the order...
Definition Mailbox.h:669
bool flush(std::size_t me)
the outboxes of me pushed, true if all of them are empty after it: a bounded family may refuse some m...
Definition Mailbox.h:464
void rearm(F &&active)
the mailbox ready for the next computation, with the workers for which active( me ) is true active an...
Definition Mailbox.h:596
std::size_t leftovers(F &&f)
leftovers( me , f ) of every worker, in the order of the workers, and the number of all the messages
Definition Mailbox.h:692
Ch Channels
< the type of the messages
Definition Mailbox.h:313
a policy of the coalescing of Mailbox for the messages Msg: NoCoalescing, or CoalesceBy< K ,...
Definition Mailbox.h:263
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
#define ADS_INVARIANT(cond)
an invariant, checked from level 3
Definition Contract.h:127
#define ADS_AUDIT(cond)
a precondition or a postcondition dearer than the operation, checked from level 3
Definition Contract.h:131
#define ADS_CONTRACTS
the level of the contracts checked: 0 none, 1 preconditions, 2 postconditions, 3 invariants and audit...
Definition Contract.h:71
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
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
std::size_t ownerOf(const K &k, std::size_t p, const H &h=H())
the owner of the key among workers or processes, for the hash function
Definition Mailbox.h:174
SortedListSet< T > merge(const SortedListSet< T > &A, const SortedListSet< T > &B, bool a, bool b, bool both)
the merge of the sorted lists of and : an item of only, of only or of both goes into the result if...
the coalescing of the messages of one key in an outbox of Mailbox: K()( x ) the key of the message ,...
Definition Mailbox.h:198
M Map
< the function of the key
Definition Mailbox.h:200
K KeyOf
Definition Mailbox.h:199
no coalescing of the messages in the outboxes of Mailbox: no member and no code, and no send() with a...
Definition Mailbox.h:188