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 //! Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
105 //!
106 //! \tparam TDev The type of device to allocate the buffer on.
107 //! \tparam TDim The dimensionality of the buffer to allocate.
108 template<typename TDev, typename TDim>
110
111 //! If supported, allocates stream-ordered memory on the given queue and the associated device.
112 //! Otherwise, allocates regular memory on the device associated to the queue.
113 //! Please note that stream-ordered and regular memory have different semantics:
114 //! this function is provided for convenience in the cases where the difference is not relevant,
115 //! and the stream-ordered memory is only used as a performance optimisation.
116 //!
117 //! \tparam TElem The element type of the returned buffer.
118 //! \tparam TIdx The linear index type of the buffer.
119 //! \tparam TExtent The extent type of the buffer.
120 //! \tparam TQueue The type of queue used to order the buffer allocation.
121 //! \param queue The queue used to order the buffer allocation.
122 //! \param extent The extent of the buffer.
123 //! \return The newly allocated buffer.
124 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TQueue = void>
125 ALPAKA_FN_HOST auto allocAsyncBufIfSupported(TQueue queue, TExtent const& extent = TExtent())
126 {
127 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
128
130 {
131 return allocAsyncBuf<TElem, Idx>(queue, extent);
132 }
133 else
134 {
135 return allocBuf<TElem, Idx>(getDev(queue), extent);
136 }
137
139 }
140
141 //! Allocates pinned/mapped host memory, accessible by all devices in the given platform.
142 //!
143 //! \tparam TElem The element type of the returned buffer.
144 //! \tparam TIdx The linear index type of the buffer.
145 //! \tparam TExtent The extent type of the buffer.
146 //! \tparam TPlatform The platform from which the buffer is accessible.
147 //! \param host The host device to allocate the buffer on.
148 //! \param extent The extent of the buffer.
149 //! \return The newly allocated buffer.
150 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TPlatform = void>
152 DevCpu const& host,
153 TPlatform const& platform,
154 TExtent const& extent = TExtent())
155 {
156 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
157
159 }
160
161 //! Allocates unified/managed memory, accessible by all devices in the given platform.
162 //!
163 //! \tparam TElem The element type of the returned buffer.
164 //! \tparam TIdx The linear index type of the buffer.
165 //! \tparam TExtent The extent type of the buffer.
166 //! \tparam TPlatform The platform from which the buffer is accessible.
167 //! \param host The host device to allocate the buffer on.
168 //! \param extent The extent of the buffer.
169 //! \return The newly allocated buffer.
170 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TPlatform = void>
172 DevCpu const& host,
173 TPlatform const& platform,
174 TExtent const& extent = TExtent())
175 {
176 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
177
179 }
180
181 //! Checks if the host can allocate a pinned/mapped host memory, accessible by all devices in the given platform.
182 //!
183 //! \tparam TPlatform The platform from which the buffer is accessible.
184 template<typename TPlatform>
186
187 //! If supported, allocates pinned/mapped host memory, accessible by all devices in the given platform.
188 //! Otherwise, allocates regular host memory.
189 //! Please note that pinned/mapped and regular memory may have different semantics:
190 //! this function is provided for convenience in the cases where the difference is not relevant,
191 //! and the pinned/mapped memory is only used as a performance optimisation.
192 //!
193 //! \tparam TElem The element type of the returned buffer.
194 //! \tparam TIdx The linear index type of the buffer.
195 //! \tparam TExtent The extent type of the buffer.
196 //! \tparam TPlatform The platform from which the buffer is accessible.
197 //! \param host The host device to allocate the buffer on.
198 //! \param extent The extent of the buffer.
199 //! \return The newly allocated buffer.
200 template<typename TElem, typename TIdx = void, typename TExtent = void, typename TPlatform = void>
202 DevCpu const& host,
203 TPlatform const& platform,
204 TExtent const& extent = TExtent())
205 {
206 using Idx = std::conditional_t<std::is_void_v<TIdx>, Idx<TExtent>, TIdx>;
208
210 {
211 return allocMappedBuf<TElem, Idx>(host, platform, extent);
212 }
213 else
214 {
215 return allocBuf<TElem, Idx>(host, extent);
216 }
217
219 }
220
221 //! Creates a constant buffer from the given mutable buffer.
222 //!
223 //! \tparam TBuf The type of the original buffer.
224 //! \param buf The original buffer.
225 //! \return The transformed buffer with only read-access allowed.
226 template<typename TBuf>
227 ALPAKA_FN_HOST auto makeConstBuf(TBuf const& buf)
228 {
230 }
231
232 template<typename TBuf>
234 {
236 }
237
238} // 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:185
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:171
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:125
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::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:227
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:151
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:109
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:201
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:97
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
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