alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Traits.hpp
Go to the documentation of this file.
1/* Copyright 2023 Alexander Matthes, Benjamin Worpitz, Andrea Bocci, Bernhard Manfred Gruber, Jan Stephan,
2 * Christian Kaever
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 trait to transform a mutable buffer into a constant one.
54 template<typename TBuf>
56
57 } // namespace trait
58
59 //! The memory buffer type trait alias template to remove the ::type for a Buffer type.
60 template<typename TDev, typename TElem, typename TDim, typename TIdx>
61 using Buf = typename trait::BufType<alpaka::Dev<TDev>, TElem, TDim, TIdx>::type;
62
63 //! The memory buffer type trait alias template to remove the ::type for a ConstBuffer type.
64 template<typename TDev, typename TElem, typename TDim, typename TIdx>
65 using ConstBuf = typename trait::ConstBufType<alpaka::Dev<TDev>, TElem, TDim, TIdx>::type;
66
67 //! Allocates memory on the given device.
68 //!
69 //! \tparam TElem The element type of the returned buffer.
70 //! \tparam TIdx The linear index type of the buffer.
71 //! \tparam TExtent The extent type of the buffer.
72 //! \tparam TDev The type of device the buffer is allocated on.
73 //! \param dev The device to allocate the buffer on.
74 //! \param extent The extent of the buffer.
75 //! \return The newly allocated buffer.
76 template<typename TElem, typename TIdx, typename TExtent, typename TDev>
77 ALPAKA_FN_HOST auto allocBuf(TDev const& dev, TExtent const& extent = TExtent())
78 {
79 return trait::BufAlloc<TElem, Dim<TExtent>, TIdx, TDev>::allocBuf(dev, extent);
80 }
81
82 //! Allocates stream-ordered memory on the given device.
83 //!
84 //! \tparam TElem The element type of the returned buffer.
85 //! \tparam TIdx The linear index type of the buffer.
86 //! \tparam TExtent The extent type of the buffer.
87 //! \tparam TQueue The type of queue used to order the buffer allocation.
88 //! \param queue The queue used to order the buffer allocation.
89 //! \param extent The extent of the buffer.
90 //! \return The newly allocated buffer.
91 template<typename TElem, typename TIdx, typename TExtent, typename TQueue>
92 ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const& extent = TExtent())
93 {
95 }
96
97 /* TODO: Remove this pragma block once support for clang versions <= 13 is removed. These versions are unable to
98 figure out that the template parameters are attached to a C++17 inline variable. */
99#if ALPAKA_COMP_CLANG
100# pragma clang diagnostic push
101# pragma clang diagnostic ignored "-Wdocumentation"
102#endif
103 //! Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
104 //!
105 //! \tparam TDev The type of device to allocate the buffer on.
106 //! \tparam TDim The dimensionality of the buffer to allocate.
107 template<typename TDev, typename TDim>
109#if ALPAKA_COMP_CLANG
110# pragma clang diagnostic pop
111#endif
112
113 //! If supported, allocates stream-ordered memory on the given queue and the associated device.
114 //! Otherwise, allocates regular memory on the device associated to the queue.
115 //! Please note that stream-ordered and regular memory have different semantics:
116 //! this function is provided for convenience in the cases where the difference is not relevant,
117 //! and the stream-ordered memory is only used as a performance optimisation.
118 //!
119 //! \tparam TElem The element type of the returned buffer.
120 //! \tparam TIdx The linear index type of the buffer.
121 //! \tparam TExtent The extent type of the buffer.
122 //! \tparam TQueue The type of queue used to order the buffer allocation.
123 //! \param queue The queue used to order the buffer allocation.
124 //! \param extent The extent of the buffer.
125 //! \return The newly allocated buffer.
126 template<typename TElem, typename TIdx, typename TExtent, typename TQueue>
127 ALPAKA_FN_HOST auto allocAsyncBufIfSupported(TQueue queue, TExtent const& extent = TExtent())
128 {
129 if constexpr(hasAsyncBufSupport<alpaka::Dev<TQueue>, Dim<TExtent>>)
130 {
131 return allocAsyncBuf<TElem, TIdx>(queue, extent);
132 }
133 else
134 {
135 return allocBuf<TElem, TIdx>(getDev(queue), extent);
136 }
137
138 ALPAKA_UNREACHABLE(allocBuf<TElem, TIdx>(getDev(queue), extent));
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, typename TExtent, typename TPlatform>
152 DevCpu const& host,
153 TPlatform const& platform,
154 TExtent const& extent = TExtent())
155 {
157 }
158
159 /* TODO: Remove this pragma block once support for clang versions <= 13 is removed. These versions are unable to
160 figure out that the template parameters are attached to a C++17 inline variable. */
161#if ALPAKA_COMP_CLANG
162# pragma clang diagnostic push
163# pragma clang diagnostic ignored "-Wdocumentation"
164#endif
165 //! Checks if the host can allocate a pinned/mapped host memory, accessible by all devices in the given platform.
166 //!
167 //! \tparam TPlatform The platform from which the buffer is accessible.
168 template<typename TPlatform>
170#if ALPAKA_COMP_CLANG
171# pragma clang diagnostic pop
172#endif
173
174 //! If supported, allocates pinned/mapped host memory, accessible by all devices in the given platform.
175 //! Otherwise, allocates regular host memory.
176 //! Please note that pinned/mapped and regular memory may have different semantics:
177 //! this function is provided for convenience in the cases where the difference is not relevant,
178 //! and the pinned/mapped memory is only used as a performance optimisation.
179 //!
180 //! \tparam TElem The element type of the returned buffer.
181 //! \tparam TIdx The linear index type of the buffer.
182 //! \tparam TExtent The extent type of the buffer.
183 //! \tparam TPlatform The platform from which the buffer is accessible.
184 //! \param host The host device to allocate the buffer on.
185 //! \param extent The extent of the buffer.
186 //! \return The newly allocated buffer.
187 template<typename TElem, typename TIdx, typename TExtent, typename TPlatform>
189 DevCpu const& host,
190 TPlatform const& platform,
191 TExtent const& extent = TExtent())
192 {
194 if constexpr(hasMappedBufSupport<Platform>)
195 {
196 return allocMappedBuf<TElem, TIdx>(host, platform, extent);
197 }
198 else
199 {
200 return allocBuf<TElem, TIdx>(host, extent);
201 }
202
203 ALPAKA_UNREACHABLE(allocBuf<TElem, TIdx>(host, extent));
204 }
205
206 //! Creates a constant buffer from the given mutable buffer.
207 //!
208 //! \tparam TBuf The type of the original buffer.
209 //! \param buf The original buffer.
210 //! \return The transformed buffer with only read-access allowed.
211 template<typename TBuf>
212 ALPAKA_FN_HOST auto makeConstBuf(TBuf const& buf)
213 {
215 }
216
217 template<typename TBuf>
219 {
221 }
222
223} // 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:169
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:127
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:212
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:77
constexpr bool hasAsyncBufSupport
Checks if the given device can allocate a stream-ordered memory buffer of the given dimensionality.
Definition Traits.hpp:108
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:188
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:61
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:65
ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const &extent=TExtent())
Allocates stream-ordered memory on the given device.
Definition Traits.hpp:92
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 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:55