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
11#include "alpaka/dev/Traits.hpp"
12#include "alpaka/dim/Traits.hpp"
15#include "alpaka/meta/Fold.hpp"
19#include "alpaka/vec/Traits.hpp"
20#include "alpaka/vec/Vec.hpp"
21
22#include <array>
23#include <cassert>
24#include <cstddef>
25#include <iosfwd>
26#include <type_traits>
27#include <vector>
28
29namespace alpaka
30{
31 namespace detail
32 {
33 //! Calculate the pitches purely from the extents.
34 template<typename TElem, typename TDim, typename TIdx>
36 {
37 Vec<TDim, TIdx> pitchBytes{};
38 constexpr auto dim = TIdx{TDim::value};
39 if constexpr(dim > 0)
40 pitchBytes.back() = static_cast<TIdx>(sizeof(TElem));
41 if constexpr(dim > 1)
42 for(TIdx i = TDim::value - 1; i > 0; i--)
43 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
44#if ALPAKA_COMP_CLANG >= ALPAKA_VERSION_NUMBER(21, 1, 0)
45# pragma clang diagnostic push
46# pragma clang diagnostic ignored "-Wnrvo"
47#endif
48 return pitchBytes;
49#if ALPAKA_COMP_CLANG >= ALPAKA_VERSION_NUMBER(21, 1, 0)
50# pragma clang diagnostic pop
51#endif
52 }
53
54 //! Calculate the pitches from the extents and the one-dimensional pitch.
55 template<typename TElem, typename TDim, typename TIdx>
57 Vec<TDim, TIdx> const& extent,
58 std::size_t pitch)
59 {
60 Vec<TDim, TIdx> pitchBytes{};
61 constexpr auto dim = TIdx{TDim::value};
62 if constexpr(dim > 0)
63 pitchBytes.back() = static_cast<TIdx>(sizeof(TElem));
64 if constexpr(dim > 1)
65 {
66 if(pitch == 0)
67 pitchBytes[TDim::value - 2] = extent.back() * pitchBytes.back();
68 else
69 pitchBytes[TDim::value - 2] = static_cast<TIdx>(
70 (static_cast<std::size_t>(extent.back() * pitchBytes.back()) + pitch - 1) / pitch * pitch);
71 }
72 if constexpr(dim > 2)
73 for(TIdx i = TDim::value - 2; i > 0; i--)
74 pitchBytes[i - 1] = extent[i] * pitchBytes[i];
75#if ALPAKA_COMP_CLANG >= ALPAKA_VERSION_NUMBER(21, 1, 0)
76# pragma clang diagnostic push
77# pragma clang diagnostic ignored "-Wnrvo"
78#endif
79 return pitchBytes;
80#if ALPAKA_COMP_CLANG >= ALPAKA_VERSION_NUMBER(21, 1, 0)
81# pragma clang diagnostic pop
82#endif
83 }
84
85 } // namespace detail
86
87 //! The view traits.
88 namespace trait
89 {
90 //! The native pointer get trait.
91 template<typename TView, typename TSfinae = void>
93
94 //! The pointer on device get trait.
95 template<typename TView, typename TDev, typename TSfinae = void>
96 struct GetPtrDev;
97
98 //! The pitch in bytes.
99 //! This is the distance in bytes in the linear memory between two consecutive elements in the next higher
100 //! dimension (TIdx-1).
101 //!
102 //! The default implementation uses the extent to calculate the pitch.
103 template<typename TIdx, typename TView, typename TSfinae = void>
104 struct [[deprecated("Use GetPitchesInBytes instead")]] GetPitchBytes
105 {
107
108 ALPAKA_FN_HOST static auto getPitchBytes(TView const& view) -> ViewIdx
109 {
110 return getPitchBytesDefault(view);
111 }
112
113 private:
114 static auto getPitchBytesDefault(TView const& view) -> ViewIdx
115 {
116 constexpr auto idx = TIdx::value;
117 constexpr auto viewDim = Dim<TView>::value;
118 if constexpr(idx < viewDim - 1)
119 {
120#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
121# pragma GCC diagnostic push
122# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
123#endif
124 return getExtents(view)[idx] * GetPitchBytes<DimInt<idx + 1>, TView>::getPitchBytes(view);
125#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
126# pragma GCC diagnostic pop
127#endif
128 }
129 else if constexpr(idx == viewDim - 1)
130 return getExtents(view)[viewDim - 1] * static_cast<ViewIdx>(sizeof(Elem<TView>));
131 else
132 return static_cast<ViewIdx>(sizeof(Elem<TView>));
134 }
135 };
136
137 //! Customization point for \ref getPitchesInBytes.
138 //! The default implementation uses the extent to calculate the pitches.
139 template<typename TView, typename TSfinae = void>
141 {
142 ALPAKA_FN_HOST_ACC constexpr auto operator()(TView const& view) const
143 {
145 }
146 };
147
148 //! The memory set task trait.
149 //!
150 //! Fills the view with data.
151 template<typename TDim, typename TDev, typename TSfinae = void>
153
154 template<typename TDim, typename TDev, typename TSfinae = void>
156
157 //! The memory copy task trait.
158 //!
159 //! Copies memory from one view into another view possibly on a different device.
160 template<typename TDim, typename TDevDst, typename TDevSrc, typename TSfinae = void>
162
163 //! The device memory view creation trait.
164 template<typename TDev, typename TSfinae = void>
166
167 //! The sub view creation trait.
168 template<typename TDev, typename TSfinae = void>
169 struct CreateSubView;
170 } // namespace trait
171
172 //! Gets the native pointer of the memory view.
173 //!
174 //! \param view The memory view.
175 //! \return The native pointer.
176 template<typename TView>
177 ALPAKA_FN_HOST auto getPtrNative(TView const& view) -> Elem<TView> const*
178 {
180 }
181
182 //! Gets the native pointer of the memory view.
183 //!
184 //! \param view The memory view.
185 //! \return The native pointer.
186 template<typename TView>
188 {
190 }
191
192 //! Gets the pointer to the view on the given device.
193 //!
194 //! \param view The memory view.
195 //! \param dev The device.
196 //! \return The pointer on the device.
197 template<typename TView, typename TDev>
198 ALPAKA_FN_HOST auto getPtrDev(TView const& view, TDev const& dev) -> Elem<TView> const*
199 {
201 }
202
203 //! Gets the pointer to the view on the given device.
204 //!
205 //! \param view The memory view.
206 //! \param dev The device.
207 //! \return The pointer on the device.
208 template<typename TView, typename TDev>
209 ALPAKA_FN_HOST auto getPtrDev(TView& view, TDev const& dev) -> Elem<TView>*
210 {
212 }
213
214 //! \return The pitch in bytes. This is the distance in bytes between two consecutive elements in the given
215 //! dimension.
216 template<std::size_t Tidx, typename TView>
217 [[deprecated("Use getPitchesInBytes instead")]] ALPAKA_FN_HOST auto getPitchBytes(TView const& view) -> Idx<TView>
218 {
219#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
220# pragma GCC diagnostic push
221# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
222#endif
224#if ALPAKA_COMP_CLANG || ALPAKA_COMP_GNUC
225# pragma GCC diagnostic pop
226#endif
227 }
228
229 //! \return The pitches in bytes as an alpaka::Vec. This is the distance in bytes between two consecutive elements
230 //! in the given dimension.
231 //! E.g. for a 3D view without padding, the 0-dim pitch is the distance in bytes to jump from one element to the
232 //! next within the same row, the 1-dim pitch (aka. the row pitch) is the distance in bytes to jump from one
233 //! element to the neighboring element on the next row. The 2-dim pitch (aka. the slice pitch) is the distance in
234 //! bytes to jump from one element to the neighboring element on the next slice.
235 //! E.g. a 3D view of floats without padding and the extents {42, 10, 2}, would have a pitch vector of {80, 8, 4}.
236 template<typename TView>
238 {
239 return trait::GetPitchesInBytes<TView>{}(view);
240 }
241
242 //! Create a memory set task.
243 //!
244 //! \param view The memory view to fill.
245 //! \param byte Value to set for each element of the specified view.
246 //! \param extent The extent of the view to fill.
247 template<typename TExtent, typename TViewFwd>
248 ALPAKA_FN_HOST auto createTaskMemset(TViewFwd&& view, std::uint8_t const& byte, TExtent const& extent)
249 {
250 using TView = std::remove_reference_t<TViewFwd>;
251 static_assert(!std::is_const_v<TView>, "The view must not be const!");
252 static_assert(
254 "The view and the extent are required to have the same dimensionality!");
255
256 assert((extent <= getExtents(view)).all() && "The memset extent must not be larger than the view's extent!");
257
259 std::forward<TViewFwd>(view),
260 byte,
261 extent);
262 }
263
264 template<typename TExtent, typename TViewFwd, typename TValue>
265 ALPAKA_FN_HOST auto createTaskFill(TViewFwd&& view, TValue const& value, TExtent const& extent)
266 {
267 using TView = std::remove_reference_t<TViewFwd>;
268 static_assert(!std::is_const_v<TView>, "The view must not be const!");
269 static_assert(
271 "The view and the extent are required to have the same dimensionality!");
272
273 assert((extent <= getExtents(view)).all() && "The fill extent must not be larger than the view's extent!");
274
276 std::forward<TViewFwd>(view),
277 value,
278 extent);
279 }
280
281 //! Sets the bytes of the memory of view, described by extent, to the given value.
282 //!
283 //! \param queue The queue to enqueue the view fill task into.
284 //! \param[in,out] view The memory view to fill. May be a temporary object.
285 //! \param byte Value to set for each element of the specified view.
286 //! \param extent The extent of the view to fill.
287 template<typename TExtent, typename TViewFwd, typename TQueue>
288 ALPAKA_FN_HOST auto memset(TQueue& queue, TViewFwd&& view, std::uint8_t const& byte, TExtent const& extent) -> void
289 {
290 enqueue(queue, createTaskMemset(std::forward<TViewFwd>(view), byte, extent));
291 }
292
293 //! Sets each byte of the memory of the entire view to the given value.
294 //!
295 //! \param queue The queue to enqueue the view fill task into.
296 //! \param[in,out] view The memory view to fill. May be a temporary object.
297 //! \param byte Value to set for each element of the specified view.
298 template<typename TViewFwd, typename TQueue>
299 ALPAKA_FN_HOST auto memset(TQueue& queue, TViewFwd&& view, std::uint8_t const& byte) -> void
300 {
301 enqueue(queue, createTaskMemset(std::forward<TViewFwd>(view), byte, getExtents(view)));
302 }
303
304 template<typename TViewFwd, typename TValue, typename TQueue>
305 ALPAKA_FN_HOST auto fill(TQueue& queue, TViewFwd&& view, TValue const& value) -> void
306 {
307 enqueue(queue, createTaskFill(std::forward<TViewFwd>(view), value, getExtents(view)));
308 }
309
310 template<typename TExtent, typename TViewFwd, typename TValue, typename TQueue>
311 ALPAKA_FN_HOST auto fill(TQueue& queue, TViewFwd&& view, TValue const& value, TExtent const& extent) -> void
312 {
313 enqueue(queue, createTaskFill(std::forward<TViewFwd>(view), value, extent));
314 }
315
316 //! Creates a memory copy task.
317 //!
318 //! \param viewDst The destination memory view.
319 //! \param viewSrc The source memory view.
320 //! \param extent The extent of the view to copy.
321 template<typename TExtent, typename TViewSrc, typename TViewDstFwd>
322 ALPAKA_FN_HOST auto createTaskMemcpy(TViewDstFwd&& viewDst, TViewSrc const& viewSrc, TExtent const& extent)
323 {
324 using TViewDst = std::remove_reference_t<TViewDstFwd>;
325 using SrcElem = Elem<TViewSrc>;
326 using DstElem = Elem<TViewDst>;
327
328 static_assert(!std::is_const_v<TViewDst>, "The destination view must not be const!");
329 static_assert(!std::is_const_v<DstElem>, "The destination view's element type must not be const!");
330 static_assert(
332 "The source and the destination view must have the same dimensionality!");
333 static_assert(
335 "The destination view and the extent must have the same dimensionality!");
336 static_assert(
337 std::is_same_v<DstElem, std::remove_const_t<SrcElem>>,
338 "The source and destination view must have the same element type!");
339
340 // The extent and the views may use different index types; compare them in a type that can
341 // represent all of the values involved, to avoid both compilation errors and truncation.
342 [[maybe_unused]] auto const extents = getExtents(extent);
343 using SrcCommon [[maybe_unused]] = std::common_type_t<Idx<TExtent>, Idx<TViewSrc>>;
344 using DstCommon [[maybe_unused]] = std::common_type_t<Idx<TExtent>, Idx<TViewDst>>;
345 assert(
346 (castVec<SrcCommon>(extents) <= castVec<SrcCommon>(getExtents(viewSrc))).all()
347 && "The memcpy extent must not be larger than the source view's extent!");
348 assert(
349 (castVec<DstCommon>(extents) <= castVec<DstCommon>(getExtents(viewDst))).all()
350 && "The memcpy extent must not be larger than the destination view's extent!");
351
353 std::forward<TViewDstFwd>(viewDst),
354 viewSrc,
355 extent);
356 }
357
358 //! Copies memory from a part of viewSrc to viewDst, described by extent. Possibly copies between different memory
359 //! spaces.
360 //!
361 //! \param queue The queue to enqueue the view copy task into.
362 //! \param[in,out] viewDst The destination memory view. May be a temporary object.
363 //! \param viewSrc The source memory view. May be a temporary object.
364 //! \param extent The extent of the view to copy.
365 template<typename TExtent, typename TViewSrc, typename TViewDstFwd, typename TQueue>
366 ALPAKA_FN_HOST auto memcpy(TQueue& queue, TViewDstFwd&& viewDst, TViewSrc const& viewSrc, TExtent const& extent)
367 -> void
368 {
369 enqueue(queue, createTaskMemcpy(std::forward<TViewDstFwd>(viewDst), viewSrc, extent));
370 }
371
372 //! Copies the entire memory of viewSrc to viewDst. Possibly copies between different memory
373 //! spaces.
374 //!
375 //! \param queue The queue to enqueue the view copy task into.
376 //! \param[in,out] viewDst The destination memory view. May be a temporary object.
377 //! \param viewSrc The source memory view. May be a temporary object.
378 template<typename TViewSrc, typename TViewDstFwd, typename TQueue>
379 ALPAKA_FN_HOST auto memcpy(TQueue& queue, TViewDstFwd&& viewDst, TViewSrc const& viewSrc) -> void
380 {
381 enqueue(queue, createTaskMemcpy(std::forward<TViewDstFwd>(viewDst), viewSrc, getExtents(viewSrc)));
382 }
383
384 namespace concepts
385 {
386 template<typename T>
388 } // namespace concepts
389
390 namespace detail
391 {
392 template<typename TDim, typename TView>
393 struct Print
394 {
396 TView const& view,
397 Elem<TView> const* const ptr,
398 Vec<Dim<TView>, Idx<TView>> const& extent,
399 std::ostream& os,
400 std::string const& elementSeparator,
401 std::string const& rowSeparator,
402 std::string const& rowPrefix,
403 std::string const& rowSuffix) -> void
404 {
405 os << rowPrefix;
406
407 auto const pitch = getPitchesInBytes(view)[TDim::value + 1];
408 auto const lastIdx(extent[TDim::value] - 1u);
409 for(auto i(decltype(lastIdx)(0)); i <= lastIdx; ++i)
410 {
412 view,
413 reinterpret_cast<Elem<TView> const*>(reinterpret_cast<std::uint8_t const*>(ptr) + i * pitch),
414 extent,
415 os,
416 elementSeparator,
417 rowSeparator,
418 rowPrefix,
419 rowSuffix);
420
421 // While we are not at the end of a row, add the row separator.
422 if(i != lastIdx)
423 {
424 os << rowSeparator;
425 }
426 }
427
428 os << rowSuffix;
429 }
430 };
431
432 template<typename TView>
433 struct Print<DimInt<Dim<TView>::value - 1u>, TView>
434 {
436 TView const& /* view */,
437 Elem<TView> const* const ptr,
438 Vec<Dim<TView>, Idx<TView>> const& extent,
439 std::ostream& os,
440 std::string const& elementSeparator,
441 std::string const& /* rowSeparator */,
442 std::string const& rowPrefix,
443 std::string const& rowSuffix) -> void
444 {
445 os << rowPrefix;
446
447 auto const lastIdx(extent[Dim<TView>::value - 1u] - 1u);
448 for(auto i(decltype(lastIdx)(0)); i <= lastIdx; ++i)
449 {
450 // Add the current element.
451 os << *(ptr + i);
452
453 // While we are not at the end of a line, add the element separator.
454 if(i != lastIdx)
455 {
456 os << elementSeparator;
457 }
458 }
459
460 os << rowSuffix;
461 }
462 };
463
464 template<typename TDeviceProvider>
465 auto getDeviceFromProvider(TDeviceProvider const& provider)
466 {
468 {
469 return provider;
470 }
471 else
472 {
473 return alpaka::getDev(provider);
474 }
475 }
476
477 } // namespace detail
478
479 //! Prints the content of the view to the given queue.
480 // \TODO: Add precision flag.
481 // \TODO: Add column alignment flag.
482 template<typename TView>
484 TView const& view,
485 std::ostream& os,
486 std::string const& elementSeparator = ", ",
487 std::string const& rowSeparator = "\n",
488 std::string const& rowPrefix = "[",
489 std::string const& rowSuffix = "]") -> void
490 {
492 view,
493 getPtrNative(view),
494 getExtents(view),
495 os,
496 elementSeparator,
497 rowSeparator,
498 rowPrefix,
499 rowSuffix);
500 }
501
502 //! \return The pitch vector.
503 template<typename TView>
504 [[deprecated("Use getPitchesInBytes instead")]] auto getPitchBytesVec(TView const& view)
506 {
507 return getPitchesInBytes(view);
508 }
509
510 //! \return The pitch but only the last N elements.
511 template<typename TDim, typename TView>
512 ALPAKA_FN_HOST auto getPitchBytesVecEnd(TView const& view = TView()) -> Vec<TDim, Idx<TView>>
513 {
515 }
516
517 //! Creates a view to a device pointer
518 //!
519 //! \param dev Object from which the device can be obtained.
520 //! \param pMem Pointer to memory. The pointer must be accessible from the given device.
521 //! \param extent Number of elements represented by the pMem.
522 //! Using a multi dimensional extent will result in a multi dimension view to the memory represented
523 //! by pMem.
524 //! \return A view to device memory.
525 template<concepts::DeviceProvider TDev, typename TElem, typename TExtent>
526 auto createView(TDev const& dev, TElem* pMem, TExtent const& extent)
527 {
530 auto const extentVec = Vec<Dim, Idx>(extent);
531 auto device = detail::getDeviceFromProvider(dev);
532 return trait::CreateViewPlainPtr<decltype(device)>::createViewPlainPtr(
533 device,
534 pMem,
535 extentVec,
537 }
538
539 //! Creates a view to a device pointer
540 //!
541 //! \param dev Object from which the device can be obtained.
542 //! \param pMem Pointer to memory. The pointer must be accessible from the given device.
543 //! \param extent Number of elements represented by the pMem.
544 //! Using a multi dimensional extent will result in a multi dimension view to the memory represented
545 //! by pMem.
546 //! \param pitch Pitch in bytes for each dimension. Dimensionality must be equal to extent.
547 //! \return A view to device memory.
548 template<concepts::DeviceProvider TDev, typename TElem, typename TExtent, typename TPitch>
549 auto createView(TDev const& dev, TElem* pMem, TExtent const& extent, TPitch pitch)
550 {
551 auto device = detail::getDeviceFromProvider(dev);
552 return trait::CreateViewPlainPtr<decltype(device)>::createViewPlainPtr(device, pMem, extent, pitch);
553 }
554
555 //! Creates a view to a contiguous container of device-accessible memory.
556 //!
557 //! \param dev Object from which the device can be obtained.
558 //! \param con Contiguous container. The container must provide a `data()` method. The data held by the container
559 //! must be accessible from the given device. The `GetExtent` trait must be defined for the container.
560 //! \return A view to device memory.
561 template<concepts::DeviceProvider TDev, typename TContainer>
562 auto createView(TDev const& dev, TContainer& con)
563 {
564 auto const device = detail::getDeviceFromProvider(dev);
565 return createView(device, std::data(con), getExtents(con));
566 }
567
568 //! Creates a view to a contiguous container of device-accessible memory.
569 //!
570 //! \param dev Object from which the device can be obtained.
571 //! \param con Contiguous container. The container must provide a `data()` method. The data held by the container
572 //! must be accessible from the given device. The `GetExtent` trait must be defined for the container.
573 //! \param extent Number of elements held by the container. Using a multi-dimensional extent will result in a
574 //! multi-dimensional view to the memory represented by the container.
575 //! \return A view to device memory.
576 template<concepts::DeviceProvider TDev, typename TContainer, typename TExtent>
577 auto createView(TDev const& dev, TContainer& con, TExtent const& extent)
578 {
579 auto const device = detail::getDeviceFromProvider(dev);
580 return createView(device, std::data(con), extent);
581 }
582
583 //! Creates a sub view to an existing view.
584 //!
585 //! \param view The view this view is a sub-view of.
586 //! \param extent Number of elements the resulting view holds.
587 //! \param offset Number of elements skipped in view for the new origin of the resulting view.
588 //! \return A sub view to a existing view.
589 template<typename TView, typename TExtent, typename TOffsets>
590 auto createSubView(TView& view, TExtent const& extent, TOffsets const& offset = TExtent())
591 {
593 }
594
595#ifdef ALPAKA_USE_MDSPAN
596 namespace experimental
597 {
598 namespace traits
599 {
600 namespace detail
601 {
602 template<typename ElementType>
603 struct ByteIndexedAccessor
604 {
605 using offset_policy = ByteIndexedAccessor;
606 using element_type = ElementType;
607 using reference = ElementType&;
608
609 using data_handle_type
610 = std::conditional_t<std::is_const_v<ElementType>, std::byte const*, std::byte*>;
611
612 constexpr ByteIndexedAccessor() noexcept = default;
613
614 ALPAKA_FN_HOST_ACC constexpr data_handle_type offset(data_handle_type p, size_t i) const noexcept
615 {
616 return p + i;
617 }
618
619 ALPAKA_FN_HOST_ACC constexpr reference access(data_handle_type p, size_t i) const noexcept
620 {
621 assert(i % alignof(ElementType) == 0);
622 return *reinterpret_cast<ElementType*>(__builtin_assume_aligned(p + i, alignof(ElementType)));
623 }
624 };
625
626 template<typename TView, std::size_t... Is>
627 ALPAKA_FN_HOST auto makeExtents(TView const& view, std::index_sequence<Is...>)
628 {
629 auto const ex = getExtents(view);
630 return dextents<Idx<TView>, Dim<TView>::value>{ex[Is]...};
631 }
632 } // namespace detail
633
634 //! Customization point for getting an mdspan from a view.
635 template<typename TView, typename TSfinae = void>
636 struct GetMdSpan
637 {
638 ALPAKA_FN_HOST static auto getMdSpan(TView& view)
639 {
640 constexpr auto dim = Dim<TView>::value;
641 using Element = Elem<TView>;
642 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
643 auto* ptr = reinterpret_cast<std::byte*>(getPtrNative(view));
644 auto const strides = toArray(getPitchesInBytes(view));
645 layout_stride::mapping<decltype(extents)> m{extents, strides};
646 return mdspan<Element, decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
647 ptr,
648 m};
649 }
650
651 ALPAKA_FN_HOST static auto getMdSpanTransposed(TView& view)
652 {
653 constexpr auto dim = Dim<TView>::value;
654 using Element = Elem<TView>;
655 auto extents = detail::makeExtents(view, std::make_index_sequence<dim>{});
656 auto* ptr = reinterpret_cast<std::byte*>(getPtrNative(view));
657 auto strides = toArray(getPitchesInBytes(view));
658 std::reverse(begin(strides), end(strides));
659 layout_stride::mapping<decltype(extents)> m{extents, strides};
660 return mdspan<Element, decltype(extents), layout_stride, detail::ByteIndexedAccessor<Element>>{
661 ptr,
662 m};
663 }
664 };
665 } // namespace traits
666
667 //! Gets a std::mdspan from the given view. The memory layout is determined by the pitches of the view.
668 template<typename TView>
669 ALPAKA_FN_HOST auto getMdSpan(TView& view)
670 {
671 return traits::GetMdSpan<TView>::getMdSpan(view);
672 }
673
674 //! Gets a std::mdspan from the given view. The memory layout is determined by the reversed pitches of the
675 //! view. This effectively also reverses the extents of the view. In order words, if you create a transposed
676 //! mdspan on a 10x5 element view, the mdspan will have an iteration space of 5x10.
677 template<typename TView>
678 ALPAKA_FN_HOST auto getMdSpanTransposed(TView& view)
679 {
680 return traits::GetMdSpan<TView>::getMdSpanTransposed(view);
681 }
682
683 template<typename TElem, typename TIdx, typename TDim>
684 using MdSpan = alpaka::experimental::mdspan<
685 TElem,
686 alpaka::experimental::dextents<TIdx, TDim::value>,
687 alpaka::experimental::layout_stride,
688 alpaka::experimental::traits::detail::ByteIndexedAccessor<TElem>>;
689 } // namespace experimental
690#endif
691} // 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:465
ALPAKA_FN_HOST_ACC constexpr auto calculatePitchesFromExtents(Vec< TDim, TIdx > const &extent)
Calculate the pitches purely from the extents.
Definition Traits.hpp:35
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:56
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.
ALPAKA_FN_HOST auto memcpy(TQueue &queue, alpaka::detail::DevGlobalImplGeneric< TTag, TTypeDst > &viewDst, TViewSrc const &viewSrc) -> void
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_FN_HOST auto getPitchBytesVecEnd(TView const &view=TView()) -> Vec< TDim, Idx< TView > >
Definition Traits.hpp:512
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:538
ALPAKA_FN_HOST auto getPitchesInBytes(TView const &view) -> Vec< Dim< TView >, Idx< TView > >
Definition Traits.hpp:237
auto getPitchBytesVec(TView const &view) -> Vec< Dim< TView >, Idx< TView > >
Definition Traits.hpp:504
ALPAKA_FN_HOST auto createTaskMemcpy(TViewDstFwd &&viewDst, TViewSrc const &viewSrc, TExtent const &extent)
Creates a memory copy task.
Definition Traits.hpp:322
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC constexpr auto castVec(TVec const &vec)
Definition Traits.hpp:82
auto createSubView(TView &view, TExtent const &extent, TOffsets const &offset=TExtent())
Creates a sub view to an existing view.
Definition Traits.hpp:590
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC constexpr auto subVecEnd(TVec const &vec)
Definition Traits.hpp:66
ALPAKA_FN_HOST auto createTaskFill(TViewFwd &&view, TValue const &value, TExtent const &extent)
Definition Traits.hpp:265
typename trait::IdxType< T >::type Idx
Definition Traits.hpp:29
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:483
ALPAKA_FN_HOST auto getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
Definition Traits.hpp:177
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto getExtents(T const &object) -> Vec< Dim< T >, Idx< T > >
Definition Traits.hpp:59
typename trait::DevType< T >::type Dev
The device type trait alias template to remove the ::type.
Definition Traits.hpp:56
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:288
ALPAKA_FN_HOST auto fill(TQueue &queue, TViewFwd &&view, TValue const &value) -> void
Definition Traits.hpp:305
ALPAKA_FN_HOST auto getDev(T const &t)
Definition Traits.hpp:75
ALPAKA_FN_HOST auto enqueue(TQueue &queue, TTask &&task) -> void
Queues the given task in the given queue.
Definition Traits.hpp:54
std::integral_constant< std::size_t, N > DimInt
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:198
ALPAKA_FN_HOST auto createTaskMemset(TViewFwd &&view, std::uint8_t const &byte, TExtent const &extent)
Create a memory set task.
Definition Traits.hpp:248
ALPAKA_FN_HOST auto getPitchBytes(TView const &view) -> Idx< TView >
Definition Traits.hpp:217
auto createView(TDev const &dev, TElem *pMem, TExtent const &extent)
Creates a view to a device pointer.
Definition Traits.hpp:526
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:435
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:395
The sub view creation trait.
The memory copy task trait.
Definition Traits.hpp:161
The memory set task trait.
Definition Traits.hpp:152
The device memory view creation trait.
Definition Traits.hpp:165
The pitch in bytes. This is the distance in bytes in the linear memory between two consecutive elemen...
Definition Traits.hpp:105
static ALPAKA_FN_HOST auto getPitchBytes(TView const &view) -> ViewIdx
Definition Traits.hpp:108
Customization point for getPitchesInBytes. The default implementation uses the extent to calculate th...
Definition Traits.hpp:141
ALPAKA_FN_HOST_ACC constexpr auto operator()(TView const &view) const
Definition Traits.hpp:142
The pointer on device get trait.
Definition Traits.hpp:96
The native pointer get trait.
Definition Traits.hpp:92