alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
AccCpuTbbBlocks.hpp
Go to the documentation of this file.
1/* Copyright 2026 Axel Huebl, Benjamin Worpitz, Erik Zenker, René Widera, Jan Stephan, Bernhard Manfred Gruber,
2 * Andrea Bocci
3 * SPDX-License-Identifier: MPL-2.0
4 */
5
6#pragma once
7
8// Base classes.
26
27// Specialized traits.
28#include "alpaka/acc/Traits.hpp"
29#include "alpaka/dev/Traits.hpp"
30#include "alpaka/idx/Traits.hpp"
33
34// Implementation details.
35#include "alpaka/acc/Tag.hpp"
39#include "alpaka/dev/DevCpu.hpp"
40
41#ifdef __cpp_lib_format
42# include <format>
43#endif
44#include <string>
45#include <typeinfo>
46
47#ifdef ALPAKA_ACC_CPU_B_TBB_T_SEQ_ENABLED
48
49# include <tbb/tbb.h>
50
51namespace alpaka
52{
53 template<typename TDim, typename TIdx, typename TKernelFnObj, typename... TArgs>
54 class TaskKernelCpuTbbBlocks;
55
56 //! The CPU TBB block accelerator.
57 template<typename TDim, typename TIdx>
58 class AccCpuTbbBlocks final
59 : public WorkDivMembers<TDim, TIdx>
60 , public gb::IdxGbRef<TDim, TIdx>
61 , public bt::IdxBtZero<TDim, TIdx>
62 , public AtomicHierarchy<
63 AtomicCpu, // grid atomics
64 AtomicCpu, // block atomics
65 AtomicNoOp> // thread atomics
66 , public math::MathStdLib
67 , public BlockSharedMemDynMember<>
68 , public BlockSharedMemStMember<>
69 , public BlockSyncNoOp
70 , public GridSyncBarrierTbb<TIdx>
71 , public IntrinsicCpu
72 , public MemFenceCpu
73# ifdef ALPAKA_DISABLE_VENDOR_RNG
74 , public rand::RandDefault
75# else
76 , public rand::RandStdLib
77# endif
78 , public warp::WarpSingleThread
79 , public interface::Implements<InterfaceAcc, AccCpuTbbBlocks<TDim, TIdx>>
80 {
81 static_assert(
82 sizeof(TIdx) >= sizeof(int),
83 "Index type is not supported, consider using int or a larger type.");
84
85 public:
86 // Partial specialization with the correct TDim and TIdx is not allowed.
87 template<typename TDim2, typename TIdx2, typename TKernelFnObj, typename... TArgs>
88 friend class ::alpaka::TaskKernelCpuTbbBlocks;
89
90 AccCpuTbbBlocks(AccCpuTbbBlocks const&) = delete;
91 AccCpuTbbBlocks(AccCpuTbbBlocks&&) = delete;
92 auto operator=(AccCpuTbbBlocks const&) -> AccCpuTbbBlocks& = delete;
93 auto operator=(AccCpuTbbBlocks&&) -> AccCpuTbbBlocks& = delete;
94
95 private:
96 template<typename TWorkDiv>
97 ALPAKA_FN_HOST AccCpuTbbBlocks(
98 TWorkDiv const& workDiv,
99 std::size_t const& blockSharedMemDynSizeBytes,
100 core::tbb::BarrierThread<TIdx>& barrier)
101 : WorkDivMembers<TDim, TIdx>(workDiv)
102 , gb::IdxGbRef<TDim, TIdx>(m_gridBlockIdx)
103 , BlockSharedMemDynMember<>(blockSharedMemDynSizeBytes)
104 , BlockSharedMemStMember<>(staticMemBegin(), staticMemCapacity())
105 , GridSyncBarrierTbb<TIdx>(barrier)
106 , m_gridBlockIdx(Vec<TDim, TIdx>::zeros())
107 {
108 }
109
110 private:
111 // getIdx
112 Vec<TDim, TIdx> mutable m_gridBlockIdx; //!< The index of the currently executed block.
113 };
114
115 namespace trait
116 {
117 //! The CPU TBB block accelerator type trait specialization.
118 template<typename TDim, typename TIdx>
119 struct AccType<AccCpuTbbBlocks<TDim, TIdx>>
120 {
121 using type = AccCpuTbbBlocks<TDim, TIdx>;
122 };
123
124 //! The CPU TBB block single thread accelerator type trait specialization.
125 template<typename TDim, typename TIdx>
126 struct IsSingleThreadAcc<AccCpuTbbBlocks<TDim, TIdx>> : std::true_type
127 {
128 };
129
130 //! The CPU TBB block multi thread accelerator type trait specialization.
131 template<typename TDim, typename TIdx>
132 struct IsMultiThreadAcc<AccCpuTbbBlocks<TDim, TIdx>> : std::false_type
133 {
134 };
135
136 //! The CPU TBB block accelerator device properties get trait specialization.
137 template<typename TDim, typename TIdx>
138 struct GetAccDevProps<AccCpuTbbBlocks<TDim, TIdx>>
139 {
140 ALPAKA_FN_HOST static auto getAccDevProps(DevCpu const& dev) -> AccDevProps<TDim, TIdx>
141 {
142 return {// m_multiProcessorCount
143 alpaka::core::clipCast<TIdx>(tbb::this_task_arena::max_concurrency()),
144 // m_gridBlockExtentMax
145 Vec<TDim, TIdx>::all(std::numeric_limits<TIdx>::max()),
146 // m_gridBlockCountMax
147 std::numeric_limits<TIdx>::max(),
148 // m_blockThreadExtentMax
150 // m_blockThreadCountMax
151 static_cast<TIdx>(1),
152 // m_threadElemExtentMax
153 Vec<TDim, TIdx>::all(std::numeric_limits<TIdx>::max()),
154 // m_threadElemCountMax
155 std::numeric_limits<TIdx>::max(),
156 // m_sharedMemSizeBytes
157 static_cast<size_t>(AccCpuTbbBlocks<TDim, TIdx>::staticAllocBytes()),
158 // m_globalMemSizeBytes
159 getMemBytes(dev),
160 // m_cooperativeLaunch
161 true};
162 }
163 };
164
165 //! The CPU TBB block accelerator name trait specialization.
166 template<typename TDim, typename TIdx>
167 struct GetAccName<AccCpuTbbBlocks<TDim, TIdx>>
168 {
169 ALPAKA_FN_HOST static auto getAccName() -> std::string
170 {
171# if ALPAKA_COMP_CLANG
172# pragma clang diagnostic push
173# pragma clang diagnostic ignored "-Wexit-time-destructors"
174# endif
175 using namespace std::literals;
176 static std::string const accName =
177# ifdef __cpp_lib_format
178 std::format("AccCpuTbbBlocks<{},{}>", TDim::value, core::demangled<TIdx>);
179# else
180 "AccCpuTbbBlocks<"s + std::to_string(TDim::value) + ","s + std::string(core::demangled<TIdx>)
181 + ">"s;
182# endif
183 return accName;
184# if ALPAKA_COMP_CLANG
185# pragma clang diagnostic pop
186# endif
187 }
188 };
189
190 //! The CPU TBB block accelerator device type trait specialization.
191 template<typename TDim, typename TIdx>
192 struct DevType<AccCpuTbbBlocks<TDim, TIdx>>
193 {
194 using type = DevCpu;
195 };
196
197 //! The CPU TBB block accelerator dimension getter trait specialization.
198 template<typename TDim, typename TIdx>
199 struct DimType<AccCpuTbbBlocks<TDim, TIdx>>
200 {
201 using type = TDim;
202 };
203
204 //! The CPU TBB block accelerator execution task type trait specialization.
205 template<typename TDim, typename TIdx, typename TWorkDiv, typename TKernelFnObj, typename... TArgs>
206 struct CreateTaskKernel<AccCpuTbbBlocks<TDim, TIdx>, TWorkDiv, TKernelFnObj, TArgs...>
207 {
209 TWorkDiv const& workDiv,
210 TKernelFnObj const& kernelFnObj,
211 TArgs&&... args)
212 {
213 if(workDiv.m_blockThreadExtent.prod() != static_cast<TIdx>(1u))
214 {
215 throw std::runtime_error(
216 "The given work division is not valid for a single thread Acc: "
217 + getAccName<AccCpuTbbBlocks<TDim, TIdx>>() + ". Threads per block should be 1!");
218 }
219
220 return TaskKernelCpuTbbBlocks<TDim, TIdx, TKernelFnObj, TArgs...>(
221 workDiv,
222 kernelFnObj,
223 std::forward<TArgs>(args)...);
224 }
225 };
226
227 //! The CPU TBB block accelerator execution cooperative task type trait specialization.
228 template<typename TDim, typename TIdx, typename TWorkDiv, typename TKernelFnObj, typename... TArgs>
229 struct CreateTaskCooperativeKernel<AccCpuTbbBlocks<TDim, TIdx>, TWorkDiv, TKernelFnObj, TArgs...>
230 {
232 TWorkDiv const& workDiv,
233 TKernelFnObj const& kernelFnObj,
234 TArgs&&... args)
235 {
236 if(workDiv.m_blockThreadExtent.prod() != static_cast<TIdx>(1u))
237 {
238 throw std::runtime_error(
239 "The given work division is not valid for a single thread Acc: "
240 + getAccName<AccCpuTbbBlocks<TDim, TIdx>>() + ". Threads per block should be 1!");
241 }
242 auto const gridBlockExtent = getWorkDiv<Grid, Blocks>(workDiv);
243 auto const maxBlocks = tbb::this_task_arena::max_concurrency();
244 if(gridBlockExtent.prod() > static_cast<TIdx>(maxBlocks))
245 {
246 throw std::runtime_error(
247 "The number of requested blocks is larger than maximuma of the device for TBB "
248 "accelerator. Requested: "
249 + std::to_string(gridBlockExtent.prod()) + ", maximum allowed: " + std::to_string(maxBlocks)
250 + ". Use getMaxActiveBlocks().");
251 }
252
253 return TaskKernelCpuTbbBlocks<TDim, TIdx, TKernelFnObj, TArgs...>(
254 workDiv,
255 kernelFnObj,
256 std::forward<TArgs>(args)...);
257 }
258 };
259
260 //! The CPU TBB block execution task platform type trait specialization.
261 template<typename TDim, typename TIdx>
262 struct PlatformType<AccCpuTbbBlocks<TDim, TIdx>>
263 {
264 using type = PlatformCpu;
265 };
266
267 //! The CPU TBB block accelerator idx type trait specialization.
268 template<typename TDim, typename TIdx>
269 struct IdxType<AccCpuTbbBlocks<TDim, TIdx>>
270 {
271 using type = TIdx;
272 };
273
274 template<typename TDim, typename TIdx>
275 struct AccToTag<alpaka::AccCpuTbbBlocks<TDim, TIdx>>
276 {
277 using type = alpaka::TagCpuTbbBlocks;
278 };
279
280 template<typename TDim, typename TIdx>
281 struct TagToAcc<alpaka::TagCpuTbbBlocks, TDim, TIdx>
282 {
283 using type = alpaka::AccCpuTbbBlocks<TDim, TIdx>;
284 };
285 } // namespace trait
286} // namespace alpaka
287
288#endif
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC constexpr auto ones() -> Vec< TDim, TVal >
One value constructor.
Definition Vec.hpp:106
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC constexpr auto all(TVal const &val) -> Vec< TDim, TVal >
Single value constructor.
Definition Vec.hpp:89
#define ALPAKA_FN_HOST
Definition Common.hpp:43
constexpr std::string_view demangled
auto clipCast(V const &val) -> T
Definition ClipCast.hpp:16
TinyMersenneTwister RandStdLib
The alpaka accelerator library.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getWorkDiv(TWorkDiv const &workDiv) -> Vec< Dim< TWorkDiv >, Idx< TWorkDiv > >
Get the extent requested.
Definition Traits.hpp:33
typename trait::AccToTag< TAcc >::type AccToTag
maps an acc type to a tag type
Definition Tag.hpp:69
ALPAKA_FN_HOST auto getAccDevProps(TDev const &dev) -> AccDevProps< Dim< TAcc >, Idx< TAcc > >
Definition Traits.hpp:95
WorkDivMembers(alpaka::Vec< TDim, TIdx > const &gridBlockExtent, alpaka::Vec< TDim, TIdx > const &blockThreadExtent, alpaka::Vec< TDim, TIdx > const &elemExtent) -> WorkDivMembers< TDim, TIdx >
Deduction guide for the constructor which can be called without explicit template type parameters.
Vec(TFirstIndex &&, TRestIndices &&...) -> Vec< DimInt< 1+sizeof...(TRestIndices)>, std::decay_t< TFirstIndex > >
ALPAKA_FN_HOST auto createTaskKernel(TWorkDiv const &workDiv, TKernelFnObj const &kernelFnObj, TArgs &&... args)
Creates a kernel execution task.
Definition Traits.hpp:423
ALPAKA_FN_HOST auto getMemBytes(TDev const &dev) -> std::size_t
Definition Traits.hpp:102
ALPAKA_FN_HOST auto createTaskCooperativeKernel(TWorkDiv const &workDiv, TKernelFnObj const &kernelFnObj, TArgs &&... args)
Creates a cooperative kernel execution task. Allows to use grid sync, but disables dynamic parallelis...
Definition Traits.hpp:467
typename trait::TagToAcc< TTag, TDim, TIdx >::type TagToAcc
maps a tag type to an acc type
Definition Tag.hpp:76
ALPAKA_FN_HOST auto getAccName() -> std::string
Definition Traits.hpp:105
alpaka::meta::InheritFromList< alpaka::meta::Unique< std::tuple< TGridAtomic, TBlockAtomic, TThreadAtomic, interface::Implements< ConceptAtomicGrids, TGridAtomic >, interface::Implements< ConceptAtomicBlocks, TBlockAtomic >, interface::Implements< ConceptAtomicThreads, TThreadAtomic > > > > AtomicHierarchy
build a single class to inherit from different atomic implementations
static ALPAKA_FN_HOST auto getAccName() -> std::string
Definition Traits.hpp:74