18 #include <type_traits>
20 #ifdef ALPAKA_ACC_SYCL_ENABLED
22 # include <sycl/sycl.hpp>
27 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
28 class BufGenericSycl :
public internal::ViewAccessOps<BufGenericSycl<TElem, TDim, TIdx, TTag>>
32 !std::is_const_v<TElem>,
33 "The elem type of the buffer can not be const because the C++ Standard forbids containers of const "
35 static_assert(!std::is_const_v<TIdx>,
"The idx type of the buffer can not be const!");
38 template<
typename TExtent,
typename Deleter>
39 BufGenericSycl(DevGenericSycl<TTag>
const& dev, TElem*
const pMem, Deleter deleter, TExtent
const& extent)
42 , m_spMem(pMem, std::move(deleter))
47 TDim::value == Dim<TExtent>::value,
48 "The dimensionality of TExtent and the dimensionality of the TDim template parameter have to be "
52 std::is_same_v<TIdx, Idx<TExtent>>,
53 "The idx type of TExtent and the TIdx template parameter have to be identical!");
56 DevGenericSycl<TTag> m_dev;
57 Vec<TDim, TIdx> m_extentElements;
58 std::shared_ptr<TElem> m_spMem;
65 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
66 struct DevType<BufGenericSycl<TElem, TDim, TIdx, TTag>>
68 using type = DevGenericSycl<TTag>;
72 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
73 struct GetDev<BufGenericSycl<TElem, TDim, TIdx, TTag>>
75 static auto getDev(BufGenericSycl<TElem, TDim, TIdx, TTag>
const& buf)
82 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
83 struct DimType<BufGenericSycl<TElem, TDim, TIdx, TTag>>
89 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
90 struct ElemType<BufGenericSycl<TElem, TDim, TIdx, TTag>>
96 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
97 struct GetExtents<BufGenericSycl<TElem, TDim, TIdx, TTag>>
99 auto operator()(BufGenericSycl<TElem, TDim, TIdx, TTag>
const& buf)
const
101 return buf.m_extentElements;
106 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
107 struct GetPtrNative<BufGenericSycl<TElem, TDim, TIdx, TTag>>
109 static auto getPtrNative(BufGenericSycl<TElem, TDim, TIdx, TTag>
const& buf) -> TElem
const*
111 return buf.m_spMem.get();
114 static auto getPtrNative(BufGenericSycl<TElem, TDim, TIdx, TTag>& buf) -> TElem*
116 return buf.m_spMem.get();
121 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
122 struct GetPtrDev<BufGenericSycl<TElem, TDim, TIdx, TTag>, DevGenericSycl<TTag>>
124 static auto getPtrDev(BufGenericSycl<TElem, TDim, TIdx, TTag>
const& buf, DevGenericSycl<TTag>
const& dev)
129 return buf.m_spMem.get();
133 throw std::runtime_error(
"The buffer is not accessible from the given device!");
137 static auto getPtrDev(BufGenericSycl<TElem, TDim, TIdx, TTag>& buf, DevGenericSycl<TTag>
const& dev) -> TElem*
141 return buf.m_spMem.get();
145 throw std::runtime_error(
"The buffer is not accessible from the given device!");
151 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
152 struct BufAlloc<TElem, TDim, TIdx, DevGenericSycl<TTag>>
154 template<
typename TExtent>
155 static auto allocBuf(DevGenericSycl<TTag>
const& dev, TExtent
const& extent)
156 -> BufGenericSycl<TElem, TDim, TIdx, TTag>
160 # if ALPAKA_DEBUG >= ALPAKA_DEBUG_FULL
161 if constexpr(TDim::value == 0)
162 std::cout << __func__ << " ewb: " << sizeof(TElem) << '\n';
163 else if constexpr(TDim::value == 1)
165 auto const width =
getWidth(extent);
167 auto const widthBytes = width *
static_cast<TIdx
>(
sizeof(TElem));
168 std::cout << __func__ <<
" ew: " << width <<
" ewb: " << widthBytes <<
'\n';
170 else if constexpr(TDim::value == 2)
172 auto const width =
getWidth(extent);
175 auto const widthBytes = width *
static_cast<TIdx
>(
sizeof(TElem));
176 std::cout << __func__ <<
" ew: " << width <<
" eh: " << height <<
" ewb: " << widthBytes
177 <<
" pitch: " << widthBytes <<
'\n';
179 else if constexpr(TDim::value == 3)
181 auto const width =
getWidth(extent);
183 auto const depth =
getDepth(extent);
185 auto const widthBytes = width *
static_cast<TIdx
>(
sizeof(TElem));
186 std::cout << __func__ <<
" ew: " << width <<
" eh: " << height <<
" ed: " << depth
187 <<
" ewb: " << widthBytes <<
" pitch: " << widthBytes <<
'\n';
191 auto const& [nativeDev, nativeContext] = dev.getNativeHandle();
192 TElem* memPtr = sycl::malloc_device<TElem>(
196 auto deleter = [ctx = nativeContext](TElem* ptr) {
sycl::free(ptr, ctx); };
198 return BufGenericSycl<TElem, TDim, TIdx, TTag>(dev, memPtr, std::move(deleter), extent);
203 template<
typename TDim, concepts::Tag TTag>
204 struct HasAsyncBufSupport<TDim, DevGenericSycl<TTag>> : std::false_type
209 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
210 struct GetOffsets<BufGenericSycl<TElem, TDim, TIdx, TTag>>
212 auto operator()(BufGenericSycl<TElem, TDim, TIdx, TTag>
const&)
const -> Vec<TDim, TIdx>
219 template<concepts::Tag TTag,
typename TElem,
typename TDim,
typename TIdx>
220 struct BufAllocMapped<PlatformGenericSycl<TTag>, TElem, TDim, TIdx>
222 template<
typename TExtent>
225 PlatformGenericSycl<TTag>
const& platform,
226 TExtent
const& extent) -> BufCpu<TElem, TDim, TIdx>
232 auto ctx = platform.syclContext();
233 TElem* memPtr = sycl::malloc_host<TElem>(
static_cast<std::size_t
>(
getExtentProduct(extent)), ctx);
234 auto deleter = [ctx](TElem* ptr) {
sycl::free(ptr, ctx); };
236 return BufCpu<TElem, TDim, TIdx>(host, memPtr, std::move(deleter), extent);
241 template<concepts::Tag TTag>
242 struct HasMappedBufSupport<PlatformGenericSycl<TTag>> :
public std::true_type
247 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
248 struct IdxType<BufGenericSycl<TElem, TDim, TIdx, TTag>>
254 template<
typename TElem,
typename TDim,
typename TIdx, concepts::Tag TTag>
255 struct GetPtrDev<BufCpu<TElem, TDim, TIdx>, DevGenericSycl<TTag>>
257 static auto getPtrDev(BufCpu<TElem, TDim, TIdx>
const& buf, DevGenericSycl<TTag>
const&) -> TElem
const*
262 static auto getPtrDev(BufCpu<TElem, TDim, TIdx>& buf, DevGenericSycl<TTag>
const&) -> TElem*
#define ALPAKA_DEBUG_MINIMAL_LOG_SCOPE
ALPAKA_NO_HOST_ACC_WARNING static constexpr ALPAKA_FN_HOST_ACC auto zeros() -> Vec< TDim, TVal >
Zero value constructor.
The alpaka accelerator library.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtentProduct(T const &object) -> Idx< T >
ALPAKA_FN_HOST auto free(TAlloc const &alloc, T const *const ptr) -> void
Frees the memory identified by the given pointer.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getHeight(TExtent const &extent=TExtent()) -> Idx< TExtent >
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.
ALPAKA_FN_HOST auto getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
ALPAKA_FN_HOST auto allocBuf(TDev const &dev, TExtent const &extent=TExtent())
Allocates memory on the given device.
ALPAKA_FN_HOST auto getDev(T const &t)
ALPAKA_FN_HOST auto getPtrDev(TView const &view, TDev const &dev) -> Elem< TView > const *
Gets the pointer to the view on the given device.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getDepth(TExtent const &extent=TExtent()) -> Idx< TExtent >
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getWidth(TExtent const &extent=TExtent()) -> Idx< TExtent >
ALPAKA_NO_HOST_ACC_WARNING constexpr ALPAKA_FN_HOST_ACC auto getExtentVecEnd(T const &object={}) -> Vec< TDim, Idx< T >>