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];
100#if ALPAKA_COMP_CLANG >= ALPAKA_VERSION_NUMBER(21, 0, 0)
101# pragma clang diagnostic push
102# pragma clang diagnostic ignored "-Wnrvo"
105#if ALPAKA_COMP_CLANG >= ALPAKA_VERSION_NUMBER(21, 0, 0)
106# pragma clang diagnostic pop
111 template<
typename TElem,
typename TDim,
typename TIdx>
117 constexpr auto dim = TIdx{TDim::value};
118 if constexpr(dim > 0)
119 pitchBytes.
back() =
static_cast<TIdx
>(
sizeof(TElem));
120 if constexpr(dim > 1)
123 pitchBytes[TDim::value - 2] = extent.
back() * pitchBytes.back();
125 pitchBytes[TDim::value - 2] =
static_cast<TIdx
>(
126 (
static_cast<std::size_t
>(extent.
back() * pitchBytes.back()) + pitch - 1) / pitch * pitch);
128 if constexpr(dim > 2)
129 for(TIdx i = TDim::value - 2; i > 0; i--)
130 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
131#if ALPAKA_COMP_CLANG >= ALPAKA_VERSION_NUMBER(21, 0, 0)
132# pragma clang diagnostic push
133# pragma clang diagnostic ignored "-Wnrvo"
136#if ALPAKA_COMP_CLANG >= ALPAKA_VERSION_NUMBER(21, 0, 0)
137# pragma clang diagnostic pop
147 template<
typename TView,
typename TSfinae =
void>
151 template<
typename TView,
typename TDev,
typename TSfinae =
void>
159 template<
typename TIdx,
typename TView,
typename TSfinae =
void>
166 return getPitchBytesDefault(view);
170 static auto getPitchBytesDefault(TView
const& view) -> ViewIdx
172 constexpr auto idx = TIdx::value;
174 if constexpr(idx < viewDim - 1)
176#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
177# pragma GCC diagnostic push
178# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
181#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
182# pragma GCC diagnostic pop
185 else if constexpr(idx == viewDim - 1)
195 template<
typename TView,
typename TSfinae =
void>
200 return alpaka::detail::calculatePitchesFromExtents<Elem<TView>>(
getExtents(view));
207 template<
typename TDim,
typename TDev,
typename TSfinae =
void>
210 template<
typename TDim,
typename TDev,
typename TSfinae =
void>
216 template<
typename TDim,
typename TDevDst,
typename TDevSrc,
typename TSfinae =
void>
220 template<
typename TDev,
typename TSfinae =
void>
224 template<
typename TDev,
typename TSfinae =
void>
232 template<
typename TView>
242 template<
typename TView>
253 template<
typename TView,
typename TDev>
264 template<
typename TView,
typename TDev>
272 template<std::
size_t T
idx,
typename TView>
275#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
276# pragma GCC diagnostic push
277# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
280#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
281# pragma GCC diagnostic pop
292 template<
typename TView>
303 template<
typename TExtent,
typename TViewFwd>
306 using TView = std::remove_reference_t<TViewFwd>;
307 static_assert(!std::is_const_v<TView>,
"The view must not be const!");
310 "The view and the extent are required to have the same dimensionality!");
312 assert((extent <=
getExtents(view)).all() &&
"The memset extent must not be larger than the view's extent!");
315 std::forward<TViewFwd>(view),
320 template<
typename TExtent,
typename TViewFwd,
typename TValue>
323 using TView = std::remove_reference_t<TViewFwd>;
324 static_assert(!std::is_const_v<TView>,
"The view must not be const!");
327 "The view and the extent are required to have the same dimensionality!");
329 assert((extent <=
getExtents(view)).all() &&
"The fill extent must not be larger than the view's extent!");
332 std::forward<TViewFwd>(view),
343 template<
typename TExtent,
typename TViewFwd,
typename TQueue>
344 ALPAKA_FN_HOST auto memset(TQueue& queue, TViewFwd&& view, std::uint8_t
const&
byte, TExtent
const& extent) ->
void
354 template<
typename TViewFwd,
typename TQueue>
360 template<
typename TViewFwd,
typename TValue,
typename TQueue>
366 template<
typename TExtent,
typename TViewFwd,
typename TValue,
typename TQueue>
367 ALPAKA_FN_HOST auto fill(TQueue& queue, TViewFwd&& view, TValue
const& value, TExtent
const& extent) ->
void
377 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd>
380 using TViewDst = std::remove_reference_t<TViewDstFwd>;
384 static_assert(!std::is_const_v<TViewDst>,
"The destination view must not be const!");
385 static_assert(!std::is_const_v<DstElem>,
"The destination view's element type must not be const!");
388 "The source and the destination view must have the same dimensionality!");
391 "The destination view and the extent must have the same dimensionality!");
393 std::is_same_v<DstElem, std::remove_const_t<SrcElem>>,
394 "The source and destination view must have the same element type!");
398 &&
"The memcpy extent must not be larger than the source view's extent!");
401 &&
"The memcpy extent must not be larger than the destination view's extent!");
404 std::forward<TViewDstFwd>(viewDst),
416 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
417 ALPAKA_FN_HOST auto memcpy(TQueue& queue, TViewDstFwd&& viewDst, TViewSrc
const& viewSrc, TExtent
const& extent)
429 template<
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
443 template<
typename TDim,
typename TView>
451 std::string
const& elementSeparator,
452 std::string
const& rowSeparator,
453 std::string
const& rowPrefix,
454 std::string
const& rowSuffix) ->
void
459 auto const lastIdx(extent[TDim::value] - 1u);
460 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
464 reinterpret_cast<Elem<TView> const*
>(
reinterpret_cast<std::uint8_t const*
>(ptr) + i * pitch),
483 template<
typename TView>
491 std::string
const& elementSeparator,
493 std::string
const& rowPrefix,
494 std::string
const& rowSuffix) ->
void
499 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
507 os << elementSeparator;
515 template<
typename TDeviceProv
ider>
518 if constexpr(alpaka::isDevice<TDeviceProvider>)
533 template<
typename TView>
537 std::string
const& elementSeparator =
", ",
538 std::string
const& rowSeparator =
"\n",
539 std::string
const& rowPrefix =
"[",
540 std::string
const& rowSuffix =
"]") ->
void
554 template<
typename TView>
562 template<
typename TDim,
typename TView>
576 template<concepts::DeviceProv
ider TDev,
typename TElem,
typename TExtent>
577 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent)
587 detail::calculatePitchesFromExtents<TElem>(extentVec));
599 template<concepts::DeviceProv
ider TDev,
typename TElem,
typename TExtent,
typename TPitch>
600 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent, TPitch pitch)
612 template<concepts::DeviceProv
ider TDev,
typename TContainer>
627 template<concepts::DeviceProv
ider TDev,
typename TContainer,
typename TExtent>
628 auto createView(TDev
const& dev, TContainer& con, TExtent
const& extent)
631 return createView(device, std::data(con), extent);
640 template<
typename TView,
typename TExtent,
typename TOffsets>
641 auto createSubView(TView& view, TExtent
const& extent, TOffsets
const& offset = TExtent())
646#ifdef ALPAKA_USE_MDSPAN
647 namespace experimental
653 template<
typename ElementType>
654 struct ByteIndexedAccessor
656 using offset_policy = ByteIndexedAccessor;
657 using element_type = ElementType;
658 using reference = ElementType&;
660 using data_handle_type
661 = std::conditional_t<std::is_const_v<ElementType>, std::byte
const*, std::byte*>;
663 constexpr ByteIndexedAccessor() noexcept = default;
665 ALPAKA_FN_HOST_ACC constexpr data_handle_type offset(data_handle_type p,
size_t i) const noexcept
670 ALPAKA_FN_HOST_ACC constexpr reference access(data_handle_type p,
size_t i)
const noexcept
672 assert(i %
alignof(ElementType) == 0);
673 return *
reinterpret_cast<ElementType*
>(__builtin_assume_aligned(p + i,
alignof(ElementType)));
677 template<
typename TView, std::size_t... Is>
678 ALPAKA_FN_HOST auto makeExtents(TView
const& view, std::index_sequence<Is...>)
681 return dextents<Idx<TView>, Dim<TView>::value>{ex[Is]...};
686 template<
typename TView,
typename TSfinae =
void>
691 constexpr auto dim = Dim<TView>::value;
692 using Element = Elem<TView>;
693 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
694 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
696 layout_stride::mapping<
decltype(extents)> m{extents, strides};
697 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
704 constexpr auto dim = Dim<TView>::value;
705 using Element = Elem<TView>;
706 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
707 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
709 std::reverse(
begin(strides),
end(strides));
710 layout_stride::mapping<
decltype(extents)> m{extents, strides};
711 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
719 template<
typename TView>
722 return traits::GetMdSpan<TView>::getMdSpan(view);
728 template<
typename TView>
731 return traits::GetMdSpan<TView>::getMdSpanTransposed(view);
734 template<
typename TElem,
typename TIdx,
typename TDim>
735 using MdSpan = alpaka::experimental::mdspan<
737 alpaka::experimental::dextents<TIdx, TDim::value>,
738 alpaka::experimental::layout_stride,
739 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.