|
ADS
Algorithms and Data Structures
|
The mailbox of the long-lived workers of an executor [see Executor.h], beyond the book, on any family of channels [see Channels.h], with the detection of the termination: a computation of \( p \) workers that create work for each other by messages (the nodes of a search sent to the worker that owns them, the vertices of a graph to the worker of their part) ends when no worker has work and no message is on its way, a state that no worker sees by itself (see The detection of the termination). More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/parallel/Channels.h"#include <atomic>#include <concepts>#include <cstddef>#include <cstdint>#include <functional>#include <memory>#include <span>#include <type_traits>#include <unordered_map>#include <utility>#include <vector>Go to the source code of this file.
Classes | |
| struct | ADS::NoCoalescing |
| no coalescing of the messages in the outboxes of Mailbox: no member and no code, and no send() with a merge; the default More... | |
| struct | ADS::CoalesceBy< K, M > |
| the coalescing of the messages of one key in an outbox of Mailbox: K()( x ) the key of the message \( x \), and M the map from a key to the place of its last message in an outbox (try_emplace(), clear() and empty() as those of std::unordered_map, or of DenseHashMap and FlatHashTable, which allocate no node per key), by default (void) a std::unordered_map of the keys to std::size_t More... | |
| class | ADS::Mailbox< Msg, Ch, Co > |
| 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... | |
Namespaces | |
| namespace | ADS |
Concepts | |
| concept | ADS::MailboxCoalescing |
| a policy of the coalescing of Mailbox for the messages Msg: NoCoalescing, or CoalesceBy< K , M > with K a function object made by default that gives the key of a message, and a map from the key to a place of std::size_t by try_emplace(), clear() and empty() | |
Functions | |
| template<class K, class H = std::hash< K >> | |
| std::size_t | ADS::ownerOf (const K &k, std::size_t p, const H &h=H()) |
| the owner of the key \( k \) among \( p \) workers or processes, \( h( k ) \bmod p \) for the hash function \( h \) | |
The mailbox of the long-lived workers of an executor [see Executor.h], beyond the book, on any family of channels [see Channels.h], with the detection of the termination: a computation of \( p \) workers that create work for each other by messages (the nodes of a search sent to the worker that owns them, the vertices of a graph to the worker of their part) ends when no worker has work and no message is on its way, a state that no worker sees by itself (see The detection of the termination).
A worker me sends by send( me , to , x ), which appends \( x \) to its outbox for to and pushes the outbox as a batch when it holds batch messages; flush( me ) pushes all its outboxes; poll( me , f ) drains its inbox, calling f( x ) on each message; quiescent( me ), called by a worker that has no work left, flushes its outboxes and tells whether the whole computation has ended. ownerOf( k , p ) is the worker that owns the key \( k \) when the keys are spread by hashing.
No operation waits for another worker. A bounded family (the mesh) may take only part of a batch, or none of it, when the queue of the pair is full: the rest stays in the outbox, send() tries it again only when a batch more has come for that destination (so that a full queue costs a push per batch and not per message), and flush() tries all the outboxes and returns false while something is left in them; sent() counts the messages pushed and batches() the pushes that the channels took. A worker whose quiescent() returns false must keep polling its inbox, which empties the queues that the others are waiting to push into.
stop(), from any thread, ends the computation at once, whatever the messages on their way (a limit on the work, or a bound that closes the search): from then on quiescent() and terminated() are true for every worker, poll() drains nothing, send() only appends to the outbox and flush() pushes nothing. Once the workers have returned from run(), leftovers( me , f ) hands to \( f \) the messages left to me, those of its outboxes and those of its inbox, and leftovers( f ) those of all; every message sent is then handed to a function exactly once, by a poll() or by leftovers(), also when stop() races with the sends (see The detection of the termination), so that the nodes of a search stopped by a limit can go back to its frontier, where they bound the optimum.
A mailbox serves a sequence of computations separated by barriers, e.g., the phases of an episode of an incremental search, or its episodes: rearm() makes it ready for the next one once the last has ended (or has been stopped and its messages recovered by leftovers()), all the workers active again, or those that active( me ) tells, so that the channels and the outboxes, \( p^2 \) queues in the mesh, are made once. The barrier is the caller's: rearm() runs in one thread while no worker calls the mailbox, after every worker has returned from its last call and before any makes its first of the next computation (in the completion function of a std::barrier, or between two run() of the executor), which orders its writes with the calls of all the workers. The counts of sent(), batches(), merged() and received() go on across the computations.
A mailbox of the policy CoalesceBy< K , M > (the third parameter, NoCoalescing by default, which adds no member and no code) merges in the outbox the messages that carry the same key: send( me , to , x , merge ) looks up the key K()( x ) in a map M of the worker for the destination, from the key to the place in the outbox of its last message sent so, and if there is one calls merge( pending , x ), which merges \( x \) into it (e.g., keeps the least of two distances) and returns true, or returns false and leaves both, \( x \) then becoming the last of its key; a merge with no result always merges. One probe of the map per send, which is emptied at each push that the channels take some of the outbox from, so that no place in it outlives a shift of the outbox. A merge removes a message before it is counted, so that the detection of the termination, the stop and leftovers() stay as they are, with each message sent either handed once or merged into one that is (see The detection of the termination); merged() counts the messages merged. A message sent by send( me , to , x ) closes the run of its key, as a refused merge does: the map marks the key closed, by the same one probe, so that a later merge appends after it and never moves a message of its key ahead of it (e.g., an invalidation between two relaxations of a state of a search), and the later messages of the key merge into the first of them; with NoCoalescing the plain send has no probe and the same instructions as before.
At the level of the audits of Contract.h, audit( pred ), called by one thread where rearm() may be called (the computation ended, or stopped and its messages recovered by leftovers(), and no worker calling the mailbox), checks the predicate of the consumer on the state that the workers have left, pred() once or pred( me ) for each worker, e.g., the comparison of an incremental state with its recomputation from scratch by Shadow of Modification.h; a predicate false is a violated audit. The barrier orders every write of the workers before the call, so that the predicate reads the state of all of them exactly; below the level of the audits the call costs nothing.
The termination is detected by counting: a counter \( C \) shared by the workers holds the number of the workers active (all of them at the start) plus the number of the messages pushed and not yet drained. A worker adds a batch of \( k \) messages to \( C \) before it pushes it, takes back an activity (adds 1) when it drains messages while passive, before it calls \( f \) on the first, subtracts the \( k \) messages it drained once it has called \( f \) on them, and subtracts its activity when it becomes passive, i.e., when it calls quiescent() with all its outboxes pushed. A message is counted from before its push to after its call, and a worker that sends is active, so that \( C = 0 \) only when no worker is active and no message exists, a state from which nothing leaves \( C \) at 0: the deficit counting of Dijkstra and Scholten, on a counter in the shared memory instead of the acknowledgments of each message. A batch costs one atomic addition at the sender and one at the receiver, so that the counter, shared by all, is touched once per batch and not per message; quiescent() of a passive worker only reads it.
The contracts [see Contract.h]: \( p \ge 1 \), a batch of at least 1; only the worker me calls the operations with me; a worker sends only while active, i.e., before its first quiescent() or after a poll() that found messages, and becomes active only by receiving (its own work comes from the messages, or it was there from the start), or after stop(); leftovers() is called when no worker calls the mailbox any more; f does not throw; a mailbox serves one computation between two calls of rearm(), which needs the last one ended or stopped, the outboxes empty and, after a stop, the inboxes drained by leftovers() (an audit, which drains them), and so does audit(); a merge does not throw, and changes neither the key of the message pending nor the mailbox.