alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
BufGenericSyclTraits.hpp
Go to the documentation of this file.
1/* Copyright 2025 Anton Reinhard
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
9
10#ifdef ALPAKA_ACC_SYCL_ENABLED
11
12namespace alpaka::trait
13{
14 //! The SYCL device memory buffer type trait specialization.
15 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
16 struct BufType<DevGenericSycl<TTag>, TElem, TDim, TIdx>
17 {
18 using type = BufGenericSycl<TElem, TDim, TIdx, TTag>;
19 };
20
21 //! The BufGenericSycl device type trait specialization.
22 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
23 struct DevType<BufGenericSycl<TElem, TDim, TIdx, TTag>>
24 {
25 using type = DevGenericSycl<TTag>;
26 };
27
28 //! The BufGenericSycl device get trait specialization.
29 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
30 struct GetDev<BufGenericSycl<TElem, TDim, TIdx, TTag>>
31 {
32 ALPAKA_FN_HOST static auto getDev(BufGenericSycl<TElem, TDim, TIdx, TTag> const& buf) -> DevGenericSycl<TTag>
33 {
34 return buf.m_spBufImpl->m_dev;
35 }
36 };
37
38 //! The BufGenericSycl dimension getter trait.
39 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
40 struct DimType<BufGenericSycl<TElem, TDim, TIdx, TTag>>
41 {
42 using type = TDim;
43 };
44
45 //! The BufGenericSycl memory element type get trait specialization.
46 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
47 struct ElemType<BufGenericSycl<TElem, TDim, TIdx, TTag>>
48 {
49 using type = TElem;
50 };
51
52 //! The BufGenericSycl width get trait specialization.
53 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
54 struct GetExtents<BufGenericSycl<TElem, TDim, TIdx, TTag>>
55 {
56 ALPAKA_FN_HOST auto operator()(BufGenericSycl<TElem, TDim, TIdx, TTag> const& buf)
57 {
58 return buf.m_spBufImpl->m_extentElements;
59 }
60 };
61
62 //! The BufGenericSycl native pointer get trait specialization.
63 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
64 struct GetPtrNative<BufGenericSycl<TElem, TDim, TIdx, TTag>>
65 {
66 ALPAKA_FN_HOST static auto getPtrNative(BufGenericSycl<TElem, TDim, TIdx, TTag> const& buf) -> TElem const*
67 {
68 return buf.m_spBufImpl->m_pMem;
69 }
70
71 ALPAKA_FN_HOST static auto getPtrNative(BufGenericSycl<TElem, TDim, TIdx, TTag>& buf) -> TElem*
72 {
73 return buf.m_spBufImpl->m_pMem;
74 }
75 };
76
77 //! The BufGenericSycl pointer on device get trait specialization.
78 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
79 struct GetPtrDev<BufGenericSycl<TElem, TDim, TIdx, TTag>, DevGenericSycl<TTag>>
80 {
81 ALPAKA_FN_HOST static auto getPtrDev(
82 BufGenericSycl<TElem, TDim, TIdx, TTag> const& buf,
83 DevGenericSycl<TTag> const& dev) -> TElem const*
84 {
85 if(dev == getDev(buf))
86 {
87 return buf.m_spBufImpl->m_pMem;
88 }
89 else
90 {
91 throw std::runtime_error("The buffer is not accessible from the given device!");
92 }
93 }
94
95 ALPAKA_FN_HOST static auto getPtrDev(
96 BufGenericSycl<TElem, TDim, TIdx, TTag>& buf,
97 DevGenericSycl<TTag> const& dev) -> TElem*
98 {
99 if(dev == getDev(buf))
100 {
101 return buf.m_spBufImpl->m_pMem;
102 }
103 else
104 {
105 throw std::runtime_error("The buffer is not accessible from the given device!");
106 }
107 }
108 };
109
110 //! The BufGenericSycl offset get trait specialization.
111 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
112 struct GetOffsets<BufGenericSycl<TElem, TDim, TIdx, TTag>>
113 {
114 ALPAKA_FN_HOST auto operator()(BufGenericSycl<TElem, TDim, TIdx, TTag> const& /*buf*/) const -> Vec<TDim, TIdx>
115 {
116 return Vec<TDim, TIdx>::zeros();
117 }
118 };
119
120 //! The BufGenericSycl idx type trait specialization.
121 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
122 struct IdxType<BufGenericSycl<TElem, TDim, TIdx, TTag>>
123 {
124 using type = TIdx;
125 };
126
127 //! The MakeConstBuf trait for Sycl buffers.
128 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
129 struct MakeConstBuf<BufGenericSycl<TElem, TDim, TIdx, TTag>>
130 {
131 ALPAKA_FN_HOST static auto makeConstBuf(BufGenericSycl<TElem, TDim, TIdx, TTag> const& buf)
132 -> ConstBufGenericSycl<TElem, TDim, TIdx, TTag>
133 {
134 return ConstBufGenericSycl<TElem, TDim, TIdx, TTag>(buf);
135 }
136
137 ALPAKA_FN_HOST static auto makeConstBuf(BufGenericSycl<TElem, TDim, TIdx, TTag>&& buf)
138 -> ConstBufGenericSycl<TElem, TDim, TIdx, TTag>
139 {
140 return ConstBufGenericSycl<TElem, TDim, TIdx, TTag>(std::move(buf));
141 }
142 };
143
144 //! The SYCL memory allocation trait specialization.
145 template<typename TElem, typename TDim, typename TIdx, concepts::Tag TTag>
146 struct BufAlloc<TElem, TDim, TIdx, DevGenericSycl<TTag>>
147 {
148 template<typename TExtent>
149 ALPAKA_FN_HOST static auto allocBuf(DevGenericSycl<TTag> const& dev, TExtent const& extent)
150 -> BufGenericSycl<TElem, TDim, TIdx, TTag>
151 {
153
154# if ALPAKA_DEBUG >= ALPAKA_DEBUG_FULL
155 if constexpr(TDim::value == 0)
156 std::cout << __func__ << " ewb: " << sizeof(TElem) << '\n';
157 else if constexpr(TDim::value == 1)
158 {
159 auto const width = getWidth(extent);
160
161 auto const widthBytes = width * static_cast<TIdx>(sizeof(TElem));
162 std::cout << __func__ << " ew: " << width << " ewb: " << widthBytes << '\n';
163 }
164 else if constexpr(TDim::value == 2)
165 {
166 auto const width = getWidth(extent);
167 auto const height = getHeight(extent);
168
169 auto const widthBytes = width * static_cast<TIdx>(sizeof(TElem));
170 std::cout << __func__ << " ew: " << width << " eh: " << height << " ewb: " << widthBytes
171 << " pitch: " << widthBytes << '\n';
172 }
173 else if constexpr(TDim::value == 3)
174 {
175 auto const width = getWidth(extent);
176 auto const height = getHeight(extent);
177 auto const depth = getDepth(extent);
178
179 auto const widthBytes = width * static_cast<TIdx>(sizeof(TElem));
180 std::cout << __func__ << " ew: " << width << " eh: " << height << " ed: " << depth
181 << " ewb: " << widthBytes << " pitch: " << widthBytes << '\n';
182 }
183# endif
184
185 auto const& [nativeDev, nativeContext] = dev.getNativeHandle();
186 TElem* memPtr = sycl::malloc_device<TElem>(
187 static_cast<std::size_t>(getExtentProduct(extent)),
188 nativeDev,
189 nativeContext);
190 auto deleter = [ctx = nativeContext](TElem* ptr) { sycl::free(ptr, ctx); };
191
192 return BufGenericSycl<TElem, TDim, TIdx, TTag>(dev, memPtr, std::move(deleter), extent);
193 }
194 };
195
196 //! The BufGenericSycl stream-ordered memory allocation capability trait specialization.
197 template<typename TDim, concepts::Tag TTag>
198 struct HasAsyncBufSupport<TDim, DevGenericSycl<TTag>> : std::false_type
199 {
200 };
201
202 //! The pinned/mapped memory allocation capability trait specialization.
203 template<concepts::Tag TTag>
204 struct HasMappedBufSupport<PlatformGenericSycl<TTag>> : public std::true_type
205 {
206 };
207
208 //! The pinned/mapped memory allocation trait specialization for the SYCL devices.
209 template<concepts::Tag TTag, typename TElem, typename TDim, typename TIdx>
210 struct BufAllocMapped<PlatformGenericSycl<TTag>, TElem, TDim, TIdx>
211 {
212 template<typename TExtent>
213 ALPAKA_FN_HOST static auto allocMappedBuf(
214 DevCpu const& host,
215 PlatformGenericSycl<TTag> const& platform,
216 TExtent const& extent) -> BufCpu<TElem, TDim, TIdx>
217 {
219
220 // Allocate SYCL page-locked memory on the host, mapped into the SYCL platform's address space and
221 // accessible to all devices in the SYCL platform.
222 auto ctx = platform.syclContext();
223 TElem* memPtr = sycl::malloc_host<TElem>(static_cast<std::size_t>(getExtentProduct(extent)), ctx);
224 auto deleter = [ctx](TElem* ptr) { sycl::free(ptr, ctx); };
225
226 return BufCpu<TElem, TDim, TIdx>(host, memPtr, std::move(deleter), extent);
227 }
228 };
229
230} // namespace alpaka::trait
231
232#endif
#define ALPAKA_DEBUG_MINIMAL_LOG_SCOPE
Definition Debug.hpp:55
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
The accelerator traits.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtentProduct(T const &object) -> Idx< T >
Definition Traits.hpp:134
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getHeight(TExtent const &extent=TExtent()) -> Idx< TExtent >
Definition Traits.hpp:108
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 getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
Definition Traits.hpp:136
ALPAKA_FN_HOST auto allocBuf(TDev const &dev, TExtent const &extent=TExtent())
Allocates memory on the given device.
Definition Traits.hpp:77
ALPAKA_FN_HOST auto getDev(T const &t)
Definition Traits.hpp:68
ALPAKA_FN_HOST auto getPtrDev(TView const &view, TDev const &dev) -> Elem< TView > const *
Gets the pointer to the view on the given device.
Definition Traits.hpp:157
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getDepth(TExtent const &extent=TExtent()) -> Idx< TExtent >
Definition Traits.hpp:121
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getWidth(TExtent const &extent=TExtent()) -> Idx< TExtent >
Definition Traits.hpp:95