65 #define __ADS_Channels
100 std::span< const typename C::Message > m ,
101 void ( &f )(
typename C::Message && ) )
103 { k.size() } -> std::convertible_to< std::size_t >;
104 { c.tryPush( i , i , m ) } -> std::convertible_to< std::size_t >;
105 { c.drain( i , f ) } -> std::convertible_to< std::size_t >;
122 std::span< const typename C::Message > m ) {
123 return( f_c->tryPush( f_me , to , m ) );
129 return( f_c->drain( f_me , std::forward< F >( f ) ) );
133 [[nodiscard]] std::size_t
index(
void )
const {
return( f_me ); }
145template<
class Msg ,
class Lock = std::mutex >
159 for(
auto & b : f_box )
160 b = std::make_unique< Box >();
164 [[nodiscard]] std::size_t
size(
void )
const {
return( f_box.size() ); }
181 std::size_t
tryPush( std::size_t from , std::size_t to ,
182 std::span< const Msg > m ) {
186 Box & b = *f_box[ to ];
187 std::lock_guard< Lock > g( b.lock );
188 b.items.insert( b.items.end() , m.begin() , m.end() );
189 b.count.store( b.items.size() , std::memory_order_release );
210 std::size_t
drain( std::size_t me , F && f ) {
212 Box & b = *f_box[ me ];
213 if( b.count.load( std::memory_order_acquire ) == 0 )
216 std::lock_guard< Lock > g( b.lock );
217 std::swap( b.items , b.taken );
218 b.count.store( 0 , std::memory_order_relaxed );
220 const std::size_t n = b.taken.size();
221 for(
auto & x : b.taken )
235 struct alignas( 128 ) Box
238 std::vector< Msg > items;
239 std::atomic< std::size_t > count{ 0 };
240 std::vector< Msg > taken;
243 std::vector< std::unique_ptr< Box > > f_box;
265 for(
auto & b : f_box )
266 b = std::make_unique< Box >();
270 [[nodiscard]] std::size_t
size(
void )
const {
return( f_box.size() ); }
286 std::size_t
tryPush( std::size_t from , std::size_t to ,
287 std::span< const Msg > m ) {
291 f_box[ to ]->inbox.push( std::vector< Msg >( m.begin() , m.end() ) );
311 std::size_t
drain( std::size_t me , F && f ) {
313 auto & in = f_box[ me ]->inbox;
317 for(
auto & b : in.take() ) {
333 struct alignas( 128 ) Box
338 std::vector< std::unique_ptr< Box > > f_box;
346template<
class Msg , std::
size_t B = 256 >
358 explicit LaneChannels( std::size_t p ) : f_box( p ) , f_lane( p * p ) {
360 for(
auto & b : f_box )
361 b = std::make_unique< Box >();
362 for( std::size_t to = 0 ; to < p ; ++to )
363 for( std::size_t from = 0 ; from < p ; ++from )
364 f_lane[ from * p + to ] = f_box[ to ]->queue.producer();
368 [[nodiscard]] std::size_t
size(
void )
const {
return( f_box.size() ); }
385 std::size_t
tryPush( std::size_t from , std::size_t to ,
386 std::span< const Msg > m ) {
388 auto & l = f_lane[ from *
size() + to ];
389 for(
const auto & x : m )
413 std::size_t
drain( std::size_t me , F && f ) {
415 Box & b = *f_box[ me ];
418 const std::size_t k = b.queue.popInto( b.buffer , B );
422 for( std::size_t j = 0 ; j < k ; ++j )
423 f( std::move( b.buffer[ j ] ) );
435 struct alignas( 128 ) Box
441 std::vector< std::unique_ptr< Box > > f_box;
443 std::vector< typename MPSCQueue< Msg , B >::Producer > f_lane;
466 : f_q( p * p ) , f_start( p ) {
468 for(
auto & q : f_q )
469 q = std::make_unique< SPSCQueue< Msg > >( c );
473 [[nodiscard]] std::size_t
size(
void )
const {
return( f_start.size() ); }
489 std::size_t
tryPush( std::size_t from , std::size_t to ,
490 std::span< const Msg > m ) {
494 return( f_q[ to *
size() + from ]->pushMany( m.begin() , m.size() ) );
513 std::size_t
drain( std::size_t me , F && f ) {
515 const std::size_t p =
size();
516 std::size_t & s = f_start[ me ].s;
518 for( std::size_t j = 0 , from = s ; j < p ;
519 ++j , from = ( from + 1 == p ? 0 : from + 1 ) )
520 n += f_q[ me * p + from ]->consume( f );
521 s = ( s + 1 == p ? 0 : s + 1 );
533 struct alignas( 64 ) Start
539 std::vector< std::unique_ptr< SPSCQueue< Msg > > > f_q;
540 std::vector< Start > f_start;
556 template<
class Msg >
561template<
class Msg ,
class E >
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
An inbox that many producer threads fill and one consumer thread empties, lock-free,...
A queue that many producer threads fill and one consumer thread empties, lock-free and unbounded,...
A bounded queue between one producer thread and one consumer thread, lock-free and wait-free,...
the channels of a family seen by one worker
Definition Channels.h:115
std::size_t drain(F &&f)
the calls f( x ) on the messages of the inbox of the worker
Definition Channels.h:128
std::size_t tryPush(std::size_t to, std::span< const typename C::Message > m)
the messages of pushed to the inbox of to, as many as fit
Definition Channels.h:121
ChannelPort(C &c, std::size_t i)
the port of the worker on the channels
Definition Channels.h:118
std::size_t index(void) const
the worker of the port
Definition Channels.h:133
Msg Message
the type of the messages
Definition Channels.h:257
ChannelPort< InboxChannels > port(std::size_t i)
the channels seen by the worker
Definition Channels.h:326
InboxChannels(std::size_t p)
the inboxes of workers
Definition Channels.h:263
std::size_t size(void) const
the number of the workers
Definition Channels.h:270
std::size_t drain(std::size_t me, F &&f)
the calls f( x ) on the messages of the inbox of me, the batches of each sender in the order of their...
Definition Channels.h:311
std::size_t tryPush(std::size_t from, std::size_t to, std::span< const Msg > m)
the messages of pushed to the inbox of to as one batch, all of them
Definition Channels.h:286
static constexpr bool bounded
whether a push may push fewer messages than given
Definition Channels.h:259
ChannelPort< LaneChannels > port(std::size_t i)
the channels seen by the worker
Definition Channels.h:428
static constexpr bool bounded
whether a push may push fewer messages than given
Definition Channels.h:354
std::size_t drain(std::size_t me, F &&f)
the calls f( x ) on the messages of the inbox of me, lane by lane, and their number
Definition Channels.h:413
std::size_t size(void) const
the number of the workers
Definition Channels.h:368
std::size_t tryPush(std::size_t from, std::size_t to, std::span< const Msg > m)
the messages of pushed to the lane of from in the inbox of to, all of them
Definition Channels.h:385
LaneChannels(std::size_t p)
the inboxes of workers, with the lanes of each
Definition Channels.h:358
Msg Message
the type of the messages
Definition Channels.h:352
LockedChannels(std::size_t p)
the inboxes of workers
Definition Channels.h:157
std::size_t drain(std::size_t me, F &&f)
the calls f( x ) on the messages of the inbox of me, in the order of their pushes,...
Definition Channels.h:210
ChannelPort< LockedChannels > port(std::size_t i)
the channels seen by the worker
Definition Channels.h:228
Msg Message
the type of the messages
Definition Channels.h:151
std::size_t tryPush(std::size_t from, std::size_t to, std::span< const Msg > m)
the messages of appended to the inbox of to under its lock, all of them
Definition Channels.h:181
std::size_t size(void) const
the number of the workers
Definition Channels.h:164
static constexpr bool bounded
whether a push may push fewer messages than given
Definition Channels.h:153
an inbox filled by many threads and emptied by one, lock-free
Definition MPSCInbox.h:92
an unbounded queue from many producers to one consumer, a lane each
Definition MPSCQueue.h:99
the bounded queues of the pairs of workers [see SPSCQueue.h]
Definition Channels.h:454
Msg Message
the type of the messages
Definition Channels.h:458
static constexpr bool bounded
whether a push may push fewer messages than given
Definition Channels.h:460
std::size_t tryPush(std::size_t from, std::size_t to, std::span< const Msg > m)
the first messages of that fit in the queue from from to to, pushed by one store of its tail,...
Definition Channels.h:489
MeshChannels(std::size_t p, std::size_t c=1024)
the queues of workers, of capacity each
Definition Channels.h:465
ChannelPort< MeshChannels > port(std::size_t i)
the channels seen by the worker
Definition Channels.h:526
std::size_t size(void) const
the number of the workers
Definition Channels.h:473
std::size_t drain(std::size_t me, F &&f)
the calls f( x ) on the messages of the queues to me, from a sender that turns at each call,...
Definition Channels.h:513
the families of channels among workers: tryPush( from , to ,m ) pushes a span of messages and return...
Definition Channels.h:99
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
typename ChannelsOf< E >::template type< Msg > DefaultChannels
the family of channels to use on the executor E
Definition Channels.h:562
the family of channels of an executor, as measured by bench/runtime.cpp (see Channels between workers...
Definition Channels.h:554
MeshChannels< Msg > type
the family for the messages Msg
Definition Channels.h:557