ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Channels.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File Channels.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
60/*--------------------------------------------------------------------------*/
61/*----------------------------- DEFINITIONS --------------------------------*/
62/*--------------------------------------------------------------------------*/
63
64#ifndef __ADS_Channels
65 #define __ADS_Channels
66/* self-identification: #endif constant */
67
68/*--------------------------------------------------------------------------*/
69/*------------------------------ INCLUDES ----------------------------------*/
70/*--------------------------------------------------------------------------*/
71
72#include "ADS/Contract.h"
77
78#include <atomic>
79#include <concepts>
80#include <cstddef>
81#include <memory>
82#include <mutex>
83#include <span>
84#include <utility>
85#include <vector>
86
87/*--------------------------------------------------------------------------*/
88/*--------------------------- NAMESPACE ------------------------------------*/
89/*--------------------------------------------------------------------------*/
90
91namespace ADS
92{
93
98template< class C >
99concept ChannelFamily = requires( C & c , const C & k , std::size_t i ,
100 std::span< const typename C::Message > m ,
101 void ( &f )( typename C::Message && ) )
102{
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 >;
106 };
107
108/*--------------------------------------------------------------------------*/
109/*--------------------------- CLASS ChannelPort ----------------------------*/
110/*--------------------------------------------------------------------------*/
111
113template< class C >
115{
116 public:
118 ChannelPort( C & c , std::size_t i ) : f_c( &c ) , f_me( i ) { }
119
121 std::size_t tryPush( std::size_t to ,
122 std::span< const typename C::Message > m ) {
123 return( f_c->tryPush( f_me , to , m ) );
124 }
125
127 template< class F >
128 std::size_t drain( F && f ) {
129 return( f_c->drain( f_me , std::forward< F >( f ) ) );
130 }
131
133 [[nodiscard]] std::size_t index( void ) const { return( f_me ); }
134
135 private:
136 C * f_c;
137 std::size_t f_me;
138 }; // end( class ChannelPort )
139
140/*--------------------------------------------------------------------------*/
141/*-------------------------- CLASS LockedChannels --------------------------*/
142/*--------------------------------------------------------------------------*/
143
145template< class Msg , class Lock = std::mutex >
147{
148
149/*--------------------------------------------------------------------------*/
150 public:
151 using Message = Msg;
153 static constexpr bool bounded = false;
154
157 explicit LockedChannels( std::size_t p ) : f_box( p ) {
158 ADS_PRE( p >= 1 );
159 for( auto & b : f_box )
160 b = std::make_unique< Box >();
161 }
162
164 [[nodiscard]] std::size_t size( void ) const { return( f_box.size() ); }
165
181 std::size_t tryPush( std::size_t from , std::size_t to ,
182 std::span< const Msg > m ) {
183 ADS_PRE( ( from < size() ) && ( to < size() ) );
184 if( m.empty() )
185 return( 0 );
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 );
190 return( m.size() );
191 }
192
209 template< class F >
210 std::size_t drain( std::size_t me , F && f ) {
211 ADS_PRE( me < size() );
212 Box & b = *f_box[ me ];
213 if( b.count.load( std::memory_order_acquire ) == 0 )
214 return( 0 );
215 {
216 std::lock_guard< Lock > g( b.lock );
217 std::swap( b.items , b.taken );
218 b.count.store( 0 , std::memory_order_relaxed );
219 }
220 const std::size_t n = b.taken.size();
221 for( auto & x : b.taken )
222 f( std::move( x ) );
223 b.taken.clear();
224 return( n );
225 }
226
228 [[nodiscard]] ChannelPort< LockedChannels > port( std::size_t i ) {
229 return( ChannelPort< LockedChannels >( *this , i ) );
230 }
231
232/*--------------------------------------------------------------------------*/
233 private:
235 struct alignas( 128 ) Box
236 {
237 Lock lock;
238 std::vector< Msg > items;
239 std::atomic< std::size_t > count{ 0 };
240 std::vector< Msg > taken;
241 };
242
243 std::vector< std::unique_ptr< Box > > f_box;
244 }; // end( class LockedChannels )
245
246/*--------------------------------------------------------------------------*/
247/*-------------------------- CLASS InboxChannels ---------------------------*/
248/*--------------------------------------------------------------------------*/
249
251template< class Msg >
253{
254
255/*--------------------------------------------------------------------------*/
256 public:
257 using Message = Msg;
259 static constexpr bool bounded = false;
260
263 explicit InboxChannels( std::size_t p ) : f_box( p ) {
264 ADS_PRE( p >= 1 );
265 for( auto & b : f_box )
266 b = std::make_unique< Box >();
267 }
268
270 [[nodiscard]] std::size_t size( void ) const { return( f_box.size() ); }
271
286 std::size_t tryPush( std::size_t from , std::size_t to ,
287 std::span< const Msg > m ) {
288 ADS_PRE( ( from < size() ) && ( to < size() ) );
289 if( m.empty() )
290 return( 0 );
291 f_box[ to ]->inbox.push( std::vector< Msg >( m.begin() , m.end() ) );
292 return( m.size() );
293 }
294
310 template< class F >
311 std::size_t drain( std::size_t me , F && f ) {
312 ADS_PRE( me < size() );
313 auto & in = f_box[ me ]->inbox;
314 if( in.empty() )
315 return( 0 );
316 std::size_t n = 0;
317 for( auto & b : in.take() ) {
318 n += b.size();
319 for( auto & x : b )
320 f( std::move( x ) );
321 }
322 return( n );
323 }
324
326 [[nodiscard]] ChannelPort< InboxChannels > port( std::size_t i ) {
327 return( ChannelPort< InboxChannels >( *this , i ) );
328 }
329
330/*--------------------------------------------------------------------------*/
331 private:
333 struct alignas( 128 ) Box
334 {
336 };
337
338 std::vector< std::unique_ptr< Box > > f_box;
339 }; // end( class InboxChannels )
340
341/*--------------------------------------------------------------------------*/
342/*--------------------------- CLASS LaneChannels ---------------------------*/
343/*--------------------------------------------------------------------------*/
344
346template< class Msg , std::size_t B = 256 >
348{
349
350/*--------------------------------------------------------------------------*/
351 public:
352 using Message = Msg;
354 static constexpr bool bounded = false;
355
358 explicit LaneChannels( std::size_t p ) : f_box( p ) , f_lane( p * p ) {
359 ADS_PRE( p >= 1 );
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();
365 }
366
368 [[nodiscard]] std::size_t size( void ) const { return( f_box.size() ); }
369
385 std::size_t tryPush( std::size_t from , std::size_t to ,
386 std::span< const Msg > m ) {
387 ADS_PRE( ( from < size() ) && ( to < size() ) );
388 auto & l = f_lane[ from * size() + to ];
389 for( const auto & x : m )
390 l.push( x );
391 return( m.size() );
392 }
393
412 template< class F >
413 std::size_t drain( std::size_t me , F && f ) {
414 ADS_PRE( me < size() );
415 Box & b = *f_box[ me ];
416 std::size_t n = 0;
417 for( ; ; ) {
418 const std::size_t k = b.queue.popInto( b.buffer , B );
419 if( k == 0 )
420 return( n );
421 n += k;
422 for( std::size_t j = 0 ; j < k ; ++j )
423 f( std::move( b.buffer[ j ] ) );
424 }
425 }
426
428 [[nodiscard]] ChannelPort< LaneChannels > port( std::size_t i ) {
429 return( ChannelPort< LaneChannels >( *this , i ) );
430 }
431
432/*--------------------------------------------------------------------------*/
433 private:
435 struct alignas( 128 ) Box
436 {
438 Msg buffer[ B ];
439 };
440
441 std::vector< std::unique_ptr< Box > > f_box;
443 std::vector< typename MPSCQueue< Msg , B >::Producer > f_lane;
444 }; // end( class LaneChannels )
445
446/*--------------------------------------------------------------------------*/
447/*--------------------------- CLASS MeshChannels ---------------------------*/
448/*--------------------------------------------------------------------------*/
449
452template< class Msg >
454{
455
456/*--------------------------------------------------------------------------*/
457 public:
458 using Message = Msg;
460 static constexpr bool bounded = true;
461
465 explicit MeshChannels( std::size_t p , std::size_t c = 1024 )
466 : f_q( p * p ) , f_start( p ) {
467 ADS_PRE( p >= 1 );
468 for( auto & q : f_q )
469 q = std::make_unique< SPSCQueue< Msg > >( c );
470 }
471
473 [[nodiscard]] std::size_t size( void ) const { return( f_start.size() ); }
474
489 std::size_t tryPush( std::size_t from , std::size_t to ,
490 std::span< const Msg > m ) {
491 ADS_PRE( ( from < size() ) && ( to < size() ) );
492 if( m.empty() )
493 return( 0 );
494 return( f_q[ to * size() + from ]->pushMany( m.begin() , m.size() ) );
495 }
496
512 template< class F >
513 std::size_t drain( std::size_t me , F && f ) {
514 ADS_PRE( me < size() );
515 const std::size_t p = size();
516 std::size_t & s = f_start[ me ].s;
517 std::size_t n = 0;
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 );
522 return( n );
523 }
524
526 [[nodiscard]] ChannelPort< MeshChannels > port( std::size_t i ) {
527 return( ChannelPort< MeshChannels >( *this , i ) );
528 }
529
530/*--------------------------------------------------------------------------*/
531 private:
533 struct alignas( 64 ) Start
534 {
535 std::size_t s = 0;
536 };
537
539 std::vector< std::unique_ptr< SPSCQueue< Msg > > > f_q;
540 std::vector< Start > f_start;
541 }; // end( class MeshChannels )
542
543/*--------------------------------------------------------------------------*/
544/*--------------------------- DEFAULT CHANNELS -----------------------------*/
545/*--------------------------------------------------------------------------*/
546
552template< class E >
554{
556 template< class Msg >
558 };
559
561template< class Msg , class E >
562using DefaultChannels = typename ChannelsOf< E >::template type< Msg >;
563
564/*--------------------------------------------------------------------------*/
565
566} // end( namespace ADS )
567
568/*--------------------------------------------------------------------------*/
569
570#endif /* Channels.h included */
571
572/*--------------------------------------------------------------------------*/
573/*------------------------- End File Channels.h ----------------------------*/
574/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96
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