|
ADS
Algorithms and Data Structures
|
the access of one thread to a MultiQueue: its draws and the two queues it keeps More...
#include <ADS/concurrent/MultiQueue.h>
Public member functions | |
| void | push (K k, T x) |
| puts the pair \( ( k , x ) \) into one of the two queues kept, or into the first free one of other draws; the key by value, as the item, since a key by reference makes the caller write it on its stack at each call, where it may share a line of cache with the data that the other threads of a parallel loop read at each operation | |
| template<class It> | |
| void | pushAll (It first, It last) |
| puts the pairs of \( [ \mathit{first} , \mathit{last} ) \), which convert to Entry, all into one of the two queues kept, under one lock, as push() does with one pair: a thread that makes several pairs at once (e.g., the successors of a node in a parallel search) takes one lock instead of one per pair, and the rank of a pop is relaxed as by a stickiness of the number of the pairs | |
| std::optional< Entry > | tryPop (void) |
| the least pair of the better of the two queues kept, or of the first draw of two queues whose better one is free; nothing if a scan of all the queues found them empty | |
Friends | |
| class | MultiQueue |
the access of one thread to a MultiQueue: its draws and the two queues it keeps
| void ADS::MultiQueue< K, T, Less, H >::Handle::push | ( | K | k, |
| T | x ) |
puts the pair \( ( k , x ) \) into one of the two queues kept, or into the first free one of other draws; the key by value, as the item, since a key by reference makes the caller write it on its stack at each call, where it may share a line of cache with the data that the other threads of a parallel loop read at each operation
In pseudocode (Rihani, Sanders and Dementiev, with the stickiness and the buffers of Williams, Sanders and Dementiev, beyond the book), \( D_j \) and \( I_j \) the buffers of the queue \( j \) and \( H_j \) its heap, \( b = 16 \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{push}( k , x ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad \textbf{if}\ \text{the two queues kept were used } s \text{ times}\ \textbf{then}\ \mathrm{draw}() \cr 4 & \qquad j \gets \text{one of the two at random} \cr 5 & \qquad \textbf{if}\ \mathrm{tryLock}( j )\ \textbf{then break} \cr 6 & \qquad \mathrm{draw}()\quad \triangleright\ \text{two random} \text{ queues, used } 0 \text{ times} \cr 7 & \quad \textbf{if}\ D_j \ne \emptyset\ \textbf{and}\ k \le \max D_j \ \textbf{then} \cr 8 & \qquad \textbf{if}\ |D_j| = b\ \textbf{then}\ \mathrm{add}( j , \max D_j )\text{, removed from } D_j \cr 9 & \qquad \text{insert } ( k , x ) \text{ into } D_j \text{, sorted} \cr 10 & \quad \textbf{else if}\ |D_j| < b\ \textbf{and}\ I_j = \emptyset \ \textbf{and}\ H_j = \emptyset\ \textbf{then}\ \text{insert } ( k , x ) \text{ as the greatest of } D_j \cr 11 & \quad \textbf{else}\ \mathrm{add}( j , ( k , x ) ) \cr 12 & \quad \mathit{top}_j \gets \text{the key of } \min D_j; \ \mathrm{unlock}( j )\quad \triangleright\ \text{release} \cr 13 & \textbf{procedure}\ \mathrm{add}( j , e ) \cr 14 & \quad \textbf{if}\ |I_j| < b\ \textbf{then}\ \text{add } e \text{ to } I_j \cr 15 & \quad \textbf{else}\ \text{push the pairs of } I_j \text{ and } e \text{ into } H_j;\ I_j \gets \emptyset \end{array}} \]
| void ADS::MultiQueue< K, T, Less, H >::Handle::pushAll | ( | It | first, |
| It | last ) |
puts the pairs of \( [ \mathit{first} , \mathit{last} ) \), which convert to Entry, all into one of the two queues kept, under one lock, as push() does with one pair: a thread that makes several pairs at once (e.g., the successors of a node in a parallel search) takes one lock instead of one per pair, and the rank of a pop is relaxed as by a stickiness of the number of the pairs
| std::optional< Entry > ADS::MultiQueue< K, T, Less, H >::Handle::tryPop | ( | void | ) |
the least pair of the better of the two queues kept, or of the first draw of two queues whose better one is free; nothing if a scan of all the queues found them empty
In pseudocode (beyond the book), with the notation of push() above:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{tryPop}() \cr 2 & \quad \textbf{loop} \cr 3 & \qquad \textbf{if}\ \text{the two queues kept were used } s \text{ times}\ \textbf{then}\ \mathrm{draw}() \cr 4 & \qquad j \gets \text{the one of the two with the least } \mathit{top}\quad \triangleright\ \text{read without locks} \cr 5 & \qquad \textbf{if}\ \mathit{top}_j = \infty\ \textbf{then return} \ \mathrm{scan}() \cr 6 & \qquad \textbf{if}\ \mathrm{tryLock}( j )\ \textbf{then break} \cr 7 & \qquad \mathrm{draw}() \cr 8 & \quad \textbf{if}\ D_j = \emptyset\ \textbf{then}\ \mathrm{unlock}( j );\ \textbf{return}\ \mathrm{scan}() \cr 9 & \quad e \gets \text{the least pair of } D_j \text{, removed} \cr 10 & \quad \textbf{if}\ D_j = \emptyset\ \textbf{then} \cr 11 & \qquad \text{push the pairs of } I_j \text{ into } H_j; \ I_j \gets \emptyset \cr 12 & \qquad \text{move the } \min( b , |H_j| ) \text{ least pairs of } H_j \text{ into } D_j \cr 13 & \quad \mathit{top}_j \gets \text{the key of } \min D_j \text{, or } \infty;\ \mathrm{unlock}( j );\ \textbf{return}\ e \cr 14 & \textbf{function}\ \mathrm{scan}() \cr 15 & \quad \textbf{foreach}\ \text{queue } j \text{, from a random one} \ \textbf{do} \cr 16 & \qquad \mathrm{lock}( j ) \cr 17 & \qquad \textbf{if}\ D_j \ne \emptyset\ \textbf{then}\ \text{take} \text{ its least pair as above};\ \textbf{return}\ \text{it} \cr 18 & \qquad \mathrm{unlock}( j ) \cr 19 & \quad \textbf{return nil} \end{array}} \]
|
friend |