alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
AccCpuOmp2Blocks.hpp
Go to the documentation of this file.
1/* Copyright 2026 Axel Huebl, Benjamin Worpitz, René Widera, Jan Stephan, Bernhard Manfred Gruber, Andrea Bocci,
2 * Maria Michailidi
3 * SPDX-License-Identifier: MPL-2.0
4 */
5
6#pragma once
7
8// Base classes.
27
28// Specialized traits.
29#include "alpaka/acc/Traits.hpp"
30#include "alpaka/dev/Traits.hpp"
31#include "alpaka/idx/Traits.hpp"
34
35// Implementation details.
36#include "alpaka/acc/Tag.hpp"
39#include "alpaka/dev/DevCpu.hpp"
40
41#ifdef __cpp_lib_format
42# include <format>
43#endif
44#include <limits>
45#include <string>
46
47#ifdef ALPAKA_ACC_CPU_B_OMP2_T_SEQ_ENABLED
48
49# if _OPENMP < 200203
50# error If ALPAKA_ACC_CPU_B_OMP2_T_SEQ_ENABLED is set, the compiler has to support OpenMP 2.0 or higher!
51# endif
52
53namespace alpaka
54{
55 template<typename TDim, typename TIdx, typename TKernelFnObj, bool TCooperative, typename... TArgs>
57
58 //! The CPU OpenMP 2.0 block accelerator.
59 //!
60 //! This accelerator allows parallel kernel execution on a CPU device.
61 //! It uses OpenMP 2.0 to implement the grid block parallelism.
62 //! The block idx is restricted to 1x1x1.
63 template<typename TDim, typename TIdx>
64 class AccCpuOmp2Blocks final
65 : public WorkDivMembers<TDim, TIdx>
66 , public gb::IdxGbRef<TDim, TIdx>
67 , public bt::IdxBtZero<TDim, TIdx>
68 , public AtomicHierarchy<
69 AtomicCpu, // grid atomics
70 AtomicOmpBuiltIn, // block atomics
71 AtomicNoOp> // thread atomics
72 , public math::MathStdLib
74 , public BlockSharedMemStMember<>
75 , public BlockSyncNoOp
76 , public GridSyncOmp
77 , public IntrinsicCpu
78 , public MemFenceOmp2Blocks
79# ifdef ALPAKA_DISABLE_VENDOR_RNG
80 , public rand::RandDefault
81# else
82 , public rand::RandStdLib
83# endif
85 , public interface::Implements<InterfaceAcc, AccCpuOmp2Blocks<TDim, TIdx>>
86 {
87 static_assert(
88 sizeof(TIdx) >= sizeof(int),
89 "Index type is not supported, consider using int or a larger type.");
90
91 public:
92 // Partial specialization with the correct TDim and TIdx is not allowed.
93 template<typename TDim2, typename TIdx2, typename TKernelFnObj, bool TCooperative, typename... TArgs>
94 friend class ::alpaka::TaskKernelCpuOmp2Blocks;
95
98 auto operator=(AccCpuOmp2Blocks const&) -> AccCpuOmp2Blocks& = delete;
100
101 private:
102 template<typename TWorkDiv>
103 ALPAKA_FN_HOST AccCpuOmp2Blocks(TWorkDiv const& workDiv, std::size_t const& blockSharedMemDynSizeBytes)
104 : WorkDivMembers<TDim, TIdx>(workDiv)
105 , gb::IdxGbRef<TDim, TIdx>(m_gridBlockIdx)
106 , BlockSharedMemDynMember<>(blockSharedMemDynSizeBytes)
108 , m_gridBlockIdx(Vec<TDim, TIdx>::zeros())
109 {
110 }
111
112 private:
113 // getIdx
114 Vec<TDim, TIdx> mutable m_gridBlockIdx; //!< The index of the currently executed block.
115 };
116
117 namespace trait
118 {
119 //! The CPU OpenMP 2.0 block accelerator accelerator type trait specialization.
120 template<typename TDim, typename TIdx>
121 struct AccType<AccCpuOmp2Blocks<TDim, TIdx>>
122 {
124 };
125
126 //! The CPU OpenMP 2.0 block single thread accelerator type trait specialization.
127 template<typename TDim, typename TIdx>
128 struct IsSingleThreadAcc<AccCpuOmp2Blocks<TDim, TIdx>> : std::true_type
129 {
130 };
131
132 //! The CPU OpenMP 2.0 block multi thread accelerator type trait specialization.
133 template<typename TDim, typename TIdx>
134 struct IsMultiThreadAcc<AccCpuOmp2Blocks<TDim, TIdx>> : std::false_type
135 {
136 };
137
138 //! The CPU OpenMP 2.0 block accelerator device properties get trait specialization.
139 template<typename TDim, typename TIdx>
141 {
143 {
144 return {// m_multiProcessorCount
145 alpaka::core::clipCast<TIdx>(omp_get_max_threads()),
146 // m_gridBlockExtentMax
147 Vec<TDim, TIdx>::all(std::numeric_limits<TIdx>::max()),
148 // m_gridBlockCountMax
149 std::numeric_limits<TIdx>::max(),
150 // m_blockThreadExtentMax
152 // m_blockThreadCountMax
153 static_cast<TIdx>(1),
154 // m_threadElemExtentMax
155 Vec<TDim, TIdx>::all(std::numeric_limits<TIdx>::max()),
156 // m_threadElemCountMax
157 std::numeric_limits<TIdx>::max(),
158 // m_sharedMemSizeBytes
160 // m_globalMemSizeBytes
161 getMemBytes(dev),
162 // m_cooperativeLaunch
163 true};
164 }
165 };
166
167 //! The CPU OpenMP 2.0 block accelerator name trait specialization.
168 template<typename TDim, typename TIdx>
169 struct GetAccName<AccCpuOmp2Blocks<TDim, TIdx>>
170 {
171 ALPAKA_FN_HOST static auto getAccName() -> std::string
172 {
173# if ALPAKA_COMP_CLANG
174# pragma clang diagnostic push
175# pragma clang diagnostic ignored "-Wexit-time-destructors"
176# endif
177 using namespace std::literals;
178 static std::string const accName =
179# ifdef __cpp_lib_format
180 std::format("AccCpuOmp2Blocks<{},{}>", TDim::value, core::demangled<TIdx>);
181# else
182 "AccCpuOmp2Blocks<"s + std::to_string(TDim::value) + ","s + std::string(core::demangled<TIdx>)
183 + ">"s;
184# endif
185 return accName;
186# if ALPAKA_COMP_CLANG
187# pragma clang diagnostic pop
188# endif
189 }
190 };
191
192 //! The CPU OpenMP 2.0 block accelerator device type trait specialization.
193 template<typename TDim, typename TIdx>
194 struct DevType<AccCpuOmp2Blocks<TDim, TIdx>>
195 {
196 using type = DevCpu;
197 };
198
199 //! The CPU OpenMP 2.0 block accelerator dimension getter trait specialization.
200 template<typename TDim, typename TIdx>
201 struct DimType<AccCpuOmp2Blocks<TDim, TIdx>>
202 {
203 using type = TDim;
204 };
205
206 //! The CPU OpenMP 2.0 block accelerator execution task type trait specialization.
207 template<typename TDim, typename TIdx, typename TWorkDiv, typename TKernelFnObj, typename... TArgs>
208 struct CreateTaskKernel<AccCpuOmp2Blocks<TDim, TIdx>, TWorkDiv, TKernelFnObj, TArgs...>
209 {
211 TWorkDiv const& workDiv,
212 TKernelFnObj const& kernelFnObj,
213 TArgs&&... args)
214 {
215 if(workDiv.m_blockThreadExtent.prod() != static_cast<TIdx>(1u))
216 {
217 throw std::runtime_error(
218 "The given work division is not valid for a single thread Acc: "
219 + getAccName<AccCpuOmp2Blocks<TDim, TIdx>>() + ". Threads per block should be 1!");
220 }
221
222 return TaskKernelCpuOmp2Blocks<TDim, TIdx, TKernelFnObj, false, TArgs...>(
223 workDiv,
224 kernelFnObj,
225 std::forward<TArgs>(args)...);
226 }
227 };
228
229 //! The CPU OpenMP 2.0 block accelerator execution cooperative task type trait specialization.
230 template<typename TDim, typename TIdx, typename TWorkDiv, typename TKernelFnObj, typename... TArgs>
231 struct CreateTaskCooperativeKernel<AccCpuOmp2Blocks<TDim, TIdx>, TWorkDiv, TKernelFnObj, TArgs...>
232 {
234 TWorkDiv const& workDiv,
235 TKernelFnObj const& kernelFnObj,
236 TArgs&&... args)
237 {
238 auto const gridBlockExtent = getWorkDiv<Grid, Blocks>(workDiv);
239 auto const maxBlocks = omp_get_max_threads();
240 if(gridBlockExtent.prod() > static_cast<TIdx>(maxBlocks))
241 {
242 throw std::runtime_error(
243 "The number of requested blocks is larger than maximuma of the device for OpenMP 2.0 blocks "
244 "accelerator. Requested: "
245 + std::to_string(gridBlockExtent.prod()) + ", maximum allowed: " + std::to_string(maxBlocks)
246 + ". Use getMaxActiveBlocks().");
247 }
248
249 return TaskKernelCpuOmp2Blocks<TDim, TIdx, TKernelFnObj, true, TArgs...>(
250 workDiv,
251 kernelFnObj,
252 std::forward<TArgs>(args)...);
253 }
254 };
255
256 //! The CPU OpenMP 2.0 block execution task platform type trait specialization.
257 template<typename TDim, typename TIdx>
258 struct PlatformType<AccCpuOmp2Blocks<TDim, TIdx>>
259 {
261 };
262
263 //! The CPU OpenMP 2.0 block accelerator idx type trait specialization.
264 template<typename TDim, typename TIdx>
265 struct IdxType<AccCpuOmp2Blocks<TDim, TIdx>>
266 {
267 using type = TIdx;
268 };
269
270 template<typename TDim, typename TIdx>
271 struct AccToTag<alpaka::AccCpuOmp2Blocks<TDim, TIdx>>
272 {
274 };
275
276 template<typename TDim, typename TIdx>
281 } // namespace trait
282} // namespace alpaka
283
284#endif
The CPU OpenMP 2.0 block accelerator.
AccCpuOmp2Blocks(AccCpuOmp2Blocks const &)=delete
auto operator=(AccCpuOmp2Blocks const &) -> AccCpuOmp2Blocks &=delete
AccCpuOmp2Blocks(AccCpuOmp2Blocks &&)=delete
auto operator=(AccCpuOmp2Blocks &&) -> AccCpuOmp2Blocks &=delete
Dynamic block shared memory provider using fixed-size member array to allocate memory on the stack or...
static constexpr auto staticAllocBytes() -> std::uint32_t
auto staticMemCapacity() const -> std::uint32_t
Static block shared memory provider using a pointer to externally allocated fixed-size memory,...
The no op block synchronization.
The CPU device handle.
Definition DevCpu.hpp:56
The grid synchronization for OMP accelerators.
The CPU intrinsic.
The CPU OpenMP 2.0 block memory fence.
The CPU OpenMP 2.0 block accelerator execution task.
A n-dimensional vector.
Definition Vec.hpp:38
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
A basic class holding the work division as grid block extent, block thread and thread element extent.
A zero block thread index provider.
Definition IdxBtZero.hpp:19
A IdxGbRef grid block index.
Definition IdxGbRef.hpp:20
IdxGbRef(Vec< TDim, TIdx > const &gridBlockIdx)
Definition IdxGbRef.hpp:22
The standard library math trait specializations.
"Tiny" state mersenne twister implementation
#define ALPAKA_FN_HOST
Definition Common.hpp:43
constexpr std::string_view demangled
auto clipCast(V const &val) -> T
Definition ClipCast.hpp:16
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
ALPAKA_FN_HOST auto getMemBytes(TDev const &dev) -> std::size_t
Definition Traits.hpp:102
ALPAKA_FN_HOST auto getAccName() -> std::string
Definition Traits.hpp:105
The acceleration properties on a device.
The CPU device platform.
Tag used in class inheritance hierarchies that describes that a specific interface (TInterface) is im...
Definition Interface.hpp:15
The accelerator type trait.
Definition Traits.hpp:42
static auto createTaskCooperativeKernel(TWorkDiv const &workDiv, TKernelFnObj const &kernelFnObj, TArgs &&... args)
The cooperative kernel execution task creation trait.
Definition Traits.hpp:46
static auto createTaskKernel(TWorkDiv const &workDiv, TKernelFnObj const &kernelFnObj, TArgs &&... args)
The kernel execution task creation trait.
Definition Traits.hpp:37
The device type trait.
Definition Traits.hpp:23
The dimension getter type trait.
Definition Traits.hpp:14
static auto getAccDevProps(DevCpu const &dev) -> alpaka::AccDevProps< TDim, TIdx >
The device properties get trait.
Definition Traits.hpp:66
The accelerator name trait.
Definition Traits.hpp:73
The idx type trait.
Definition Traits.hpp:25
The multi thread accelerator trait.
Definition Traits.hpp:61
The single thread accelerator trait.
Definition Traits.hpp:51
The platform type trait.
Definition Traits.hpp:38
The single-threaded warp to emulate it on CPUs.