alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
BufCpuTraits.hpp
Go to the documentation of this file.
1/* Copyright 2025 Anton Reinhard
2 * SPDX-License-Identifier: MPL-2.0
3 */
4#pragma once
5
9
10namespace alpaka::trait
11{
12 //! The CPU device memory buffer type trait specialization.
13 template<typename TElem, typename TDim, typename TIdx>
14 struct BufType<DevCpu, TElem, TDim, TIdx>
15 {
17 };
18
19 //! The BufCpu device type trait specialization.
20 template<typename TElem, typename TDim, typename TIdx>
21 struct DevType<BufCpu<TElem, TDim, TIdx>>
22 {
23 using type = DevCpu;
24 };
25
26 //! The BufCpu device get trait specialization.
27 template<typename TElem, typename TDim, typename TIdx>
28 struct GetDev<BufCpu<TElem, TDim, TIdx>>
29 {
31 {
32 return buf.m_spBufImpl->m_dev;
33 }
34 };
35
36 //! The BufCpu dimension getter trait.
37 template<typename TElem, typename TDim, typename TIdx>
38 struct DimType<BufCpu<TElem, TDim, TIdx>>
39 {
40 using type = TDim;
41 };
42
43 //! The BufCpu memory element type get trait specialization.
44 template<typename TElem, typename TDim, typename TIdx>
45 struct ElemType<BufCpu<TElem, TDim, TIdx>>
46 {
47 using type = TElem;
48 };
49
50 //! The BufCpu width get trait specialization.
51 template<typename TElem, typename TDim, typename TIdx>
52 struct GetExtents<BufCpu<TElem, TDim, TIdx>>
53 {
55 {
56 return buf.m_spBufImpl->m_extentElements;
57 }
58 };
59
60 //! The BufCpu native pointer get trait specialization.
61 template<typename TElem, typename TDim, typename TIdx>
62 struct GetPtrNative<BufCpu<TElem, TDim, TIdx>>
63 {
64 ALPAKA_FN_HOST static auto getPtrNative(BufCpu<TElem, TDim, TIdx> const& buf) -> TElem const*
65 {
66 return buf.m_spBufImpl->m_pMem;
67 }
68
70 {
71 return buf.m_spBufImpl->m_pMem;
72 }
73 };
74
75 //! The BufCpu pointer on device get trait specialization.
76 template<typename TElem, typename TDim, typename TIdx>
77 struct GetPtrDev<BufCpu<TElem, TDim, TIdx>, DevCpu>
78 {
79 ALPAKA_FN_HOST static auto getPtrDev(BufCpu<TElem, TDim, TIdx> const& buf, DevCpu const& dev) -> TElem const*
80 {
81 if(dev == getDev(buf))
82 {
83 return buf.m_spBufImpl->m_pMem;
84 }
85 else
86 {
87 throw std::runtime_error("The buffer is not accessible from the given device!");
88 }
89 }
90
91 ALPAKA_FN_HOST static auto getPtrDev(BufCpu<TElem, TDim, TIdx>& buf, DevCpu const& dev) -> TElem*
92 {
93 if(dev == getDev(buf))
94 {
95 return buf.m_spBufImpl->m_pMem;
96 }
97 else
98 {
99 throw std::runtime_error("The buffer is not accessible from the given device!");
100 }
101 }
102 };
103
104 //! The BufCpu offset get trait specialization.
105 template<typename TElem, typename TDim, typename TIdx>
106 struct GetOffsets<BufCpu<TElem, TDim, TIdx>>
107 {
109 {
110 return Vec<TDim, TIdx>::zeros();
111 }
112 };
113
114 //! The BufCpu idx type trait specialization.
115 template<typename TElem, typename TDim, typename TIdx>
116 struct IdxType<BufCpu<TElem, TDim, TIdx>>
117 {
118 using type = TIdx;
119 };
120
121 //! The MakeConstBuf trait for CPU buffers.
122 template<typename TElem, typename TDim, typename TIdx>
123 struct MakeConstBuf<BufCpu<TElem, TDim, TIdx>>
124 {
129
134 };
135
136 //! The BufCpu memory allocation trait specialization.
137 template<typename TElem, typename TDim, typename TIdx>
138 struct BufAlloc<TElem, TDim, TIdx, DevCpu>
139 {
140 template<typename TExtent>
141 ALPAKA_FN_HOST static auto allocBuf(DevCpu const& dev, TExtent const& extent) -> BufCpu<TElem, TDim, TIdx>
142 {
144
145 // If ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT is defined, positive, and a power of 2, use it as the
146 // default alignment for host memory allocations. Otherwise, the alignment is chosen to enable
147 // optimal performance dependant on the target architecture.
148#if defined(ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT)
149 static_assert(
150 ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT > 0
151 && ((ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT & (ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT - 1)) == 0),
152 "If defined, ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT must be a power of 2.");
153 constexpr std::size_t alignment = static_cast<std::size_t>(ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT);
154#else
155 constexpr std::size_t alignment = core::vectorization::defaultAlignment;
156#endif
157 // alpaka::AllocCpuAligned is stateless
159 static_assert(std::is_empty_v<Allocator>, "AllocCpuAligned is expected to be stateless");
160 auto* memPtr = alpaka::malloc<TElem>(Allocator{}, static_cast<std::size_t>(getExtentProduct(extent)));
161 auto deleter = [](TElem* ptr) { alpaka::free(Allocator{}, ptr); };
162
163 return BufCpu<TElem, TDim, TIdx>(dev, memPtr, std::move(deleter), extent);
164 }
165 };
166
167 //! The ConstBufCpu stream-ordered memory allocation capability trait specialization.
168 template<typename TDim>
169 struct HasAsyncBufSupport<TDim, DevCpu> : public std::true_type
170 {
171 };
172
173 //! The BufCpu stream-ordered memory allocation trait specialization.
174 template<typename TElem, typename TDim, typename TIdx>
175 struct AsyncBufAlloc<TElem, TDim, TIdx, DevCpu>
176 {
177 template<typename TQueue, typename TExtent>
178 ALPAKA_FN_HOST static auto allocAsyncBuf(TQueue queue, TExtent const& extent) -> BufCpu<TElem, TDim, TIdx>
179 {
181
182 static_assert(
183 std::is_same_v<Dev<TQueue>, DevCpu>,
184 "The BufCpu buffer can only be used with a queue on a DevCpu device!");
185 DevCpu const& dev = getDev(queue);
186
187 // If ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT is defined, positive, and a power of 2, use it as the
188 // default alignment for host memory allocations. Otherwise, the alignment is chosen to enable
189 // optimal performance dependant on the target architecture.
190#if defined(ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT)
191 static_assert(
192 ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT > 0
193 && ((ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT & (ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT - 1)) == 0),
194 "If defined, ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT must be a power of 2.");
195 constexpr std::size_t alignment = static_cast<std::size_t>(ALPAKA_DEFAULT_HOST_MEMORY_ALIGNMENT);
196#else
197 constexpr std::size_t alignment = core::vectorization::defaultAlignment;
198#endif
199 // alpaka::AllocCpuAligned is stateless
201 static_assert(std::is_empty_v<Allocator>, "AllocCpuAligned is expected to be stateless");
202 auto* memPtr = alpaka::malloc<TElem>(Allocator{}, static_cast<std::size_t>(getExtentProduct(extent)));
203 auto deleter = [l_queue = std::move(queue)](TElem* ptr) mutable
204 {
206 l_queue,
207 [ptr]()
208 {
209 // free the memory
210 alpaka::free(Allocator{}, ptr);
211 });
212 };
213
214 return BufCpu<TElem, TDim, TIdx>(dev, memPtr, std::move(deleter), extent);
215 }
216 };
217
218 //! The pinned/mapped memory allocation capability trait specialization.
219 template<>
220 struct HasMappedBufSupport<PlatformCpu> : public std::true_type
221 {
222 };
223
224 //! The pinned/mapped memory allocation trait specialization.
225 template<typename TElem, typename TDim, typename TIdx>
226 struct BufAllocMapped<PlatformCpu, TElem, TDim, TIdx>
227 {
228 template<typename TExtent>
230 DevCpu const& host,
231 PlatformCpu const& /*platform*/,
232 TExtent const& extent) -> BufCpu<TElem, TDim, TIdx>
233 {
234 // Allocate standard host memory.
235 return allocBuf<TElem, TIdx>(host, extent);
236 }
237 };
238
239} // 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:34
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:58
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:54
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