-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparyfor.hpp
811 lines (687 loc) · 29.5 KB
/
paryfor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
#pragma once
// atomic_queue
// Copyright (c) 2019 Maxim Egorushkin. MIT License
// see LICENSE.atomic_queue for full license
#include <atomic>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <type_traits>
#include <utility>
#include <thread>
#include <functional>
#include <vector>
#if defined(__x86_64__) || defined(_M_X64) || \
defined(__i386__) || defined(_M_IX86)
#include <emmintrin.h>
namespace paryfor {
namespace atomic_queue {
constexpr int CACHE_LINE_SIZE = 64;
static inline void spin_loop_pause() noexcept {
_mm_pause();
}
} // namespace atomic_queue
} // namespace paryfor
#elif defined(__arm__) || defined(__aarch64__)
// TODO: These need to be verified as I do not have access to ARM platform.
namespace paryfor {
namespace atomic_queue {
constexpr int CACHE_LINE_SIZE = 64;
static inline void spin_loop_pause() noexcept {
#if (defined(__ARM_ARCH_6K__) || \
defined(__ARM_ARCH_6Z__) || \
defined(__ARM_ARCH_6ZK__) || \
defined(__ARM_ARCH_6T2__) || \
defined(__ARM_ARCH_7__) || \
defined(__ARM_ARCH_7A__) || \
defined(__ARM_ARCH_7R__) || \
defined(__ARM_ARCH_7M__) || \
defined(__ARM_ARCH_7S__) || \
defined(__ARM_ARCH_8A__) || \
defined(__aarch64__))
asm volatile ("yield" ::: "memory");
#else
asm volatile ("nop" ::: "memory");
#endif
}
} // namespace atomic_queue
} // namespace paryfor
#elif defined(__riscv) && (__riscv_xlen == 64)
namespace paryfor {
namespace atomic_queue {
constexpr int CACHE_LINE_SIZE = 64;
static inline void spin_loop_pause() noexcept {
asm volatile ("nop" ::: "memory");
}
} // namespace atomic_queue
} // namespace paryfor
#else
#error "Unknown CPU architecture."
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace paryfor {
namespace atomic_queue {
#if defined(__GNUC__) || defined(__clang__)
#define ATOMIC_QUEUE_LIKELY(expr) __builtin_expect(static_cast<bool>(expr), 1)
#define ATOMIC_QUEUE_UNLIKELY(expr) __builtin_expect(static_cast<bool>(expr), 0)
#else
#define ATOMIC_QUEUE_LIKELY(expr) expr
#define ATOMIC_QUEUE_UNLIKELY(expr) expr
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
auto constexpr A = std::memory_order_acquire;
auto constexpr R = std::memory_order_release;
auto constexpr X = std::memory_order_relaxed;
auto constexpr C = std::memory_order_seq_cst;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace atomic_queue
} // namespace paryfor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace paryfor {
namespace atomic_queue {
using std::uint32_t;
using std::uint64_t;
using std::uint8_t;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace details {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<size_t elements_per_cache_line> struct GetCacheLineIndexBits { static int constexpr value = 0; };
template<> struct GetCacheLineIndexBits<64> { static int constexpr value = 6; };
template<> struct GetCacheLineIndexBits<32> { static int constexpr value = 5; };
template<> struct GetCacheLineIndexBits<16> { static int constexpr value = 4; };
template<> struct GetCacheLineIndexBits< 8> { static int constexpr value = 3; };
template<> struct GetCacheLineIndexBits< 4> { static int constexpr value = 2; };
template<> struct GetCacheLineIndexBits< 2> { static int constexpr value = 1; };
template<bool minimize_contention, unsigned array_size, size_t elements_per_cache_line>
struct GetIndexShuffleBits {
static int constexpr bits = GetCacheLineIndexBits<elements_per_cache_line>::value;
static unsigned constexpr min_size = 1u << (bits * 2);
static int constexpr value = array_size < min_size ? 0 : bits;
};
template<unsigned array_size, size_t elements_per_cache_line>
struct GetIndexShuffleBits<false, array_size, elements_per_cache_line> {
static int constexpr value = 0;
};
// Multiple writers/readers contend on the same cache line when storing/loading elements at
// subsequent indexes, aka false sharing. For power of 2 ring buffer size it is possible to re-map
// the index in such a way that each subsequent element resides on another cache line, which
// minimizes contention. This is done by swapping the lowest order N bits (which are the index of
// the element within the cache line) with the next N bits (which are the index of the cache line)
// of the element index.
template<int BITS>
constexpr unsigned remap_index(unsigned index) noexcept {
constexpr unsigned MASK = (1u << BITS) - 1;
unsigned mix = (index ^ (index >> BITS)) & MASK;
return index ^ mix ^ (mix << BITS);
}
template<>
constexpr unsigned remap_index<0>(unsigned index) noexcept {
return index;
}
template<int BITS, class T>
constexpr T& map(T* elements, unsigned index) noexcept {
index = remap_index<BITS>(index);
return elements[index];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Implement a "bit-twiddling hack" for finding the next power of 2
// in either 32 bits or 64 bits in C++11 compatible constexpr functions
// "Runtime" version for 32 bits
// --a;
// a |= a >> 1;
// a |= a >> 2;
// a |= a >> 4;
// a |= a >> 8;
// a |= a >> 16;
// ++a;
template<class T>
constexpr T decrement(T x) {
return x - 1;
}
template<class T>
constexpr T increment(T x) {
return x + 1;
}
template<class T>
constexpr T or_equal(T x, unsigned u) {
return (x | x >> u);
}
template<class T, class... Args>
constexpr T or_equal(T x, unsigned u, Args... rest) {
return or_equal(or_equal(x, u), rest...);
}
constexpr uint32_t round_up_to_power_of_2(uint32_t a) noexcept {
return increment(or_equal(decrement(a), 1, 2, 4, 8, 16));
}
constexpr uint64_t round_up_to_power_of_2(uint64_t a) noexcept {
return increment(or_equal(decrement(a), 1, 2, 4, 8, 16, 32));
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace details
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
class AtomicQueueCommon {
protected:
// Put these on different cache lines to avoid false sharing between readers and writers.
alignas(CACHE_LINE_SIZE) std::atomic<unsigned> head_ = {};
alignas(CACHE_LINE_SIZE) std::atomic<unsigned> tail_ = {};
// The special member functions are not thread-safe.
AtomicQueueCommon() noexcept = default;
AtomicQueueCommon(AtomicQueueCommon const& b) noexcept
: head_(b.head_.load(X))
, tail_(b.tail_.load(X)) {}
AtomicQueueCommon& operator=(AtomicQueueCommon const& b) noexcept {
head_.store(b.head_.load(X), X);
tail_.store(b.tail_.load(X), X);
return *this;
}
void swap(AtomicQueueCommon& b) noexcept {
unsigned h = head_.load(X);
unsigned t = tail_.load(X);
head_.store(b.head_.load(X), X);
tail_.store(b.tail_.load(X), X);
b.head_.store(h, X);
b.tail_.store(t, X);
}
template<class T, T NIL>
static T do_pop_atomic(std::atomic<T>& q_element) noexcept {
if(Derived::spsc_) {
for(;;) {
T element = q_element.load(X);
if(ATOMIC_QUEUE_LIKELY(element != NIL)) {
q_element.store(NIL, R);
return element;
}
if(Derived::maximize_throughput_)
spin_loop_pause();
}
}
else {
for(;;) {
T element = q_element.exchange(NIL, R); // (2) The store to wait for.
if(ATOMIC_QUEUE_LIKELY(element != NIL))
return element;
// Do speculative loads while busy-waiting to avoid broadcasting RFO messages.
do
spin_loop_pause();
while(Derived::maximize_throughput_ && q_element.load(X) == NIL);
}
}
}
template<class T, T NIL>
static void do_push_atomic(T element, std::atomic<T>& q_element) noexcept {
assert(element != NIL);
if(Derived::spsc_) {
while(ATOMIC_QUEUE_UNLIKELY(q_element.load(X) != NIL))
if(Derived::maximize_throughput_)
spin_loop_pause();
q_element.store(element, R);
}
else {
for(T expected = NIL; ATOMIC_QUEUE_UNLIKELY(!q_element.compare_exchange_strong(expected, element, R, X)); expected = NIL) {
do
spin_loop_pause(); // (1) Wait for store (2) to complete.
while(Derived::maximize_throughput_ && q_element.load(X) != NIL);
}
}
}
enum State : unsigned char { EMPTY, STORING, STORED, LOADING };
template<class T>
static T do_pop_any(std::atomic<unsigned char>& state, T& q_element) noexcept {
if(Derived::spsc_) {
while(ATOMIC_QUEUE_UNLIKELY(state.load(A) != STORED))
if(Derived::maximize_throughput_)
spin_loop_pause();
T element{std::move(q_element)};
state.store(EMPTY, R);
return element;
}
else {
for(;;) {
unsigned char expected = STORED;
if(ATOMIC_QUEUE_LIKELY(state.compare_exchange_strong(expected, LOADING, X, X))) {
T element{std::move(q_element)};
state.store(EMPTY, R);
return element;
}
// Do speculative loads while busy-waiting to avoid broadcasting RFO messages.
do
spin_loop_pause();
while(Derived::maximize_throughput_ && state.load(X) != STORED);
}
}
}
template<class U, class T>
static void do_push_any(U&& element, std::atomic<unsigned char>& state, T& q_element) noexcept {
if(Derived::spsc_) {
while(ATOMIC_QUEUE_UNLIKELY(state.load(A) != EMPTY))
if(Derived::maximize_throughput_)
spin_loop_pause();
q_element = std::forward<U>(element);
state.store(STORED, R);
}
else {
for(;;) {
unsigned char expected = EMPTY;
if(ATOMIC_QUEUE_LIKELY(state.compare_exchange_strong(expected, STORING, X, X))) {
q_element = std::forward<U>(element);
state.store(STORED, R);
return;
}
// Do speculative loads while busy-waiting to avoid broadcasting RFO messages.
do
spin_loop_pause();
while(Derived::maximize_throughput_ && state.load(X) != EMPTY);
}
}
}
public:
template<class T>
bool try_push(T&& element) noexcept {
auto head = head_.load(X);
if(Derived::spsc_) {
if(static_cast<int>(head - tail_.load(X)) >= static_cast<int>(static_cast<Derived&>(*this).size_))
return false;
head_.store(head + 1, X);
}
else {
do {
if(static_cast<int>(head - tail_.load(X)) >= static_cast<int>(static_cast<Derived&>(*this).size_))
return false;
} while(ATOMIC_QUEUE_UNLIKELY(!head_.compare_exchange_strong(head, head + 1, A, X))); // This loop is not FIFO.
}
static_cast<Derived&>(*this).do_push(std::forward<T>(element), head);
return true;
}
template<class T>
bool try_pop(T& element) noexcept {
auto tail = tail_.load(X);
if(Derived::spsc_) {
if(static_cast<int>(head_.load(X) - tail) <= 0)
return false;
tail_.store(tail + 1, X);
}
else {
do {
if(static_cast<int>(head_.load(X) - tail) <= 0)
return false;
} while(ATOMIC_QUEUE_UNLIKELY(!tail_.compare_exchange_strong(tail, tail + 1, A, X))); // This loop is not FIFO.
}
element = static_cast<Derived&>(*this).do_pop(tail);
return true;
}
template<class T>
void push(T&& element) noexcept {
unsigned head;
if(Derived::spsc_) {
head = head_.load(X);
head_.store(head + 1, X);
}
else {
constexpr auto memory_order = Derived::total_order_ ? std::memory_order_seq_cst : std::memory_order_acquire;
head = head_.fetch_add(1, memory_order); // FIFO and total order on Intel regardless, as of 2019.
}
static_cast<Derived&>(*this).do_push(std::forward<T>(element), head);
}
auto pop() noexcept {
unsigned tail;
if(Derived::spsc_) {
tail = tail_.load(X);
tail_.store(tail + 1, X);
}
else {
constexpr auto memory_order = Derived::total_order_ ? std::memory_order_seq_cst : std::memory_order_acquire;
tail = tail_.fetch_add(1, memory_order); // FIFO and total order on Intel regardless, as of 2019.
}
return static_cast<Derived&>(*this).do_pop(tail);
}
bool was_empty() const noexcept {
return static_cast<int>(head_.load(X) - tail_.load(X)) <= 0;
}
bool was_full() const noexcept {
return static_cast<int>(head_.load(X) - tail_.load(X)) >= static_cast<int>(static_cast<Derived const&>(*this).size_);
}
unsigned capacity() const noexcept {
return static_cast<Derived const&>(*this).size_;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T, unsigned SIZE, T NIL = T{}, bool MINIMIZE_CONTENTION = true, bool MAXIMIZE_THROUGHPUT = true, bool TOTAL_ORDER = false, bool SPSC = false>
class AtomicQueue : public AtomicQueueCommon<AtomicQueue<T, SIZE, NIL, MINIMIZE_CONTENTION, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>> {
using Base = AtomicQueueCommon<AtomicQueue<T, SIZE, NIL, MINIMIZE_CONTENTION, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>>;
friend Base;
static constexpr unsigned size_ = MINIMIZE_CONTENTION ? details::round_up_to_power_of_2(SIZE) : SIZE;
static constexpr int SHUFFLE_BITS = details::GetIndexShuffleBits<MINIMIZE_CONTENTION, size_, CACHE_LINE_SIZE / sizeof(std::atomic<T>)>::value;
static constexpr bool total_order_ = TOTAL_ORDER;
static constexpr bool spsc_ = SPSC;
static constexpr bool maximize_throughput_ = MAXIMIZE_THROUGHPUT;
alignas(CACHE_LINE_SIZE) std::atomic<T> elements_[size_] = {}; // Empty elements are NIL.
T do_pop(unsigned tail) noexcept {
std::atomic<T>& q_element = details::map<SHUFFLE_BITS>(elements_, tail % size_);
return Base::template do_pop_atomic<T, NIL>(q_element);
}
void do_push(T element, unsigned head) noexcept {
std::atomic<T>& q_element = details::map<SHUFFLE_BITS>(elements_, head % size_);
Base::template do_push_atomic<T, NIL>(element, q_element);
}
public:
using value_type = T;
AtomicQueue() noexcept {
assert(std::atomic<T>{NIL}.is_lock_free()); // This queue is for atomic elements only. AtomicQueue2 is for non-atomic ones.
if(T{} != NIL)
for(auto& element : elements_)
element.store(NIL, X);
}
AtomicQueue(AtomicQueue const&) = delete;
AtomicQueue& operator=(AtomicQueue const&) = delete;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T, unsigned SIZE, bool MINIMIZE_CONTENTION = true, bool MAXIMIZE_THROUGHPUT = true, bool TOTAL_ORDER = false, bool SPSC = false>
class AtomicQueue2 : public AtomicQueueCommon<AtomicQueue2<T, SIZE, MINIMIZE_CONTENTION, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>> {
using Base = AtomicQueueCommon<AtomicQueue2<T, SIZE, MINIMIZE_CONTENTION, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>>;
using State = typename Base::State;
friend Base;
static constexpr unsigned size_ = MINIMIZE_CONTENTION ? details::round_up_to_power_of_2(SIZE) : SIZE;
static constexpr int SHUFFLE_BITS = details::GetIndexShuffleBits<MINIMIZE_CONTENTION, size_, CACHE_LINE_SIZE / sizeof(State)>::value;
static constexpr bool total_order_ = TOTAL_ORDER;
static constexpr bool spsc_ = SPSC;
static constexpr bool maximize_throughput_ = MAXIMIZE_THROUGHPUT;
alignas(CACHE_LINE_SIZE) std::atomic<unsigned char> states_[size_] = {};
alignas(CACHE_LINE_SIZE) T elements_[size_] = {};
T do_pop(unsigned tail) noexcept {
unsigned index = details::remap_index<SHUFFLE_BITS>(tail % size_);
return Base::template do_pop_any(states_[index], elements_[index]);
}
template<class U>
void do_push(U&& element, unsigned head) noexcept {
unsigned index = details::remap_index<SHUFFLE_BITS>(head % size_);
Base::template do_push_any(std::forward<U>(element), states_[index], elements_[index]);
}
public:
using value_type = T;
AtomicQueue2() noexcept = default;
AtomicQueue2(AtomicQueue2 const&) = delete;
AtomicQueue2& operator=(AtomicQueue2 const&) = delete;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T, class A = std::allocator<T>, T NIL = T{}, bool MAXIMIZE_THROUGHPUT = true, bool TOTAL_ORDER = false, bool SPSC = false>
class AtomicQueueB : public AtomicQueueCommon<AtomicQueueB<T, A, NIL, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>>,
private std::allocator_traits<A>::template rebind_alloc<std::atomic<T>> {
using Base = AtomicQueueCommon<AtomicQueueB<T, A, NIL, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>>;
friend Base;
static constexpr bool total_order_ = TOTAL_ORDER;
static constexpr bool spsc_ = SPSC;
static constexpr bool maximize_throughput_ = MAXIMIZE_THROUGHPUT;
using AllocatorElements = typename std::allocator_traits<A>::template rebind_alloc<std::atomic<T>>;
static constexpr auto ELEMENTS_PER_CACHE_LINE = CACHE_LINE_SIZE / sizeof(std::atomic<T>);
static_assert(ELEMENTS_PER_CACHE_LINE, "Unexpected ELEMENTS_PER_CACHE_LINE.");
static constexpr auto SHUFFLE_BITS = details::GetCacheLineIndexBits<ELEMENTS_PER_CACHE_LINE>::value;
static_assert(SHUFFLE_BITS, "Unexpected SHUFFLE_BITS.");
// AtomicQueueCommon members are stored into by readers and writers.
// Allocate these immutable members on another cache line which never gets invalidated by stores.
alignas(CACHE_LINE_SIZE) unsigned size_;
std::atomic<T>* elements_;
T do_pop(unsigned tail) noexcept {
std::atomic<T>& q_element = details::map<SHUFFLE_BITS>(elements_, tail & (size_ - 1));
return Base::template do_pop_atomic<T, NIL>(q_element);
}
void do_push(T element, unsigned head) noexcept {
std::atomic<T>& q_element = details::map<SHUFFLE_BITS>(elements_, head & (size_ - 1));
Base::template do_push_atomic<T, NIL>(element, q_element);
}
public:
using value_type = T;
// The special member functions are not thread-safe.
AtomicQueueB(unsigned size)
: size_(std::max(details::round_up_to_power_of_2(size), 1u << (SHUFFLE_BITS * 2)))
, elements_(AllocatorElements::allocate(size_)) {
assert(std::atomic<T>{NIL}.is_lock_free()); // This queue is for atomic elements only. AtomicQueueB2 is for non-atomic ones.
for(auto p = elements_, q = elements_ + size_; p < q; ++p)
p->store(NIL, X);
}
AtomicQueueB(AtomicQueueB&& b) noexcept
: Base(static_cast<Base&&>(b))
, AllocatorElements(static_cast<AllocatorElements&&>(b)) // TODO: This must be noexcept, static_assert that.
, size_(b.size_)
, elements_(b.elements_) {
b.size_ = 0;
b.elements_ = 0;
}
AtomicQueueB& operator=(AtomicQueueB&& b) noexcept {
b.swap(*this);
return *this;
}
~AtomicQueueB() noexcept {
if(elements_)
AllocatorElements::deallocate(elements_, size_); // TODO: This must be noexcept, static_assert that.
}
void swap(AtomicQueueB& b) noexcept {
using std::swap;
this->Base::swap(b);
swap(static_cast<AllocatorElements&>(*this), static_cast<AllocatorElements&>(b));
swap(size_, b.size_);
swap(elements_, b.elements_);
}
friend void swap(AtomicQueueB& a, AtomicQueueB& b) {
a.swap(b);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T, class A = std::allocator<T>, bool MAXIMIZE_THROUGHPUT = true, bool TOTAL_ORDER = false, bool SPSC = false>
class AtomicQueueB2 : public AtomicQueueCommon<AtomicQueueB2<T, A, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>>,
private A,
private std::allocator_traits<A>::template rebind_alloc<std::atomic<uint8_t>> {
using Base = AtomicQueueCommon<AtomicQueueB2<T, A, MAXIMIZE_THROUGHPUT, TOTAL_ORDER, SPSC>>;
using State = typename Base::State;
friend Base;
static constexpr bool total_order_ = TOTAL_ORDER;
static constexpr bool spsc_ = SPSC;
static constexpr bool maximize_throughput_ = MAXIMIZE_THROUGHPUT;
using AllocatorElements = A;
using AllocatorStates = typename std::allocator_traits<A>::template rebind_alloc<std::atomic<uint8_t>>;
// AtomicQueueCommon members are stored into by readers and writers.
// Allocate these immutable members on another cache line which never gets invalidated by stores.
alignas(CACHE_LINE_SIZE) unsigned size_;
std::atomic<unsigned char>* states_;
T* elements_;
static constexpr auto STATES_PER_CACHE_LINE = CACHE_LINE_SIZE / sizeof(State);
static_assert(STATES_PER_CACHE_LINE, "Unexpected STATES_PER_CACHE_LINE.");
static constexpr auto SHUFFLE_BITS = details::GetCacheLineIndexBits<STATES_PER_CACHE_LINE>::value;
static_assert(SHUFFLE_BITS, "Unexpected SHUFFLE_BITS.");
T do_pop(unsigned tail) noexcept {
unsigned index = details::remap_index<SHUFFLE_BITS>(tail & (size_ - 1));
return Base::template do_pop_any(states_[index], elements_[index]);
}
template<class U>
void do_push(U&& element, unsigned head) noexcept {
unsigned index = details::remap_index<SHUFFLE_BITS>(head & (size_ - 1));
Base::template do_push_any(std::forward<U>(element), states_[index], elements_[index]);
}
public:
using value_type = T;
// The special member functions are not thread-safe.
AtomicQueueB2(unsigned size)
: size_(std::max(details::round_up_to_power_of_2(size), 1u << (SHUFFLE_BITS * 2)))
, states_(AllocatorStates::allocate(size_))
, elements_(AllocatorElements::allocate(size_)) {
for(auto p = states_, q = states_ + size_; p < q; ++p)
p->store(Base::EMPTY, X);
AllocatorElements& ae = *this;
for(auto p = elements_, q = elements_ + size_; p < q; ++p)
std::allocator_traits<AllocatorElements>::construct(ae, p);
}
AtomicQueueB2(AtomicQueueB2&& b) noexcept
: Base(static_cast<Base&&>(b))
, AllocatorElements(static_cast<AllocatorElements&&>(b)) // TODO: This must be noexcept, static_assert that.
, AllocatorStates(static_cast<AllocatorStates&&>(b)) // TODO: This must be noexcept, static_assert that.
, size_(b.size_)
, states_(b.states_)
, elements_(b.elements_) {
b.size_ = 0;
b.states_ = 0;
b.elements_ = 0;
}
AtomicQueueB2& operator=(AtomicQueueB2&& b) noexcept {
b.swap(*this);
return *this;
}
~AtomicQueueB2() noexcept {
if(elements_) {
AllocatorElements& ae = *this;
for(auto p = elements_, q = elements_ + size_; p < q; ++p)
std::allocator_traits<AllocatorElements>::destroy(ae, p);
AllocatorElements::deallocate(elements_, size_); // TODO: This must be noexcept, static_assert that.
AllocatorStates::deallocate(states_, size_); // TODO: This must be noexcept, static_assert that.
}
}
void swap(AtomicQueueB2& b) noexcept {
using std::swap;
this->Base::swap(b);
swap(static_cast<AllocatorElements&>(*this), static_cast<AllocatorElements&>(b));
swap(static_cast<AllocatorStates&>(*this), static_cast<AllocatorStates&>(b));
swap(size_, b.size_);
swap(states_, b.states_);
swap(elements_, b.elements_);
}
friend void swap(AtomicQueueB2& a, AtomicQueueB2& b) noexcept {
a.swap(b);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Queue>
struct RetryDecorator : Queue {
using T = typename Queue::value_type;
using Queue::Queue;
void push(T element) noexcept {
while(!this->try_push(element))
spin_loop_pause();
}
T pop() noexcept {
T element;
while(!this->try_pop(element))
spin_loop_pause();
return element;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace atomic_queue
} // namespace paryfor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// paryfor parallel for implementation
// Copyright (c) 2020 Erik Garrison. MIT License
// see LICENSE for details
namespace paryfor {
using std::uint32_t;
using std::uint64_t;
using std::uint8_t;
template <typename I>
void parallel_for(const I& begin,
const I& end,
const uint64_t& nthreads,
const std::function<void(I, int)>& func) {
auto queue_ptr = new atomic_queue::AtomicQueue2<I, 2 << 16>;
auto& queue = *queue_ptr;
std::atomic<bool> work_todo;
auto worker =
[&queue,&work_todo,&func](int thread_id) {
I i;
while (work_todo.load()) {
if (queue.try_pop(i)) {
func(i, thread_id);
} else {
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
}
}
};
std::vector<std::thread> workers;
workers.reserve(nthreads);
work_todo.store(true);
for (uint64_t t = 0; t < nthreads; ++t) {
workers.emplace_back(worker, t);
}
I todo_i = begin;
while (todo_i != end) {
if (queue.try_push(todo_i)) {
++todo_i;
} else {
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
}
}
while (!queue.was_empty()) {
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
}
work_todo.store(false);
for (uint64_t t = 0; t < nthreads; ++t) {
workers[t].join();
}
delete queue_ptr;
}
// specialization where we don't use the thread id
template <typename I>
void parallel_for(const I& begin,
const I& end,
const uint64_t& nthreads,
const std::function<void(I)>& func) {
parallel_for<I>(begin, end, nthreads, [&func](I i, int id) { func(i); });
}
template <typename I>
void parallel_for(const I& begin,
const I& end,
const uint64_t& nthreads,
const uint64_t& chunk_size,
const std::function<void(I, int)>& func) {
auto queue_ptr = new atomic_queue::AtomicQueue2<std::pair<I, I>, 2 << 16>;
auto& queue = *queue_ptr;
std::atomic<bool> work_todo;
auto worker =
[&queue,&work_todo,&func](int thread_id) {
std::pair<I, I> p;
while (work_todo.load()) {
if (queue.try_pop(p)) {
for (I i = p.first; i < p.second; ++i) {
func(i, thread_id);
}
} else {
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
}
}
};
std::vector<std::thread> workers;
workers.reserve(nthreads);
work_todo.store(true);
for (uint64_t t = 0; t < nthreads; ++t) {
workers.emplace_back(worker, t);
}
std::pair<I, I> todo_range = std::make_pair(begin, std::min(begin + chunk_size, end));
I& todo_i = todo_range.first;
I& todo_j = todo_range.second;
while (todo_i != end) {
if (queue.try_push(todo_range)) {
todo_i = std::min(todo_i + chunk_size, end);
todo_j = std::min(todo_j + chunk_size, end);
} else {
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
}
}
while (!queue.was_empty()) {
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
}
work_todo.store(false);
for (uint64_t t = 0; t < nthreads; ++t) {
workers[t].join();
}
delete queue_ptr;
}
// specialization where we don't use the thread id
template <typename I>
void parallel_for(const I& begin,
const I& end,
const uint64_t& nthreads,
const uint64_t& chunk_size,
const std::function<void(I)>& func) {
parallel_for<I>(begin, end, nthreads, chunk_size, [&func](I i, int id) { func(i); });
}
}