alpaka
Abstraction Library for Parallel Kernel Acceleration
Traits.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Benjamin Worpitz, RenĂ© Widera, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/atomic/Op.hpp"
8 #include "alpaka/core/Common.hpp"
11 
12 #include <type_traits>
13 
14 namespace 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  {
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  {
102  return trait::AtomicOp<TOp, ImplementationBase, T, THierarchy>::atomicOp(atomic, addr, compare, value);
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 decrement operation.
214  //!
215  //! \tparam T The value type.
216  //! \tparam TAtomic The atomic implementation type.
217  //! \param addr The value to change atomically.
218  //! \param value The value used in the atomic operation.
219  //! \param atomic The atomic implementation.
221  template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
223  TAtomic const& atomic,
224  T* const addr,
225  T const& value,
226  THierarchy const& hier = THierarchy()) -> T
227  {
228  return atomicOp<AtomicDec>(atomic, addr, value, hier);
229  }
230 
231  //! Executes an atomic and operation.
232  //!
233  //! \tparam T The value type.
234  //! \tparam TAtomic The atomic implementation type.
235  //! \param addr The value to change atomically.
236  //! \param value The value used in the atomic operation.
237  //! \param atomic The atomic implementation.
239  template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
241  TAtomic const& atomic,
242  T* const addr,
243  T const& value,
244  THierarchy const& hier = THierarchy()) -> T
245  {
246  return atomicOp<AtomicAnd>(atomic, addr, value, hier);
247  }
248 
249  //! Executes an atomic or operation.
250  //!
251  //! \tparam T The value type.
252  //! \tparam TAtomic The atomic implementation type.
253  //! \param addr The value to change atomically.
254  //! \param value The value used in the atomic operation.
255  //! \param atomic The atomic implementation.
257  template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
259  TAtomic const& atomic,
260  T* const addr,
261  T const& value,
262  THierarchy const& hier = THierarchy()) -> T
263  {
264  return atomicOp<AtomicOr>(atomic, addr, value, hier);
265  }
266 
267  //! Executes an atomic xor operation.
268  //!
269  //! \tparam T The value type.
270  //! \tparam TAtomic The atomic implementation type.
271  //! \param addr The value to change atomically.
272  //! \param value The value used in the atomic operation.
273  //! \param atomic The atomic implementation.
275  template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
277  TAtomic const& atomic,
278  T* const addr,
279  T const& value,
280  THierarchy const& hier = THierarchy()) -> T
281  {
282  return atomicOp<AtomicXor>(atomic, addr, value, hier);
283  }
284 
285  //! Executes an atomic compare-and-swap operation.
286  //!
287  //! \tparam TAtomic The atomic implementation type.
288  //! \tparam T The value type.
289  //! \param atomic The atomic implementation.
290  //! \param addr The value to change atomically.
291  //! \param compare The comparison value used in the atomic operation.
292  //! \param value The value used in the atomic operation.
294  template<typename TAtomic, typename T, typename THierarchy = hierarchy::Grids>
296  TAtomic const& atomic,
297  T* const addr,
298  T const& compare,
299  T const& value,
300  THierarchy const& hier = THierarchy()) -> T
301  {
302  return atomicOp<AtomicCas>(atomic, addr, compare, value, hier);
303  }
304 } // 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:276
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 atomicOp(TAtomic const &atomic, T *const addr, T const &compare, T const &value, THierarchy const &=THierarchy()) -> T
Executes the given operation atomically.
Definition: Traits.hpp:94
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:222
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:295
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:258
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:240
The atomic operation trait.
Definition: Traits.hpp:60