|
ADS
Algorithms and Data Structures
|
the mailbox of \( p \) workers on the channels Ch (by default the mesh, the family that DefaultChannels gives every executor), with outboxes of batches, the detection of the termination, the stop and the recovery of the messages left, and the coalescing of the messages of one key in an outbox by the policy Co More...
#include <ADS/parallel/Mailbox.h>
Public types | |
| using | Message = Msg |
| using | Channels = Ch |
| < the type of the messages | |
| using | Coalescing = Co |
| < the family of the channels | |
Public member functions | |
| template<class... A> | |
| Mailbox (std::size_t p, std::size_t batch=64, A &&... args) | |
| < the policy of the coalescing | |
| std::size_t | size (void) const |
| the number \( p \) of the workers | |
| void | send (std::size_t me, std::size_t to, Msg x) |
| \( x \) appended to the outbox of me for to, which is pushed when it holds a batch more than a push left in it; never waits, and after stop() only appends; with a policy that merges, the run of the key of \( x \) closed by one probe of the map, so that no later merge moves a message of that key ahead of \( x \) | |
| template<class F> requires ( coalescing && merges< F > ) | |
| void | send (std::size_t me, std::size_t to, Msg x, F &&merge) |
| send( me , to , x ) with the coalescing of the policy: \( x \) merged into the last message of its key in the outbox sent with a merge since the last push, if there is one, no message of its key was sent without a merge after it and merge( pending , x ) returns true (or nothing), appended otherwise; one probe of the map of the outbox | |
| 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 messages, which stay in their outbox for a later push, and the call never waits; after stop() nothing is pushed | |
| template<class F> | |
| 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 messages becomes active; after stop() nothing is drained, and the messages stay for leftovers() | |
| bool | quiescent (std::size_t me) |
| true if the computation has ended or the mailbox has stopped; called by me with no work left, it first pushes its outboxes and, if they are all empty, becomes passive; while it returns false, me must keep polling its inbox, which lets the others push what they retain | |
| bool | terminated (void) const |
| true once the computation has ended or the mailbox has stopped (any thread may ask) | |
| void | stop (void) |
| ends the computation at once, whatever the messages on their way: from then on quiescent() and terminated() are true for every worker, poll() drains nothing and send() and flush() push nothing, so that each message sent is either handed to \( f \) by a poll() or left for leftovers(), once; any thread may call it, any number of times (see The detection of the termination) | |
| bool | stopped (void) const |
| true once stop() has been called | |
| template<class F> requires std::predicate< F & , std::size_t > | |
| void | rearm (F &&active) |
| the mailbox ready for the next computation, with the workers for which active( me ) is true active and the others passive: the counter set to the number of the active ones, the stop withdrawn, the channels and the outboxes kept; called by one thread at a barrier, while no worker calls the mailbox | |
| void | rearm (void) |
| rearm() above with all the workers active, as at the construction | |
| template<class P> requires ( std::predicate< P & , std::size_t > || std::predicate< P & > ) | |
| void | audit (P &&pred) |
| at the level of the audits, the predicate of the consumer checked at a quiescent point: pred( me ) for each worker, if it takes one, or pred(), a violated audit if it is false; called by one thread at a barrier, while no worker calls the mailbox, under the precondition of rearm(), so that the predicate reads the state that the workers left with no message on its way; nothing below the level of the audits | |
| template<class F> | |
| 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 of their sends, then those of its inbox; their number. | |
| template<class F> | |
| 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 | |
| std::uint64_t | sent (std::size_t me) const |
| the messages that me has pushed so far | |
| 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 of | |
| 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 | |
| std::uint64_t | received (std::size_t me) const |
| the messages that me has drained so far by poll() | |
| Ch & | channels (void) |
| the channels underneath | |
the mailbox of \( p \) workers on the channels Ch (by default the mesh, the family that DefaultChannels gives every executor), with outboxes of batches, the detection of the termination, the stop and the recovery of the messages left, and the coalescing of the messages of one key in an outbox by the policy Co
| using ADS::Mailbox< Msg, Ch, Co >::Channels = Ch |
< the type of the messages
| using ADS::Mailbox< Msg, Ch, Co >::Coalescing = Co |
< the family of the channels
| using ADS::Mailbox< Msg, Ch, Co >::Message = Msg |
|
inlineexplicit |
< the policy of the coalescing
the mailbox of \( p \) workers, all active, pushing batches of batch messages, on channels built from \( p \) and args
|
inline |
at the level of the audits, the predicate of the consumer checked at a quiescent point: pred( me ) for each worker, if it takes one, or pred(), a violated audit if it is false; called by one thread at a barrier, while no worker calls the mailbox, under the precondition of rearm(), so that the predicate reads the state that the workers left with no message on its way; nothing below the level of the audits
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{audit}( \mathit{pred} ) \cr 2 & \quad \textbf{assert}\ \text{the outboxes and the inboxes are} \text{ empty, and } C = 0 \text{ or stopped} \cr 3 & \quad \textbf{foreach}\ \text{worker}\ \mathit{me} \ \textbf{do}\ \textbf{assert}\ \mathit{pred}( \mathit{me} ) \end{array}} \]
|
inlinenodiscard |
the batches that me has pushed so far, i.e., the pushes that the channels took at least one message of
|
inlinenodiscard |
the channels underneath
|
inline |
the outboxes of me pushed, true if all of them are empty after it: a bounded family may refuse some messages, which stay in their outbox for a later push, and the call never waits; after stop() nothing is pushed
In pseudocode (beyond the book), \( C \) the shared counter:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{flush}( \mathit{me} ) \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{push}( \mathit{me} , \mathit{to} ) \cr 3 & \quad \textbf{return}\ \text{all the outboxes are empty} \cr 4 & \textbf{procedure}\ \mathrm{push}( \mathit{me} , \mathit{to} ) \cr 5 & \quad k \gets |O_{\mathit{to}}|;\ C \gets C + k\quad \triangleright\ \text{before the messages can be drained} \cr 6 & \quad j \gets \mathrm{tryPush}( \mathit{me} , \mathit{to} , O_{\mathit{to}} );\ C \gets C - ( k - j ) \cr 7 & \quad \text{remove the first } j \text{ messages of } O_{\mathit{to}};\ N_{\mathit{to}} \gets |O_{\mathit{to}}| + \mathit{batch} \end{array}} \]
|
inline |
leftovers( me , f ) of every worker, in the order of the workers, and the number of all the messages
|
inline |
the calls f( x ) on the messages left to me: those of its outboxes, for each destination in the order of their sends, then those of its inbox; their number.
Called after the workers have returned from run() (or have all stopped calling the mailbox), by any thread, it empties both, so that with stop() every message sent has been handed to \( f \) exactly once, by a poll() or here; after a termination detected by quiescent() there is none
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{leftovers}( \mathit{me} , f ) \cr 2 & \quad \textbf{foreach}\ \mathit{to} \text{ with } O_{\mathit{to}} \ne \emptyset\ \textbf{do}\ \textbf{foreach}\ x \in O_{\mathit{to}} \ \textbf{do}\ f( x );\ O_{\mathit{to}} \gets \emptyset \cr 3 & \quad \textbf{return}\ \text{those and}\ \mathrm{drain}( \mathit{me} , f ) \end{array}} \]
|
inlinenodiscard |
the messages that me has merged so far into one pending, by send() with a merge; 0 with NoCoalescing
|
inline |
the calls f( x ) on the messages of the inbox of me, and their number; a passive worker that finds messages becomes active; after stop() nothing is drained, and the messages stay for leftovers()
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{poll}( \mathit{me} , f ) \cr 2 & \quad \textbf{if}\ \text{stopped}\ \textbf{then return}\ 0 \cr 3 & \quad k \gets \mathrm{drain}( \mathit{me} , x \mapsto \textbf{if} \ \textbf{not}\ \mathit{active}\ \textbf{then}\ C \gets C + 1; \ \mathit{active} \gets \textbf{true}; \cr & \qquad f( x ) ) \cr 4 & \quad \textbf{if}\ k > 0\ \textbf{then}\ C \gets C - k \cr 5 & \quad \textbf{return}\ k \end{array}} \]
|
inline |
true if the computation has ended or the mailbox has stopped; called by me with no work left, it first pushes its outboxes and, if they are all empty, becomes passive; while it returns false, me must keep polling its inbox, which lets the others push what they retain
In pseudocode (the deficit counting of Dijkstra and Scholten on a shared counter, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{quiescent}( \mathit{me} ) \cr 2 & \quad \textbf{if}\ \text{stopped}\ \textbf{then return true} \cr 3 & \quad \textbf{if}\ \mathit{active}\ \textbf{then} \cr 4 & \qquad \textbf{if}\ \textbf{not}\ \mathrm{flush}( \mathit{me} ) \ \textbf{then}\ \textbf{return false} \cr 5 & \qquad \mathit{active} \gets \textbf{false};\ C \gets C - 1 \cr 6 & \quad \textbf{return}\ C = 0 \end{array}} \]
|
inline |
the mailbox ready for the next computation, with the workers for which active( me ) is true active and the others passive: the counter set to the number of the active ones, the stop withdrawn, the channels and the outboxes kept; called by one thread at a barrier, while no worker calls the mailbox
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{rearm}( \mathit{active} ) \cr 2 & \quad \textbf{foreach}\ \text{worker}\ \mathit{me}\ \textbf{do} \ \mathit{active}_{\mathit{me}} \gets \mathit{active}( \mathit{me} ) \cr 3 & \quad C \gets |\{ \mathit{me} : \mathit{active}_{\mathit{me}} \}|;\ \text{stopped} \gets \textbf{false} \end{array}} \]
|
inline |
rearm() above with all the workers active, as at the construction
|
inlinenodiscard |
the messages that me has drained so far by poll()
|
inline |
\( x \) appended to the outbox of me for to, which is pushed when it holds a batch more than a push left in it; never waits, and after stop() only appends; with a policy that merges, the run of the key of \( x \) closed by one probe of the map, so that no later merge moves a message of that key ahead of \( x \)
In pseudocode (beyond the book), \( O_{\mathit{to}} \) the outbox and \( N_{\mathit{to}} \) its threshold, at first \( \mathit{batch} \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{send}( \mathit{me} , \mathit{to} , x ) \cr 2 & \quad \text{append } x \text{ to } O_{\mathit{to}} \cr 3 & \quad \textbf{if}\ \text{the policy merges}\ \textbf{then} \ S_{\mathit{to}}[ \kappa( x ) ] \gets \bot\quad \triangleright\ \text{the run of the key closed} \cr 4 & \quad \textbf{if}\ |O_{\mathit{to}}| \ge N_{\mathit{to}} \ \textbf{and not stopped then}\ \mathrm{push}( \mathit{me} , \mathit{to} ) \end{array}} \]
|
inline |
send( me , to , x ) with the coalescing of the policy: \( x \) merged into the last message of its key in the outbox sent with a merge since the last push, if there is one, no message of its key was sent without a merge after it and merge( pending , x ) returns true (or nothing), appended otherwise; one probe of the map of the outbox
In pseudocode (beyond the book), \( S_{\mathit{to}} \) the map of the outbox, from a key to a place in it or to \( \bot \) for a run closed by a plain send, and \( \kappa \) the key:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{send}( \mathit{me} , \mathit{to} , x , \mathit{merge} ) \cr 2 & \quad \text{append } x \text{ to } O_{\mathit{to}};\ i \gets |O_{\mathit{to}}| \cr 3 & \quad \textbf{if}\ S_{\mathit{to}}[ \kappa( x ) ] = j \ne \bot \ \textbf{then} \cr 4 & \qquad \textbf{if}\ \mathit{merge}( O_{\mathit{to}}[ j ] , x ) \ \textbf{then}\ \text{remove } x \text{ from } O_{\mathit{to}}; \ \mathit{merged} \gets \mathit{merged} + 1;\ \textbf{return} \cr 5 & \quad S_{\mathit{to}}[ \kappa( x ) ] \gets i\quad \triangleright\ \text{one probe for lines 3 and 5} \cr 6 & \quad \textbf{if}\ |O_{\mathit{to}}| \ge N_{\mathit{to}} \ \textbf{and not stopped then}\ \mathrm{push}( \mathit{me} , \mathit{to} ) \end{array}} \]
and \( \mathrm{push} \) empties \( S_{\mathit{to}} \) when the channel takes a message.
|
inlinenodiscard |
the messages that me has pushed so far
|
inlinenodiscard |
the number \( p \) of the workers
|
inline |
ends the computation at once, whatever the messages on their way: from then on quiescent() and terminated() are true for every worker, poll() drains nothing and send() and flush() push nothing, so that each message sent is either handed to \( f \) by a poll() or left for leftovers(), once; any thread may call it, any number of times (see The detection of the termination)
|
inlinenodiscard |
true once stop() has been called
|
inlinenodiscard |
true once the computation has ended or the mailbox has stopped (any thread may ask)