alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Traits.hpp
Go to the documentation of this file.
1/* Copyright 2025 Benjamin Worpitz, René Widera, Bernhard Manfred Gruber, Simone Balducci
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
11
12#include <type_traits>
13
14namespace alpaka
15{
17 {
18 };
19
21 {
22 };
23
25 {
26 };
27
28 namespace detail
29 {
30 template<typename THierarchy>
32
33 template<>
34 struct AtomicHierarchyConceptType<hierarchy::Threads>
35 {
37 };
38
39 template<>
40 struct AtomicHierarchyConceptType<hierarchy::Blocks>
41 {
43 };
44
45 template<>
46 struct AtomicHierarchyConceptType<hierarchy::Grids>
47 {
49 };
50 } // namespace detail
51
52 template<typename THierarchy>
54
55 //! The atomic operation trait.
56 namespace trait
57 {
58 //! The atomic operation trait.
59 template<typename TOp, typename TAtomic, typename T, typename THierarchy, typename TSfinae = void>
60 struct AtomicOp;
61 } // namespace trait
62
63 //! Executes the given operation atomically.
64 //!
65 //! \tparam TOp The operation type.
66 //! \tparam T The value type.
67 //! \tparam TAtomic The atomic implementation type.
68 //! \param addr The value to change atomically.
69 //! \param value The value used in the atomic operation.
70 //! \param atomic The atomic implementation.
72 template<typename TOp, typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
74 TAtomic const& atomic,
75 T* const addr,
76 T const& value,
77 THierarchy const& = THierarchy()) -> T
78 {
79 using ImplementationBase = typename interface::ImplementationBase<AtomicHierarchyConcept<THierarchy>, TAtomic>;
81 }
82
83 //! Executes the given operation atomically.
84 //!
85 //! \tparam TOp The operation type.
86 //! \tparam TAtomic The atomic implementation type.
87 //! \tparam T The value type.
88 //! \param atomic The atomic implementation.
89 //! \param addr The value to change atomically.
90 //! \param compare The comparison value used in the atomic operation.
91 //! \param value The value used in the atomic operation.
93 template<typename TOp, typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
95 TAtomic const& atomic,
96 T* const addr,
97 T const& compare,
98 T const& value,
99 THierarchy const& = THierarchy()) -> T
100 {
101 using ImplementationBase = typename interface::ImplementationBase<AtomicHierarchyConcept<THierarchy>, TAtomic>;
103 }
104
105 //! Executes an atomic add operation.
106 //!
107 //! \tparam T The value type.
108 //! \tparam TAtomic The atomic implementation type.
109 //! \param addr The value to change atomically.
110 //! \param value The value used in the atomic operation.
111 //! \param atomic The atomic implementation.
113 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
115 TAtomic const& atomic,
116 T* const addr,
117 T const& value,
118 THierarchy const& hier = THierarchy()) -> T
119 {
120 return atomicOp<AtomicAdd>(atomic, addr, value, hier);
121 }
122
123 //! Executes an atomic sub operation.
124 //!
125 //! \tparam T The value type.
126 //! \tparam TAtomic The atomic implementation type.
127 //! \param addr The value to change atomically.
128 //! \param value The value used in the atomic operation.
129 //! \param atomic The atomic implementation.
131 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
133 TAtomic const& atomic,
134 T* const addr,
135 T const& value,
136 THierarchy const& hier = THierarchy()) -> T
137 {
138 return atomicOp<AtomicSub>(atomic, addr, value, hier);
139 }
140
141 //! Executes an atomic min operation.
142 //!
143 //! \tparam T The value type.
144 //! \tparam TAtomic The atomic implementation type.
145 //! \param addr The value to change atomically.
146 //! \param value The value used in the atomic operation.
147 //! \param atomic The atomic implementation.
149 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
151 TAtomic const& atomic,
152 T* const addr,
153 T const& value,
154 THierarchy const& hier = THierarchy()) -> T
155 {
156 return atomicOp<AtomicMin>(atomic, addr, value, hier);
157 }
158
159 //! Executes an atomic max operation.
160 //!
161 //! \tparam T The value type.
162 //! \tparam TAtomic The atomic implementation type.
163 //! \param addr The value to change atomically.
164 //! \param value The value used in the atomic operation.
165 //! \param atomic The atomic implementation.
167 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
169 TAtomic const& atomic,
170 T* const addr,
171 T const& value,
172 THierarchy const& hier = THierarchy()) -> T
173 {
174 return atomicOp<AtomicMax>(atomic, addr, value, hier);
175 }
176
177 //! Executes an atomic exchange operation.
178 //!
179 //! \tparam T The value type.
180 //! \tparam TAtomic The atomic implementation type.
181 //! \param addr The value to change atomically.
182 //! \param value The value used in the atomic operation.
183 //! \param atomic The atomic implementation.
185 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
187 TAtomic const& atomic,
188 T* const addr,
189 T const& value,
190 THierarchy const& hier = THierarchy()) -> T
191 {
192 return atomicOp<AtomicExch>(atomic, addr, value, hier);
193 }
194
195 //! Executes an atomic increment operation.
196 //!
197 //! \tparam T The value type.
198 //! \tparam TAtomic The atomic implementation type.
199 //! \param addr The value to change atomically.
200 //! \param value The value used in the atomic operation.
201 //! \param atomic The atomic implementation.
203 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
205 TAtomic const& atomic,
206 T* const addr,
207 T const& value,
208 THierarchy const& hier = THierarchy()) -> T
209 {
210 return atomicOp<AtomicInc>(atomic, addr, value, hier);
211 }
212
213 //! Executes an atomic increment operation, using the numeric limit as ceiling.
214 //!
215 //! \tparam T The value type.
216 //! \tparam TAtomic The atomic implementation type.
217 //! \param addr The value to change atomically.
218 //! \param atomic The atomic implementation.
219 //! NOTE: The limit value is deduced as std::numeric_limits<T>::max()
221 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
222 ALPAKA_FN_HOST_ACC auto atomicInc(TAtomic const& atomic, T* const addr, THierarchy const& hier = THierarchy()) -> T
223 {
224 T const value = std::numeric_limits<T>::max();
225 return atomicOp<AtomicInc>(atomic, addr, value, hier);
226 }
227
228 //! Executes an atomic decrement operation.
229 //!
230 //! \tparam T The value type.
231 //! \tparam TAtomic The atomic implementation type.
232 //! \param addr The value to change atomically.
233 //! \param value The value used in the atomic operation.
234 //! \param atomic The atomic implementation.
236 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
238 TAtomic const& atomic,
239 T* const addr,
240 T const& value,
241 THierarchy const& hier = THierarchy()) -> T
242 {
243 return atomicOp<AtomicDec>(atomic, addr, value, hier);
244 }
245
246 //! Executes an atomic decrement operation.
247 //!
248 //! \tparam T The value type.
249 //! \tparam TAtomic The atomic implementation type.
250 //! \param addr The value to change atomically.
251 //! \param atomic The atomic implementation.
252 //! NOTE: The limit value is deduced as std::numeric_limits<T>::max()
254 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
255 ALPAKA_FN_HOST_ACC auto atomicDec(TAtomic const& atomic, T* const addr, THierarchy const& hier = THierarchy()) -> T
256 {
257 T const value = std::numeric_limits<T>::max();
258 return atomicOp<AtomicDec>(atomic, addr, value, hier);
259 }
260
261 //! Executes an atomic and operation.
262 //!
263 //! \tparam T The value type.
264 //! \tparam TAtomic The atomic implementation type.
265 //! \param addr The value to change atomically.
266 //! \param value The value used in the atomic operation.
267 //! \param atomic The atomic implementation.
269 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
271 TAtomic const& atomic,
272 T* const addr,
273 T const& value,
274 THierarchy const& hier = THierarchy()) -> T
275 {
276 return atomicOp<AtomicAnd>(atomic, addr, value, hier);
277 }
278
279 //! Executes an atomic or operation.
280 //!
281 //! \tparam T The value type.
282 //! \tparam TAtomic The atomic implementation type.
283 //! \param addr The value to change atomically.
284 //! \param value The value used in the atomic operation.
285 //! \param atomic The atomic implementation.
287 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
289 TAtomic const& atomic,
290 T* const addr,
291 T const& value,
292 THierarchy const& hier = THierarchy()) -> T
293 {
294 return atomicOp<AtomicOr>(atomic, addr, value, hier);
295 }
296
297 //! Executes an atomic xor operation.
298 //!
299 //! \tparam T The value type.
300 //! \tparam TAtomic The atomic implementation type.
301 //! \param addr The value to change atomically.
302 //! \param value The value used in the atomic operation.
303 //! \param atomic The atomic implementation.
305 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
307 TAtomic const& atomic,
308 T* const addr,
309 T const& value,
310 THierarchy const& hier = THierarchy()) -> T
311 {
312 return atomicOp<AtomicXor>(atomic, addr, value, hier);
313 }
314
315 //! Executes an atomic compare-and-swap operation.
316 //!
317 //! \tparam TAtomic The atomic implementation type.
318 //! \tparam T The value type.
319 //! \param atomic The atomic implementation.
320 //! \param addr The value to change atomically.
321 //! \param compare The comparison value used in the atomic operation.
322 //! \param value The value used in the atomic operation.
324 template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
326 TAtomic const& atomic,
327 T* const addr,
328 T const& compare,
329 T const& value,
330 THierarchy const& hier = THierarchy()) -> T
331 {
332 return atomicOp<AtomicCas>(atomic, addr, compare, value, hier);
333 }
334} // namespace alpaka
#define ALPAKA_FN_HOST_ACC
Definition Common.hpp:39
#define ALPAKA_NO_HOST_ACC_WARNING
Disable nvcc warning: 'calling a host function from host device function.' Usage: ALPAKA_NO_HOST_ACC_...
Definition Common.hpp:82
typename detail::ImplementationBaseType< TInterface, TDerived >::type ImplementationBase
Returns the type that implements the given interface in the inheritance hierarchy.
Definition Interface.hpp:66
The alpaka accelerator library.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicXor(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic xor operation.
Definition Traits.hpp:306
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicExch(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic exchange operation.
Definition Traits.hpp:186
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicSub(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic sub operation.
Definition Traits.hpp:132
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicMin(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic min operation.
Definition Traits.hpp:150
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicInc(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic increment operation.
Definition Traits.hpp:204
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicDec(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic decrement operation.
Definition Traits.hpp:237
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicMax(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic max operation.
Definition Traits.hpp:168
typename detail::AtomicHierarchyConceptType< THierarchy >::type AtomicHierarchyConcept
Definition Traits.hpp:53
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicCas(TAtomic const &atomic, T *const addr, T const &compare, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic compare-and-swap operation.
Definition Traits.hpp:325
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicAdd(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic add operation.
Definition Traits.hpp:114
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicOr(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic or operation.
Definition Traits.hpp:288
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicOp(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &=THierarchy()) -> T
Executes the given operation atomically.
Definition Traits.hpp:73
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicAnd(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &hier=THierarchy()) -> T
Executes an atomic and operation.
Definition Traits.hpp:270
The atomic operation trait.
Definition Traits.hpp:60