ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MultiQueue.h File Reference

A relaxed priority queue shared by threads, beyond the book: the MultiQueue of Rihani, Sanders and Dementiev, with the stickiness and the buffers of Williams, Sanders and Dementiev. More...

#include "ADS/Capabilities.h"
#include "ADS/Contract.h"
#include "ADS/priority/DaryHeap.h"
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <optional>
#include <type_traits>
#include <utility>
#include "ADS/concurrent/impl/MultiQueue.tpp"

Go to the source code of this file.

Classes

class  ADS::MultiQueue< K, T, Less, H >
 a relaxed PRIORITY QUEUE shared by threads: \( n \) queues under locks, the pop from the better of two, each queue an H behind two buffers More...
struct  ADS::MultiQueue< K, T, Less, H >::Entry
 a pair of the queue, its key and its item More...
class  ADS::MultiQueue< K, T, Less, H >::Handle
 the access of one thread to a MultiQueue: its draws and the two queues it keeps More...

Namespaces

namespace  ADS

Typedefs

template<class E, class C>
using ADS::MultiQueueHeap = DaryHeap< E , C , 8 >
 the inner queue of a MultiQueue by default, the 8-ary heap

Detailed description

A relaxed priority queue shared by threads, beyond the book: the MultiQueue of Rihani, Sanders and Dementiev, with the stickiness and the buffers of Williams, Sanders and Dementiev.

It is meant as the shared OPEN of a parallel best-first search, i.e., of pairs \( ( k , x ) \) of a key and an item, the integer or real key \( k \) deciding the order by less (the least first, as \( f \) in a search).

The \( n \) queues are sequential priority queues, each with a lock of its own and with the key of its minimum kept apart in an atomic word, which the other threads read without the lock. A push puts the pair in a random queue whose lock it takes at the first try, and draws another one when the lock is held. A pop reads the minima of two distinct random queues, locks the queue with the smaller one, if free (draws two other queues otherwise), and takes its minimum. The pop therefore returns an item that is not always the least of all: its rank error is the number of the pairs present with a smaller key. With \( n \) queues in the sequential model (the pairs spread at random over the queues, then removed by the better of two distinct queues drawn at random), the expected rank error converges to

\[ \sum_{i=1}^{n-1} \sigma_{\le i} \, \frac{n - i - 1}{i} , \qquad \sigma_{\le i} = 1 - \frac{( n - i ) ( n - i - 1 )} {n ( n - 1 )} , \]

a little less than the \( \frac{5}{6} n - 1 + \frac{1}{6 n} \) of two draws with replacement, and its maximum over \( \mathrm{poly}( n ) \) pops is \( O( n \log n ) \) with high probability (Walzer and Williams; the bounds \( O( n ) \) and \( O( n \log n ) \) are those of Alistarh et al.), while a pop from a single random queue makes it grow without bound [see Relaxed priority queues]. A run of \( p \) threads with \( n = 2 p \) queues keeps the lock of a queue rarely contended; with \( n = 1 \) the queue is exact, a priority queue under one lock, for testing and for comparison.

Two devices of Williams, Sanders and Dementiev save most of the misses of the cache. A thread keeps the two queues it drew for \( s \) operations (the stickiness), so that their lines stay in its cache, and draws two others after \( s \) operations or when a lock is held; a push goes to one of the two at random, a pop to the better one. Each queue is an 8-ary heap [see DaryHeap.h] behind two buffers of 16 pairs: a deletion buffer \( D \), sorted, holding the least pairs of the queue, from which a pop takes its pair, and an insertion buffer \( I \), unsorted, which a push fills and which goes into the heap only when full or when \( D \) runs out; a pair with a key less than the greatest of \( D \) goes into \( D \), whose greatest pair goes into \( I \). The invariant of a queue is that \( D \) is empty only if the queue is, and that no pair of \( I \) or of the heap is less than one of \( D \): each queue stays exact, the least of its pairs being the least of \( D \), and the heap is touched once per 16 operations or so. The heap is the template H, given the type of the pairs and their order: by default the 8-ary heap, and for the keys of packFG(), the pairs \( ( f , g ) \) of a best-first search with integer costs, the BucketHeap of BucketQueue.h, whose pushes and pops take \( O( 1 ) \) amortized time against the \( O( \log n ) \) of the heap (cf. the MultiQueue of bucket queues of Zhang, Posluns and Jeffrey, whose buckets are shared by the threads, where here each queue has its own under its lock). The stickiness has no theory yet: the measures of Williams, Sanders and Dementiev show rank errors that grow about linearly in \( s \), which is why \( s \) is a parameter, 1 for the bounds above.

A pop returns nothing only after a scan of all the queues, each locked in turn, found them empty: when no push runs at the same time, an empty answer means an empty structure, which a search uses for its termination. The locks are test-and-test-and-set on a byte, taken by an exchange after a plain read; a pop holds one lock, a push one, and the scan one at a time, so that no deadlock is possible. Each operation of a single queue is linearizable at its lock; the structure as a whole is relaxed, i.e., it is not a linearizable priority queue but for \( n = 1 \).

The keys must be integers or reals that an atomic word holds without a lock; the greatest key under less (the infinity of a real) stands for an empty queue in the atomic minima, and a pair with that key is popped only by the scan. The items must be copyable and default constructible. The threads use the structure through a Handle each, which holds the generator of their draws and their stickiness.

The contracts [see Contract.h]: \( n \ge 1 \) and \( s \ge 1 \); the invariant of a queue, checked under its lock after each operation, is that of the buffers above, and consistent() checks it on all the queues, together with their atomic minima. The operations are defined in MultiQueue.tpp.

See also
H. Rihani, P. Sanders, R. Dementiev, MultiQueues: simple relaxed concurrent priority queues, ACM Symposium on Parallelism in Algorithms and Architectures (SPAA), 80-82, 2015, doi:10.1145/2755573.2755616
D. Alistarh, J. Kopinsky, J. Li, G. Nadiradze, The power of choice in priority scheduling, ACM Symposium on Principles of Distributed Computing (PODC), 283-292, 2017, doi:10.1145/3087801.3087810
M. Williams, P. Sanders, R. Dementiev, Engineering MultiQueues: fast relaxed concurrent priority queues, European Symposium on Algorithms (ESA), LIPIcs 204, 81:1-81:17, 2021, doi:10.4230/LIPIcs.ESA.2021.81
M. Williams, P. Sanders, The MultiQueue: a simple and fast relaxed concurrent priority queue, ACM Transactions on Parallel Computing 12(4), 1-39, 2025, doi:10.1145/3771738
S. Walzer, M. Williams, A simple yet exact analysis of the MultiQueue, European Symposium on Algorithms (ESA), LIPIcs 351, 85:1-85:14, 2025, doi:10.4230/LIPIcs.ESA.2025.85
G. Zhang, G. Posluns, M. C. Jeffrey, Multi bucket queues: efficient concurrent priority scheduling, ACM Symposium on Parallelism in Algorithms and Architectures (SPAA), 2024, doi:10.1145/3626183.3659962
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa