alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
LinearizedIdxGenerator.hpp
Go to the documentation of this file.
1/* Copyright 2025 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/Simd.hpp"
11
12namespace alpaka
13{
14 /** Generate a linearized scalar index.
15 *
16 * The generator behaves like an n-dimensional data container, but it is not pointing to any memory.
17 * The index to access the generator is linearized based on the provided extents.
18 */
19 template<typename T_IndexType, uint32_t T_dim>
21 {
23 {
24 }
25
26 using value_type = T_IndexType;
28
29 static consteval uint32_t dim()
30 {
31 return T_dim;
32 }
33
34 /** Get alignment of the generators value_type */
35 static constexpr auto getAlignment()
36 {
37 return alpaka::Alignment<alignof(value_type)>{};
38 }
39
40 /** Get value at the given index
41 *
42 * @param idx n-dimensional offset, range [0, extents)
43 * @return linearized index
44 */
46 {
47 return linearize(m_extents, idx);
48 }
49
50 /** Get value at the given index
51 *
52 * @param idx n-dimensional offset, range [0, extents)
53 * @return linearized index
54 */
56 {
57 return linearize(m_extents, idx);
58 }
59
60 /** Get value at the given index
61 *
62 * @param idx n-dimensional offset, range [0, extents)
63 * @return linearized index
64 */
65 constexpr value_type operator[](std::integral auto const& idx) const requires(dim() == 1u)
66 {
67 return idx;
68 }
69
70 /** Get value at the given index
71 *
72 * @param idx n-dimensional offset, range [0, extents)
73 * @return linearized index
74 */
75 constexpr value_type operator[](std::integral auto const& idx) requires(dim() == 1u)
76 {
77 return idx;
78 }
79
80 /** supported index range
81 *
82 * @return virtual extents of the generator
83 */
84 constexpr auto getExtents() const
85 {
86 return m_extents;
87 }
88
89 constexpr auto getPitches() const
90 {
92 }
93
94 [[nodiscard]] constexpr explicit operator bool() const noexcept
95 {
96 return true;
97 }
98
99 private:
101 };
102
103 template<concepts::VectorOrScalar T_Extents>
106
107 namespace internal
108 {
109 /** Add support to use the generator with SimdPtr. */
110 template<
111 typename T_IndexType,
112 uint32_t T_dim,
113 alpaka::concepts::Alignment T_MdSpanAlignment,
115 struct LoadAsSimd::Op<LinearizedIdxGenerator<T_IndexType, T_dim>, T_MdSpanAlignment, T_Idx>
116 {
117 template<uint32_t T_simdWidth>
118 constexpr auto load(auto&& linearIdxGenerator, T_MdSpanAlignment alignment, T_Idx const& idx) const
119 {
120 alpaka::unused(alignment);
121 static_assert(
122 std::is_same_v<LinearizedIdxGenerator<T_IndexType, T_dim>, ALPAKA_TYPEOF(linearIdxGenerator)>,
123 "Type of linearIdxGenerator must match the class template signature.");
124 using DataTypeType = std::remove_reference_t<decltype(linearIdxGenerator[idx])>;
125 using DstType = std::conditional_t<
126 std::is_const_v<DataTypeType>,
127 Simd<std::decay_t<DataTypeType>, T_simdWidth> const,
128 Simd<std::decay_t<DataTypeType>, T_simdWidth>>;
129
130 return DstType(
131 [&](auto i)
132 {
133 // rAssign() is used because, SIMD vectors can only be loaded from the fast moving index
134 return linearIdxGenerator[idx + T_Idx::fill(0).rAssign(i)];
135 });
136 }
137 };
138 } // namespace internal
139} // namespace alpaka
#define ALPAKA_FN_HOST_ACC
All functions that can be used on an accelerator have to be attributed with ALPAKA_FN_ACC or ALPAKA_F...
Definition common.hpp:31
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:153
Concept to check for an alignment object.
Definition Alignment.hpp:89
Check whether the specified type is a multidimensional index.
Definition IndexVec.hpp:19
Concept to check if a type is a vector or scalar variable.
Definition Vec.hpp:64
Concept to check if a type is a vector.
Definition Vec.hpp:53
alpaka internal implementations.
Definition generic.hpp:19
constexpr uint32_t getDim_v
Definition trait.hpp:41
main alpaka namespace.
Definition alpaka.hpp:76
constexpr auto calculatePitchesFromExtents(T_Vec const &extent)
Calculate the pitches purely from the extents.
constexpr T_IntegralType linearize(Vec< T_IntegralType, T_dim - 1u, T_Storage > const &dim, Vec< T_IntegralType, T_dim, T_OtherStorage > const &idx)
Give the linear index of an N-dimensional index within an N-dimensional index space.
Definition Vec.hpp:832
ALPAKA_FN_HOST_ACC LinearizedIdxGenerator(T_Extents const &) -> LinearizedIdxGenerator< trait::GetValueType_t< T_Extents >, trait::getDim_v< T_Extents > >
Strongly typed and constexpr representation of a byte-alignment of memory.
Definition Alignment.hpp:26
Generate a linearized scalar index.
constexpr value_type operator[](std::integral auto const &idx)
Get value at the given index.
constexpr auto getExtents() const
supported index range
constexpr value_type operator[](alpaka::concepts::IndexVec< T_IndexType, T_dim > auto const &idx) const
Get value at the given index.
constexpr LinearizedIdxGenerator(alpaka::concepts::VectorOrScalar auto const &size)
constexpr value_type operator[](alpaka::concepts::IndexVec< T_IndexType, T_dim > auto const &idx)
Get value at the given index.
constexpr value_type operator[](std::integral auto const &idx) const
Get value at the given index.
alpaka::Vec< T_IndexType, T_dim > m_extents
static constexpr auto getAlignment()
Get alignment of the generators value_type.
Simd vector.
Definition Simd.hpp:78
constexpr auto load(auto &&linearIdxGenerator, T_MdSpanAlignment alignment, T_Idx const &idx) const