alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
MdSpanArray.hpp
Go to the documentation of this file.
1/* Copyright 2025 René Widera, Simeon Ehrig
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/CVec.hpp"
8#include "alpaka/Vec.hpp"
15#include "alpaka/mem/trait.hpp"
16#include "alpaka/trait.hpp"
17#include "concepts/IndexVec.hpp"
18
19#include <concepts>
20#include <type_traits>
21
22namespace alpaka
23{
24 /** access a C array with compile time extents via a runtime md index. */
25 template<std::integral auto T_numDims, uint32_t T_dim = 0u>
27 {
28 constexpr decltype(auto) operator()(auto arrayPtr, concepts::Vector auto const& idx) const
29 {
30 return ResolveArrayAccess<T_numDims - 1u, T_dim + 1u>{}(arrayPtr[idx[T_dim]], idx);
31 }
32 };
33
34 template<uint32_t T_dim>
35 struct ResolveArrayAccess<1u, T_dim>
36 {
37 constexpr decltype(auto) operator()(auto arrayPtr, concepts::Vector auto const& idx) const
38 {
39 return arrayPtr[idx[T_dim]];
40 }
41 };
42
43 /** build C array type with compile time extents from a scalar value based on the compile time extents vector */
44 template<typename T, concepts::CVector T_Extents, uint32_t T_numDims = T_Extents::dim(), uint32_t T_dim = 0u>
46 {
47 using type =
48 typename CArrayType<T[T_Extents{}[T_numDims - T_dim - 1u]], T_Extents, T_numDims - 1u, T_dim + 1u>::type;
49 };
50
51 template<typename T, concepts::CVector T_Extents, uint32_t T_dim>
52 struct CArrayType<T, T_Extents, 1u, T_dim>
53 {
54 using type = T[T_Extents{}[0u]];
55 };
56
57 template<typename T_ArrayType, std::integral T_IndexType, concepts::Alignment T_MemAlignment = Alignment<>>
59 {
60 static_assert(
61 sizeof(T_ArrayType) && false,
62 "MdSpanArray can only be used if std::is_array_v<T> is true for the given type.");
63 };
64
65 template<alpaka::concepts::CStaticArray T_ArrayType, std::integral T_IndexType, concepts::Alignment T_MemAlignment>
66 struct MdSpanArray<T_ArrayType, T_IndexType, T_MemAlignment>
67 {
68 private:
69 using MutArrayType = std::remove_cv_t<T_ArrayType>;
70 using ConstArrayType = std::add_const_t<MutArrayType>;
71
72 public:
73 using value_type = std::remove_all_extents_t<T_ArrayType>;
77 using const_pointer = value_type const*;
78 using index_type = T_IndexType;
79
80 static consteval uint32_t dim()
81 {
82 return std::rank_v<T_ArrayType>;
83 }
84
85 /** return value the origin pointer is pointing to
86 *
87 * @return value at the current location
88 */
89 constexpr const_reference operator*() const
90 {
91 return *this->data();
92 }
93
94 constexpr reference operator*()
95 {
96 return *this->data();
97 }
98
99 /** get origin pointer */
100 constexpr const_pointer data() const
101 {
102 return reinterpret_cast<const_pointer>(this->m_ptr);
103 }
104
105 constexpr pointer data()
106 {
107 return reinterpret_cast<pointer>(this->m_ptr);
108 }
109
110 constexpr auto begin() const
111 {
112 return MdForwardIter{this->getConstMdSpan()};
113 }
114
115 constexpr auto begin()
116 {
117 return MdForwardIter{*this};
118 }
119
120 constexpr auto end() const
121 {
122 return MdForwardIterEnd{this->getConstMdSpan()};
123 }
124
125 constexpr auto end()
126 {
127 return MdForwardIterEnd{*this};
128 }
129
130 constexpr auto getConstMdSpan() const
131 {
133 }
134
135 constexpr auto cbegin() const
136 {
137 return MdForwardIter{this->getConstMdSpan()};
138 }
139
140 constexpr auto cend() const
141 {
142 return MdForwardIterEnd{this->getConstMdSpan()};
143 }
144
145 /*Object must init by copy a valid instance*/
146 constexpr MdSpanArray() = default;
147
148 /** Constructor
149 *
150 * @param pointer pointer to the memory
151 */
152 constexpr MdSpanArray(T_ArrayType& staticSizedArray) : m_ptr(const_cast<MutArrayType*>(&staticSizedArray))
153 {
154 }
155
156 template<alpaka::concepts::CStaticArray T_OtherArrayType>
162
163 constexpr MdSpanArray(MdSpanArray const&) = default;
164
165 /** Assignment operator keeping const-ness
166 *
167 * alpaka keeps track that const references to a span can not be cast to non const.
168 * If the inner value type is not const the second assignment operator with non const will be used.
169 */
170 constexpr MdSpanArray& operator=(MdSpanArray const& other)
171 requires(std::is_const_v<typename ALPAKA_TYPEOF(other)::value_type>)
172 = default;
173 constexpr MdSpanArray& operator=(MdSpanArray&) = default;
174
175 constexpr MdSpanArray(MdSpanArray&&) = default;
176
177 template<alpaka::concepts::CStaticArray T_OtherArrayType>
182
183 constexpr MdSpanArray& operator=(MdSpanArray&&) = default;
184
185 static constexpr auto getAlignment()
186 {
187 return T_MemAlignment{};
188 }
189
190 /** get value at the given index
191 *
192 * @param idx offset relative to the origin pointer
193 * @return reference to the value
194 * @{
195 */
197 // cannot use dim() or std::rank_v<T_ArrayType> because the cause a segmentation fault in nvcc
198 concepts::IndexVec<index_type, std::rank<T_ArrayType>::value> auto const& idx) const
199 {
200 return ResolveArrayAccess<dim()>{}(*m_ptr, idx);
201 }
202
204 // cannot use dim() or std::rank_v<T_ArrayType> because the cause a segmentation fault in nvcc
205 concepts::IndexVec<index_type, std::rank<T_ArrayType>::value> auto const& idx)
206 {
207 return ResolveArrayAccess<dim()>{}(*m_ptr, idx);
208 }
209
210 constexpr const_reference operator[](index_type const& idx) const
211 {
212 return (*m_ptr)[idx];
213 }
214
215 constexpr reference operator[](index_type const& idx)
216 {
217 return (*m_ptr)[idx];
218 }
219
220 constexpr bool operator==(MdSpanArray const other) const
221 {
222 return m_ptr == other.m_ptr;
223 }
224
225 /** @} */
226
227 constexpr auto getExtents() const
228 {
229 // uint32_t is the data type of dim()
230 auto const createExtents = []<uint32_t... T_extent>(std::integer_sequence<uint32_t, T_extent...>)
232 return createExtents(std::make_integer_sequence<uint32_t, dim()>{});
233 }
234
235 constexpr auto getPitches() const
236 {
238 }
239
240 /** True if MdSpanArray is pointing to valid memory.
241 *
242 * @details
243 * An MdSpanArray remains valid even after being moved. The reason is, that it use stack memory which cannot be
244 * freed.
245 */
246 [[nodiscard]] constexpr explicit operator bool() const noexcept
247 {
248 return true;
249 }
250
251 // Needs to be friend of itself with that the copy and move constructor can access the m_ptr of other, if the
252 // const modifier of the C static array type of the other type is different.
255
256 protected:
257 // we store the C static array as mutable type that we can assign it another MdSpanArray with const or
258 // non-const inner type
259 // Depending on the value_type, the const is added at memory access
261 };
262
263 template<
265 std::integral T_IndexType,
266 alpaka::concepts::Alignment T_MemAlignment>
267 struct internal::CopyConstructableDataSource<MdSpanArray<T_ArrayType, T_IndexType, T_MemAlignment>>
268 : std::true_type
269 {
270 using InnerMutable = MdSpanArray<std::remove_const_t<T_ArrayType>, T_IndexType, T_MemAlignment>;
271 using InnerConst = MdSpanArray<std::add_const_t<T_ArrayType>, T_IndexType, T_MemAlignment>;
272 };
273} // namespace alpaka
special implementation to define the end
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:153
Concept to check for an alignment object.
Definition Alignment.hpp:89
Concept to check if the given type is a C static array.
Definition types.hpp:14
Check whether the specified type is a multidimensional index.
Definition IndexVec.hpp:19
Concept to check if a type is a vector.
Definition Vec.hpp:53
Concept to restrict copy or move constructor of a DataSource which creates a new object with a differ...
main alpaka namespace.
Definition alpaka.hpp:76
constexpr auto calculatePitchesFromExtents(T_Vec const &extent)
Calculate the pitches purely from the extents.
Vec< T, sizeof...(T_values), detail::CVec< T, T_values... > > CVec
A vector with compile-time known values.
Definition CVec.hpp:31
build C array type with compile time extents from a scalar value based on the compile time extents ve...
typename CArrayType< T[T_Extents{}[T_numDims - T_dim - 1u]], T_Extents, T_numDims - 1u, T_dim+1u >::type type
constexpr const_reference operator*() const
return value the origin pointer is pointing to
constexpr MdSpanArray(MdSpanArray< T_OtherArrayType, T_IndexType, T_MemAlignment > &&other)
constexpr MdSpanArray & operator=(MdSpanArray &)=default
constexpr const_reference operator[](concepts::IndexVec< index_type, std::rank< T_ArrayType >::value > auto const &idx) const
get value at the given index
constexpr reference operator[](concepts::IndexVec< index_type, std::rank< T_ArrayType >::value > auto const &idx)
get value at the given index
constexpr MdSpanArray & operator=(MdSpanArray &&)=default
constexpr const_reference operator[](index_type const &idx) const
get value at the given index
constexpr MdSpanArray(MdSpanArray< T_OtherArrayType, T_IndexType, T_MemAlignment > const &other)
constexpr const_pointer data() const
get origin pointer
constexpr MdSpanArray(T_ArrayType &staticSizedArray)
Constructor.
constexpr bool operator==(MdSpanArray const other) const
get value at the given index
constexpr MdSpanArray & operator=(MdSpanArray const &other)=default
Assignment operator keeping const-ness.
std::remove_all_extents_t< T_ArrayType > value_type
constexpr reference operator[](index_type const &idx)
get value at the given index
access a C array with compile time extents via a runtime md index.
MdSpanArray< std::add_const_t< T_ArrayType >, T_IndexType, T_MemAlignment > InnerConst
MdSpanArray< std::remove_const_t< T_ArrayType >, T_IndexType, T_MemAlignment > InnerMutable
Specialize the trait for DataSource class if the object is copyable.
Definition trait.hpp:67