alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
AccCpuSerial.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"
36#include "alpaka/dev/DevCpu.hpp"
37
38#ifdef __cpp_lib_format
39# include <format>
40#endif
41#include <memory>
42#include <string>
43
44#ifdef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
45
46namespace alpaka
47{
48 template<typename TDim, typename TIdx, typename TKernelFnObj, typename... TArgs>
49 class TaskKernelCpuSerial;
50
51 //! The CPU serial accelerator.
52 //!
53 //! This accelerator allows serial kernel execution on a CPU device.
54 //! The block idx is restricted to 1x1x1 and all blocks are executed serially so there is no parallelism at all.
55 template<typename TDim, typename TIdx>
56 class AccCpuSerial final
57 : public WorkDivMembers<TDim, TIdx>
58 , public gb::IdxGbRef<TDim, TIdx>
59 , public bt::IdxBtZero<TDim, TIdx>
60 , public AtomicHierarchy<
61 AtomicCpu, // grid atomics
62 AtomicNoOp, // block atomics
63 AtomicNoOp> // thread atomics
64 , public math::MathStdLib
66 , public BlockSharedMemStMember<>
67 , public BlockSyncNoOp
68 , public GridSyncNoOp
69 , public IntrinsicCpu
70 , public MemFenceCpuSerial
71# ifdef ALPAKA_DISABLE_VENDOR_RNG
72 , public rand::RandDefault
73# else
74 , public rand::RandStdLib
75# endif
77 , public interface::Implements<InterfaceAcc, AccCpuSerial<TDim, TIdx>>
78 {
79 static_assert(
80 sizeof(TIdx) >= sizeof(int),
81 "Index type is not supported, consider using int or a larger type.");
82
83 public:
84 // Partial specialization with the correct TDim and TIdx is not allowed.
85 template<typename TDim2, typename TIdx2, typename TKernelFnObj, typename... TArgs>
86 friend class ::alpaka::TaskKernelCpuSerial;
87
88 AccCpuSerial(AccCpuSerial const&) = delete;
90 auto operator=(AccCpuSerial const&) -> AccCpuSerial& = delete;
91 auto operator=(AccCpuSerial&&) -> AccCpuSerial& = delete;
92
93 private:
94 template<typename TWorkDiv>
95 ALPAKA_FN_HOST AccCpuSerial(TWorkDiv const& workDiv, size_t const& blockSharedMemDynSizeBytes)
96 : WorkDivMembers<TDim, TIdx>(workDiv)
97 , gb::IdxGbRef<TDim, TIdx>(m_gridBlockIdx)
98 , BlockSharedMemDynMember<>(blockSharedMemDynSizeBytes)
100 , m_gridBlockIdx(Vec<TDim, TIdx>::zeros())
101 {
102 }
103
104 private:
105 // getIdx
106 Vec<TDim, TIdx> mutable m_gridBlockIdx; //!< The index of the currently executed block.
107 };
108
109 namespace trait
110 {
111 //! The CPU serial accelerator accelerator type trait specialization.
112 template<typename TDim, typename TIdx>
113 struct AccType<AccCpuSerial<TDim, TIdx>>
114 {
116 };
117
118 //! The CPU serial single thread accelerator type trait specialization.
119 template<typename TDim, typename TIdx>
120 struct IsSingleThreadAcc<AccCpuSerial<TDim, TIdx>> : std::true_type
121 {
122 };
123
124 //! The CPU serial multi thread accelerator type trait specialization.
125 template<typename TDim, typename TIdx>
126 struct IsMultiThreadAcc<AccCpuSerial<TDim, TIdx>> : std::false_type
127 {
128 };
129
130 //! The CPU serial accelerator device properties get trait specialization.
131 template<typename TDim, typename TIdx>
132 struct GetAccDevProps<AccCpuSerial<TDim, TIdx>>
133 {
135 {
136 return {// m_multiProcessorCount
137 static_cast<TIdx>(1),
138 // m_gridBlockExtentMax
139 Vec<TDim, TIdx>::all(std::numeric_limits<TIdx>::max()),
140 // m_gridBlockCountMax
141 std::numeric_limits<TIdx>::max(),
142 // m_blockThreadExtentMax
144 // m_blockThreadCountMax
145 static_cast<TIdx>(1),
146 // m_threadElemExtentMax
147 Vec<TDim, TIdx>::all(std::numeric_limits<TIdx>::max()),
148 // m_threadElemCountMax
149 std::numeric_limits<TIdx>::max(),
150 // m_sharedMemSizeBytes
151 static_cast<size_t>(AccCpuSerial<TDim, TIdx>::staticAllocBytes()),
152 // m_globalMemSizeBytes
153 getMemBytes(dev),
154 // m_cooperativeLaunch
155 false};
156 }
157 };
158
159 //! The CPU serial accelerator name trait specialization.
160 template<typename TDim, typename TIdx>
161 struct GetAccName<AccCpuSerial<TDim, TIdx>>
162 {
163 ALPAKA_FN_HOST static auto getAccName() -> std::string
164 {
165# if ALPAKA_COMP_CLANG
166# pragma clang diagnostic push
167# pragma clang diagnostic ignored "-Wexit-time-destructors"
168# endif
169 using namespace std::literals;
170 static std::string const accName =
171# ifdef __cpp_lib_format
172 std::format("AccCpuSerial<{},{}>", TDim::value, core::demangled<TIdx>);
173# else
174 "AccCpuSerial<"s + std::to_string(TDim::value) + ","s + std::string(core::demangled<TIdx>) + ">"s;
175# endif
176 return accName;
177# if ALPAKA_COMP_CLANG
178# pragma clang diagnostic pop
179# endif
180 }
181 };
182
183 //! The CPU serial accelerator device type trait specialization.
184 template<typename TDim, typename TIdx>
185 struct DevType<AccCpuSerial<TDim, TIdx>>
186 {
187 using type = DevCpu;
188 };
189
190 //! The CPU serial accelerator dimension getter trait specialization.
191 template<typename TDim, typename TIdx>
192 struct DimType<AccCpuSerial<TDim, TIdx>>
193 {
194 using type = TDim;
195 };
196
197 //! The CPU serial accelerator execution task type trait specialization.
198 template<typename TDim, typename TIdx, typename TWorkDiv, typename TKernelFnObj, typename... TArgs>
199 struct CreateTaskKernel<AccCpuSerial<TDim, TIdx>, TWorkDiv, TKernelFnObj, TArgs...>
200 {
202 TWorkDiv const& workDiv,
203 TKernelFnObj const& kernelFnObj,
204 TArgs&&... args)
205 {
206 if(workDiv.m_blockThreadExtent.prod() != static_cast<TIdx>(1u))
207 {
208 throw std::runtime_error(
209 "The given work division is not valid for a single thread Acc: "
210 + getAccName<AccCpuSerial<TDim, TIdx>>() + ". Threads per block should be 1!");
211 }
212
213 return TaskKernelCpuSerial<TDim, TIdx, TKernelFnObj, TArgs...>(
214 workDiv,
215 kernelFnObj,
216 std::forward<TArgs>(args)...);
217 }
218 };
219
220 //! The CPU serial accelerator execution cooperative task type trait specialization.
221 template<typename TDim, typename TIdx, typename TWorkDiv, typename TKernelFnObj, typename... TArgs>
222 struct CreateTaskCooperativeKernel<AccCpuSerial<TDim, TIdx>, TWorkDiv, TKernelFnObj, TArgs...>
223 {
225 TWorkDiv const& workDiv,
226 TKernelFnObj const& kernelFnObj,
227 TArgs&&... args)
228 {
229 auto const gridBlockExtent = getWorkDiv<Grid, Blocks>(workDiv);
230 if(gridBlockExtent.prod() != static_cast<TIdx>(1u))
231 {
232 throw std::runtime_error(
233 "Serial accelerator supports only a single block operation with cooperative kernel!\n"
234 "Consider useing a different CPU accelerator.");
235 }
236
237 return TaskKernelCpuSerial<TDim, TIdx, TKernelFnObj, TArgs...>(
238 workDiv,
239 kernelFnObj,
240 std::forward<TArgs>(args)...);
241 }
242 };
243
244 //! The CPU serial execution task platform type trait specialization.
245 template<typename TDim, typename TIdx>
246 struct PlatformType<AccCpuSerial<TDim, TIdx>>
247 {
249 };
250
251 //! The CPU serial accelerator idx type trait specialization.
252 template<typename TDim, typename TIdx>
253 struct IdxType<AccCpuSerial<TDim, TIdx>>
254 {
255 using type = TIdx;
256 };
257
258 template<typename TDim, typename TIdx>
259 struct AccToTag<alpaka::AccCpuSerial<TDim, TIdx>>
260 {
262 };
263
264 template<typename TDim, typename TIdx>
265 struct TagToAcc<alpaka::TagCpuSerial, TDim, TIdx>
266 {
268 };
269 } // namespace trait
270} // namespace alpaka
271
272#endif
The CPU serial accelerator.
auto operator=(AccCpuSerial &&) -> AccCpuSerial &=delete
auto operator=(AccCpuSerial const &) -> AccCpuSerial &=delete
AccCpuSerial(AccCpuSerial &&)=delete
AccCpuSerial(AccCpuSerial const &)=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 NoOp grid synchronization for accelerators that only support a single thread with cooperative ker...
The CPU intrinsic.
The serial CPU memory fence.
The CPU serial execution task implementation.
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
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
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 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) -> 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.