26#ifdef ALPAKA_USE_MDSPAN
27# include <experimental/mdspan>
35 template<
typename TElem,
typename TDim,
typename TIdx>
39 constexpr auto dim = TIdx{TDim::value};
41 pitchBytes.
back() =
static_cast<TIdx
>(
sizeof(TElem));
43 for(TIdx i = TDim::value - 1; i > 0; i--)
44 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
53 template<
typename TView,
typename TSfinae =
void>
57 template<
typename TView,
typename TDev,
typename TSfinae =
void>
65 template<
typename TIdx,
typename TView,
typename TSfinae =
void>
72 return getPitchBytesDefault(view);
76 static auto getPitchBytesDefault(TView
const& view) -> ViewIdx
78 constexpr auto idx = TIdx::value;
80 if constexpr(idx < viewDim - 1)
82#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
83# pragma GCC diagnostic push
84# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
87#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
88# pragma GCC diagnostic pop
91 else if constexpr(idx == viewDim - 1)
101 template<
typename TView,
typename TSfinae =
void>
106 return alpaka::detail::calculatePitchesFromExtents<Elem<TView>>(
getExtents(view));
113 template<
typename TDim,
typename TDev,
typename TSfinae =
void>
116 template<
typename TDim,
typename TDev,
typename TSfinae =
void>
122 template<
typename TDim,
typename TDevDst,
typename TDevSrc,
typename TSfinae =
void>
126 template<
typename TDev,
typename TSfinae =
void>
130 template<
typename TDev,
typename TSfinae =
void>
138 template<
typename TView>
148 template<
typename TView>
159 template<
typename TView,
typename TDev>
170 template<
typename TView,
typename TDev>
178 template<std::
size_t T
idx,
typename TView>
181#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
182# pragma GCC diagnostic push
183# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
186#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
187# pragma GCC diagnostic pop
198 template<
typename TView>
209 template<
typename TExtent,
typename TViewFwd>
212 using TView = std::remove_reference_t<TViewFwd>;
213 static_assert(!std::is_const_v<TView>,
"The view must not be const!");
216 "The view and the extent are required to have the same dimensionality!");
219 "The view and the extent must have compatible index types!");
222 std::forward<TViewFwd>(view),
227 template<
typename TExtent,
typename TViewFwd,
typename TValue>
230 using TView = std::remove_reference_t<TViewFwd>;
231 static_assert(!std::is_const_v<TView>,
"The view must not be const!");
234 "The view and the extent are required to have the same dimensionality!");
237 "The view and the extent must have compatible index types!");
240 std::forward<TViewFwd>(view),
251 template<
typename TExtent,
typename TViewFwd,
typename TQueue>
252 ALPAKA_FN_HOST auto memset(TQueue& queue, TViewFwd&& view, std::uint8_t
const&
byte, TExtent
const& extent) ->
void
262 template<
typename TViewFwd,
typename TQueue>
268 template<
typename TViewFwd,
typename TValue,
typename TQueue>
274 template<
typename TExtent,
typename TViewFwd,
typename TValue,
typename TQueue>
275 ALPAKA_FN_HOST auto fill(TQueue& queue, TViewFwd&& view, TValue
const& value, TExtent
const& extent) ->
void
285 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd>
288 using TViewDst = std::remove_reference_t<TViewDstFwd>;
295 static_assert(!std::is_const_v<TViewDst>,
"The destination view must not be const!");
296 static_assert(!std::is_const_v<DstElem>,
"The destination view's element type must not be const!");
299 "The source and the destination view must have the same dimensionality!");
302 "The destination view and the extent must have the same dimensionality!");
304 std::is_same_v<DstElem, std::remove_const_t<SrcElem>>,
305 "The source and destination view must have the same element type!");
308 "The destination view and the extent are required to have compatible index types!");
311 "The source view and the extent are required to have compatible index types!");
314 std::forward<TViewDstFwd>(viewDst),
326 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
327 ALPAKA_FN_HOST auto memcpy(TQueue& queue, TViewDstFwd&& viewDst, TViewSrc
const& viewSrc, TExtent
const& extent)
339 template<
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
347 template<
typename TDim,
typename TView>
355 std::string
const& elementSeparator,
356 std::string
const& rowSeparator,
357 std::string
const& rowPrefix,
358 std::string
const& rowSuffix) ->
void
363 auto const lastIdx(extent[TDim::value] - 1u);
364 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
368 reinterpret_cast<Elem<TView> const*
>(
reinterpret_cast<std::uint8_t const*
>(ptr) + i * pitch),
387 template<
typename TView>
395 std::string
const& elementSeparator,
397 std::string
const& rowPrefix,
398 std::string
const& rowSuffix) ->
void
403 for(
auto i(
decltype(lastIdx)(0)); i <= lastIdx; ++i)
411 os << elementSeparator;
423 template<
typename TView>
427 std::string
const& elementSeparator =
", ",
428 std::string
const& rowSeparator =
"\n",
429 std::string
const& rowPrefix =
"[",
430 std::string
const& rowSuffix =
"]") ->
void
444 template<
typename TView>
452 template<
typename TDim,
typename TView>
466 template<
typename TDev,
typename TElem,
typename TExtent>
467 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent)
476 detail::calculatePitchesFromExtents<TElem>(extentVec));
488 template<
typename TDev,
typename TElem,
typename TExtent,
typename TPitch>
489 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent, TPitch pitch)
500 template<
typename TDev,
typename TContainer>
514 template<
typename TDev,
typename TContainer,
typename TExtent>
515 auto createView(TDev
const& dev, TContainer& con, TExtent
const& extent)
517 return createView(dev, std::data(con), extent);
526 template<
typename TView,
typename TExtent,
typename TOffsets>
527 auto createSubView(TView& view, TExtent
const& extent, TOffsets
const& offset = TExtent())
532#ifdef ALPAKA_USE_MDSPAN
533 namespace experimental
536 using std::experimental::default_accessor;
537 using std::experimental::dextents;
538 using std::experimental::extents;
539 using std::experimental::layout_left;
540 using std::experimental::layout_right;
541 using std::experimental::layout_stride;
542 using std::experimental::mdspan;
544 using std::experimental::full_extent;
545 using std::experimental::submdspan;
551 template<
typename ElementType>
552 struct ByteIndexedAccessor
554 using offset_policy = ByteIndexedAccessor;
555 using element_type = ElementType;
556 using reference = ElementType&;
558 using data_handle_type
559 = std::conditional_t<std::is_const_v<ElementType>, std::byte
const*, std::byte*>;
561 constexpr ByteIndexedAccessor() noexcept = default;
563 ALPAKA_FN_HOST_ACC constexpr data_handle_type offset(data_handle_type p,
size_t i) const noexcept
568 ALPAKA_FN_HOST_ACC constexpr reference access(data_handle_type p,
size_t i)
const noexcept
570 assert(i %
alignof(ElementType) == 0);
572# pragma GCC diagnostic push
573# pragma GCC diagnostic ignored "-Wcast-align"
575 return *
reinterpret_cast<ElementType*
>(p + i);
577# pragma GCC diagnostic pop
582 template<
typename TView, std::size_t... Is>
583 ALPAKA_FN_HOST auto makeExtents(TView
const& view, std::index_sequence<Is...>)
586 return std::experimental::dextents<Idx<TView>, Dim<TView>::value>{ex[Is]...};
591 template<
typename TView,
typename TSfinae =
void>
596 constexpr auto dim = Dim<TView>::value;
597 using Element = Elem<TView>;
598 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
599 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
601 layout_stride::mapping<
decltype(extents)> m{extents, strides};
602 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
609 constexpr auto dim = Dim<TView>::value;
610 using Element = Elem<TView>;
611 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
612 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
614 std::reverse(
begin(strides),
end(strides));
615 layout_stride::mapping<
decltype(extents)> m{extents, strides};
616 return mdspan<Element,
decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
624 template<
typename TView>
627 return traits::GetMdSpan<TView>::getMdSpan(view);
633 template<
typename TView>
636 return traits::GetMdSpan<TView>::getMdSpanTransposed(view);
639 template<
typename TElem,
typename TIdx,
typename TDim>
640 using MdSpan = alpaka::experimental::mdspan<
642 alpaka::experimental::dextents<TIdx, TDim::value>,
643 alpaka::experimental::layout_stride,
644 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 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.