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!");
244 assert((extent <=
getExtents(view)).all() &&
"The memset extent must not be larger than the view's extent!");
247 std::forward<TViewFwd>(view),
252 template<
typename TExtent,
typename TViewFwd,
typename TValue>
255 using TView = std::remove_reference_t<TViewFwd>;
256 static_assert(!std::is_const_v<TView>,
"The view must not be const!");
259 "The view and the extent are required to have the same dimensionality!");
261 assert((extent <=
getExtents(view)).all() &&
"The fill extent must not be larger than the view's extent!");
264 std::forward<TViewFwd>(view),
275 template<
typename TExtent,
typename TViewFwd,
typename TQueue>
276 ALPAKA_FN_HOST auto memset(TQueue& queue, TViewFwd&& view, std::uint8_t
const&
byte, TExtent
const& extent) ->
void
286 template<
typename TViewFwd,
typename TQueue>
292 template<
typename TViewFwd,
typename TValue,
typename TQueue>
298 template<
typename TExtent,
typename TViewFwd,
typename TValue,
typename TQueue>
299 ALPAKA_FN_HOST auto fill(TQueue& queue, TViewFwd&& view, TValue
const& value, TExtent
const& extent) ->
void
309 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd>
312 using TViewDst = std::remove_reference_t<TViewDstFwd>;
316 static_assert(!std::is_const_v<TViewDst>,
"The destination view must not be const!");
317 static_assert(!std::is_const_v<DstElem>,
"The destination view's element type must not be const!");
320 "The source and the destination view must have the same dimensionality!");
323 "The destination view and the extent must have the same dimensionality!");
325 std::is_same_v<DstElem, std::remove_const_t<SrcElem>>,
326 "The source and destination view must have the same element type!");
330 &&
"The memcpy extent must not be larger than the source view's extent!");
333 &&
"The memcpy extent must not be larger than the destination view's extent!");
336 std::forward<TViewDstFwd>(viewDst),
348 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
349 ALPAKA_FN_HOST auto memcpy(TQueue& queue, TViewDstFwd&& viewDst, TViewSrc
const& viewSrc, TExtent
const& extent)
361 template<
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
369 template<
typename TDim,
typename TView>
377 std::string
const& elementSeparator,
378 std::string
const& rowSeparator,
379 std::string
const& rowPrefix,
380 std::string
const& rowSuffix) ->
void
385 auto const lastIdx(extent[TDim::value] - 1u);
386 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
390 reinterpret_cast<Elem<TView> const*
>(
reinterpret_cast<std::uint8_t const*
>(ptr) + i * pitch),
409 template<
typename TView>
417 std::string
const& elementSeparator,
419 std::string
const& rowPrefix,
420 std::string
const& rowSuffix) ->
void
425 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
433 os << elementSeparator;
445 template<
typename TView>
449 std::string
const& elementSeparator =
", ",
450 std::string
const& rowSeparator =
"\n",
451 std::string
const& rowPrefix =
"[",
452 std::string
const& rowSuffix =
"]") ->
void
466 template<
typename TView>
474 template<
typename TDim,
typename TView>
488 template<
typename TDev,
typename TElem,
typename TExtent>
489 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent)
498 detail::calculatePitchesFromExtents<TElem>(extentVec));
510 template<
typename TDev,
typename TElem,
typename TExtent,
typename TPitch>
511 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent, TPitch pitch)
522 template<
typename TDev,
typename TContainer>
536 template<
typename TDev,
typename TContainer,
typename TExtent>
537 auto createView(TDev
const& dev, TContainer& con, TExtent
const& extent)
539 return createView(dev, std::data(con), extent);
548 template<
typename TView,
typename TExtent,
typename TOffsets>
549 auto createSubView(TView& view, TExtent
const& extent, TOffsets
const& offset = TExtent())
554#ifdef ALPAKA_USE_MDSPAN
555 namespace experimental
558 using std::experimental::default_accessor;
559 using std::experimental::dextents;
560 using std::experimental::extents;
561 using std::experimental::layout_left;
562 using std::experimental::layout_right;
563 using std::experimental::layout_stride;
564 using std::experimental::mdspan;
566 using std::experimental::full_extent;
567 using std::experimental::submdspan;
573 template<
typename ElementType>
574 struct ByteIndexedAccessor
576 using offset_policy = ByteIndexedAccessor;
577 using element_type = ElementType;
578 using reference = ElementType&;
580 using data_handle_type
581 = std::conditional_t<std::is_const_v<ElementType>, std::byte
const*, std::byte*>;
583 constexpr ByteIndexedAccessor() noexcept = default;
585 ALPAKA_FN_HOST_ACC constexpr data_handle_type offset(data_handle_type p,
size_t i) const noexcept
590 ALPAKA_FN_HOST_ACC constexpr reference access(data_handle_type p,
size_t i)
const noexcept
592 assert(i %
alignof(ElementType) == 0);
593 return *
reinterpret_cast<ElementType*
>(__builtin_assume_aligned(p + i,
alignof(ElementType)));
597 template<
typename TView, std::size_t... Is>
598 ALPAKA_FN_HOST auto makeExtents(TView
const& view, std::index_sequence<Is...>)
601 return std::experimental::dextents<Idx<TView>, Dim<TView>::value>{ex[Is]...};
606 template<
typename TView,
typename TSfinae =
void>
611 constexpr auto dim = Dim<TView>::value;
612 using Element = Elem<TView>;
613 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
614 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
616 layout_stride::mapping<
decltype(extents)> m{extents, strides};
617 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
624 constexpr auto dim = Dim<TView>::value;
625 using Element = Elem<TView>;
626 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
627 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
629 std::reverse(
begin(strides),
end(strides));
630 layout_stride::mapping<
decltype(extents)> m{extents, strides};
631 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
639 template<
typename TView>
642 return traits::GetMdSpan<TView>::getMdSpan(view);
648 template<
typename TView>
651 return traits::GetMdSpan<TView>::getMdSpanTransposed(view);
654 template<
typename TElem,
typename TIdx,
typename TDim>
655 using MdSpan = alpaka::experimental::mdspan<
657 alpaka::experimental::dextents<TIdx, TDim::value>,
658 alpaka::experimental::layout_stride,
659 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.