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
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 BufCpu stream-ordered memory allocation capability trait specialization.
169 template<typename TDim>
170 struct HasAsyncBufSupport<TDim, DevCpu> : public std::true_type
171 {
172 };
173
174 //! The BufCpu stream-ordered memory allocation trait specialization.
175 template<typename TElem, typename TDim, typename TIdx>
176 struct AsyncBufAlloc<TElem, TDim, TIdx, DevCpu>
177 {
178 template<typename TQueue, typename TExtent>
179 ALPAKA_FN_HOST static auto allocAsyncBuf(TQueue queue, TExtent const& extent) -> BufCpu<TElem, TDim, TIdx>
180 {
182
183 static_assert(
184 std::is_same_v<Dev<TQueue>, DevCpu>,
185 "The BufCpu buffer can only be used with a queue on a DevCpu device!");
186 DevCpu const& dev = getDev(queue);
187
188 // If ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT is defined, positive, and a power of 2, use it as the
189 // default alignment for host memory allocations. Otherwise, the alignment is chosen to enable
190 // optimal performance dependant on the target architecture.
191#if defined(ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT)
192 static_assert(
193 ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT > 0
194 && ((ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT & (ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT - 1)) == 0),
195 "If defined, ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT must be a power of 2.");
196 constexpr std::size_t alignment = static_cast<std::size_t>(ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT);
197#else
198 constexpr std::size_t alignment = core::vectorization::defaultAlignment;
199#endif
200 // alpaka::AllocCpuAligned is stateless
202 static_assert(std::is_empty_v<Allocator>, "AllocCpuAligned is expected to be stateless");
203 auto* memPtr = alpaka::malloc<TElem>(Allocator{}, static_cast<std::size_t>(getExtentProduct(extent)));
204 auto deleter = [l_queue = std::move(queue)](TElem* ptr) mutable
205 {
207 l_queue,
208 [ptr]()
209 {
210 // free the memory
211 alpaka::free(Allocator{}, ptr);
212 });
213 };
214
215 return BufCpu<TElem, TDim, TIdx>(dev, memPtr, std::move(deleter), extent);
216 }
217 };
218
219 //! The pinned/mapped memory allocation capability trait specialization.
220 template<>
221 struct HasMappedBufSupport<PlatformCpu> : public std::true_type
222 {
223 };
224
225 //! The pinned/mapped memory allocation trait specialization.
226 template<typename TElem, typename TDim, typename TIdx>
227 struct BufAllocMapped<PlatformCpu, TElem, TDim, TIdx>
228 {
229 template<typename TExtent>
231 DevCpu const& host,
232 PlatformCpu const& /*platform*/,
233 TExtent const& extent) -> BufCpu<TElem, TDim, TIdx>
234 {
235 // Allocate standard host memory.
236 return allocBuf<TElem, TIdx>(host, extent);
237 }
238 };
239
240} // 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.
Definition BufCpu.hpp:24
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:40
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 getDev(T const &t)
Definition Traits.hpp:68
ALPAKA_FN_HOST auto enqueue(TQueue &queue, TTask &&task) -> void
Queues the given task in the given queue.
Definition Traits.hpp:47
The CPU device platform.
static ALPAKA_FN_HOST auto allocAsyncBuf(TQueue queue, TExtent const &extent) -> BufCpu< TElem, TDim, TIdx >
The stream-ordered memory allocator trait.
Definition Traits.hpp:35
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:45
static ALPAKA_FN_HOST auto allocBuf(DevCpu const &dev, TExtent const &extent) -> BufCpu< TElem, TDim, TIdx >
The memory allocator trait.
Definition Traits.hpp:31
The memory buffer type trait.
Definition Traits.hpp:23
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:84
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:80
The stream-ordered memory allocation capability trait.
Definition Traits.hpp:40
The pinned/mapped memory allocation capability trait.
Definition Traits.hpp:50
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:55