alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
BufCpuTraits.hpp
Go to the documentation of this file.
1/* Copyright 2025 Alexander Matthes, Axel Huebl, Benjamin Worpitz, Andrea Bocci, Jan Stephan, Bernhard Manfred Gruber,
2 * Anton Reinhard, Maria Michailidi
3 * SPDX-License-Identifier: MPL-2.0
4 */
5#pragma once
6
10
11namespace alpaka::trait
12{
13 //! The CPU device memory buffer type trait specialization.
14 template<typename TElem, typename TDim, typename TIdx>
15 struct BufType<DevCpu, TElem, TDim, TIdx>
16 {
18 };
19
20 //! The BufCpu device type trait specialization.
21 template<typename TElem, typename TDim, typename TIdx>
22 struct DevType<BufCpu<TElem, TDim, TIdx>>
23 {
24 using type = DevCpu;
25 };
26
27 //! The BufCpu device get trait specialization.
28 template<typename TElem, typename TDim, typename TIdx>
29 struct GetDev<BufCpu<TElem, TDim, TIdx>>
30 {
32 {
33 return buf.m_spBufImpl->m_dev;
34 }
35 };
36
37 //! The BufCpu dimension getter trait.
38 template<typename TElem, typename TDim, typename TIdx>
39 struct DimType<BufCpu<TElem, TDim, TIdx>>
40 {
41 using type = TDim;
42 };
43
44 //! The BufCpu memory element type get trait specialization.
45 template<typename TElem, typename TDim, typename TIdx>
46 struct ElemType<BufCpu<TElem, TDim, TIdx>>
47 {
48 using type = TElem;
49 };
50
51 //! The BufCpu width get trait specialization.
52 template<typename TElem, typename TDim, typename TIdx>
53 struct GetExtents<BufCpu<TElem, TDim, TIdx>>
54 {
56 {
57 return buf.m_spBufImpl->m_extentElements;
58 }
59 };
60
61 //! The BufCpu native pointer get trait specialization.
62 template<typename TElem, typename TDim, typename TIdx>
63 struct GetPtrNative<BufCpu<TElem, TDim, TIdx>>
64 {
65 ALPAKA_FN_HOST static auto getPtrNative(BufCpu<TElem, TDim, TIdx> const& buf) -> TElem const*
66 {
67 return buf.m_spBufImpl->m_pMem;
68 }
69
71 {
72 return buf.m_spBufImpl->m_pMem;
73 }
74 };
75
76 //! The BufCpu pointer on device get trait specialization.
77 template<typename TElem, typename TDim, typename TIdx>
78 struct GetPtrDev<BufCpu<TElem, TDim, TIdx>, DevCpu>
79 {
80 ALPAKA_FN_HOST static auto getPtrDev(BufCpu<TElem, TDim, TIdx> const& buf, DevCpu const& dev) -> TElem const*
81 {
82 if(dev == getDev(buf))
83 {
84 return buf.m_spBufImpl->m_pMem;
85 }
86 else
87 {
88 throw std::runtime_error("The buffer is not accessible from the given device!");
89 }
90 }
91
92 ALPAKA_FN_HOST static auto getPtrDev(BufCpu<TElem, TDim, TIdx>& buf, DevCpu const& dev) -> TElem*
93 {
94 if(dev == getDev(buf))
95 {
96 return buf.m_spBufImpl->m_pMem;
97 }
98 else
99 {
100 throw std::runtime_error("The buffer is not accessible from the given device!");
101 }
102 }
103 };
104
105 //! The BufCpu offset get trait specialization.
106 template<typename TElem, typename TDim, typename TIdx>
107 struct GetOffsets<BufCpu<TElem, TDim, TIdx>>
108 {
110 {
111 return Vec<TDim, TIdx>::zeros();
112 }
113 };
114
115 //! The BufCpu idx type trait specialization.
116 template<typename TElem, typename TDim, typename TIdx>
117 struct IdxType<BufCpu<TElem, TDim, TIdx>>
118 {
119 using type = TIdx;
120 };
121
122 //! The MakeConstBuf trait for CPU buffers.
123 template<typename TElem, typename TDim, typename TIdx>
124 struct MakeConstBuf<BufCpu<TElem, TDim, TIdx>>
125 {
130
135 };
136
137 //! The BufCpu memory allocation trait specialization.
138 template<typename TElem, typename TDim, typename TIdx>
139 struct BufAlloc<TElem, TDim, TIdx, DevCpu>
140 {
141 template<typename TExtent>
142 ALPAKA_FN_HOST static auto allocBuf(DevCpu const& dev, TExtent const& extent) -> BufCpu<TElem, TDim, TIdx>
143 {
145
146 // If ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT is defined, positive, and a power of 2, use it as the
147 // default alignment for host memory allocations. Otherwise, the alignment is chosen to enable
148 // optimal performance dependant on the target architecture.
149#if defined(ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT)
150 static_assert(
151 ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT > 0
152 && ((ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT & (ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT - 1)) == 0),
153 "If defined, ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT must be a power of 2.");
154 constexpr std::size_t alignment = static_cast<std::size_t>(ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT);
155#else
156 constexpr std::size_t alignment = core::vectorization::defaultAlignment;
157#endif
158 // alpaka::AllocCpuAligned is stateless
160 static_assert(std::is_empty_v<Allocator>, "AllocCpuAligned is expected to be stateless");
161 auto* memPtr = alpaka::malloc<TElem>(Allocator{}, static_cast<std::size_t>(getExtentProduct(extent)));
162 auto deleter = [](TElem* ptr) { alpaka::free(Allocator{}, ptr); };
163
164 return BufCpu<TElem, TDim, TIdx>(dev, memPtr, std::move(deleter), extent);
165 }
166 };
167
168 //! The CPU caching memory allocation trait specialization.
169 template<typename TElem, typename TDim, typename TIdx, typename TAllocator>
170 struct BufAllocWithAllocator<TElem, TDim, TIdx, DevCpu, TAllocator>
171 {
172 template<typename TExtent>
173 ALPAKA_FN_HOST static auto allocBuf(DevCpu const& dev, TExtent const& extent, TAllocator allocator)
175 {
177
178 std::size_t const bytes = static_cast<std::size_t>(getExtentProduct(extent)) * sizeof(TElem);
179 void* const memPtr = allocator.allocate(bytes, alignof(TElem));
180 auto deleter = [alloc = std::move(allocator)](TElem* ptr) mutable { alloc.deallocate(ptr); };
181 return BufCpu<TElem, TDim, TIdx>(dev, static_cast<TElem*>(memPtr), std::move(deleter), extent);
182 }
183 };
184
185 //! The BufCpu stream-ordered memory allocation capability trait specialization.
186 template<typename TDim>
187 struct HasAsyncBufSupport<TDim, DevCpu> : public std::true_type
188 {
189 };
190
191 //! The CPU caching stream-ordered memory allocation trait specialization.
192 template<typename TElem, typename TDim, typename TIdx, typename TAllocator>
193 struct AsyncBufAllocWithAllocator<TElem, TDim, TIdx, DevCpu, TAllocator>
194 {
195 template<typename TQueue, typename TExtent>
196 ALPAKA_FN_HOST static auto allocAsyncBuf(TQueue queue, TExtent const& extent, TAllocator allocator)
198 {
200
201 static_assert(
202 std::is_same_v<Dev<TQueue>, DevCpu>,
203 "The BufCpu buffer can only be used with a queue on a DevCpu device!");
204 auto const dev = getDev(queue);
205 std::size_t const bytes = static_cast<std::size_t>(getExtentProduct(extent)) * sizeof(TElem);
206 void* const memPtr = allocator.allocate(bytes, alignof(TElem));
207 auto deleter = [l_queue = std::move(queue), alloc = std::move(allocator)](TElem* ptr) mutable
208 { alpaka::enqueue(l_queue, [ptr, alloc]() mutable { alloc.deallocate(ptr); }); };
209 return BufCpu<TElem, TDim, TIdx>(dev, static_cast<TElem*>(memPtr), std::move(deleter), extent);
210 }
211 };
212
213 //! The BufCpu stream-ordered memory allocation trait specialization.
214 template<typename TElem, typename TDim, typename TIdx>
215 struct AsyncBufAlloc<TElem, TDim, TIdx, DevCpu>
216 {
217 template<typename TQueue, typename TExtent>
218 ALPAKA_FN_HOST static auto allocAsyncBuf(TQueue queue, TExtent const& extent) -> BufCpu<TElem, TDim, TIdx>
219 {
221
222 static_assert(
223 std::is_same_v<Dev<TQueue>, DevCpu>,
224 "The BufCpu buffer can only be used with a queue on a DevCpu device!");
225 DevCpu const& dev = getDev(queue);
226
227 // If ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT is defined, positive, and a power of 2, use it as the
228 // default alignment for host memory allocations. Otherwise, the alignment is chosen to enable
229 // optimal performance dependant on the target architecture.
230#if defined(ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT)
231 static_assert(
232 ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT > 0
233 && ((ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT & (ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT - 1)) == 0),
234 "If defined, ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT must be a power of 2.");
235 constexpr std::size_t alignment = static_cast<std::size_t>(ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT);
236#else
237 constexpr std::size_t alignment = core::vectorization::defaultAlignment;
238#endif
239 // alpaka::AllocCpuAligned is stateless
241 static_assert(std::is_empty_v<Allocator>, "AllocCpuAligned is expected to be stateless");
242 auto* memPtr = alpaka::malloc<TElem>(Allocator{}, static_cast<std::size_t>(getExtentProduct(extent)));
243 auto deleter = [l_queue = std::move(queue)](TElem* ptr) mutable
244 {
246 l_queue,
247 [ptr]()
248 {
249 // free the memory
250 alpaka::free(Allocator{}, ptr);
251 });
252 };
253
254 return BufCpu<TElem, TDim, TIdx>(dev, memPtr, std::move(deleter), extent);
255 }
256 };
257
258 //! The pinned/mapped memory allocation capability trait specialization.
259 template<>
260 struct HasMappedBufSupport<PlatformCpu> : public std::true_type
261 {
262 };
263
264 //! The pinned/mapped memory allocation trait specialization.
265 template<typename TElem, typename TDim, typename TIdx>
266 struct BufAllocMapped<PlatformCpu, TElem, TDim, TIdx>
267 {
268 template<typename TExtent>
270 DevCpu const& host,
271 PlatformCpu const& /*platform*/,
272 TExtent const& extent) -> BufCpu<TElem, TDim, TIdx>
273 {
274 // Allocate standard host memory.
275 return allocBuf<TElem, TIdx>(host, extent);
276 }
277 };
278
279 //! The unified/managed memory allocation trait specialization.
280 template<typename TElem, typename TDim, typename TIdx>
281 struct BufAllocManaged<PlatformCpu, TElem, TDim, TIdx>
282 {
283 template<typename TExtent>
285 DevCpu const& host,
286 PlatformCpu const& /*platform*/,
287 TExtent const& extent) -> BufCpu<TElem, TDim, TIdx>
288 {
289 return allocBuf<TElem, TIdx>(host, extent);
290 }
291 };
292
293} // namespace alpaka::trait
#define ALPAKA_DEBUG_MINIMAL_LOG_SCOPE
Definition Debug.hpp:55
The CPU boost aligned allocator.
The CPU memory buffer template implementing muting accessors.
std::shared_ptr< TBufImpl > m_spBufImpl
Definition BufCpu.hpp:35
The CPU memory buffer.
The CPU device handle.
Definition DevCpu.hpp:56
A n-dimensional vector.
Definition Vec.hpp:38
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC constexpr auto zeros() -> Vec< TDim, TVal >
Zero value constructor.
Definition Vec.hpp:99
#define ALPAKA_FN_HOST
Definition Common.hpp:43
constexpr std::size_t defaultAlignment
Definition Vectorize.hpp:33
The accelerator traits.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtentProduct(T const &object) -> Idx< T >
Definition Traits.hpp:134
ALPAKA_FN_HOST auto free(TAlloc const &alloc, T const *const ptr) -> void
Frees the memory identified by the given pointer.
Definition Traits.hpp:41
ALPAKA_FN_HOST auto malloc(TAlloc const &alloc, std::size_t const &sizeElems) -> T *
Definition Traits.hpp:33
ALPAKA_FN_HOST auto allocBuf(TDev const &dev, TExtent const &extent=TExtent())
Allocates memory on the given device.
Definition Traits.hpp:119
ALPAKA_FN_HOST auto getDev(T const &t)
Definition Traits.hpp:75
ALPAKA_FN_HOST auto enqueue(TQueue &queue, TTask &&task) -> void
Queues the given task in the given queue.
Definition Traits.hpp:54
The CPU device platform.
static ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const &extent, TAllocator allocator) -> BufCpu< TElem, TDim, TIdx >
The allocator-aware stream-ordered memory allocator trait.
Definition Traits.hpp:93
static ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const &extent) -> BufCpu< TElem, TDim, TIdx >
The stream-ordered memory allocator trait.
Definition Traits.hpp:53
static ALPAKA_FN_HOST auto allocManagedBuf(DevCpu const &host, PlatformCpu const &, TExtent const &extent) -> BufCpu< TElem, TDim, TIdx >
The managed (unified) memory allocator trait.
Definition Traits.hpp:73
static ALPAKA_FN_HOST auto allocMappedBuf(DevCpu const &host, PlatformCpu const &, TExtent const &extent) -> BufCpu< TElem, TDim, TIdx >
The pinned/mapped memory allocator trait.
Definition Traits.hpp:63
static ALPAKA_FN_HOST auto allocBuf(DevCpu const &dev, TExtent const &extent, TAllocator allocator) -> BufCpu< TElem, TDim, TIdx >
The allocator-aware memory allocator trait.
Definition Traits.hpp:83
static ALPAKA_FN_HOST auto allocBuf(DevCpu const &dev, TExtent const &extent) -> BufCpu< TElem, TDim, TIdx >
The memory allocator trait.
Definition Traits.hpp:49
The memory buffer type trait.
Definition Traits.hpp:41
The device type trait.
Definition Traits.hpp:23
The dimension getter type trait.
Definition Traits.hpp:14
The element type trait.
Definition Traits.hpp:16
static ALPAKA_FN_HOST auto getDev(BufCpu< TElem, TDim, TIdx > const &buf) -> DevCpu
The device get trait.
Definition Traits.hpp:27
ALPAKA_FN_HOST auto operator()(BufCpu< TElem, TDim, TIdx > const &buf)
The GetExtents trait for getting the extents of an object as an alpaka::Vec.
Definition Traits.hpp:37
ALPAKA_FN_HOST auto operator()(BufCpu< TElem, TDim, TIdx > const &) const -> Vec< TDim, TIdx >
The GetOffsets trait for getting the offsets of an object as an alpaka::Vec.
Definition Traits.hpp:33
static ALPAKA_FN_HOST auto getPtrDev(BufCpu< TElem, TDim, TIdx > &buf, DevCpu const &dev) -> TElem *
static ALPAKA_FN_HOST auto getPtrDev(BufCpu< TElem, TDim, TIdx > const &buf, DevCpu const &dev) -> TElem const *
The pointer on device get trait.
Definition Traits.hpp:96
static ALPAKA_FN_HOST auto getPtrNative(BufCpu< TElem, TDim, TIdx > &buf) -> TElem *
static ALPAKA_FN_HOST auto getPtrNative(BufCpu< TElem, TDim, TIdx > const &buf) -> TElem const *
The native pointer get trait.
Definition Traits.hpp:92
The stream-ordered memory allocation capability trait.
Definition Traits.hpp:58
The pinned/mapped memory allocation capability trait.
Definition Traits.hpp:68
The idx type trait.
Definition Traits.hpp:25
static ALPAKA_FN_HOST auto makeConstBuf(BufCpu< TElem, TDim, TIdx > &&buf) -> ConstBufCpu< TElem, TDim, TIdx >
static ALPAKA_FN_HOST auto makeConstBuf(BufCpu< TElem, TDim, TIdx > const &buf) -> ConstBufCpu< TElem, TDim, TIdx >
The trait to transform a mutable buffer into a constant one.
Definition Traits.hpp:97