24 #include <type_traits>
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 BOOST_COMP_CLANG || BOOST_COMP_GNUC
83 # pragma GCC diagnostic push
84 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
87 #if BOOST_COMP_CLANG || BOOST_COMP_GNUC
88 # pragma GCC diagnostic pop
91 else if constexpr(idx == viewDim - 1)
92 return
getExtents(view)[viewDim - 1] * static_cast<ViewIdx>(sizeof(
Elem<TView>));
94 return static_cast<ViewIdx>(sizeof(
Elem<TView>));
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>
119 template<
typename TDim,
typename TDevDst,
typename TDevSrc,
typename TSfinae =
void>
123 template<
typename TDev,
typename TSfinae =
void>
127 template<
typename TDev,
typename TSfinae =
void>
135 template<
typename TView>
145 template<
typename TView>
156 template<
typename TView,
typename TDev>
167 template<
typename TView,
typename TDev>
175 template<std::
size_t T
idx,
typename TView>
178 #if BOOST_COMP_CLANG || BOOST_COMP_GNUC
179 # pragma GCC diagnostic push
180 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
183 #if BOOST_COMP_CLANG || BOOST_COMP_GNUC
184 # pragma GCC diagnostic pop
195 template<
typename TView>
206 template<
typename TExtent,
typename TViewFwd>
209 using TView = std::remove_reference_t<TViewFwd>;
210 static_assert(!std::is_const_v<TView>,
"The view must not be const!");
213 "The view and the extent are required to have the same dimensionality!");
216 "The view and the extent must have compatible index types!");
219 std::forward<TViewFwd>(view),
230 template<
typename TExtent,
typename TViewFwd,
typename TQueue>
231 ALPAKA_FN_HOST auto memset(TQueue& queue, TViewFwd&& view, std::uint8_t
const&
byte, TExtent
const& extent) ->
void
241 template<
typename TViewFwd,
typename TQueue>
252 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd>
255 using TViewDst = std::remove_reference_t<TViewDstFwd>;
262 static_assert(!std::is_const_v<TViewDst>,
"The destination view must not be const!");
263 static_assert(!std::is_const_v<DstElem>,
"The destination view's element type must not be const!");
266 "The source and the destination view must have the same dimensionality!");
269 "The destination view and the extent must have the same dimensionality!");
271 std::is_same_v<DstElem, std::remove_const_t<SrcElem>>,
272 "The source and destination view must have the same element type!");
275 "The destination view and the extent are required to have compatible index types!");
278 "The source view and the extent are required to have compatible index types!");
281 std::forward<TViewDstFwd>(viewDst),
293 template<
typename TExtent,
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
294 ALPAKA_FN_HOST auto memcpy(TQueue& queue, TViewDstFwd&& viewDst, TViewSrc
const& viewSrc, TExtent
const& extent)
306 template<
typename TViewSrc,
typename TViewDstFwd,
typename TQueue>
314 template<
typename TDim,
typename TView>
322 std::string
const& elementSeparator,
323 std::string
const& rowSeparator,
324 std::string
const& rowPrefix,
325 std::string
const& rowSuffix) ->
void
330 auto const lastIdx(extent[TDim::value] - 1u);
331 for(
auto i(decltype(lastIdx)(0)); i <= lastIdx; ++i)
335 reinterpret_cast<Elem<TView> const*
>(
reinterpret_cast<std::uint8_t const*
>(ptr) + i * pitch),
354 template<
typename TView>
362 std::string
const& elementSeparator,
364 std::string
const& rowPrefix,
365 std::string
const& rowSuffix) ->
void
370 for(
auto i(decltype(lastIdx)(0)); i <= lastIdx; ++i)
378 os << elementSeparator;
390 template<
typename TView>
394 std::string
const& elementSeparator =
", ",
395 std::string
const& rowSeparator =
"\n",
396 std::string
const& rowPrefix =
"[",
397 std::string
const& rowSuffix =
"]") ->
void
411 template<
typename TView>
419 template<
typename TDim,
typename TView>
433 template<
typename TDev,
typename TElem,
typename TExtent>
434 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent)
443 detail::calculatePitchesFromExtents<TElem>(extentVec));
455 template<
typename TDev,
typename TElem,
typename TExtent,
typename TPitch>
456 auto createView(TDev
const& dev, TElem* pMem, TExtent
const& extent, TPitch pitch)
467 template<
typename TDev,
typename TContainer>
481 template<
typename TDev,
typename TContainer,
typename TExtent>
482 auto createView(TDev
const& dev, TContainer& con, TExtent
const& extent)
484 return createView(dev, std::data(con), extent);
493 template<
typename TView,
typename TExtent,
typename TOffsets>
499 #ifdef ALPAKA_USE_MDSPAN
500 namespace experimental
503 using std::experimental::default_accessor;
504 using std::experimental::dextents;
505 using std::experimental::extents;
506 using std::experimental::layout_left;
507 using std::experimental::layout_right;
508 using std::experimental::layout_stride;
509 using std::experimental::mdspan;
511 using std::experimental::full_extent;
512 using std::experimental::submdspan;
518 template<
typename ElementType>
519 struct ByteIndexedAccessor
521 using offset_policy = ByteIndexedAccessor;
522 using element_type = ElementType;
523 using reference = ElementType&;
525 using data_handle_type
526 = std::conditional_t<std::is_const_v<ElementType>, std::byte
const*, std::byte*>;
528 constexpr ByteIndexedAccessor() noexcept = default;
535 ALPAKA_FN_HOST_ACC constexpr reference access(data_handle_type p,
size_t i)
const noexcept
537 assert(i %
alignof(ElementType) == 0);
539 # pragma GCC diagnostic push
540 # pragma GCC diagnostic ignored "-Wcast-align"
542 return *
reinterpret_cast<ElementType*
>(p + i);
544 # pragma GCC diagnostic pop
549 template<
typename TView, std::size_t... Is>
550 ALPAKA_FN_HOST auto makeExtents(TView
const& view, std::index_sequence<Is...>)
553 return std::experimental::dextents<Idx<TView>, Dim<TView>::value>{ex[Is]...};
558 template<
typename TView,
typename TSfinae =
void>
563 constexpr
auto dim = Dim<TView>::value;
564 using Element = Elem<TView>;
565 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
566 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
568 layout_stride::mapping<decltype(extents)> m{extents, strides};
569 return mdspan<Element, decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
576 constexpr
auto dim = Dim<TView>::value;
577 using Element = Elem<TView>;
578 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
579 auto* ptr =
reinterpret_cast<std::byte*
>(
getPtrNative(view));
581 std::reverse(
begin(strides),
end(strides));
582 layout_stride::mapping<decltype(extents)> m{extents, strides};
583 return mdspan<Element, decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
591 template<
typename TView>
594 return traits::GetMdSpan<TView>::getMdSpan(view);
600 template<
typename TView>
603 return traits::GetMdSpan<TView>::getMdSpanTransposed(view);
606 template<
typename TElem,
typename TIdx,
typename TDim>
607 using MdSpan = alpaka::experimental::mdspan<
609 alpaka::experimental::dextents<TIdx, TDim::value>,
610 alpaka::experimental::layout_stride,
611 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....
constexpr ALPAKA_FN_HOST_ACC auto back() -> TVal &
#define ALPAKA_FN_HOST_ACC
constexpr ALPAKA_FN_HOST_ACC 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 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 getPtrDev(TView &view, TDev const &dev) -> Elem< TView > *
Gets the pointer to the view on the given device.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtents(T const &object) -> Vec< Dim< T >, Idx< T >>
ALPAKA_FN_HOST auto getPitchesInBytes(TView const &view) -> Vec< Dim< TView >, Idx< TView >>
ALPAKA_FN_HOST auto getPitchBytesVecEnd(TView const &view=TView()) -> Vec< TDim, Idx< TView >>
constexpr ALPAKA_FN_HOST_ACC auto toArray(Vec< TDim, TVal > const &v) -> std::array< TVal, TDim::value >
Converts a Vec to a std::array.
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_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.
auto getPitchBytesVec(TView const &view) -> Vec< Dim< TView >, Idx< TView >>
ALPAKA_FN_HOST auto getPtrNative(TView &view) -> Elem< TView > *
Gets the native pointer of the memory view.
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...
constexpr ALPAKA_FN_HOST_ACC auto operator()(TView const &view) const
The pointer on device get trait.
The native pointer get trait.