28#ifdef ALPAKA_USE_MDSPAN
29# ifdef ALPAKA_HAS_STD_MDSPAN
34namespace alpaka::experimental
38 using ::std::default_accessor;
39 using ::std::dextents;
41 using ::std::layout_left;
42 using ::std::layout_right;
43 using ::std::layout_stride;
45# ifdef __cpp_lib_submdspan
48 using ::std::full_extent;
49 using ::std::submdspan;
55# ifdef ALPAKA_ACC_SYCL_ENABLED
57# pragma push_macro("printf")
63# if defined(ALPAKA_ACC_SYCL_ENABLED) && (defined(ALPAKA_SYCL_ONEAPI_FPGA) || defined(ALPAKA_SYCL_TARGET_FPGA))
68# include <experimental/__p0009_bits/config.hpp>
69# undef MDSPAN_IMPL_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS
70# undef MDSPAN_IMPL_NO_UNIQUE_ADDRESS
71# define MDSPAN_IMPL_NO_UNIQUE_ADDRESS
75# define MDSPAN_IMPL_STANDARD_NAMESPACE alpaka::experimental
76# include <experimental/mdspan>
78# ifdef ALPAKA_ACC_SYCL_ENABLED
80# pragma pop_macro("printf")
90 template<
typename TElem,
typename TDim,
typename TIdx>
94 constexpr auto dim = TIdx{TDim::value};
96 pitchBytes.
back() =
static_cast<TIdx
>(
sizeof(TElem));
98 for(TIdx i = TDim::value - 1; i > 0; i--)
99 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
104 template<
typename TElem,
typename TDim,
typename TIdx>
110 constexpr auto dim = TIdx{TDim::value};
111 if constexpr(dim > 0)
112 pitchBytes.
back() =
static_cast<TIdx
>(
sizeof(TElem));
113 if constexpr(dim > 1)
116 pitchBytes[TDim::value - 2] = extent.
back() * pitchBytes.back();
118 pitchBytes[TDim::value - 2] =
static_cast<TIdx
>(
119 (
static_cast<std::size_t
>(extent.
back() * pitchBytes.back()) + pitch - 1) / pitch * pitch);
121 if constexpr(dim > 2)
122 for(TIdx i = TDim::value - 2; i > 0; i--)
123 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
133 template<
typename TView,
typename TSfinae =
void>
137 template<
typename TView,
typename TDev,
typename TSfinae =
void>
145 template<
typename TIdx,
typename TView,
typename TSfinae =
void>
152 return getPitchBytesDefault(view);
156 static auto getPitchBytesDefault(TView
const& view) -> ViewIdx
158 constexpr auto idx = TIdx::value;
160 if constexpr(idx < viewDim - 1)
162#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
163# pragma GCC diagnostic push
164# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
167#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
168# pragma GCC diagnostic pop
171 else if constexpr(idx == viewDim - 1)
181 template<
typename TView,
typename TSfinae =
void>
186 return alpaka::detail::calculatePitchesFromExtents<Elem<TView>>(
getExtents(view));
193 template<
typename TDim,
typename TDev,
typename TSfinae =
void>
196 template<
typename TDim,
typename TDev,
typename TSfinae =
void>
202 template<
typename TDim,
typename TDevDst,
typename TDevSrc,
typename TSfinae =
void>
206 template<
typename TDev,
typename TSfinae =
void>
210 template<
typename TDev,
typename TSfinae =
void>
218 template<
typename TView>
228 template<
typename TView>
239 template<
typename TView,
typename TDev>
250 template<
typename TView,
typename TDev>
258 template<std::
size_t T
idx,
typename TView>
261#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
262# pragma GCC diagnostic push
263# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
266#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
267# pragma GCC diagnostic pop
278 template<
typename TView>
289 template<
typename TExtent,
typename TViewFwd>
292 using TView = std::remove_reference_t<TViewFwd>;
293 static_assert(!std::is_const_v<TView>,
"The view must not be const!");
296 "The view and the extent are required to have the same dimensionality!");
298 assert((extent <=
getExtents(view)).all() &&
"The memset extent must not be larger than the view's extent!");
301 std::forward<TViewFwd>(view),
306 template<
typename TExtent,
typename TViewFwd,
typename TValue>
309 using TView = std::remove_reference_t<TViewFwd>;
310 static_assert(!std::is_const_v<TView>,
"The view must not be const!");
313 "The view and the extent are required to have the same dimensionality!");
315 assert((extent <=
getExtents(view)).all() &&
"The fill extent must not be larger than the view's extent!");
318 std::forward<TViewFwd>(view),
329 template<
typename TExtent,
typename TViewFwd,
typename TQueue>
330 ALPAKA_FN_HOST auto memset(TQueue& queue, TViewFwd&& view, std::uint8_t
const&
byte, TExtent
const& extent) ->
void
340 template<
typename TViewFwd,
typename TQueue>
346 template<
typename TViewFwd,
typename TValue,
typename TQueue>
352 template<
typename TExtent,
typename TViewFwd,
typename TValue,
typename TQueue>
353 ALPAKA_FN_HOST auto fill(TQueue& queue, TViewFwd&& view, TValue
const& value, TExtent
const& extent) ->
void
363 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd>
366 using TViewDst = std::remove_reference_t<TViewDstFwd>;
370 static_assert(!std::is_const_v<TViewDst>,
"The destination view must not be const!");
371 static_assert(!std::is_const_v<DstElem>,
"The destination view's element type must not be const!");
374 "The source and the destination view must have the same dimensionality!");
377 "The destination view and the extent must have the same dimensionality!");
379 std::is_same_v<DstElem, std::remove_const_t<SrcElem>>,
380 "The source and destination view must have the same element type!");
384 &&
"The memcpy extent must not be larger than the source view's extent!");
387 &&
"The memcpy extent must not be larger than the destination view's extent!");
390 std::forward<TViewDstFwd>(viewDst),
402 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
403 ALPAKA_FN_HOST auto memcpy(TQueue& queue, TViewDstFwd&& viewDst, TViewSrc
const& viewSrc, TExtent
const& extent)
415 template<
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
429 template<
typename TDim,
typename TView>
437 std::string
const& elementSeparator,
438 std::string
const& rowSeparator,
439 std::string
const& rowPrefix,
440 std::string
const& rowSuffix) ->
void
445 auto const lastIdx(extent[TDim::value] - 1u);
446 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
450 reinterpret_cast<Elem<TView> const*
>(
reinterpret_cast<std::uint8_t const*
>(ptr) + i * pitch),
469 template<
typename TView>
477 std::string
const& elementSeparator,
479 std::string
const& rowPrefix,
480 std::string
const& rowSuffix) ->
void
485 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
493 os << elementSeparator;
501 template<
typename TDeviceProv
ider>
504 if constexpr(alpaka::isDevice<TDeviceProvider>)
519 template<
typename TView>
523 std::string
const& elementSeparator =
", ",
524 std::string
const& rowSeparator =
"\n",
525 std::string
const& rowPrefix =
"[",
526 std::string
const& rowSuffix =
"]") ->
void
540 template<
typename TView>
548 template<
typename TDim,
typename TView>
562 template<concepts::DeviceProv
ider TDev,
typename TElem,
typename TExtent>
563 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent)
573 detail::calculatePitchesFromExtents<TElem>(extentVec));
585 template<concepts::DeviceProv
ider TDev,
typename TElem,
typename TExtent,
typename TPitch>
586 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent, TPitch pitch)
598 template<concepts::DeviceProv
ider TDev,
typename TContainer>
613 template<concepts::DeviceProv
ider TDev,
typename TContainer,
typename TExtent>
614 auto createView(TDev
const& dev, TContainer& con, TExtent
const& extent)
617 return createView(device, std::data(con), extent);
626 template<
typename TView,
typename TExtent,
typename TOffsets>
627 auto createSubView(TView& view, TExtent
const& extent, TOffsets
const& offset = TExtent())
632#ifdef ALPAKA_USE_MDSPAN
633 namespace experimental
639 template<
typename ElementType>
640 struct ByteIndexedAccessor
642 using offset_policy = ByteIndexedAccessor;
643 using element_type = ElementType;
644 using reference = ElementType&;
646 using data_handle_type
647 = std::conditional_t<std::is_const_v<ElementType>, std::byte
const*, std::byte*>;
649 constexpr ByteIndexedAccessor() noexcept = default;
651 ALPAKA_FN_HOST_ACC constexpr data_handle_type offset(data_handle_type p,
size_t i) const noexcept
656 ALPAKA_FN_HOST_ACC constexpr reference access(data_handle_type p,
size_t i)
const noexcept
658 assert(i %
alignof(ElementType) == 0);
659 return *
reinterpret_cast<ElementType*
>(__builtin_assume_aligned(p + i,
alignof(ElementType)));
663 template<
typename TView, std::size_t... Is>
664 ALPAKA_FN_HOST auto makeExtents(TView
const& view, std::index_sequence<Is...>)
667 return dextents<Idx<TView>, Dim<TView>::value>{ex[Is]...};
672 template<
typename TView,
typename TSfinae =
void>
677 constexpr auto dim = Dim<TView>::value;
678 using Element = Elem<TView>;
679 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
680 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
682 layout_stride::mapping<
decltype(extents)> m{extents, strides};
683 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
690 constexpr auto dim = Dim<TView>::value;
691 using Element = Elem<TView>;
692 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
693 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
695 std::reverse(
begin(strides),
end(strides));
696 layout_stride::mapping<
decltype(extents)> m{extents, strides};
697 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
705 template<
typename TView>
708 return traits::GetMdSpan<TView>::getMdSpan(view);
714 template<
typename TView>
717 return traits::GetMdSpan<TView>::getMdSpanTransposed(view);
720 template<
typename TElem,
typename TIdx,
typename TDim>
721 using MdSpan = alpaka::experimental::mdspan<
723 alpaka::experimental::dextents<TIdx, TDim::value>,
724 alpaka::experimental::layout_stride,
725 alpaka::experimental::traits::detail::ByteIndexedAccessor<TElem>>;
#define ALPAKA_UNREACHABLE(...)
Before CUDA 11.5 nvcc is unable to correctly identify return statements in 'if constexpr' branches....
ALPAKA_FN_HOST_ACC constexpr auto back() -> TVal &
#define ALPAKA_FN_HOST_ACC
auto getDeviceFromProvider(TDeviceProvider const &provider)
ALPAKA_FN_HOST_ACC constexpr auto calculatePitchesFromExtents(Vec< TDim, TIdx > const &extent)
Calculate the pitches purely from the extents.
ALPAKA_FN_HOST_ACC constexpr auto calculatePitchesFromExtentsAndPitch(Vec< TDim, TIdx > const &extent, std::size_t pitch)
Calculate the pitches from the extents and the one-dimensional pitch.
ALPAKA_FN_HOST auto end(TView &view) -> Iterator< TView >
ALPAKA_FN_HOST auto begin(TView &view) -> Iterator< TView >
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
ALPAKA_FN_HOST auto memcpy(TQueue &queue, alpaka::detail::DevGlobalImplGeneric< TTag, TTypeDst > &viewDst, TViewSrc const &viewSrc) -> void
ALPAKA_FN_HOST auto getPitchBytesVecEnd(TView const &view=TView()) -> Vec< TDim, Idx< TView > >
ALPAKA_FN_HOST_ACC constexpr auto toArray(Vec< TDim, TVal > const &v) -> std::array< TVal, TDim::value >
Converts a Vec to a std::array.
ALPAKA_FN_HOST auto getPitchesInBytes(TView const &view) -> Vec< Dim< TView >, Idx< TView > >
auto getPitchBytesVec(TView const &view) -> Vec< Dim< TView >, Idx< TView > >
ALPAKA_FN_HOST auto createTaskMemcpy(TViewDstFwd &&viewDst, TViewSrc const &viewSrc, TExtent const &extent)
Creates a memory copy task.
typename trait::DevType< T >::type Dev
The device type trait alias template to remove the ::type.
auto createSubView(TView &view, TExtent const &extent, TOffsets const &offset=TExtent())
Creates a sub view to an existing view.
ALPAKA_FN_HOST auto createTaskFill(TViewFwd &&view, TValue const &value, TExtent const &extent)
ALPAKA_FN_HOST auto print(TView const &view, std::ostream &os, std::string const &elementSeparator=", ", std::string const &rowSeparator="\n", std::string const &rowPrefix="[", std::string const &rowSuffix="]") -> void
Prints the content of the view to the given queue.
ALPAKA_FN_HOST auto getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
std::remove_volatile_t< typename trait::ElemType< TView >::type > Elem
The element type trait alias template to remove the ::type.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtents(T const &object) -> Vec< Dim< T >, Idx< T > >
ALPAKA_FN_HOST auto memset(TQueue &queue, TViewFwd &&view, std::uint8_t const &byte, TExtent const &extent) -> void
Sets the bytes of the memory of view, described by extent, to the given value.
ALPAKA_FN_HOST auto fill(TQueue &queue, TViewFwd &&view, TValue const &value) -> void
ALPAKA_FN_HOST auto getDev(T const &t)
ALPAKA_FN_HOST auto enqueue(TQueue &queue, TTask &&task) -> void
Queues the given task in the given queue.
ALPAKA_FN_HOST auto getPtrDev(TView const &view, TDev const &dev) -> Elem< TView > const *
Gets the pointer to the view on the given device.
std::integral_constant< std::size_t, N > DimInt
ALPAKA_FN_HOST auto createTaskMemset(TViewFwd &&view, std::uint8_t const &byte, TExtent const &extent)
Create a memory set task.
ALPAKA_FN_HOST auto getPitchBytes(TView const &view) -> Idx< TView >
auto createView(TDev const &dev, TElem *pMem, TExtent const &extent)
Creates a view to a device pointer.
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
static ALPAKA_FN_HOST auto print(TView const &, Elem< TView > const *const ptr, Vec< Dim< TView >, Idx< TView > > const &extent, std::ostream &os, std::string const &elementSeparator, std::string const &, std::string const &rowPrefix, std::string const &rowSuffix) -> void
static ALPAKA_FN_HOST auto print(TView const &view, Elem< TView > const *const ptr, Vec< Dim< TView >, Idx< TView > > const &extent, std::ostream &os, std::string const &elementSeparator, std::string const &rowSeparator, std::string const &rowPrefix, std::string const &rowSuffix) -> void
The sub view creation trait.
The memory copy task trait.
The memory set task trait.
The device memory view creation trait.
The pitch in bytes. This is the distance in bytes in the linear memory between two consecutive elemen...
static ALPAKA_FN_HOST auto getPitchBytes(TView const &view) -> ViewIdx
Customization point for getPitchesInBytes. The default implementation uses the extent to calculate th...
ALPAKA_FN_HOST_ACC constexpr auto operator()(TView const &view) const
The pointer on device get trait.
The native pointer get trait.