ADS
Algorithms and Data Structures
Toggle main menu visibility
Loading...
Searching...
No Matches
WorkStealingDeque.h
Go to the documentation of this file.
1
/*--------------------------------------------------------------------------*/
2
/*------------------------ File WorkStealingDeque.h ------------------------*/
3
/*--------------------------------------------------------------------------*/
49
/*--------------------------------------------------------------------------*/
50
/*----------------------------- DEFINITIONS --------------------------------*/
51
/*--------------------------------------------------------------------------*/
52
53
#ifndef __ADS_WorkStealingDeque
54
#define __ADS_WorkStealingDeque
55
/* self-identification: #endif constant */
56
57
/*--------------------------------------------------------------------------*/
58
/*------------------------------ INCLUDES ----------------------------------*/
59
/*--------------------------------------------------------------------------*/
60
61
#include "
ADS/Contract.h
"
62
63
#include <atomic>
64
#include <bit>
65
#include <cstddef>
66
#include <cstdint>
67
#include <memory>
68
#include <optional>
69
#include <type_traits>
70
#include <vector>
71
72
/*--------------------------------------------------------------------------*/
73
/*--------------------------- NAMESPACE ------------------------------------*/
74
/*--------------------------------------------------------------------------*/
75
76
namespace
ADS
77
{
78
79
/*--------------------------------------------------------------------------*/
80
/*------------------------ CLASS WorkStealingDeque -------------------------*/
81
/*--------------------------------------------------------------------------*/
82
84
template
<
class
T >
85
class
WorkStealingDeque
86
{
87
static_assert
( std::is_trivially_copyable_v< T > ,
88
"the items are copied through atomics"
);
89
91
struct
Ring
92
{
94
std::int64_t mask;
96
std::unique_ptr< std::atomic< T >[] > place;
98
explicit
Ring( std::int64_t c )
99
: mask( c - 1 ) , place(
new
std::atomic< T >[ std::size_t( c ) ] ) { }
101
[[nodiscard]] T get( std::int64_t i )
const
{
102
return
(
103
place[ std::size_t( i & mask ) ].
load
( std::memory_order_relaxed ) );
104
}
106
void
put( std::int64_t i , T x ) {
107
place[ std::size_t( i & mask ) ].store( x , std::memory_order_relaxed );
108
}
109
};
110
111
/*--------------------------------------------------------------------------*/
112
public
:
113
using
Item
= T;
114
117
explicit
WorkStealingDeque
( std::size_t
capacity
= 1024 );
119
WorkStealingDeque
(
const
WorkStealingDeque
& ) =
delete
;
121
WorkStealingDeque
&
operator=
(
const
WorkStealingDeque
& ) =
delete
;
122
144
void
push
( T x );
145
170
std::optional< T >
take
(
void
);
171
191
bool
steal
( T & x );
192
195
[[nodiscard]] std::size_t
size
(
void
)
const
;
196
198
[[nodiscard]]
bool
empty
(
void
)
const
{
return
(
size
() == 0 ); }
199
201
[[nodiscard]] std::size_t
capacity
(
void
)
const
{
202
return
( std::size_t( f_ring.load( std::memory_order_relaxed )->mask + 1 ) );
203
}
204
205
/*--------------------------------------------------------------------------*/
206
private
:
207
static
constexpr
std::size_t line = 64;
208
209
alignas
( 2 * line ) std::atomic< std::int64_t > f_top{ 0 };
210
alignas
( 2 * line ) std::atomic< std::int64_t > f_bottom{ 0 };
211
std::atomic< Ring * > f_ring;
212
std::vector< std::unique_ptr< Ring > > f_rings;
213
};
// end( class WorkStealingDeque )
214
215
/*--------------------------------------------------------------------------*/
216
217
}
// end( namespace ADS )
218
219
/*--------------------------------------------------------------------------*/
220
/*-------------------------- IMPLEMENTATION --------------------------------*/
221
/*--------------------------------------------------------------------------*/
222
223
#include "ADS/concurrent/impl/WorkStealingDeque.tpp"
224
225
/*--------------------------------------------------------------------------*/
226
227
#endif
/* WorkStealingDeque.h included */
228
229
/*--------------------------------------------------------------------------*/
230
/*---------------------- End File WorkStealingDeque.h ----------------------*/
231
/*--------------------------------------------------------------------------*/
Contract.h
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
ADS::WorkStealingDeque::WorkStealingDeque
WorkStealingDeque(std::size_t capacity=1024)
the empty deque on a vector of capacity
ADS::WorkStealingDeque::WorkStealingDeque
WorkStealingDeque(const WorkStealingDeque &)=delete
no copy, since the thieves share it by its address
ADS::WorkStealingDeque::steal
bool steal(T &x)
steals the item at the top, the first pushed, into ; false if the deque is empty or another thread to...
ADS::WorkStealingDeque::operator=
WorkStealingDeque & operator=(const WorkStealingDeque &)=delete
no copy, since the thieves share it by its address
ADS::WorkStealingDeque::size
std::size_t size(void) const
the number of items at some moment of the call, , exact for the owner
ADS::WorkStealingDeque::empty
bool empty(void) const
true if the deque held no item at some moment of the call
Definition
WorkStealingDeque.h:198
ADS::WorkStealingDeque::Item
T Item
the type of the items
Definition
WorkStealingDeque.h:113
ADS::WorkStealingDeque::push
void push(T x)
pushes at the bottom, doubling the vector if it is full; only the owner calls it
ADS::WorkStealingDeque::take
std::optional< T > take(void)
takes the item at the bottom, the last pushed, nothing if the deque is empty or a thief took the last...
ADS::WorkStealingDeque::capacity
std::size_t capacity(void) const
the places of the current vector; only the owner calls it
Definition
WorkStealingDeque.h:201
ADS::load
void load(Ar &a, T &x)
the structure read from the archive of cereal
Definition
Cereal.h:238
ADS
Definition
ADT.h:96
ADS
concurrent
WorkStealingDeque.h
Generated by
1.17.0