alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
BarrierTbb.h
Go to the documentation of this file.
1/* Copyright 2026 Mykhailo Varvarin, Maria Michailidi, Andrea Bocci
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#ifdef ALPAKA_ACC_CPU_B_TBB_T_SEQ_ENABLED
8
10
11# include <oneapi/tbb/task.h>
12
13# include <mutex>
14# include <vector>
15
16namespace alpaka::core
17{
18 namespace tbb
19 {
20 // A reusable barrier for TBB tasks using suspend/resume
21 template<typename TIdx>
22 class BarrierThread final
23 {
24 public:
25 explicit BarrierThread(TIdx const& threadCount) : m_threadCount(threadCount)
26 {
27 assertValueUnsigned(threadCount);
28 m_suspended.reserve(static_cast<std::size_t>(threadCount));
29 }
30
31 // Called from inside a task to wait until all have arrived
32 auto wait() -> void
33 {
34 oneapi::tbb::task::suspend(
35 [this](oneapi::tbb::task::suspend_point sp)
36 {
37 // The last task to arrive takes all the suspended tasks, and leaves the barrier empty and
38 // ready to be reused by the next wait().
39 std::vector<oneapi::tbb::task::suspend_point> arrived;
40 {
41 std::lock_guard<std::mutex> lock{m_mutex};
42 m_suspended.push_back(sp);
43 if(m_suspended.size() == static_cast<std::size_t>(m_threadCount))
44 {
45 arrived.reserve(m_suspended.capacity());
46 arrived.swap(m_suspended);
47 }
48 }
49
50 // Resume the tasks outside of the critical section.
51 for(auto point : arrived)
52 {
53 oneapi::tbb::task::resume(point);
54 }
55 });
56 }
57
58 private:
59 TIdx const m_threadCount;
60 std::mutex m_mutex;
61 std::vector<oneapi::tbb::task::suspend_point> m_suspended;
62 };
63 } // namespace tbb
64} // namespace alpaka::core
65
66#endif
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC constexpr auto assertValueUnsigned(TArg const &arg) -> void
This method checks integral values if they are greater or equal zero. The implementation prevents war...
Definition Assert.hpp:77
ALPAKA_FN_HOST auto wait(TAwaited const &awaited) -> void
Waits the thread for the completion of the given awaited action to complete.
Definition Traits.hpp:34