alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Traits.hpp
Go to the documentation of this file.
1/* Copyright 2026 Axel Hübl, Benjamin Worpitz, Matthias Werner, Andrea Bocci, Jan Stephan, Bernhard Manfred Gruber,
2 * Aurora Perego, Simone Balducci
3 * SPDX-License-Identifier: MPL-2.0
4 */
5
6#pragma once
7
10#include "alpaka/dev/Traits.hpp"
11#include "alpaka/dim/Traits.hpp"
14#include "alpaka/meta/Fold.hpp"
18#include "alpaka/vec/Traits.hpp"
19#include "alpaka/vec/Vec.hpp"
20
21#include <array>
22#include <cassert>
23#include <cstddef>
24#include <iosfwd>
25#include <type_traits>
26#include <vector>
27
28#ifdef ALPAKA_USE_MDSPAN
29# ifdef ALPAKA_HAS_STD_MDSPAN
30// mdspan from the standard library
31# include <mdspan>
32# include <version>
33
34namespace alpaka::experimental
35{
36 // Import C++23 mdspan into alpaka::experimental namespace.
37 // See https://wg21.link/P0009R18 .
38 using ::std::default_accessor;
39 using ::std::dextents;
40 using ::std::extents;
41 using ::std::layout_left;
42 using ::std::layout_right;
43 using ::std::layout_stride;
44 using ::std::mdspan;
45# ifdef __cpp_lib_submdspan
46 // Import C++26 submdspan into alpaka::experimental namespace.
47 // See https://wg21.link/P2630R4 .
48 using ::std::full_extent;
49 using ::std::submdspan;
50# endif
51} // namespace alpaka::experimental
52
53# else
54
55# ifdef ALPAKA_ACC_SYCL_ENABLED
56// do not expose the macro definition of printf
57# pragma push_macro("printf")
58# ifdef printf
59# undef printf
60# endif
61# endif // ALPAKA_ACC_SYCL_ENABLED
62
63# if defined(ALPAKA_ACC_SYCL_ENABLED) && (defined(ALPAKA_SYCL_ONEAPI_FPGA) || defined(ALPAKA_SYCL_TARGET_FPGA))
64// the fpga compiler does not handle well the [[no_unique_address]] attribute, resulting in:
65// GEP has !intel-tbaa annotation but its "shape" is unexpected!
66// /opt/intel/oneapi/compiler/2025.0/bin/compiler/llvm-link: error: linked module is broken!
67// icpx: error: sycl-link command failed with exit code 1 (use -v to see invocation)
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
72# endif
73
74// mdspan from the Kokkos reference implementation
75# define MDSPAN_IMPL_STANDARD_NAMESPACE alpaka::experimental
76# include <experimental/mdspan>
77
78# ifdef ALPAKA_ACC_SYCL_ENABLED
79// restore the macro definition of printf
80# pragma pop_macro("printf")
81# endif // ALPAKA_ACC_SYCL_ENABLED
82# endif
83#endif
84
85namespace alpaka
86{
87 namespace detail
88 {
89 //! Calculate the pitches purely from the extents.
90 template<typename TElem, typename TDim, typename TIdx>
92 {
93 Vec<TDim, TIdx> pitchBytes{};
94 constexpr auto dim = TIdx{TDim::value};
95 if constexpr(dim > 0)
96 pitchBytes.back() = static_cast<TIdx>(sizeof(TElem));
97 if constexpr(dim > 1)
98 for(TIdx i = TDim::value - 1; i > 0; i--)
99 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
100 return pitchBytes;
101 }
102
103 //! Calculate the pitches from the extents and the one-dimensional pitch.
104 template<typename TElem, typename TDim, typename TIdx>
106 Vec<TDim, TIdx> const& extent,
107 std::size_t pitch)
108 {
109 Vec<TDim, TIdx> pitchBytes{};
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)
114 {
115 if(pitch == 0)
116 pitchBytes[TDim::value - 2] = extent.back() * pitchBytes.back();
117 else
118 pitchBytes[TDim::value - 2] = static_cast<TIdx>(
119 (static_cast<std::size_t>(extent.back() * pitchBytes.back()) + pitch - 1) / pitch * pitch);
120 }
121 if constexpr(dim > 2)
122 for(TIdx i = TDim::value - 2; i > 0; i--)
123 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
124 return pitchBytes;
125 }
126
127 } // namespace detail
128
129 //! The view traits.
130 namespace trait
131 {
132 //! The native pointer get trait.
133 template<typename TView, typename TSfinae = void>
135
136 //! The pointer on device get trait.
137 template<typename TView, typename TDev, typename TSfinae = void>
138 struct GetPtrDev;
139
140 //! The pitch in bytes.
141 //! This is the distance in bytes in the linear memory between two consecutive elements in the next higher
142 //! dimension (TIdx-1).
143 //!
144 //! The default implementation uses the extent to calculate the pitch.
145 template<typename TIdx, typename TView, typename TSfinae = void>
146 struct [[deprecated("Use GetPitchesInBytes instead")]] GetPitchBytes
147 {
149
150 ALPAKA_FN_HOST static auto getPitchBytes(TView const& view) -> ViewIdx
151 {
152 return getPitchBytesDefault(view);
153 }
154
155 private:
156 static auto getPitchBytesDefault(TView const& view) -> ViewIdx
157 {
158 constexpr auto idx = TIdx::value;
159 constexpr auto viewDim = Dim<TView>::value;
160 if constexpr(idx < viewDim - 1)
161 {
162#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
163# pragma GCC diagnostic push
164# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
165#endif
166 return getExtents(view)[idx] * GetPitchBytes<DimInt<idx + 1>, TView>::getPitchBytes(view);
167#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
168# pragma GCC diagnostic pop
169#endif
170 }
171 else if constexpr(idx == viewDim - 1)
172 return getExtents(view)[viewDim - 1] * static_cast<ViewIdx>(sizeof(Elem<TView>));
173 else
174 return static_cast<ViewIdx>(sizeof(Elem<TView>));
176 }
177 };
178
179 //! Customization point for \ref getPitchesInBytes.
180 //! The default implementation uses the extent to calculate the pitches.
181 template<typename TView, typename TSfinae = void>
183 {
184 ALPAKA_FN_HOST_ACC constexpr auto operator()(TView const& view) const
185 {
186 return alpaka::detail::calculatePitchesFromExtents<Elem<TView>>(getExtents(view));
187 }
188 };
189
190 //! The memory set task trait.
191 //!
192 //! Fills the view with data.
193 template<typename TDim, typename TDev, typename TSfinae = void>
195
196 template<typename TDim, typename TDev, typename TSfinae = void>
198
199 //! The memory copy task trait.
200 //!
201 //! Copies memory from one view into another view possibly on a different device.
202 template<typename TDim, typename TDevDst, typename TDevSrc, typename TSfinae = void>
204
205 //! The device memory view creation trait.
206 template<typename TDev, typename TSfinae = void>
208
209 //! The sub view creation trait.
210 template<typename TDev, typename TSfinae = void>
211 struct CreateSubView;
212 } // namespace trait
213
214 //! Gets the native pointer of the memory view.
215 //!
216 //! \param view The memory view.
217 //! \return The native pointer.
218 template<typename TView>
219 ALPAKA_FN_HOST auto getPtrNative(TView const& view) -> Elem<TView> const*
220 {
222 }
223
224 //! Gets the native pointer of the memory view.
225 //!
226 //! \param view The memory view.
227 //! \return The native pointer.
228 template<typename TView>
230 {
232 }
233
234 //! Gets the pointer to the view on the given device.
235 //!
236 //! \param view The memory view.
237 //! \param dev The device.
238 //! \return The pointer on the device.
239 template<typename TView, typename TDev>
240 ALPAKA_FN_HOST auto getPtrDev(TView const& view, TDev const& dev) -> Elem<TView> const*
241 {
243 }
244
245 //! Gets the pointer to the view on the given device.
246 //!
247 //! \param view The memory view.
248 //! \param dev The device.
249 //! \return The pointer on the device.
250 template<typename TView, typename TDev>
251 ALPAKA_FN_HOST auto getPtrDev(TView& view, TDev const& dev) -> Elem<TView>*
252 {
254 }
255
256 //! \return The pitch in bytes. This is the distance in bytes between two consecutive elements in the given
257 //! dimension.
258 template<std::size_t Tidx, typename TView>
259 [[deprecated("Use getPitchesInBytes instead")]] ALPAKA_FN_HOST auto getPitchBytes(TView const& view) -> Idx<TView>
260 {
261#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
262# pragma GCC diagnostic push
263# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
264#endif
266#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
267# pragma GCC diagnostic pop
268#endif
269 }
270
271 //! \return The pitches in bytes as an alpaka::Vec. This is the distance in bytes between two consecutive elements
272 //! in the given dimension.
273 //! E.g. for a 3D view without padding, the 0-dim pitch is the distance in bytes to jump from one element to the
274 //! next within the same row, the 1-dim pitch (aka. the row pitch) is the distance in bytes to jump from one
275 //! element to the neighboring element on the next row. The 2-dim pitch (aka. the slice pitch) is the distance in
276 //! bytes to jump from one element to the neighboring element on the next slice.
277 //! E.g. a 3D view of floats without padding and the extents {42, 10, 2}, would have a pitch vector of {80, 8, 4}.
278 template<typename TView>
280 {
281 return trait::GetPitchesInBytes<TView>{}(view);
282 }
283
284 //! Create a memory set task.
285 //!
286 //! \param view The memory view to fill.
287 //! \param byte Value to set for each element of the specified view.
288 //! \param extent The extent of the view to fill.
289 template<typename TExtent, typename TViewFwd>
290 ALPAKA_FN_HOST auto createTaskMemset(TViewFwd&& view, std::uint8_t const& byte, TExtent const& extent)
291 {
292 using TView = std::remove_reference_t<TViewFwd>;
293 static_assert(!std::is_const_v<TView>, "The view must not be const!");
294 static_assert(
296 "The view and the extent are required to have the same dimensionality!");
297
298 assert((extent <= getExtents(view)).all() && "The memset extent must not be larger than the view's extent!");
299
301 std::forward<TViewFwd>(view),
302 byte,
303 extent);
304 }
305
306 template<typename TExtent, typename TViewFwd, typename TValue>
307 ALPAKA_FN_HOST auto createTaskFill(TViewFwd&& view, TValue const& value, TExtent const& extent)
308 {
309 using TView = std::remove_reference_t<TViewFwd>;
310 static_assert(!std::is_const_v<TView>, "The view must not be const!");
311 static_assert(
313 "The view and the extent are required to have the same dimensionality!");
314
315 assert((extent <= getExtents(view)).all() && "The fill extent must not be larger than the view's extent!");
316
318 std::forward<TViewFwd>(view),
319 value,
320 extent);
321 }
322
323 //! Sets the bytes of the memory of view, described by extent, to the given value.
324 //!
325 //! \param queue The queue to enqueue the view fill task into.
326 //! \param[in,out] view The memory view to fill. May be a temporary object.
327 //! \param byte Value to set for each element of the specified view.
328 //! \param extent The extent of the view to fill.
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
331 {
332 enqueue(queue, createTaskMemset(std::forward<TViewFwd>(view), byte, extent));
333 }
334
335 //! Sets each byte of the memory of the entire view to the given value.
336 //!
337 //! \param queue The queue to enqueue the view fill task into.
338 //! \param[in,out] view The memory view to fill. May be a temporary object.
339 //! \param byte Value to set for each element of the specified view.
340 template<typename TViewFwd, typename TQueue>
341 ALPAKA_FN_HOST auto memset(TQueue& queue, TViewFwd&& view, std::uint8_t const& byte) -> void
342 {
343 enqueue(queue, createTaskMemset(std::forward<TViewFwd>(view), byte, getExtents(view)));
344 }
345
346 template<typename TViewFwd, typename TValue, typename TQueue>
347 ALPAKA_FN_HOST auto fill(TQueue& queue, TViewFwd&& view, TValue const& value) -> void
348 {
349 enqueue(queue, createTaskFill(std::forward<TViewFwd>(view), value, getExtents(view)));
350 }
351
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
354 {
355 enqueue(queue, createTaskFill(std::forward<TViewFwd>(view), value, extent));
356 }
357
358 //! Creates a memory copy task.
359 //!
360 //! \param viewDst The destination memory view.
361 //! \param viewSrc The source memory view.
362 //! \param extent The extent of the view to copy.
363 template<typename TExtent, typename TViewSrc, typename TViewDstFwd>
364 ALPAKA_FN_HOST auto createTaskMemcpy(TViewDstFwd&& viewDst, TViewSrc const& viewSrc, TExtent const& extent)
365 {
366 using TViewDst = std::remove_reference_t<TViewDstFwd>;
367 using SrcElem = Elem<TViewSrc>;
368 using DstElem = Elem<TViewDst>;
369
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!");
372 static_assert(
374 "The source and the destination view must have the same dimensionality!");
375 static_assert(
377 "The destination view and the extent must have the same dimensionality!");
378 static_assert(
379 std::is_same_v<DstElem, std::remove_const_t<SrcElem>>,
380 "The source and destination view must have the same element type!");
381
382 assert(
383 (extent <= getExtents(viewSrc)).all()
384 && "The memcpy extent must not be larger than the source view's extent!");
385 assert(
386 (extent <= getExtents(viewDst)).all()
387 && "The memcpy extent must not be larger than the destination view's extent!");
388
390 std::forward<TViewDstFwd>(viewDst),
391 viewSrc,
392 extent);
393 }
394
395 //! Copies memory from a part of viewSrc to viewDst, described by extent. Possibly copies between different memory
396 //! spaces.
397 //!
398 //! \param queue The queue to enqueue the view copy task into.
399 //! \param[in,out] viewDst The destination memory view. May be a temporary object.
400 //! \param viewSrc The source memory view. May be a temporary object.
401 //! \param extent The extent of the view to copy.
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)
404 -> void
405 {
406 enqueue(queue, createTaskMemcpy(std::forward<TViewDstFwd>(viewDst), viewSrc, extent));
407 }
408
409 //! Copies the entire memory of viewSrc to viewDst. Possibly copies between different memory
410 //! spaces.
411 //!
412 //! \param queue The queue to enqueue the view copy task into.
413 //! \param[in,out] viewDst The destination memory view. May be a temporary object.
414 //! \param viewSrc The source memory view. May be a temporary object.
415 template<typename TViewSrc, typename TViewDstFwd, typename TQueue>
416 ALPAKA_FN_HOST auto memcpy(TQueue& queue, TViewDstFwd&& viewDst, TViewSrc const& viewSrc) -> void
417 {
418 enqueue(queue, createTaskMemcpy(std::forward<TViewDstFwd>(viewDst), viewSrc, getExtents(viewSrc)));
419 }
420
421 namespace concepts
422 {
423 template<typename T>
424 concept DeviceProvider = alpaka::isDevice<T> || alpaka::isQueue<T>;
425 } // namespace concepts
426
427 namespace detail
428 {
429 template<typename TDim, typename TView>
430 struct Print
431 {
433 TView const& view,
434 Elem<TView> const* const ptr,
435 Vec<Dim<TView>, Idx<TView>> const& extent,
436 std::ostream& os,
437 std::string const& elementSeparator,
438 std::string const& rowSeparator,
439 std::string const& rowPrefix,
440 std::string const& rowSuffix) -> void
441 {
442 os << rowPrefix;
443
444 auto const pitch = getPitchesInBytes(view)[TDim::value + 1];
445 auto const lastIdx(extent[TDim::value] - 1u);
446 for(auto i(decltype(lastIdx)(0)); i <= lastIdx; ++i)
447 {
449 view,
450 reinterpret_cast<Elem<TView> const*>(reinterpret_cast<std::uint8_t const*>(ptr) + i * pitch),
451 extent,
452 os,
453 elementSeparator,
454 rowSeparator,
455 rowPrefix,
456 rowSuffix);
457
458 // While we are not at the end of a row, add the row separator.
459 if(i != lastIdx)
460 {
461 os << rowSeparator;
462 }
463 }
464
465 os << rowSuffix;
466 }
467 };
468
469 template<typename TView>
470 struct Print<DimInt<Dim<TView>::value - 1u>, TView>
471 {
473 TView const& /* view */,
474 Elem<TView> const* const ptr,
475 Vec<Dim<TView>, Idx<TView>> const& extent,
476 std::ostream& os,
477 std::string const& elementSeparator,
478 std::string const& /* rowSeparator */,
479 std::string const& rowPrefix,
480 std::string const& rowSuffix) -> void
481 {
482 os << rowPrefix;
483
484 auto const lastIdx(extent[Dim<TView>::value - 1u] - 1u);
485 for(auto i(decltype(lastIdx)(0)); i <= lastIdx; ++i)
486 {
487 // Add the current element.
488 os << *(ptr + i);
489
490 // While we are not at the end of a line, add the element separator.
491 if(i != lastIdx)
492 {
493 os << elementSeparator;
494 }
495 }
496
497 os << rowSuffix;
498 }
499 };
500
501 template<typename TDeviceProvider>
502 auto getDeviceFromProvider(TDeviceProvider const& provider)
503 {
504 if constexpr(alpaka::isDevice<TDeviceProvider>)
505 {
506 return provider;
507 }
508 else
509 {
510 return alpaka::getDev(provider);
511 }
512 }
513
514 } // namespace detail
515
516 //! Prints the content of the view to the given queue.
517 // \TODO: Add precision flag.
518 // \TODO: Add column alignment flag.
519 template<typename TView>
521 TView const& view,
522 std::ostream& os,
523 std::string const& elementSeparator = ", ",
524 std::string const& rowSeparator = "\n",
525 std::string const& rowPrefix = "[",
526 std::string const& rowSuffix = "]") -> void
527 {
529 view,
530 getPtrNative(view),
531 getExtents(view),
532 os,
533 elementSeparator,
534 rowSeparator,
535 rowPrefix,
536 rowSuffix);
537 }
538
539 //! \return The pitch vector.
540 template<typename TView>
541 [[deprecated("Use getPitchesInBytes instead")]] auto getPitchBytesVec(TView const& view)
543 {
544 return getPitchesInBytes(view);
545 }
546
547 //! \return The pitch but only the last N elements.
548 template<typename TDim, typename TView>
549 ALPAKA_FN_HOST auto getPitchBytesVecEnd(TView const& view = TView()) -> Vec<TDim, Idx<TView>>
550 {
551 return subVecEnd<TDim>(getPitchesInBytes(view));
552 }
553
554 //! Creates a view to a device pointer
555 //!
556 //! \param dev Object from which the device can be obtained.
557 //! \param pMem Pointer to memory. The pointer must be accessible from the given device.
558 //! \param extent Number of elements represented by the pMem.
559 //! Using a multi dimensional extent will result in a multi dimension view to the memory represented
560 //! by pMem.
561 //! \return A view to device memory.
562 template<concepts::DeviceProvider TDev, typename TElem, typename TExtent>
563 auto createView(TDev const& dev, TElem* pMem, TExtent const& extent)
564 {
567 auto const extentVec = Vec<Dim, Idx>(extent);
568 auto device = detail::getDeviceFromProvider(dev);
569 return trait::CreateViewPlainPtr<decltype(device)>::createViewPlainPtr(
570 device,
571 pMem,
572 extentVec,
573 detail::calculatePitchesFromExtents<TElem>(extentVec));
574 }
575
576 //! Creates a view to a device pointer
577 //!
578 //! \param dev Object from which the device can be obtained.
579 //! \param pMem Pointer to memory. The pointer must be accessible from the given device.
580 //! \param extent Number of elements represented by the pMem.
581 //! Using a multi dimensional extent will result in a multi dimension view to the memory represented
582 //! by pMem.
583 //! \param pitch Pitch in bytes for each dimension. Dimensionality must be equal to extent.
584 //! \return A view to device memory.
585 template<concepts::DeviceProvider TDev, typename TElem, typename TExtent, typename TPitch>
586 auto createView(TDev const& dev, TElem* pMem, TExtent const& extent, TPitch pitch)
587 {
588 auto device = detail::getDeviceFromProvider(dev);
589 return trait::CreateViewPlainPtr<decltype(device)>::createViewPlainPtr(device, pMem, extent, pitch);
590 }
591
592 //! Creates a view to a contiguous container of device-accessible memory.
593 //!
594 //! \param dev Object from which the device can be obtained.
595 //! \param con Contiguous container. The container must provide a `data()` method. The data held by the container
596 //! must be accessible from the given device. The `GetExtent` trait must be defined for the container.
597 //! \return A view to device memory.
598 template<concepts::DeviceProvider TDev, typename TContainer>
599 auto createView(TDev const& dev, TContainer& con)
600 {
601 auto const device = detail::getDeviceFromProvider(dev);
602 return createView(device, std::data(con), getExtents(con));
603 }
604
605 //! Creates a view to a contiguous container of device-accessible memory.
606 //!
607 //! \param dev Object from which the device can be obtained.
608 //! \param con Contiguous container. The container must provide a `data()` method. The data held by the container
609 //! must be accessible from the given device. The `GetExtent` trait must be defined for the container.
610 //! \param extent Number of elements held by the container. Using a multi-dimensional extent will result in a
611 //! multi-dimensional view to the memory represented by the container.
612 //! \return A view to device memory.
613 template<concepts::DeviceProvider TDev, typename TContainer, typename TExtent>
614 auto createView(TDev const& dev, TContainer& con, TExtent const& extent)
615 {
616 auto const device = detail::getDeviceFromProvider(dev);
617 return createView(device, std::data(con), extent);
618 }
619
620 //! Creates a sub view to an existing view.
621 //!
622 //! \param view The view this view is a sub-view of.
623 //! \param extent Number of elements the resulting view holds.
624 //! \param offset Number of elements skipped in view for the new origin of the resulting view.
625 //! \return A sub view to a existing view.
626 template<typename TView, typename TExtent, typename TOffsets>
627 auto createSubView(TView& view, TExtent const& extent, TOffsets const& offset = TExtent())
628 {
630 }
631
632#ifdef ALPAKA_USE_MDSPAN
633 namespace experimental
634 {
635 namespace traits
636 {
637 namespace detail
638 {
639 template<typename ElementType>
640 struct ByteIndexedAccessor
641 {
642 using offset_policy = ByteIndexedAccessor;
643 using element_type = ElementType;
644 using reference = ElementType&;
645
646 using data_handle_type
647 = std::conditional_t<std::is_const_v<ElementType>, std::byte const*, std::byte*>;
648
649 constexpr ByteIndexedAccessor() noexcept = default;
650
651 ALPAKA_FN_HOST_ACC constexpr data_handle_type offset(data_handle_type p, size_t i) const noexcept
652 {
653 return p + i;
654 }
655
656 ALPAKA_FN_HOST_ACC constexpr reference access(data_handle_type p, size_t i) const noexcept
657 {
658 assert(i % alignof(ElementType) == 0);
659 return *reinterpret_cast<ElementType*>(__builtin_assume_aligned(p + i, alignof(ElementType)));
660 }
661 };
662
663 template<typename TView, std::size_t... Is>
664 ALPAKA_FN_HOST auto makeExtents(TView const& view, std::index_sequence<Is...>)
665 {
666 auto const ex = getExtents(view);
667 return dextents<Idx<TView>, Dim<TView>::value>{ex[Is]...};
668 }
669 } // namespace detail
670
671 //! Customization point for getting an mdspan from a view.
672 template<typename TView, typename TSfinae = void>
673 struct GetMdSpan
674 {
675 ALPAKA_FN_HOST static auto getMdSpan(TView& view)
676 {
677 constexpr auto dim = Dim<TView>::value;
678 using Element = Elem<TView>;
679 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
680 auto* ptr = reinterpret_cast<std::byte*>(getPtrNative(view));
681 auto const strides = toArray(getPitchesInBytes(view));
682 layout_stride::mapping<decltype(extents)> m{extents, strides};
683 return mdspan<Element, decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
684 ptr,
685 m};
686 }
687
688 ALPAKA_FN_HOST static auto getMdSpanTransposed(TView& view)
689 {
690 constexpr auto dim = Dim<TView>::value;
691 using Element = Elem<TView>;
692 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
693 auto* ptr = reinterpret_cast<std::byte*>(getPtrNative(view));
694 auto strides = toArray(getPitchesInBytes(view));
695 std::reverse(begin(strides), end(strides));
696 layout_stride::mapping<decltype(extents)> m{extents, strides};
697 return mdspan<Element, decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
698 ptr,
699 m};
700 }
701 };
702 } // namespace traits
703
704 //! Gets a std::mdspan from the given view. The memory layout is determined by the pitches of the view.
705 template<typename TView>
706 ALPAKA_FN_HOST auto getMdSpan(TView& view)
707 {
708 return traits::GetMdSpan<TView>::getMdSpan(view);
709 }
710
711 //! Gets a std::mdspan from the given view. The memory layout is determined by the reversed pitches of the
712 //! view. This effectively also reverses the extents of the view. In order words, if you create a transposed
713 //! mdspan on a 10x5 element view, the mdspan will have an iteration space of 5x10.
714 template<typename TView>
715 ALPAKA_FN_HOST auto getMdSpanTransposed(TView& view)
716 {
717 return traits::GetMdSpan<TView>::getMdSpanTransposed(view);
718 }
719
720 template<typename TElem, typename TIdx, typename TDim>
721 using MdSpan = alpaka::experimental::mdspan<
722 TElem,
723 alpaka::experimental::dextents<TIdx, TDim::value>,
724 alpaka::experimental::layout_stride,
725 alpaka::experimental::traits::detail::ByteIndexedAccessor<TElem>>;
726 } // namespace experimental
727#endif
728} // namespace alpaka
#define ALPAKA_UNREACHABLE(...)
Before CUDA 11.5 nvcc is unable to correctly identify return statements in 'if constexpr' branches....
A n-dimensional vector.
Definition Vec.hpp:38
ALPAKA_FN_HOST_ACC constexpr auto back() -> TVal &
Definition Vec.hpp:141
#define ALPAKA_FN_HOST
Definition Common.hpp:43
#define ALPAKA_FN_HOST_ACC
Definition Common.hpp:42
auto getDeviceFromProvider(TDeviceProvider const &provider)
Definition Traits.hpp:502
ALPAKA_FN_HOST_ACC constexpr auto calculatePitchesFromExtents(Vec< TDim, TIdx > const &extent)
Calculate the pitches purely from the extents.
Definition Traits.hpp:91
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.
Definition Traits.hpp:105
ALPAKA_FN_HOST auto end(TView &view) -> Iterator< TView >
Definition Iterator.hpp:133
ALPAKA_FN_HOST auto begin(TView &view) -> Iterator< TView >
Definition Iterator.hpp:127
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition Traits.hpp:29
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 > >
Definition Traits.hpp:549
ALPAKA_FN_HOST_ACC constexpr auto toArray(Vec< TDim, TVal > const &v) -> std::array< TVal, TDim::value >
Converts a Vec to a std::array.
Definition Vec.hpp:536
ALPAKA_FN_HOST auto getPitchesInBytes(TView const &view) -> Vec< Dim< TView >, Idx< TView > >
Definition Traits.hpp:279
auto getPitchBytesVec(TView const &view) -> Vec< Dim< TView >, Idx< TView > >
Definition Traits.hpp:541
ALPAKA_FN_HOST auto createTaskMemcpy(TViewDstFwd &&viewDst, TViewSrc const &viewSrc, TExtent const &extent)
Creates a memory copy task.
Definition Traits.hpp:364
typename trait::DevType< T >::type Dev
The device type trait alias template to remove the ::type.
Definition Traits.hpp:56
auto createSubView(TView &view, TExtent const &extent, TOffsets const &offset=TExtent())
Creates a sub view to an existing view.
Definition Traits.hpp:627
ALPAKA_FN_HOST auto createTaskFill(TViewFwd &&view, TValue const &value, TExtent const &extent)
Definition Traits.hpp:307
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.
Definition Traits.hpp:520
ALPAKA_FN_HOST auto getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
Definition Traits.hpp:219
std::remove_volatile_t< typename trait::ElemType< TView >::type > Elem
The element type trait alias template to remove the ::type.
Definition Traits.hpp:21
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtents(T const &object) -> Vec< Dim< T >, Idx< T > >
Definition Traits.hpp:59
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.
Definition Traits.hpp:330
ALPAKA_FN_HOST auto fill(TQueue &queue, TViewFwd &&view, TValue const &value) -> void
Definition Traits.hpp:347
ALPAKA_FN_HOST auto getDev(T const &t)
Definition Traits.hpp:68
ALPAKA_FN_HOST auto enqueue(TQueue &queue, TTask &&task) -> void
Queues the given task in the given queue.
Definition Traits.hpp:47
ALPAKA_FN_HOST auto getPtrDev(TView const &view, TDev const &dev) -> Elem< TView > const *
Gets the pointer to the view on the given device.
Definition Traits.hpp:240
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.
Definition Traits.hpp:290
ALPAKA_FN_HOST auto getPitchBytes(TView const &view) -> Idx< TView >
Definition Traits.hpp:259
auto createView(TDev const &dev, TElem *pMem, TExtent const &extent)
Creates a view to a device pointer.
Definition Traits.hpp:563
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition Traits.hpp:19
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
Definition Traits.hpp:472
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
Definition Traits.hpp:432
The sub view creation trait.
The memory copy task trait.
Definition Traits.hpp:203
The memory set task trait.
Definition Traits.hpp:194
The device memory view creation trait.
Definition Traits.hpp:207
The pitch in bytes. This is the distance in bytes in the linear memory between two consecutive elemen...
Definition Traits.hpp:147
static ALPAKA_FN_HOST auto getPitchBytes(TView const &view) -> ViewIdx
Definition Traits.hpp:150
Customization point for getPitchesInBytes. The default implementation uses the extent to calculate th...
Definition Traits.hpp:183
ALPAKA_FN_HOST_ACC constexpr auto operator()(TView const &view) const
Definition Traits.hpp:184
The pointer on device get trait.
Definition Traits.hpp:138
The native pointer get trait.
Definition Traits.hpp:134