alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Acc.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera, Tim Hanel
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/Vec.hpp"
9#include "alpaka/core/Tag.hpp"
14#include "alpaka/tag.hpp"
15
16#include <cassert>
17#include <tuple>
18
19namespace alpaka::onAcc
20{
21 template<typename T_Storage>
22 struct Acc : T_Storage
23 {
24 constexpr Acc(T_Storage const& storage) : T_Storage{storage}
25 {
26 }
27
28 constexpr Acc(Acc const&) = delete;
29 constexpr Acc(Acc&&) = delete;
30 constexpr Acc& operator=(Acc const&) = delete;
31 constexpr Acc& operator=(Acc&&) = delete;
32
33 /** Get the n-dimensional indices within the origin in the quantity of the selected unit
34 *
35 * @attention if origin or unit is warp/s a one dimensional vector is returned
36 */
38 const
39 {
40 return internalCompute::GetIdxWithin::Op<Acc, ALPAKA_TYPEOF(origin), ALPAKA_TYPEOF(unit)>{}(
41 *this,
42 origin,
43 unit);
44 }
45
46 /** Get the n-dimensional extents of an origin in the quantity of the selected unit
47 *
48 * @attention if origin or unit is warp/s a one dimensional vector is returned
49 */
51 const
52 {
53 return internalCompute::GetExtentsOf::Op<Acc, ALPAKA_TYPEOF(origin), ALPAKA_TYPEOF(unit)>{}(
54 *this,
55 origin,
56 unit);
57 }
58
59 static constexpr bool hasKey(auto key)
60 {
61 constexpr auto idx = alpaka::internal::KeyIdx<ALPAKA_TYPEOF(key), std::decay_t<T_Storage>>::value;
62 return idx != -1;
63 }
64
65 constexpr auto getApi() const
66 {
67 return T_Storage::operator[](object::api);
68 }
69
70 constexpr auto getDeviceKind() const
71 {
72 return T_Storage::operator[](object::deviceKind);
73 }
74
75 constexpr auto getExecutor() const
76 {
77 return T_Storage::operator[](object::exec);
78 }
79
80 /** Check if a frame spec was used to enqueue the kernel
81 *
82 * To be able to use the result as constexpr value you must call the static function via the type
83 * `Acc_t::launchedWithFrameSpec()` else the warp size can only be used as runtime value.
84 *
85 * If the kernel was enqueued via a frame specification the thread specification to within the kernel can
86 * differ, the number of thread block is not necessarily equal to the number of frames and the thread block
87 * extent is not necessarily equal to the frame extents.
88 *
89 * @return true if the kernel was launched based on a frame specification, false if a thread specification was
90 * used.
91 */
92 static constexpr bool launchedWithFrameSpec()
93 {
94 // is std::true_type or std::false_type
95 return ALPAKA_TYPEOF(std::declval<T_Storage>()[object::launchedWidthFrameSpec])::value;
96 }
97
98 /** Get the warp size
99 *
100 * To be able to use the warp size as constexpr value you must call the static function via the type
101 * `Acc_t::getWarpSize()` else the warp size can only be used as runtime value.
102 * Alternative you can use the free function onAcc::warp::getSize<Acc_t>();
103 */
104 static constexpr uint32_t getWarpSize()
105 {
106 constexpr uint32_t w = ALPAKA_TYPEOF(std::declval<T_Storage>()[object::warpSize])::value;
107 return w;
108 }
109 };
110
111 namespace concepts
112 {
113 /** Concept to check if a type is an accelerator
114 *
115 * @tparam T_Acc Type to check
116 * @tparam T_Api Enforce an API type, if not provided api type is not checked
117 */
118 template<typename T_Acc, typename T_Api = alpaka::NotRequired>
120 && (std::same_as<T_Api, ALPAKA_TYPEOF(std::declval<T_Acc>().getApi())>
121 || std::same_as<T_Api, alpaka::NotRequired>);
122 } // namespace concepts
123
124 /** Synchronize all threads within a given scope */
125 template<alpaka::concepts::Layer T_Scope>
126 constexpr void sync(concepts::Acc auto const& acc, T_Scope scope)
127 {
128 internalCompute::sync(acc, scope);
129 }
130
131 /** Synchronize all threads within a thread block */
132 constexpr void syncBlockThreads(concepts::Acc auto const& acc)
133 {
134 internalCompute::sync(acc, alpaka::layer::block);
135 }
136
137 /** Create a variable located in the thread blocks shared memory
138 *
139 * @code{.cpp}
140 * // creates a reference to a float value
141 * auto& foo = declareSharedVar<float, uniqueId()>(acc);
142 * @endcode
143 *
144 * @attention The data is not initialized; it can contain garbage.
145 *
146 * @tparam T The type that should be created; the constructor is not called
147 * @tparam T_uniqueId ID that is unique inside a kernel.
148 * Reusing the id will return the same memory declared before with the same id.
149 * @return Result should be taken by reference
150 */
151 template<typename T, size_t T_uniqueId>
152 constexpr decltype(auto) declareSharedVar(concepts::Acc auto const& acc)
153 {
154 return internalCompute::declareSharedVar<T, T_uniqueId>(acc);
155 }
156
157 /** creates an M-dimensional array
158 *
159 * @code{.cpp}
160 * // creates a MdSpan view to a float value, do NOT use a reference here
161 * auto fooArrayMd = declareSharedVar<float, uniqueId()>(acc, CVec<uint32_t, 5, 8>{});
162 * @endcode
163 *
164 * @attention The data is not initialized it can contains garbage.
165 *
166 * @tparam T type which should be created, the constructor is not called
167 * @tparam T_uniqueId id those is unique inside a kernel.
168 * Reusing the id will return the same memory declared before with the same id.
169 * @param extent M-dimensional extent in elements for each dimension, 1 - M dimensions are supported
170 * @return MdSpan non owning view to the corresponding data, you should NOT store a reference to the handle
171 */
172 template<typename T, size_t T_uniqueId>
173 constexpr decltype(auto) declareSharedMdArray(
174 concepts::Acc auto const& acc,
175 alpaka::concepts::CVector auto const& extent)
176 {
177 using CArrayType = typename CArrayType<T, ALPAKA_TYPEOF(extent)>::type;
178 /* XOR with hash to avoid issues in case the user is using the same id to create an array and normal shared
179 * variables.
180 */
181 constexpr size_t id = T_uniqueId ^ 0x9e37'79b9'7f4a'7c15;
182 constexpr auto alignment = Alignment<alignof(T)>{};
183 return MdSpanArray<CArrayType, typename ALPAKA_TYPEOF(extent)::type, ALPAKA_TYPEOF(alignment)>{
185 }
186
187 /** Get block shared dynamic memory.
188 *
189 * The available size of the memory can be defined by specializing 'onHost::trait::GetDynSharedMemBytes' or adding
190 * a public member variable 'uint32_t dynSharedMemBytes' for a kernel. The Memory can be accessed by all threads
191 * within a block. Access to the memory is not thread safe.
192 *
193 * \tparam T The element type.
194 * \return Pointer to pre-allocated contiguous memory.
195 */
196 template<typename T>
197 constexpr auto getDynSharedMem(concepts::Acc auto const& acc) -> T*
198 {
199 return internalCompute::declareDynamicSharedMem<T>(acc);
200 }
201
202} // namespace alpaka::onAcc
203
204namespace alpaka::onAcc::internalCompute
205{
206 /** synchronize all threads within a thread block */
207 template<concepts::Acc T_Acc>
208 struct Sync::Op<T_Acc, alpaka::layer::Block>
209 {
210 constexpr auto operator()(T_Acc const& acc, alpaka::layer::Block const scope) const
211 {
212 alpaka::unused(scope);
213 acc[action::threadBlockSync]();
214 }
215 };
216} // namespace alpaka::onAcc::internalCompute
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:154
Concept to check if a type is a CVector.
Definition Vec.hpp:75
Validates if T is a specialization of the unspecialized template type U.
Definition utility.hpp:118
Concept to check if a type is a vector.
Definition Vec.hpp:54
Concept to check if a type is an accelerator.
Definition Acc.hpp:119
constexpr auto block
Definition tag.hpp:259
constexpr WarpSize warpSize
Definition tag.hpp:42
constexpr DeviceKind deviceKind
Definition tag.hpp:30
constexpr Api api
Definition tag.hpp:24
Origin of index domains.
Definition tag.hpp:21
Unit of index domains.
Definition tag.hpp:33
functionality which is usable on the accelerator compute device from within a kernel.
Definition executor.hpp:38
constexpr decltype(auto) declareSharedMdArray(concepts::Acc auto const &acc, alpaka::concepts::CVector auto const &extent)
creates an M-dimensional array
Definition Acc.hpp:173
constexpr decltype(auto) declareSharedVar(concepts::Acc auto const &acc)
Create a variable located in the thread blocks shared memory.
Definition Acc.hpp:152
constexpr auto getDynSharedMem(concepts::Acc auto const &acc) -> T *
Get block shared dynamic memory.
Definition Acc.hpp:197
constexpr void syncBlockThreads(concepts::Acc auto const &acc)
Synchronize all threads within a thread block.
Definition Acc.hpp:132
constexpr void sync(concepts::Acc auto const &acc, T_Scope scope)
Synchronize all threads within a given scope.
Definition Acc.hpp:126
main alpaka namespace.
Definition alpaka.hpp:76
On some constexpr function signatures ALPAKA_FN_HOST_ACC is required for CUDA; otherwise a __host__ f...
Strongly typed and constexpr representation of a byte-alignment of memory.
Definition Alignment.hpp:26
build C array type with compile time extents from a scalar value based on the compile time extents ve...
constexpr auto getDeviceKind() const
Definition Acc.hpp:70
constexpr Acc & operator=(Acc &&)=delete
static constexpr bool hasKey(auto key)
Definition Acc.hpp:59
constexpr alpaka::concepts::Vector auto getExtentsOf(concepts::Origin auto origin, concepts::Unit auto unit) const
Get the n-dimensional extents of an origin in the quantity of the selected unit.
Definition Acc.hpp:50
constexpr Acc(T_Storage const &storage)
Definition Acc.hpp:24
static constexpr bool launchedWithFrameSpec()
Check if a frame spec was used to enqueue the kernel.
Definition Acc.hpp:92
constexpr Acc(Acc &&)=delete
constexpr auto getApi() const
Definition Acc.hpp:65
constexpr Acc & operator=(Acc const &)=delete
constexpr auto getExecutor() const
Definition Acc.hpp:75
constexpr alpaka::concepts::Vector auto getIdxWithin(concepts::Origin auto origin, concepts::Unit auto unit) const
Get the n-dimensional indices within the origin in the quantity of the selected unit.
Definition Acc.hpp:37
static constexpr uint32_t getWarpSize()
Get the warp size.
Definition Acc.hpp:104
constexpr Acc(Acc const &)=delete