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
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 TIdx The linear index type of the buffer.
75 //! \tparam TExtent The extent type of the buffer.
76 //! \tparam TDev The type of device the buffer is allocated on.
77 //! \param dev The device to allocate the buffer on.
78 //! \param extent The extent of the buffer.
79 //! \return The newly allocated buffer.
80 template<typename TElem, typename TIdx, typename TExtent, typename TDev>
81 ALPAKA_FN_HOST auto allocBuf(TDev const& dev, TExtent const& extent = TExtent())
82 {
83 return trait::BufAlloc<TElem, Dim<TExtent>, TIdx, TDev>::allocBuf(dev, extent);
84 }
85
86 //! Allocates stream-ordered memory on the given device.
87 //!
88 //! \tparam TElem The element type of the returned buffer.
89 //! \tparam TIdx The linear index type of the buffer.
90 //! \tparam TExtent The extent type of the buffer.
91 //! \tparam TQueue The type of queue used to order the buffer allocation.
92 //! \param queue The queue used to order the buffer allocation.
93 //! \param extent The extent of the buffer.
94 //! \return The newly allocated buffer.
95 template<typename TElem, typename TIdx, typename TExtent, typename TQueue>
96 ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const& extent = TExtent())
97 {
99 }
100
101 /* TODO: Remove this pragma block once support for clang versions <= 13 is removed. These versions are unable to
102 figure out that the template parameters are attached to a C++17 inline variable. */
103#if ALPAKA_COMP_CLANG
104# pragma clang diagnostic push
105# pragma clang diagnostic ignored "-Wdocumentation"
106#endif
107 //! Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
108 //!
109 //! \tparam TDev The type of device to allocate the buffer on.
110 //! \tparam TDim The dimensionality of the buffer to allocate.
111 template<typename TDev, typename TDim>
113#if ALPAKA_COMP_CLANG
114# pragma clang diagnostic pop
115#endif
116
117 //! If supported, allocates stream-ordered memory on the given queue and the associated device.
118 //! Otherwise, allocates regular memory on the device associated to the queue.
119 //! Please note that stream-ordered and regular memory have different semantics:
120 //! this function is provided for convenience in the cases where the difference is not relevant,
121 //! and the stream-ordered memory is only used as a performance optimisation.
122 //!
123 //! \tparam TElem The element type of the returned buffer.
124 //! \tparam TIdx The linear index type of the buffer.
125 //! \tparam TExtent The extent type of the buffer.
126 //! \tparam TQueue The type of queue used to order the buffer allocation.
127 //! \param queue The queue used to order the buffer allocation.
128 //! \param extent The extent of the buffer.
129 //! \return The newly allocated buffer.
130 template<typename TElem, typename TIdx, typename TExtent, typename TQueue>
131 ALPAKA_FN_HOST auto allocAsyncBufIfSupported(TQueue queue, TExtent const& extent = TExtent())
132 {
133 if constexpr(hasAsyncBufSupport<alpaka::Dev<TQueue>, Dim<TExtent>>)
134 {
135 return allocAsyncBuf<TElem, TIdx>(queue, extent);
136 }
137 else
138 {
139 return allocBuf<TElem, TIdx>(getDev(queue), extent);
140 }
141
142 ALPAKA_UNREACHABLE(allocBuf<TElem, TIdx>(getDev(queue), extent));
143 }
144
145 //! Allocates pinned/mapped host memory, accessible by all devices in the given platform.
146 //!
147 //! \tparam TElem The element type of the returned buffer.
148 //! \tparam TIdx The linear index type of the buffer.
149 //! \tparam TExtent The extent type of the buffer.
150 //! \tparam TPlatform The platform from which the buffer is accessible.
151 //! \param host The host device to allocate the buffer on.
152 //! \param extent The extent of the buffer.
153 //! \return The newly allocated buffer.
154 template<typename TElem, typename TIdx, typename TExtent, typename TPlatform>
156 DevCpu const& host,
157 TPlatform const& platform,
158 TExtent const& extent = TExtent())
159 {
161 }
162
163 //! Allocates unified/managed memory, accessible by all devices in the given platform.
164 //!
165 //! \tparam TElem The element type of the returned buffer.
166 //! \tparam TIdx The linear index type of the buffer.
167 //! \tparam TExtent The extent type of the buffer.
168 //! \tparam TPlatform The platform from which the buffer is accessible.
169 //! \param host The host device to allocate the buffer on.
170 //! \param extent The extent of the buffer.
171 //! \return The newly allocated buffer.
172 template<typename TElem, typename TIdx, typename TExtent, typename TPlatform>
174 DevCpu const& host,
175 TPlatform const& platform,
176 TExtent const& extent = TExtent())
177 {
179 }
180
181 /* TODO: Remove this pragma block once support for clang versions <= 13 is removed. These versions are unable to
182 figure out that the template parameters are attached to a C++17 inline variable. */
183#if ALPAKA_COMP_CLANG
184# pragma clang diagnostic push
185# pragma clang diagnostic ignored "-Wdocumentation"
186#endif
187 //! Checks if the host can allocate a pinned/mapped host memory, accessible by all devices in the given platform.
188 //!
189 //! \tparam TPlatform The platform from which the buffer is accessible.
190 template<typename TPlatform>
192#if ALPAKA_COMP_CLANG
193# pragma clang diagnostic pop
194#endif
195
196 //! If supported, allocates pinned/mapped host memory, accessible by all devices in the given platform.
197 //! Otherwise, allocates regular host memory.
198 //! Please note that pinned/mapped and regular memory may have different semantics:
199 //! this function is provided for convenience in the cases where the difference is not relevant,
200 //! and the pinned/mapped memory is only used as a performance optimisation.
201 //!
202 //! \tparam TElem The element type of the returned buffer.
203 //! \tparam TIdx The linear index type of the buffer.
204 //! \tparam TExtent The extent type of the buffer.
205 //! \tparam TPlatform The platform from which the buffer is accessible.
206 //! \param host The host device to allocate the buffer on.
207 //! \param extent The extent of the buffer.
208 //! \return The newly allocated buffer.
209 template<typename TElem, typename TIdx, typename TExtent, typename TPlatform>
211 DevCpu const& host,
212 TPlatform const& platform,
213 TExtent const& extent = TExtent())
214 {
216 if constexpr(hasMappedBufSupport<Platform>)
217 {
218 return allocMappedBuf<TElem, TIdx>(host, platform, extent);
219 }
220 else
221 {
222 return allocBuf<TElem, TIdx>(host, extent);
223 }
224
225 ALPAKA_UNREACHABLE(allocBuf<TElem, TIdx>(host, extent));
226 }
227
228 //! Creates a constant buffer from the given mutable buffer.
229 //!
230 //! \tparam TBuf The type of the original buffer.
231 //! \param buf The original buffer.
232 //! \return The transformed buffer with only read-access allowed.
233 template<typename TBuf>
234 ALPAKA_FN_HOST auto makeConstBuf(TBuf const& buf)
235 {
237 }
238
239 template<typename TBuf>
241 {
243 }
244
245} // 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.
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:191
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:173
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:131
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:234
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:155
ALPAKA_FN_HOST auto allocBuf(TDev const &dev, TExtent const &extent=TExtent())
Allocates memory on the given device.
Definition Traits.hpp:81
constexpr bool hasAsyncBufSupport
Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
Definition Traits.hpp:112
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:210
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:96
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