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>
423 template<
typename TDim,
typename TView>
431 std::string
const& elementSeparator,
432 std::string
const& rowSeparator,
433 std::string
const& rowPrefix,
434 std::string
const& rowSuffix) ->
void
439 auto const lastIdx(extent[TDim::value] - 1u);
440 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
444 reinterpret_cast<Elem<TView> const*
>(
reinterpret_cast<std::uint8_t const*
>(ptr) + i * pitch),
463 template<
typename TView>
471 std::string
const& elementSeparator,
473 std::string
const& rowPrefix,
474 std::string
const& rowSuffix) ->
void
479 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
487 os << elementSeparator;
499 template<
typename TView>
503 std::string
const& elementSeparator =
", ",
504 std::string
const& rowSeparator =
"\n",
505 std::string
const& rowPrefix =
"[",
506 std::string
const& rowSuffix =
"]") ->
void
520 template<
typename TView>
528 template<
typename TDim,
typename TView>
542 template<
typename TDev,
typename TElem,
typename TExtent>
543 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent)
552 detail::calculatePitchesFromExtents<TElem>(extentVec));
564 template<
typename TDev,
typename TElem,
typename TExtent,
typename TPitch>
565 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent, TPitch pitch)
576 template<
typename TDev,
typename TContainer>
590 template<
typename TDev,
typename TContainer,
typename TExtent>
591 auto createView(TDev
const& dev, TContainer& con, TExtent
const& extent)
593 return createView(dev, std::data(con), extent);
602 template<
typename TView,
typename TExtent,
typename TOffsets>
603 auto createSubView(TView& view, TExtent
const& extent, TOffsets
const& offset = TExtent())
608#ifdef ALPAKA_USE_MDSPAN
609 namespace experimental
615 template<
typename ElementType>
616 struct ByteIndexedAccessor
618 using offset_policy = ByteIndexedAccessor;
619 using element_type = ElementType;
620 using reference = ElementType&;
622 using data_handle_type
623 = std::conditional_t<std::is_const_v<ElementType>, std::byte
const*, std::byte*>;
625 constexpr ByteIndexedAccessor() noexcept = default;
627 ALPAKA_FN_HOST_ACC constexpr data_handle_type offset(data_handle_type p,
size_t i) const noexcept
632 ALPAKA_FN_HOST_ACC constexpr reference access(data_handle_type p,
size_t i)
const noexcept
634 assert(i %
alignof(ElementType) == 0);
635 return *
reinterpret_cast<ElementType*
>(__builtin_assume_aligned(p + i,
alignof(ElementType)));
639 template<
typename TView, std::size_t... Is>
640 ALPAKA_FN_HOST auto makeExtents(TView
const& view, std::index_sequence<Is...>)
643 return dextents<Idx<TView>, Dim<TView>::value>{ex[Is]...};
648 template<
typename TView,
typename TSfinae =
void>
653 constexpr auto dim = Dim<TView>::value;
654 using Element = Elem<TView>;
655 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
656 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
658 layout_stride::mapping<
decltype(extents)> m{extents, strides};
659 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
666 constexpr auto dim = Dim<TView>::value;
667 using Element = Elem<TView>;
668 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
669 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
671 std::reverse(
begin(strides),
end(strides));
672 layout_stride::mapping<
decltype(extents)> m{extents, strides};
673 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
681 template<
typename TView>
684 return traits::GetMdSpan<TView>::getMdSpan(view);
690 template<
typename TView>
693 return traits::GetMdSpan<TView>::getMdSpanTransposed(view);
696 template<
typename TElem,
typename TIdx,
typename TDim>
697 using MdSpan = alpaka::experimental::mdspan<
699 alpaka::experimental::dextents<TIdx, TDim::value>,
700 alpaka::experimental::layout_stride,
701 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
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 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.