alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
AccCpuOmp2Threads.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 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7// Base classes.
25
26// Specialized traits.
27#include "alpaka/acc/Traits.hpp"
28#include "alpaka/dev/Traits.hpp"
29#include "alpaka/idx/Traits.hpp"
32
33// Implementation details.
34#include "alpaka/acc/Tag.hpp"
37#include "alpaka/dev/DevCpu.hpp"
38
39#ifdef __cpp_lib_format
40# include <format>
41#endif
42#include <limits>
43#include <string>
44
45#ifdef ALPAKA_ACC_CPU_B_SEQ_T_OMP2_ENABLED
46
47# if _OPENMP < 200203
48# error If ALPAKA_ACC_CPU_B_SEQ_T_OMP2_ENABLED is set, the compiler has to support OpenMP 2.0 or higher!
49# endif
50
51# include <omp.h>
52
53namespace alpaka
54{
55 template<typename TDim, typename TIdx, typename TKernelFnObj, typename... TArgs>
56 class TaskKernelCpuOmp2Threads;
57
58 //! The CPU OpenMP 2.0 thread accelerator.
59 //!
60 //! This accelerator allows parallel kernel execution on a CPU device.
61 //! It uses OpenMP 2.0 to implement the block thread parallelism.
62 template<typename TDim, typename TIdx>
64 : public WorkDivMembers<TDim, TIdx>
65 , public gb::IdxGbRef<TDim, TIdx>
66 , public bt::IdxBtOmp<TDim, TIdx>
67 , public AtomicHierarchy<
68 AtomicCpu, // grid atomics
69 AtomicOmpBuiltIn, // block atomics
70 AtomicOmpBuiltIn> // thread atomics
71 , public math::MathStdLib
74 , public BlockSyncBarrierOmp
75 , public GridSyncOmp
76 , public IntrinsicCpu
77 , public MemFenceOmp2Threads
78# ifdef ALPAKA_DISABLE_VENDOR_RNG
79 , public rand::RandDefault
80# else
81 , public rand::RandStdLib
82# endif
84 , public interface::Implements<InterfaceAcc, AccCpuOmp2Threads<TDim, TIdx>>
85 {
86 static_assert(
87 sizeof(TIdx) >= sizeof(int),
88 "Index type is not supported, consider using int or a larger type.");
89
90 public:
91 // Partial specialization with the correct TDim and TIdx is not allowed.
92 template<typename TDim2, typename TIdx2, typename TKernelFnObj, typename... TArgs>
93 friend class ::alpaka::TaskKernelCpuOmp2Threads;
94
99
100 private:
101 template<typename TWorkDiv>
102 ALPAKA_FN_HOST AccCpuOmp2Threads(TWorkDiv const& workDiv, std::size_t const& blockSharedMemDynSizeBytes)
103 : WorkDivMembers<TDim, TIdx>(workDiv)
104 , gb::IdxGbRef<TDim, TIdx>(m_gridBlockIdx)
105 , BlockSharedMemDynMember<>(blockSharedMemDynSizeBytes)
109 [this]() { syncBlockThreads(*this); },
110 []() noexcept { return (::omp_get_thread_num() == 0); })
111 , m_gridBlockIdx(Vec<TDim, TIdx>::zeros())
112 {
113 }
114
115 private:
116 // getIdx
117 Vec<TDim, TIdx> mutable m_gridBlockIdx; //!< The index of the currently executed block.
118 };
119
120 namespace trait
121 {
122 //! The CPU OpenMP 2.0 thread accelerator accelerator type trait specialization.
123 template<typename TDim, typename TIdx>
124 struct AccType<AccCpuOmp2Threads<TDim, TIdx>>
125 {
127 };
128
129 //! The CPU OpenMP 2.0 thread single thread accelerator type trait specialization.
130 template<typename TDim, typename TIdx>
131 struct IsSingleThreadAcc<AccCpuOmp2Threads<TDim, TIdx>> : std::false_type
132 {
133 };
134
135 //! The CPU OpenMP 2.0 thread multi thread accelerator type trait specialization.
136 template<typename TDim, typename TIdx>
137 struct IsMultiThreadAcc<AccCpuOmp2Threads<TDim, TIdx>> : std::true_type
138 {
139 };
140
141 //! The CPU OpenMP 2.0 thread accelerator device properties get trait specialization.
142 template<typename TDim, typename TIdx>
144 {
146 {
147# ifdef ALPAKA_CI
148 auto const blockThreadCountMax = alpaka::core::clipCast<TIdx>(std::min(4, ::omp_get_max_threads()));
149# else
150 auto const blockThreadCountMax = alpaka::core::clipCast<TIdx>(::omp_get_max_threads());
151# endif
152 auto const memBytes = getMemBytes(dev);
153 return {// m_multiProcessorCount
154 static_cast<TIdx>(1),
155 // m_gridBlockExtentMax
156 Vec<TDim, TIdx>::all(std::numeric_limits<TIdx>::max()),
157 // m_gridBlockCountMax
158 std::numeric_limits<TIdx>::max(),
159 // m_blockThreadExtentMax
160 Vec<TDim, TIdx>::all(blockThreadCountMax),
161 // m_blockThreadCountMax
162 blockThreadCountMax,
163 // m_threadElemExtentMax
164 Vec<TDim, TIdx>::all(std::numeric_limits<TIdx>::max()),
165 // m_threadElemCountMax
166 std::numeric_limits<TIdx>::max(),
167 // m_sharedMemSizeBytes
168 memBytes,
169 // m_globalMemSizeBytes
170 memBytes,
171 // m_cooperativeLaunch
172 true};
173 }
174 };
175
176 //! The CPU OpenMP 2.0 thread accelerator name trait specialization.
177 template<typename TDim, typename TIdx>
178 struct GetAccName<AccCpuOmp2Threads<TDim, TIdx>>
179 {
180 ALPAKA_FN_HOST static auto getAccName() -> std::string
181 {
182# if ALPAKA_COMP_CLANG
183# pragma clang diagnostic push
184# pragma clang diagnostic ignored "-Wexit-time-destructors"
185# endif
186 using namespace std::literals;
187 static std::string const accName =
188# ifdef __cpp_lib_format
189 std::format("AccCpuOmp2Threads<{},{}>", TDim::value, core::demangled<TIdx>);
190# else
191 "AccCpuOmp2Threads<"s + std::to_string(TDim::value) + ","s + std::string(core::demangled<TIdx>)
192 + ">"s;
193# endif
194 return accName;
195# if ALPAKA_COMP_CLANG
196# pragma clang diagnostic pop
197# endif
198 }
199 };
200
201 //! The CPU OpenMP 2.0 thread accelerator device type trait specialization.
202 template<typename TDim, typename TIdx>
203 struct DevType<AccCpuOmp2Threads<TDim, TIdx>>
204 {
205 using type = DevCpu;
206 };
207
208 //! The CPU OpenMP 2.0 thread accelerator dimension getter trait specialization.
209 template<typename TDim, typename TIdx>
210 struct DimType<AccCpuOmp2Threads<TDim, TIdx>>
211 {
212 using type = TDim;
213 };
214
215 //! The CPU OpenMP 2.0 thread accelerator execution task type trait specialization.
216 template<typename TDim, typename TIdx, typename TWorkDiv, typename TKernelFnObj, typename... TArgs>
217 struct CreateTaskKernel<AccCpuOmp2Threads<TDim, TIdx>, TWorkDiv, TKernelFnObj, TArgs...>
218 {
220 TWorkDiv const& workDiv,
221 TKernelFnObj const& kernelFnObj,
222 TArgs&&... args)
223 {
224 return TaskKernelCpuOmp2Threads<TDim, TIdx, TKernelFnObj, TArgs...>(
225 workDiv,
226 kernelFnObj,
227 std::forward<TArgs>(args)...);
228 }
229 };
230
231 //! The CPU OpenMP 2.0 thread accelerator execution cooperative task type trait specialization.
232 template<typename TDim, typename TIdx, typename TWorkDiv, typename TKernelFnObj, typename... TArgs>
233 struct CreateTaskCooperativeKernel<AccCpuOmp2Threads<TDim, TIdx>, TWorkDiv, TKernelFnObj, TArgs...>
234 {
236 TWorkDiv const& workDiv,
237 TKernelFnObj const& kernelFnObj,
238 TArgs&&... args)
239 {
240 auto const gridBlockExtent = getWorkDiv<Grid, Blocks>(workDiv);
241 if(gridBlockExtent.prod() != static_cast<TIdx>(1u))
242 {
243 throw std::runtime_error("OpenMP 2.0 thread accelerator supports only a single block operation "
244 "with cooperative kernel!\n"
245 "Consider useing a different CPU accelerator.");
246 }
247
248 return TaskKernelCpuOmp2Threads<TDim, TIdx, TKernelFnObj, TArgs...>(
249 workDiv,
250 kernelFnObj,
251 std::forward<TArgs>(args)...);
252 }
253 };
254
255 //! The CPU OpenMP 2.0 thread execution task platform type trait specialization.
256 template<typename TDim, typename TIdx>
257 struct PlatformType<AccCpuOmp2Threads<TDim, TIdx>>
258 {
260 };
261
262 //! The CPU OpenMP 2.0 thread accelerator idx type trait specialization.
263 template<typename TDim, typename TIdx>
264 struct IdxType<AccCpuOmp2Threads<TDim, TIdx>>
265 {
266 using type = TIdx;
267 };
268
269 template<typename TDim, typename TIdx>
270 struct AccToTag<alpaka::AccCpuOmp2Threads<TDim, TIdx>>
271 {
273 };
274
275 template<typename TDim, typename TIdx>
280 } // namespace trait
281} // namespace alpaka
282
283#endif
The CPU OpenMP 2.0 thread accelerator.
AccCpuOmp2Threads(AccCpuOmp2Threads &&)=delete
AccCpuOmp2Threads(AccCpuOmp2Threads const &)=delete
auto operator=(AccCpuOmp2Threads &&) -> AccCpuOmp2Threads &=delete
auto operator=(AccCpuOmp2Threads const &) -> AccCpuOmp2Threads &=delete
Dynamic block shared memory provider using fixed-size member array to allocate memory on the stack or...
auto staticMemCapacity() const -> std::uint32_t
The OpenMP barrier 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 thread accelerator execution task.
A n-dimensional vector.
Definition Vec.hpp:38
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC constexpr auto zeros() -> Vec< TDim, TVal >
Zero value constructor.
Definition Vec.hpp:99
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.
The OpenMP accelerator index provider.
Definition IdxBtOmp.hpp:26
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_NO_HOST_ACC_WARNING ALPAKA_FN_ACC auto syncBlockThreads(TBlockSync const &blockSync) -> void
Synchronizes all threads within the current block (independently for all blocks).
Definition Traits.hpp:36
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 ALPAKA_FN_HOST auto createTaskCooperativeKernel(TWorkDiv const &workDiv, TKernelFnObj const &kernelFnObj, TArgs &&... args)
The cooperative kernel execution task creation trait.
Definition Traits.hpp:46
static ALPAKA_FN_HOST 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 ALPAKA_FN_HOST auto getAccDevProps(DevCpu const &dev) -> alpaka::AccDevProps< TDim, TIdx >
The device properties get trait.
Definition Traits.hpp:66
static ALPAKA_FN_HOST auto getAccName() -> std::string
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.