27#ifdef ALPAKA_USE_MDSPAN
28# include <experimental/mdspan>
36 template<
typename TElem,
typename TDim,
typename TIdx>
40 constexpr auto dim = TIdx{TDim::value};
42 pitchBytes.
back() =
static_cast<TIdx
>(
sizeof(TElem));
44 for(TIdx i = TDim::value - 1; i > 0; i--)
45 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
50 template<
typename TElem,
typename TDim,
typename TIdx>
56 constexpr auto dim = TIdx{TDim::value};
58 pitchBytes.
back() =
static_cast<TIdx
>(
sizeof(TElem));
62 pitchBytes[TDim::value - 2] = extent.
back() * pitchBytes.back();
64 pitchBytes[TDim::value - 2] =
static_cast<TIdx
>(
65 (
static_cast<std::size_t
>(extent.
back() * pitchBytes.back()) + pitch - 1) / pitch * pitch);
68 for(TIdx i = TDim::value - 2; i > 0; i--)
69 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
79 template<
typename TView,
typename TSfinae =
void>
83 template<
typename TView,
typename TDev,
typename TSfinae =
void>
91 template<
typename TIdx,
typename TView,
typename TSfinae =
void>
98 return getPitchBytesDefault(view);
102 static auto getPitchBytesDefault(TView
const& view) -> ViewIdx
104 constexpr auto idx = TIdx::value;
106 if constexpr(idx < viewDim - 1)
108#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
109# pragma GCC diagnostic push
110# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
113#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
114# pragma GCC diagnostic pop
117 else if constexpr(idx == viewDim - 1)
127 template<
typename TView,
typename TSfinae =
void>
132 return alpaka::detail::calculatePitchesFromExtents<Elem<TView>>(
getExtents(view));
139 template<
typename TDim,
typename TDev,
typename TSfinae =
void>
142 template<
typename TDim,
typename TDev,
typename TSfinae =
void>
148 template<
typename TDim,
typename TDevDst,
typename TDevSrc,
typename TSfinae =
void>
152 template<
typename TDev,
typename TSfinae =
void>
156 template<
typename TDev,
typename TSfinae =
void>
164 template<
typename TView>
174 template<
typename TView>
185 template<
typename TView,
typename TDev>
196 template<
typename TView,
typename TDev>
204 template<std::
size_t T
idx,
typename TView>
207#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
208# pragma GCC diagnostic push
209# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
212#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
213# pragma GCC diagnostic pop
224 template<
typename TView>
235 template<
typename TExtent,
typename TViewFwd>
238 using TView = std::remove_reference_t<TViewFwd>;
239 static_assert(!std::is_const_v<TView>,
"The view must not be const!");
242 "The view and the extent are required to have the same dimensionality!");
245 "The view and the extent must have compatible index types!");
248 std::forward<TViewFwd>(view),
253 template<
typename TExtent,
typename TViewFwd,
typename TValue>
256 using TView = std::remove_reference_t<TViewFwd>;
257 static_assert(!std::is_const_v<TView>,
"The view must not be const!");
260 "The view and the extent are required to have the same dimensionality!");
263 "The view and the extent must have compatible index types!");
266 std::forward<TViewFwd>(view),
277 template<
typename TExtent,
typename TViewFwd,
typename TQueue>
278 ALPAKA_FN_HOST auto memset(TQueue& queue, TViewFwd&& view, std::uint8_t
const&
byte, TExtent
const& extent) ->
void
288 template<
typename TViewFwd,
typename TQueue>
294 template<
typename TViewFwd,
typename TValue,
typename TQueue>
300 template<
typename TExtent,
typename TViewFwd,
typename TValue,
typename TQueue>
301 ALPAKA_FN_HOST auto fill(TQueue& queue, TViewFwd&& view, TValue
const& value, TExtent
const& extent) ->
void
311 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd>
314 using TViewDst = std::remove_reference_t<TViewDstFwd>;
321 static_assert(!std::is_const_v<TViewDst>,
"The destination view must not be const!");
322 static_assert(!std::is_const_v<DstElem>,
"The destination view's element type must not be const!");
325 "The source and the destination view must have the same dimensionality!");
328 "The destination view and the extent must have the same dimensionality!");
330 std::is_same_v<DstElem, std::remove_const_t<SrcElem>>,
331 "The source and destination view must have the same element type!");
334 "The destination view and the extent are required to have compatible index types!");
337 "The source view and the extent are required to have compatible index types!");
340 std::forward<TViewDstFwd>(viewDst),
352 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
353 ALPAKA_FN_HOST auto memcpy(TQueue& queue, TViewDstFwd&& viewDst, TViewSrc
const& viewSrc, TExtent
const& extent)
365 template<
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
373 template<
typename TDim,
typename TView>
381 std::string
const& elementSeparator,
382 std::string
const& rowSeparator,
383 std::string
const& rowPrefix,
384 std::string
const& rowSuffix) ->
void
389 auto const lastIdx(extent[TDim::value] - 1u);
390 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
394 reinterpret_cast<Elem<TView> const*
>(
reinterpret_cast<std::uint8_t const*
>(ptr) + i * pitch),
413 template<
typename TView>
421 std::string
const& elementSeparator,
423 std::string
const& rowPrefix,
424 std::string
const& rowSuffix) ->
void
429 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
437 os << elementSeparator;
449 template<
typename TView>
453 std::string
const& elementSeparator =
", ",
454 std::string
const& rowSeparator =
"\n",
455 std::string
const& rowPrefix =
"[",
456 std::string
const& rowSuffix =
"]") ->
void
470 template<
typename TView>
478 template<
typename TDim,
typename TView>
492 template<
typename TDev,
typename TElem,
typename TExtent>
493 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent)
502 detail::calculatePitchesFromExtents<TElem>(extentVec));
514 template<
typename TDev,
typename TElem,
typename TExtent,
typename TPitch>
515 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent, TPitch pitch)
526 template<
typename TDev,
typename TContainer>
540 template<
typename TDev,
typename TContainer,
typename TExtent>
541 auto createView(TDev
const& dev, TContainer& con, TExtent
const& extent)
543 return createView(dev, std::data(con), extent);
552 template<
typename TView,
typename TExtent,
typename TOffsets>
553 auto createSubView(TView& view, TExtent
const& extent, TOffsets
const& offset = TExtent())
558#ifdef ALPAKA_USE_MDSPAN
559 namespace experimental
562 using std::experimental::default_accessor;
563 using std::experimental::dextents;
564 using std::experimental::extents;
565 using std::experimental::layout_left;
566 using std::experimental::layout_right;
567 using std::experimental::layout_stride;
568 using std::experimental::mdspan;
570 using std::experimental::full_extent;
571 using std::experimental::submdspan;
577 template<
typename ElementType>
578 struct ByteIndexedAccessor
580 using offset_policy = ByteIndexedAccessor;
581 using element_type = ElementType;
582 using reference = ElementType&;
584 using data_handle_type
585 = std::conditional_t<std::is_const_v<ElementType>, std::byte
const*, std::byte*>;
587 constexpr ByteIndexedAccessor() noexcept = default;
589 ALPAKA_FN_HOST_ACC constexpr data_handle_type offset(data_handle_type p,
size_t i) const noexcept
594 ALPAKA_FN_HOST_ACC constexpr reference access(data_handle_type p,
size_t i)
const noexcept
596 assert(i %
alignof(ElementType) == 0);
597 return *
reinterpret_cast<ElementType*
>(__builtin_assume_aligned(p + i,
alignof(ElementType)));
601 template<
typename TView, std::size_t... Is>
602 ALPAKA_FN_HOST auto makeExtents(TView
const& view, std::index_sequence<Is...>)
605 return std::experimental::dextents<Idx<TView>, Dim<TView>::value>{ex[Is]...};
610 template<
typename TView,
typename TSfinae =
void>
615 constexpr auto dim = Dim<TView>::value;
616 using Element = Elem<TView>;
617 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
618 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
620 layout_stride::mapping<
decltype(extents)> m{extents, strides};
621 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
628 constexpr auto dim = Dim<TView>::value;
629 using Element = Elem<TView>;
630 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
631 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
633 std::reverse(
begin(strides),
end(strides));
634 layout_stride::mapping<
decltype(extents)> m{extents, strides};
635 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
643 template<
typename TView>
646 return traits::GetMdSpan<TView>::getMdSpan(view);
652 template<
typename TView>
655 return traits::GetMdSpan<TView>::getMdSpanTransposed(view);
658 template<
typename TElem,
typename TIdx,
typename TDim>
659 using MdSpan = alpaka::experimental::mdspan<
661 alpaka::experimental::dextents<TIdx, TDim::value>,
662 alpaka::experimental::layout_stride,
663 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.