alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Traits.hpp
Go to the documentation of this file.
1/* Copyright 2026 Alexander Matthes, Benjamin Worpitz, Andrea Bocci, Bernhard Manfred Gruber, Jan Stephan,
2 * Christian Kaever, Maria Michailidi, Simone Balducci
3 * SPDX-License-Identifier: MPL-2.0
4 */
5
6#pragma once
7
12
13#include <concepts>
14#include <cstddef>
15#include <type_traits>
16
17namespace alpaka
18{
19 //! The CPU device handle.
20 class DevCpu;
21
22 namespace concepts
23 {
24
25 template<typename TAllocator>
26 concept Allocator = std::copyable<TAllocator>
27 && requires(TAllocator alloc, void* ptr, std::size_t bytes, std::size_t align) {
28 {
29 alloc.allocate(bytes, align)
30 } -> std::same_as<void*>;
31 alloc.deallocate(ptr);
32 };
33
34 } // namespace concepts
35
36 //! The buffer traits.
37 namespace trait
38 {
39 //! The memory buffer type trait.
40 template<typename TDev, typename TElem, typename TDim, typename TIdx, typename TSfinae = void>
41 struct BufType;
42
43 //! The memory const-buffer type trait.
44 template<typename TDev, typename TElem, typename TDim, typename TIdx, typename TSfinae = void>
46
47 //! The memory allocator trait.
48 template<typename TElem, typename TDim, typename TIdx, typename TDev, typename TSfinae = void>
49 struct BufAlloc;
50
51 //! The stream-ordered memory allocator trait.
52 template<typename TElem, typename TDim, typename TIdx, typename TDev, typename TSfinae = void>
54
55 //! The stream-ordered memory allocation capability trait.
56 template<typename TDim, typename TDev>
57 struct HasAsyncBufSupport : public std::false_type
58 {
59 };
60
61 //! The pinned/mapped memory allocator trait.
62 template<typename TPlatform, typename TElem, typename TDim, typename TIdx>
64
65 //! The pinned/mapped memory allocation capability trait.
66 template<typename TPlatform>
67 struct HasMappedBufSupport : public std::false_type
68 {
69 };
70
71 //! The managed (unified) memory allocator trait.
72 template<typename TPlatform, typename TElem, typename TDim, typename TIdx>
74
75 //! The allocator-aware memory allocator trait.
76 template<
77 typename TElem,
78 typename TDim,
79 typename TIdx,
80 typename TDev,
81 concepts::Allocator TAllocator,
82 typename TSfinae = void>
84
85 //! The allocator-aware stream-ordered memory allocator trait.
86 template<
87 typename TElem,
88 typename TDim,
89 typename TIdx,
90 typename TDev,
91 concepts::Allocator TAllocator,
92 typename TSfinae = void>
94
95 //! The trait to transform a mutable buffer into a constant one.
96 template<typename TBuf>
98
99 } // namespace trait
100
101 //! The memory buffer type trait alias template to remove the ::type for a Buffer type.
102 template<typename TDev, typename TElem, typename TDim, typename TIdx>
103 using Buf = typename trait::BufType<alpaka::Dev<TDev>, TElem, TDim, TIdx>::type;
104
105 //! The memory buffer type trait alias template to remove the ::type for a ConstBuffer type.
106 template<typename TDev, typename TElem, typename TDim, typename TIdx>
107 using ConstBuf = typename trait::ConstBufType<alpaka::Dev<TDev>, TElem, TDim, TIdx>::type;
108
109 //! Allocates memory on the given device.
110 //!
111 //! \tparam TElem The element type of the returned buffer.
112 //! \tparam TIdx The linear index type of the buffer.
113 //! \tparam TExtent The extent type of the buffer.
114 //! \tparam TDev The type of device the buffer is allocated on.
115 //! \param dev The device to allocate the buffer on.
116 //! \param extent The extent of the buffer.
117 //! \return The newly allocated buffer.
118 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TDev = void>
119 ALPAKA_FN_HOST auto allocBuf(TDev const& dev, TExtent const& extent = TExtent())
120 {
121 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
122 return trait::BufAlloc<TElem, Dim<TExtent>, Idx, TDev>::allocBuf(dev, extent);
123 }
124
125 //! Allocates memory on the given device using an external allocator.
126 //!
127 //! \tparam TElem The element type of the returned buffer.
128 //! \tparam TIdx The linear index type of the buffer.
129 //! \tparam TExtent The extent type of the buffer.
130 //! \tparam TDev The type of device the buffer is allocated on.
131 //! \tparam TAllocator The type of the allocator wrapper.
132 //! \param dev The device to allocate the buffer on.
133 //! \param extent The extent of the buffer.
134 //! \param allocator The allocator wrapper to use.
135 //! \return The newly allocated buffer.
136 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TDev = void, typename TAllocator>
137 requires concepts::Allocator<std::remove_cvref_t<TAllocator>>
138 ALPAKA_FN_HOST auto allocBuf(TDev const& dev, TExtent const& extent, TAllocator&& allocator)
139 {
140 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
141 return trait::BufAllocWithAllocator<TElem, Dim<TExtent>, Idx, TDev, std::remove_cvref_t<TAllocator>>::allocBuf(
142 dev,
143 extent,
144 std::forward<TAllocator>(allocator));
145 }
146
147 //! Allocates stream-ordered memory on the given device.
148 //!
149 //! \tparam TElem The element type of the returned buffer.
150 //! \tparam TIdx The linear index type of the buffer.
151 //! \tparam TExtent The extent type of the buffer.
152 //! \tparam TQueue The type of queue used to order the buffer allocation.
153 //! \param queue The queue used to order the buffer allocation.
154 //! \param extent The extent of the buffer.
155 //! \return The newly allocated buffer.
156 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TQueue = void>
157 ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const& extent = TExtent())
158 {
159 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
160
162 }
163
164 //! Allocates stream-ordered memory on the given device using an external allocator.
165 template<
166 typename TElem,
167 typename TIdx = void,
168 typename TExtent = void,
169 typename TQueue = void,
170 typename TAllocator>
171 requires concepts::Allocator<std::remove_cvref_t<TAllocator>>
172 ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const& extent, TAllocator&& allocator)
173 {
174 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
176 TElem,
178 Idx,
180 std::remove_cvref_t<TAllocator>>::
181 allocAsyncBuf(std::move(queue), extent, std::forward<TAllocator>(allocator));
182 }
183
184 //! Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
185 //!
186 //! \tparam TDev The type of device to allocate the buffer on.
187 //! \tparam TDim The dimensionality of the buffer to allocate.
188 template<typename TDev, typename TDim>
190
191 //! If supported, allocates stream-ordered memory on the given queue and the associated device.
192 //! Otherwise, allocates regular memory on the device associated to the queue.
193 //! Please note that stream-ordered and regular memory have different semantics:
194 //! this function is provided for convenience in the cases where the difference is not relevant,
195 //! and the stream-ordered memory is only used as a performance optimisation.
196 //!
197 //! \tparam TElem The element type of the returned buffer.
198 //! \tparam TIdx The linear index type of the buffer.
199 //! \tparam TExtent The extent type of the buffer.
200 //! \tparam TQueue The type of queue used to order the buffer allocation.
201 //! \param queue The queue used to order the buffer allocation.
202 //! \param extent The extent of the buffer.
203 //! \return The newly allocated buffer.
204 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TQueue = void>
205 ALPAKA_FN_HOST auto allocAsyncBufIfSupported(TQueue queue, TExtent const& extent = TExtent())
206 {
207 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
208
210 {
211 return allocAsyncBuf<TElem, Idx>(queue, extent);
212 }
213 else
214 {
215 return allocBuf<TElem, Idx>(getDev(queue), extent);
216 }
217
219 }
220
221 //! If supported, allocates stream-ordered memory using an external allocator; otherwise falls back to cached
222 //! allocBuf.
223 template<
224 typename TElem,
225 typename TIdx = void,
226 typename TExtent = void,
227 typename TQueue = void,
228 typename TAllocator>
229 requires concepts::Allocator<std::remove_cvref_t<TAllocator>>
230 ALPAKA_FN_HOST auto allocAsyncBufIfSupported(TQueue queue, TExtent const& extent, TAllocator&& allocator)
231 {
232 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
233
235 {
236 return allocAsyncBuf<TElem, Idx>(std::move(queue), extent, std::forward<TAllocator>(allocator));
237 }
238 else
239 {
240 return allocBuf<TElem, Idx>(getDev(queue), extent, std::forward<TAllocator>(allocator));
241 }
242
244 }
245
246 //! Allocates pinned/mapped host memory, accessible by all devices in the given platform.
247 //!
248 //! \tparam TElem The element type of the returned buffer.
249 //! \tparam TIdx The linear index type of the buffer.
250 //! \tparam TExtent The extent type of the buffer.
251 //! \tparam TPlatform The platform from which the buffer is accessible.
252 //! \param host The host device to allocate the buffer on.
253 //! \param extent The extent of the buffer.
254 //! \return The newly allocated buffer.
255 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TPlatform = void>
257 DevCpu const& host,
258 TPlatform const& platform,
259 TExtent const& extent = TExtent())
260 {
261 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
262
264 }
265
266 //! Allocates unified/managed memory, accessible by all devices in the given platform.
267 //!
268 //! \tparam TElem The element type of the returned buffer.
269 //! \tparam TIdx The linear index type of the buffer.
270 //! \tparam TExtent The extent type of the buffer.
271 //! \tparam TPlatform The platform from which the buffer is accessible.
272 //! \param host The host device to allocate the buffer on.
273 //! \param extent The extent of the buffer.
274 //! \return The newly allocated buffer.
275 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TPlatform = void>
277 DevCpu const& host,
278 TPlatform const& platform,
279 TExtent const& extent = TExtent())
280 {
281 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
282
284 }
285
286 //! Checks if the host can allocate a pinned/mapped host memory, accessible by all devices in the given platform.
287 //!
288 //! \tparam TPlatform The platform from which the buffer is accessible.
289 template<typename TPlatform>
291
292 //! If supported, allocates pinned/mapped host memory, accessible by all devices in the given platform.
293 //! Otherwise, allocates regular host memory.
294 //! Please note that pinned/mapped and regular memory may have different semantics:
295 //! this function is provided for convenience in the cases where the difference is not relevant,
296 //! and the pinned/mapped memory is only used as a performance optimisation.
297 //!
298 //! \tparam TElem The element type of the returned buffer.
299 //! \tparam TIdx The linear index type of the buffer.
300 //! \tparam TExtent The extent type of the buffer.
301 //! \tparam TPlatform The platform from which the buffer is accessible.
302 //! \param host The host device to allocate the buffer on.
303 //! \param extent The extent of the buffer.
304 //! \return The newly allocated buffer.
305 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TPlatform = void>
307 DevCpu const& host,
308 TPlatform const& platform,
309 TExtent const& extent = TExtent())
310 {
311 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
313
315 {
316 return allocMappedBuf<TElem, Idx>(host, platform, extent);
317 }
318 else
319 {
320 return allocBuf<TElem, Idx>(host, extent);
321 }
322
324 }
325
326 //! Creates a constant buffer from the given mutable buffer.
327 //!
328 //! \tparam TBuf The type of the original buffer.
329 //! \param buf The original buffer.
330 //! \return The transformed buffer with only read-access allowed.
331 template<typename TBuf>
332 ALPAKA_FN_HOST auto makeConstBuf(TBuf const& buf)
333 {
335 }
336
337 template<typename TBuf>
339 {
341 }
342
343} // namespace alpaka
#define ALPAKA_UNREACHABLE(...)
Before CUDA 11.5 nvcc is unable to correctly identify return statements in 'if constexpr' branches....
The CPU device handle.
Definition DevCpu.hpp:56
#define ALPAKA_FN_HOST
Definition Common.hpp:43
The alpaka accelerator library.
constexpr bool hasMappedBufSupport
Checks if the host can allocate a pinned/mapped host memory, accessible by all devices in the given p...
Definition Traits.hpp:290
ALPAKA_FN_HOST auto allocManagedBuf(DevCpu const &host, TPlatform const &platform, TExtent const &extent=TExtent())
Allocates unified/managed memory, accessible by all devices in the given platform.
Definition Traits.hpp:276
ALPAKA_FN_HOST auto allocAsyncBufIfSupported(TQueue queue, TExtent const &extent=TExtent())
If supported, allocates stream-ordered memory on the given queue and the associated device....
Definition Traits.hpp:205
typename trait::BufType< alpaka::Dev< TDev >, TElem, TDim, TIdx >::type Buf
The memory buffer type trait alias template to remove the ::type for a Buffer type.
Definition Traits.hpp:103
typename trait::IdxType< T >::type Idx
Definition Traits.hpp:29
ALPAKA_FN_HOST auto makeConstBuf(TBuf const &buf)
Creates a constant buffer from the given mutable buffer.
Definition Traits.hpp:332
ALPAKA_FN_HOST auto allocMappedBuf(DevCpu const &host, TPlatform const &platform, TExtent const &extent=TExtent())
Allocates pinned/mapped host memory, accessible by all devices in the given platform.
Definition Traits.hpp:256
ALPAKA_FN_HOST auto allocBuf(TDev const &dev, TExtent const &extent=TExtent())
Allocates memory on the given device.
Definition Traits.hpp:119
constexpr bool hasAsyncBufSupport
Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
Definition Traits.hpp:189
typename trait::DevType< T >::type Dev
The device type trait alias template to remove the ::type.
Definition Traits.hpp:56
ALPAKA_FN_HOST auto getDev(T const &t)
Definition Traits.hpp:75
ALPAKA_FN_HOST auto allocMappedBufIfSupported(DevCpu const &host, TPlatform const &platform, TExtent const &extent=TExtent())
If supported, allocates pinned/mapped host memory, accessible by all devices in the given platform....
Definition Traits.hpp:306
typename trait::PlatformType< T >::type Platform
The platform type trait alias template to remove the ::type.
Definition Traits.hpp:59
ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const &extent=TExtent())
Allocates stream-ordered memory on the given device.
Definition Traits.hpp:157
typename trait::ConstBufType< alpaka::Dev< TDev >, TElem, TDim, TIdx >::type ConstBuf
The memory buffer type trait alias template to remove the ::type for a ConstBuffer type.
Definition Traits.hpp:107
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition Traits.hpp:19
The allocator-aware stream-ordered memory allocator trait.
Definition Traits.hpp:93
The stream-ordered memory allocator trait.
Definition Traits.hpp:53
The managed (unified) memory allocator trait.
Definition Traits.hpp:73
The pinned/mapped memory allocator trait.
Definition Traits.hpp:63
The allocator-aware memory allocator trait.
Definition Traits.hpp:83
The memory allocator trait.
Definition Traits.hpp:49
The memory buffer type trait.
Definition Traits.hpp:41
The memory const-buffer type trait.
Definition Traits.hpp:45
The stream-ordered memory allocation capability trait.
Definition Traits.hpp:58
The pinned/mapped memory allocation capability trait.
Definition Traits.hpp:68
The trait to transform a mutable buffer into a constant one.
Definition Traits.hpp:97