13 #include <condition_variable>
15 #ifndef ALPAKA_THREAD_BARRIER_DISABLE_SPINLOCK
25 template<
typename TIdx>
30 : m_threadCount(threadCount)
31 , m_curThreadCount(threadCount)
39 TIdx
const generationWhenEnteredTheWait = m_generation;
40 #ifdef ALPAKA_THREAD_BARRIER_DISABLE_SPINLOCK
41 std::unique_lock<std::mutex> lock(m_mtxBarrier);
43 if(--m_curThreadCount == 0)
45 m_curThreadCount = m_threadCount;
47 #ifdef ALPAKA_THREAD_BARRIER_DISABLE_SPINLOCK
48 m_cvAllThreadsReachedBarrier.notify_all();
53 #ifdef ALPAKA_THREAD_BARRIER_DISABLE_SPINLOCK
54 m_cvAllThreadsReachedBarrier.wait(
56 [
this, generationWhenEnteredTheWait] {
return generationWhenEnteredTheWait != m_generation; });
58 while(generationWhenEnteredTheWait == m_generation)
60 std::this_thread::yield();
67 #ifdef ALPAKA_THREAD_BARRIER_DISABLE_SPINLOCK
68 std::mutex m_mtxBarrier;
69 std::condition_variable m_cvAllThreadsReachedBarrier;
71 const TIdx m_threadCount;
72 #ifdef ALPAKA_THREAD_BARRIER_DISABLE_SPINLOCK
73 TIdx m_curThreadCount;
76 std::atomic<TIdx> m_curThreadCount;
77 std::atomic<TIdx> m_generation;
83 template<
typename TOp>
91 result +=
static_cast<int>(value);
100 result &=
static_cast<int>(value);
109 result |=
static_cast<int>(value);
115 template<
typename TIdx>
120 : m_threadCount(threadCount)
121 , m_curThreadCount(threadCount)
127 template<
typename TOp>
130 TIdx
const generationWhenEnteredTheWait = m_generation;
131 std::unique_lock<std::mutex> lock(m_mtxBarrier);
133 auto const generationMod2 = m_generation %
static_cast<TIdx
>(2u);
134 if(m_curThreadCount == m_threadCount)
136 m_result[generationMod2] = TOp::InitialValue;
139 std::atomic<int>& result(m_result[generationMod2]);
140 bool const predicateBool(predicate != 0);
144 if(--m_curThreadCount == 0)
146 m_curThreadCount = m_threadCount;
148 m_cvAllThreadsReachedBarrier.notify_all();
152 m_cvAllThreadsReachedBarrier.wait(
154 [
this, generationWhenEnteredTheWait] {
return generationWhenEnteredTheWait != m_generation; });
156 return m_result[generationMod2];
160 std::mutex m_mtxBarrier;
161 std::condition_variable m_cvAllThreadsReachedBarrier;
162 const TIdx m_threadCount;
163 TIdx m_curThreadCount;
165 std::atomic<int> m_result[2];
A self-resetting barrier with barrier.
BarrierThreadWithPredicate(TIdx const &threadCount)
ALPAKA_FN_HOST auto wait(int predicate) -> int
Waits for all the other threads to reach the barrier.
A self-resetting barrier.
BarrierThread(TIdx const &threadCount)
auto wait() -> void
Waits for all the other threads to reach the barrier.
The logical and function object.
The counting function object.
The logical or function object.
void operator()(std::atomic< int > &result, bool value)
void operator()(std::atomic< int > &result, bool value)
void operator()(std::atomic< int > &result, bool value)