ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
MultiQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File MultiQueue.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
116/*--------------------------------------------------------------------------*/
117/*----------------------------- DEFINITIONS --------------------------------*/
118/*--------------------------------------------------------------------------*/
119
120#ifndef __ADS_MultiQueue
121 #define __ADS_MultiQueue
122/* self-identification: #endif constant */
123
124/*--------------------------------------------------------------------------*/
125/*------------------------------ INCLUDES ----------------------------------*/
126/*--------------------------------------------------------------------------*/
127
128#include "ADS/Capabilities.h"
129#include "ADS/Contract.h"
131
132#include <atomic>
133#include <cstddef>
134#include <cstdint>
135#include <functional>
136#include <limits>
137#include <memory>
138#include <optional>
139#include <type_traits>
140#include <utility>
141
142/*--------------------------------------------------------------------------*/
143/*--------------------------- NAMESPACE ------------------------------------*/
144/*--------------------------------------------------------------------------*/
145
146namespace ADS
147{
148
149/*--------------------------------------------------------------------------*/
150/*--------------------------- CLASS MultiQueue -----------------------------*/
151/*--------------------------------------------------------------------------*/
152
154template< class E , class C >
156
159template< class K , class T , class Less = std::less< K > ,
160 template< class , class > class H = MultiQueueHeap >
162{
163
164 static_assert( std::numeric_limits< K >::is_specialized ,
165 "the keys are integers or reals" );
166 static_assert( std::atomic< K >::is_always_lock_free ,
167 "the keys fit an atomic word without a lock" );
168
169/*--------------------------------------------------------------------------*/
170 public:
171 using Key = K;
172 using Item = T;
173
177
179 struct Entry
180 {
181 K key;
183 };
184
185 class Handle;
186
191 explicit MultiQueue( std::size_t n , unsigned s = 16 ,
192 std::uint64_t seed = 1 ,
193 Less less = Less() );
194
196 MultiQueue( const MultiQueue & ) = delete;
198 MultiQueue & operator=( const MultiQueue & ) = delete;
199
202
204 [[nodiscard]] Handle handle( void );
205
207 [[nodiscard]] std::size_t queues( void ) const { return( f_n ); }
208
210 [[nodiscard]] unsigned stickiness( void ) const { return( f_s ); }
211
214 [[nodiscard]] std::size_t size( void ) const;
215
217 [[nodiscard]] bool empty( void ) const { return( size() == 0 ); }
218
222 [[nodiscard]] bool consistent( void ) const;
223
224/*--------------------------------------------------------------------------*/
225 private:
226 static constexpr std::size_t kBuf = 16;
227
229 struct ByKey
230 {
231 Less less;
232 bool operator()( const Entry & a , const Entry & b ) const {
233 return( less( a.key , b.key ) );
234 }
235 };
236
239 struct alignas( 64 ) Queue
240 {
242 std::atomic< bool > lock{ false };
244 std::uint8_t nd = 0 , ni = 0;
246 std::atomic< K > top;
250 Entry D[ kBuf ];
252 Entry I[ kBuf ];
254 H< Entry , ByKey > heap;
256 explicit Queue( const ByKey & less , K none ) : top( none ) ,
257 heap( less ) { }
258 };
259
260
262 [[nodiscard]] static K none( const Less & less );
263
265 [[nodiscard]] static bool tryLock( Queue & q );
266
268 static void lock( Queue & q );
269
271 static void unlock( Queue & q );
272
274 void put( Queue & q , Entry && e ) const;
275
277 void flush( Queue & q ) const;
278
280 Entry take( Queue & q ) const;
281
283 void publish( Queue & q ) const;
284
286 [[nodiscard]] bool ordered( const Queue & q ) const;
287
290 std::optional< Entry > scan( std::size_t r );
291
292 std::size_t f_n;
293 unsigned f_s;
294 ByKey f_less;
295 K f_none;
296 Queue * f_q;
297 std::atomic< std::uint64_t > f_seed;
298
299 }; // end( class MultiQueue )
300
301/*--------------------------------------------------------------------------*/
302/*------------------------ CLASS MultiQueue::Handle ------------------------*/
303/*--------------------------------------------------------------------------*/
304
307template< class K , class T , class Less , template< class , class > class H >
308class MultiQueue< K , T , Less , H >::Handle
309{
310
311/*--------------------------------------------------------------------------*/
312 public:
351 void push( K k , T x );
352
359 template< class It >
360 void pushAll( It first , It last );
361
399 std::optional< Entry > tryPop( void );
400
401/*--------------------------------------------------------------------------*/
402 private:
403 friend class MultiQueue;
404
406 Handle( MultiQueue & m , std::uint64_t seed ) : f_m( &m ) , f_x( seed ) { }
407
409 std::uint64_t next( void );
410
412 std::size_t bit( void );
413
415 std::size_t any( void );
416
418 void draw( void );
419
421 void use( void );
422
423 MultiQueue * f_m;
424 std::uint64_t f_x;
425 std::size_t f_k[ 2 ] = { 0 , 0 };
426 unsigned f_used = ~0u;
427 std::uint64_t f_bits = 0;
428 int f_nbits = 0;
429
430 }; // end( class MultiQueue::Handle )
431
432/*--------------------------------------------------------------------------*/
433
434} // end( namespace ADS )
435
436/*--------------------------------------------------------------------------*/
437/*-------------------------- IMPLEMENTATION --------------------------------*/
438/*--------------------------------------------------------------------------*/
439
440#include "ADS/concurrent/impl/MultiQueue.tpp"
441
442/*--------------------------------------------------------------------------*/
443
444#endif /* MultiQueue.h included */
445
446/*--------------------------------------------------------------------------*/
447/*------------------------- End File MultiQueue.h --------------------------*/
448/*--------------------------------------------------------------------------*/
The capabilities of the structures, beyond the book: what an algorithm may ask of its argument to cho...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A -ary heap of plain items, beyond the book: the heap of section 10.1 with children per node in a ve...
a -ary heap of plain items, the best one by better() on top; pop() by the walk down from the top,...
Definition DaryHeap.h:128
the access of one thread to a MultiQueue: its draws and the two queues it keeps
Definition MultiQueue.h:309
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 ...
void pushAll(It first, It last)
puts the pairs of , which convert to Entry, all into one of the two queues kept, under one lock,...
void push(K k, T x)
puts the pair into one of the two queues kept, or into the first free one of other draws; the key by...
friend class MultiQueue
Definition MultiQueue.h:403
MultiQueue(std::size_t n, unsigned s=16, std::uint64_t seed=1, Less less=Less())
the structure of empty queues, kept by a thread for operations, with the draws from the seed seed; ...
std::size_t size(void) const
the number of the pairs, counted in each queue under its lock, exact when no operation runs
MultiQueue & operator=(const MultiQueue &)=delete
no copy, since the threads share it by its address
MultiQueue(const MultiQueue &)=delete
no copy, since the threads share it by its address
K Key
the type of the keys
Definition MultiQueue.h:171
~MultiQueue()
the frees of the queues
static constexpr decrease_key_kind decrease_key
a relaxed queue with no handles, on which a search lowers a key by a new insertion [see decrease_key_...
Definition MultiQueue.h:176
unsigned stickiness(void) const
the operations a thread keeps its two queues for
Definition MultiQueue.h:210
bool empty(void) const
whether size() is 0
Definition MultiQueue.h:217
bool consistent(void) const
the buffers of each queue ordered and holding its least pairs, and the atomic minima equal to them; u...
std::size_t queues(void) const
the number of the queues
Definition MultiQueue.h:207
T Item
the type of the items
Definition MultiQueue.h:172
Handle handle(void)
a handle for one thread, with a generator of its own
decrease_key_kind
how a priority queue lowers the priority of an item: it cannot (none), by a new insertion and the sta...
Definition Capabilities.h:553
@ lazy
Definition Capabilities.h:553
@ none
Definition Capabilities.h:553
DaryHeap< E, C, 8 > MultiQueueHeap
the inner queue of a MultiQueue by default, the 8-ary heap
Definition MultiQueue.h:155
Definition ADT.h:96
a pair of the queue, its key and its item
Definition MultiQueue.h:180
T item
the item
Definition MultiQueue.h:182
K key
the key, the least popped first
Definition MultiQueue.h:181