|
ADS
Algorithms and Data Structures
|
The priority queue of one worker that the other workers steal from, beyond the book, and the frontier of \( p \) such queues of a parallel best-first search or branch-and-bound with work stealing. More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/concurrent/Backoff.h"#include "ADS/memory/HugePageAllocator.h"#include "ADS/priority/DaryHeap.h"#include <atomic>#include <cstddef>#include <cstdint>#include <exception>#include <functional>#include <iterator>#include <memory>#include <new>#include <optional>#include <type_traits>#include <utility>#include <vector>#include "ADS/concurrent/impl/StealablePriorityQueue.tpp"Go to the source code of this file.
Classes | |
| class | ADS::StealablePriorityQueue< K, T, Less, Bd, BdLess > |
| the exact PRIORITY QUEUE of one owner, of which a thief takes about half of the pairs, offered by the owner at its next operation; with a bound per pair of the type Bd (none if void), ordered by BdLess, it also knows the least bound of the pairs that its owner holds More... | |
| class | ADS::StealingFrontier< K, T, Less, Bd, BdLess > |
| the frontier of a best-first search of \( p \) workers, a StealablePriorityQueue each, with the steals from random victims and the detection of the termination; with the bounds Bd of the pairs, if not void, ordered by BdLess More... | |
Namespaces | |
| namespace | ADS |
Macros | |
| #define | ADS_SPQ_INLINE ADS_ALWAYS_INLINE |
| a function of the common path of the owner, inlined even where the compiler would not | |
| #define | ADS_SPQ_NOINLINE ADS_NOINLINE |
| a function never inlined, the rare path out of the common one | |
The priority queue of one worker that the other workers steal from, beyond the book, and the frontier of \( p \) such queues of a parallel best-first search or branch-and-bound with work stealing.
Each queue holds pairs \( ( k , x ) \) of a key and an item, the least key first by less, and its owner pops them in the exact order of the keys of the pairs it holds, which a search by bounds needs for its limits on the nodes and for a run that the same steals make the same; a thread that has no work takes about half of the pairs of another queue (see Priority queues that are stolen). It is neither a relaxed queue shared by all [see MultiQueue.h] nor a deque of tasks in the order of their pushes [see WorkStealingDeque.h].
The pairs of a queue are kept in two 4-ary heaps of the owner [see DaryHeap.h], \( H_0 \) and \( H_1 \), which no other thread reads: a push goes into the smaller of the two (into each in turn when they are as large), and a pop takes the better of their two least pairs. The pairs pushed one after the other, e.g., the children of a node, are so dealt to the two heaps in turn, and each heap holds about half of the pairs of every range of keys, an interleaved half. A thief that finds no pair to claim raises a flag of the queue (hungry); the owner reads it, a load without order, at its next operation, and then moves the heap whose least pair is not the least of the queue, in \( O( 1 ) \), into the offer of the queue, an atomic pointer to it, which a thief claims by an exchange with null and makes its own. The owner keeps its least pair, and so its next pop; when the two heaps differ in size by more than a factor of 4 it first deals all the pairs again to two heaps of equal sizes, in \( O( m ) \) for \( m \) pairs. Until a thief claims the offer, its pairs still belong to the owner: a pop whose least local key is not less than the least key of the offer takes the offer back by the same exchange, so that whoever of the two exchanges first gets it all, and the owner never pops a pair while a better one it owns waits in the offer. The common case of the owner, a push or a pop with no thief hungry, costs therefore no read-modify-write and no fence, since no other thread can take a pair from its heaps (an owner that raced with the thieves for each pair, as in a deque of work stealing, would need one of the two at each operation, by the laws of order of Attiya et al.); a steal costs the thief one exchange and a store of the flag, and the owner a store of a pointer, or \( O( m ) \) when it deals the pairs again. The price is that a thief waits for the next operation of the owner, which serve() brings forward in a long computation of the owner. A steal moves one of the two interleaved heaps, about half of the pairs, and the pairs stolen keep their keys, their bounds and their items; drain( f ) takes back the offer that no thief has claimed and empties both heaps.
With a type Bd of bounds (none by default), each pair carries a bound besides its key, e.g., the key of selection of a branch-and-bound by depth first or by best estimate and the bound by which it prunes, and minBound() gives the least bound of the pairs that the owner holds, the selection by one key and the least of another of IndexedHeapPair [see IndexedHeapPair.h] on a queue that is stolen. Each heap keeps its least bound while no pop takes a pair of that bound, finds it again by a scan when one does, and, once the scans cost too much (the bounds that go with the keys), keeps for each node the least bound of its subtree, which a push lowers on its way up and a pop computes again on the paths it changes, so that the least bound is at the root; the offer carries the least bound of its pairs, which minBound() counts as long as no thief has claimed it, so that the value is exact at the moment it looks at the offer (see Priority queues that are stolen). A queue whose least bound cannot improve the incumbent is dropped whole by drain().
StealingFrontier holds the queues of \( p \) workers, one per call \( f( i ) \) of the run( p , f ) of an executor [see Executor.h], and their termination: pop( i ) returns the least pair of the queue of the worker \( i \) or, when it is empty, the least pair of what it steals from victims drawn at random, and nothing only when no worker holds a pair, or after stop(). A counter \( C \) shared by the workers holds the number of the workers that hold pairs or work on one, plus the thieves that are trying a steal: a worker leaves it when its pop() finds its queue and its offer empty, a thief enters it before the exchange that claims an offer and leaves it if the exchange fails, so that \( C = 0 \) only when no pair is left (see Priority queues that are stolen).
The keys and the items must be copyable, with moves that do not throw when there are bounds, whose copies do not throw either; a pair is moved or copied only by its owner of the moment. The contracts [see Contract.h]: one thread at a time, the owner, calls the operations of a queue but the steal() that names it as the victim; a thief steals into its own queue, empty; the worker \( i \) alone calls push( i , ... ), pop( i ), serve( i ), minBound( i ) and drain( i , f ) of a frontier, and pushes only between a pop( i ) that returned a pair and the next one (or before the run); the invariant of a queue is that of its two heaps, checked by consistent(). The operations are defined in StealablePriorityQueue.tpp.