alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Traits.hpp
Go to the documentation of this file.
1/* Copyright 2025 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
13namespace alpaka
14{
15 //! The CPU device handle.
16 class DevCpu;
17
18 //! The buffer traits.
19 namespace trait
20 {
21 //! The memory buffer type trait.
22 template<typename TDev, typename TElem, typename TDim, typename TIdx, typename TSfinae = void>
23 struct BufType;
24
25 //! The memory const-buffer type trait.
26 template<typename TDev, typename TElem, typename TDim, typename TIdx, typename TSfinae = void>
28
29 //! The memory allocator trait.
30 template<typename TElem, typename TDim, typename TIdx, typename TDev, typename TSfinae = void>
31 struct BufAlloc;
32
33 //! The stream-ordered memory allocator trait.
34 template<typename TElem, typename TDim, typename TIdx, typename TDev, typename TSfinae = void>
36
37 //! The stream-ordered memory allocation capability trait.
38 template<typename TDim, typename TDev>
39 struct HasAsyncBufSupport : public std::false_type
40 {
41 };
42
43 //! The pinned/mapped memory allocator trait.
44 template<typename TPlatform, typename TElem, typename TDim, typename TIdx>
46
47 //! The pinned/mapped memory allocation capability trait.
48 template<typename TPlatform>
49 struct HasMappedBufSupport : public std::false_type
50 {
51 };
52
53 //! The managed (unified) memory allocator trait.
54 template<typename TPlatform, typename TElem, typename TDim, typename TIdx>
56
57 //! The trait to transform a mutable buffer into a constant one.
58 template<typename TBuf>
60
61 } // namespace trait
62
63 //! The memory buffer type trait alias template to remove the ::type for a Buffer type.
64 template<typename TDev, typename TElem, typename TDim, typename TIdx>
65 using Buf = typename trait::BufType<alpaka::Dev<TDev>, TElem, TDim, TIdx>::type;
66
67 //! The memory buffer type trait alias template to remove the ::type for a ConstBuffer type.
68 template<typename TDev, typename TElem, typename TDim, typename TIdx>
69 using ConstBuf = typename trait::ConstBufType<alpaka::Dev<TDev>, TElem, TDim, TIdx>::type;
70
71 //! Allocates memory on the given device.
72 //!
73 //! \tparam TElem The element type of the returned buffer.
74 //! \tparam TExtent The extent type of the buffer.
75 //! \tparam TDev The type of device the buffer is allocated on.
76 //! \param dev The device to allocate the buffer on.
77 //! \param extent The extent of the buffer.
78 //! \return The newly allocated buffer.
79 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TDev = void>
80 ALPAKA_FN_HOST auto allocBuf(TDev const& dev, TExtent const& extent = TExtent())
81 {
82 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
83
84 return trait::BufAlloc<TElem, Dim<TExtent>, Idx, TDev>::allocBuf(dev, extent);
85 }
86
87 //! Allocates stream-ordered memory on the given device.
88 //!
89 //! \tparam TElem The element type of the returned buffer.
90 //! \tparam TIdx The linear index type of the buffer.
91 //! \tparam TExtent The extent type of the buffer.
92 //! \tparam TQueue The type of queue used to order the buffer allocation.
93 //! \param queue The queue used to order the buffer allocation.
94 //! \param extent The extent of the buffer.
95 //! \return The newly allocated buffer.
96 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TQueue = void>
97 ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const& extent = TExtent())
98 {
99 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
100
102 }
103
104 /* TODO: Remove this pragma block once support for clang versions <= 13 is removed. These versions are unable to
105 figure out that the template parameters are attached to a C++17 inline variable. */
106#if ALPAKA_COMP_CLANG
107# pragma clang diagnostic push
108# pragma clang diagnostic ignored "-Wdocumentation"
109#endif
110 //! Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
111 //!
112 //! \tparam TDev The type of device to allocate the buffer on.
113 //! \tparam TDim The dimensionality of the buffer to allocate.
114 template<typename TDev, typename TDim>
116#if ALPAKA_COMP_CLANG
117# pragma clang diagnostic pop
118#endif
119
120 //! If supported, allocates stream-ordered memory on the given queue and the associated device.
121 //! Otherwise, allocates regular memory on the device associated to the queue.
122 //! Please note that stream-ordered and regular memory have different semantics:
123 //! this function is provided for convenience in the cases where the difference is not relevant,
124 //! and the stream-ordered memory is only used as a performance optimisation.
125 //!
126 //! \tparam TElem The element type of the returned buffer.
127 //! \tparam TIdx The linear index type of the buffer.
128 //! \tparam TExtent The extent type of the buffer.
129 //! \tparam TQueue The type of queue used to order the buffer allocation.
130 //! \param queue The queue used to order the buffer allocation.
131 //! \param extent The extent of the buffer.
132 //! \return The newly allocated buffer.
133 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TQueue = void>
134 ALPAKA_FN_HOST auto allocAsyncBufIfSupported(TQueue queue, TExtent const& extent = TExtent())
135 {
136 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
137
138 if constexpr(hasAsyncBufSupport<alpaka::Dev<TQueue>, Dim<TExtent>>)
139 {
140 return allocAsyncBuf<TElem, Idx>(queue, extent);
141 }
142 else
143 {
144 return allocBuf<TElem, Idx>(getDev(queue), extent);
145 }
146
147 ALPAKA_UNREACHABLE(allocBuf<TElem, TIdx>(getDev(queue), extent));
148 }
149
150 //! Allocates pinned/mapped host memory, accessible by all devices in the given platform.
151 //!
152 //! \tparam TElem The element type of the returned buffer.
153 //! \tparam TIdx The linear index type of the buffer.
154 //! \tparam TExtent The extent type of the buffer.
155 //! \tparam TPlatform The platform from which the buffer is accessible.
156 //! \param host The host device to allocate the buffer on.
157 //! \param extent The extent of the buffer.
158 //! \return The newly allocated buffer.
159 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TPlatform = void>
161 DevCpu const& host,
162 TPlatform const& platform,
163 TExtent const& extent = TExtent())
164 {
165 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
166
168 }
169
170 //! Allocates unified/managed memory, accessible by all devices in the given platform.
171 //!
172 //! \tparam TElem The element type of the returned buffer.
173 //! \tparam TIdx The linear index type of the buffer.
174 //! \tparam TExtent The extent type of the buffer.
175 //! \tparam TPlatform The platform from which the buffer is accessible.
176 //! \param host The host device to allocate the buffer on.
177 //! \param extent The extent of the buffer.
178 //! \return The newly allocated buffer.
179 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TPlatform = void>
181 DevCpu const& host,
182 TPlatform const& platform,
183 TExtent const& extent = TExtent())
184 {
185 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
186
188 }
189
190 /* TODO: Remove this pragma block once support for clang versions <= 13 is removed. These versions are unable to
191 figure out that the template parameters are attached to a C++17 inline variable. */
192#if ALPAKA_COMP_CLANG
193# pragma clang diagnostic push
194# pragma clang diagnostic ignored "-Wdocumentation"
195#endif
196 //! Checks if the host can allocate a pinned/mapped host memory, accessible by all devices in the given platform.
197 //!
198 //! \tparam TPlatform The platform from which the buffer is accessible.
199 template<typename TPlatform>
201#if ALPAKA_COMP_CLANG
202# pragma clang diagnostic pop
203#endif
204
205 //! If supported, allocates pinned/mapped host memory, accessible by all devices in the given platform.
206 //! Otherwise, allocates regular host memory.
207 //! Please note that pinned/mapped and regular memory may have different semantics:
208 //! this function is provided for convenience in the cases where the difference is not relevant,
209 //! and the pinned/mapped memory is only used as a performance optimisation.
210 //!
211 //! \tparam TElem The element type of the returned buffer.
212 //! \tparam TIdx The linear index type of the buffer.
213 //! \tparam TExtent The extent type of the buffer.
214 //! \tparam TPlatform The platform from which the buffer is accessible.
215 //! \param host The host device to allocate the buffer on.
216 //! \param extent The extent of the buffer.
217 //! \return The newly allocated buffer.
218 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TPlatform = void>
220 DevCpu const& host,
221 TPlatform const& platform,
222 TExtent const& extent = TExtent())
223 {
224 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
226
227 if constexpr(hasMappedBufSupport<Platform>)
228 {
229 return allocMappedBuf<TElem, Idx>(host, platform, extent);
230 }
231 else
232 {
233 return allocBuf<TElem, Idx>(host, extent);
234 }
235
236 ALPAKA_UNREACHABLE(allocBuf<TElem, TIdx>(host, extent));
237 }
238
239 //! Creates a constant buffer from the given mutable buffer.
240 //!
241 //! \tparam TBuf The type of the original buffer.
242 //! \param buf The original buffer.
243 //! \return The transformed buffer with only read-access allowed.
244 template<typename TBuf>
245 ALPAKA_FN_HOST auto makeConstBuf(TBuf const& buf)
246 {
248 }
249
250 template<typename TBuf>
252 {
254 }
255
256} // 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:40
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition Traits.hpp:29
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:200
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:180
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:134
typename trait::DevType< T >::type Dev
The device type trait alias template to remove the ::type.
Definition Traits.hpp:56
ALPAKA_FN_HOST auto makeConstBuf(TBuf const &buf)
Creates a constant buffer from the given mutable buffer.
Definition Traits.hpp:245
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:160
ALPAKA_FN_HOST auto allocBuf(TDev const &dev, TExtent const &extent=TExtent())
Allocates memory on the given device.
Definition Traits.hpp:80
constexpr bool hasAsyncBufSupport
Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
Definition Traits.hpp:115
ALPAKA_FN_HOST auto getDev(T const &t)
Definition Traits.hpp:68
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:219
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:65
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:69
ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const &extent=TExtent())
Allocates stream-ordered memory on the given device.
Definition Traits.hpp:97
typename trait::PlatformType< T >::type Platform
The platform type trait alias template to remove the ::type.
Definition Traits.hpp:51
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition Traits.hpp:19
The stream-ordered memory allocator trait.
Definition Traits.hpp:35
The managed (unified) memory allocator trait.
Definition Traits.hpp:55
The pinned/mapped memory allocator trait.
Definition Traits.hpp:45
The memory allocator trait.
Definition Traits.hpp:31
The memory buffer type trait.
Definition Traits.hpp:23
The memory const-buffer type trait.
Definition Traits.hpp:27
The stream-ordered memory allocation capability trait.
Definition Traits.hpp:40
The pinned/mapped memory allocation capability trait.
Definition Traits.hpp:50
The trait to transform a mutable buffer into a constant one.
Definition Traits.hpp:59