alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
TaskKernelCpuTbbBlocks.hpp
Go to the documentation of this file.
1/* Copyright 2026 Benjamin Worpitz, Erik Zenker, René Widera, Felice Pantaleo, Bernhard Manfred Gruber
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7// Specialized traits.
10#include "alpaka/dim/Traits.hpp"
11#include "alpaka/idx/Traits.hpp"
13
14// Implementation details.
16#include "alpaka/core/Decay.hpp"
17#include "alpaka/dev/DevCpu.hpp"
18#include "alpaka/idx/MapIdx.hpp"
24
25#include <functional>
26#include <stdexcept>
27#include <tuple>
28#include <type_traits>
29#if ALPAKA_DEBUG >= ALPAKA_DEBUG_MINIMAL
30# include <iostream>
31#endif
32
33#ifdef ALPAKA_ACC_CPU_B_TBB_T_SEQ_ENABLED
34
35# include <tbb/blocked_range.h>
36# include <tbb/parallel_for.h>
37# include <tbb/task_group.h>
38
39namespace alpaka
40{
41 //! The CPU TBB block accelerator execution task.
42 template<typename TDim, typename TIdx, typename TKernelFnObj, typename... TArgs>
43 class TaskKernelCpuTbbBlocks final : public WorkDivMembers<TDim, TIdx>
44 {
45 public:
46 template<typename TWorkDiv>
47 ALPAKA_FN_HOST TaskKernelCpuTbbBlocks(TWorkDiv&& workDiv, TKernelFnObj const& kernelFnObj, TArgs&&... args)
48 : WorkDivMembers<TDim, TIdx>(std::forward<TWorkDiv>(workDiv))
49 , m_kernelFnObj(kernelFnObj)
50 , m_args(std::forward<TArgs>(args)...)
51 {
52 static_assert(
53 Dim<std::decay_t<TWorkDiv>>::value == TDim::value,
54 "The work division and the execution task have to be of the same dimensionality!");
55 }
56
57 //! Executes the kernel function object.
58 ALPAKA_FN_HOST auto operator()() const -> void
59 {
61
62 auto const gridBlockExtent = getWorkDiv<Grid, Blocks>(*this);
63 auto const blockThreadExtent = getWorkDiv<Block, Threads>(*this);
64 auto const threadElemExtent = getWorkDiv<Thread, Elems>(*this);
65
66 // Get the size of the block shared dynamic memory.
67 auto const blockSharedMemDynSizeBytes = std::apply(
68 [&](std::decay_t<TArgs> const&... args)
69 {
70 return getBlockSharedMemDynSizeBytes<AccCpuTbbBlocks<TDim, TIdx>>(
71 m_kernelFnObj,
72 blockThreadExtent,
73 threadElemExtent,
74 args...);
75 },
76 m_args);
77
78# if ALPAKA_DEBUG >= ALPAKA_DEBUG_FULL
79 std::cout << __func__ << " blockSharedMemDynSizeBytes: " << blockSharedMemDynSizeBytes << " B"
80 << std::endl;
81# endif
82
83 // The number of blocks in the grid.
84 TIdx const numBlocksInGrid = gridBlockExtent.prod();
85
86 tbb::this_task_arena::isolate(
87 [&]
88 {
89 // Create a shared barrier for grid sync, which will be passed by reference to each thread to
90 // achieve shared state
91 core::tbb::BarrierThread<TIdx> barrier(numBlocksInGrid);
92
93 tbb::parallel_for(
94 static_cast<TIdx>(0),
95 static_cast<TIdx>(numBlocksInGrid),
96 [&](TIdx i)
97 {
98 AccCpuTbbBlocks<TDim, TIdx> acc(
99 *static_cast<WorkDivMembers<TDim, TIdx> const*>(this),
100 blockSharedMemDynSizeBytes,
101 barrier);
102
103 acc.m_gridBlockIdx
104 = mapIdx<TDim::value>(Vec<DimInt<1u>, TIdx>(static_cast<TIdx>(i)), gridBlockExtent);
105
106 std::apply(
107 [&](auto&&... argsWithAcc) {
108 detail::checkKernelReturnType(
109 m_kernelFnObj,
110 std::forward<decltype(argsWithAcc)>(argsWithAcc)...);
111 },
112 std::tuple_cat(std::tie(acc), m_args));
113 std::apply(m_kernelFnObj, std::tuple_cat(std::tie(acc), m_args));
114
115 freeSharedVars(acc);
116 });
117 });
118 }
119
120 private:
121 TKernelFnObj m_kernelFnObj;
122 std::tuple<std::decay_t<TArgs>...> m_args;
123 };
124
125 namespace trait
126 {
127 //! The CPU TBB block execution task accelerator type trait specialization.
128 template<typename TDim, typename TIdx, typename TKernelFnObj, typename... TArgs>
129 struct AccType<TaskKernelCpuTbbBlocks<TDim, TIdx, TKernelFnObj, TArgs...>>
130 {
131 using type = AccCpuTbbBlocks<TDim, TIdx>;
132 };
133
134 //! The CPU TBB block execution task device type trait specialization.
135 template<typename TDim, typename TIdx, typename TKernelFnObj, typename... TArgs>
136 struct DevType<TaskKernelCpuTbbBlocks<TDim, TIdx, TKernelFnObj, TArgs...>>
137 {
138 using type = DevCpu;
139 };
140
141 //! The CPU TBB block execution task dimension getter trait specialization.
142 template<typename TDim, typename TIdx, typename TKernelFnObj, typename... TArgs>
143 struct DimType<TaskKernelCpuTbbBlocks<TDim, TIdx, TKernelFnObj, TArgs...>>
144 {
145 using type = TDim;
146 };
147
148 //! The CPU TBB block execution task platform type trait specialization.
149 template<typename TDim, typename TIdx, typename TKernelFnObj, typename... TArgs>
150 struct PlatformType<TaskKernelCpuTbbBlocks<TDim, TIdx, TKernelFnObj, TArgs...>>
151 {
152 using type = PlatformCpu;
153 };
154
155 //! The CPU TBB block execution task idx type trait specialization.
156 template<typename TDim, typename TIdx, typename TKernelFnObj, typename... TArgs>
157 struct IdxType<TaskKernelCpuTbbBlocks<TDim, TIdx, TKernelFnObj, TArgs...>>
158 {
159 using type = TIdx;
160 };
161
162 //! \brief Specialisation of the class template FunctionAttributes
163 //! \tparam TDev The device type.
164 //! \tparam TDim The dimensionality of the accelerator device properties.
165 //! \tparam TIdx The idx type of the accelerator device properties.
166 //! \tparam TKernelFn Kernel function object type.
167 //! \tparam TArgs Kernel function object argument types as a parameter pack.
168 template<typename TDev, typename TDim, typename TIdx, typename TKernelFn, typename... TArgs>
169 struct FunctionAttributes<AccCpuTbbBlocks<TDim, TIdx>, TDev, TKernelFn, TArgs...>
170 {
171 //! \param dev The device instance
172 //! \param kernelFn The kernel function object which should be executed.
173 //! \param args The kernel invocation arguments.
174 //! \return KernelFunctionAttributes instance. The default version always returns an instance with zero
175 //! fields. For CPU, the field of max threads allowed by kernel function for the block is 1.
177 TDev const& dev,
178 [[maybe_unused]] TKernelFn const& kernelFn,
179 [[maybe_unused]] TArgs&&... args) -> alpaka::KernelFunctionAttributes
180 {
181 alpaka::KernelFunctionAttributes kernelFunctionAttributes;
182
183 // set function properties for maxThreadsPerBlock to device properties, since API doesn't have function
184 // properties function.
186 kernelFunctionAttributes.maxThreadsPerBlock = static_cast<int>(props.m_blockThreadCountMax);
187 kernelFunctionAttributes.maxDynamicSharedSizeBytes
188 = static_cast<int>(alpaka::BlockSharedDynMemberAllocKiB * 1024);
189 return kernelFunctionAttributes;
190 }
191 };
192
193 //! The CPU CPU OMP2 blocks get max active blocks for cooperative kernel specialization.
194 template<typename TDev, typename TKernelFnObj, typename TDim, typename TIdx, typename... TArgs>
195 struct MaxActiveBlocks<AccCpuTbbBlocks<TDim, TIdx>, TDev, TKernelFnObj, TDim, TIdx, TArgs...>
196 {
198 TKernelFnObj const& /*kernelFnObj*/,
199 TDev const& device,
200 alpaka::Vec<TDim, TIdx> const& /*blockThreadExtent*/,
201 alpaka::Vec<TDim, TIdx> const& /*threadElemExtent*/,
202 TArgs const&... /*args*/) -> int
203 {
204 return static_cast<int>(
205 trait::GetAccDevProps<AccCpuTbbBlocks<TDim, TIdx>>::getAccDevProps(device).m_multiProcessorCount);
206 }
207 };
208 } // namespace trait
209} // namespace alpaka
210
211#endif
#define ALPAKA_DEBUG_MINIMAL_LOG_SCOPE
Definition Debug.hpp:55
A n-dimensional vector.
Definition Vec.hpp:38
#define ALPAKA_FN_HOST
Definition Common.hpp:43
The alpaka accelerator library.
constexpr std::uint32_t BlockSharedDynMemberAllocKiB
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.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getMaxActiveBlocks(TDev const &device, TKernelFnObj const &kernelFnObj, alpaka::Vec< TDim, TIdx > const &blockThreadExtent, alpaka::Vec< TDim, TIdx > const &threadElemExtent, TArgs const &... args) -> int
Definition Traits.hpp:248
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_ACC auto freeSharedVars(TBlockSharedMemSt &blockSharedMemSt) -> void
Frees all memory used by block shared variables.
Definition Traits.hpp:54
STL namespace.
Kernel function attributes struct. Attributes are filled by calling the API of the accelerator using ...
static ALPAKA_FN_HOST auto getFunctionAttributes(TDev const &dev, TKernelFnObj const &kernelFn, TArgs &&... args) -> alpaka::KernelFunctionAttributes
Definition Traits.hpp:100