Debian Codegraph @

packages/kokkos/tpls/mdspan/include/experimental/__p2630_bits/submdspan_extents.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #pragma once
18
19 #include <complex>
20
21 #include "strided_slice.hpp"
22 #include "../__p0009_bits/utility.hpp"
23
24 namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
25 namespace detail {
26
27 // Mapping from submapping ranks to srcmapping ranks
28 // InvMapRank is an index_sequence, which we build recursively
29 // to contain the mapped indices.
30 // end of recursion specialization containing the final index_sequence
31 template <size_t Counter, size_t... MapIdxs>
32 MDSPAN_INLINE_FUNCTION
33 constexpr auto inv_map_rank(std::integral_constant<size_t, Counter>, std::index_sequence<MapIdxs...>) {
34 return std::index_sequence<MapIdxs...>();
35 }
36
37 // specialization reducing rank by one (i.e., integral slice specifier)
38 template<size_t Counter, class Slice, class... SliceSpecifiers, size_t... MapIdxs>
39 MDSPAN_INLINE_FUNCTION
40 constexpr auto inv_map_rank(std::integral_constant<size_t, Counter>, std::index_sequence<MapIdxs...>, Slice,
41 SliceSpecifiers... slices) {
42 using next_idx_seq_t = std::conditional_t<std::is_convertible_v<Slice, size_t>,
43 std::index_sequence<MapIdxs...>,
44 std::index_sequence<MapIdxs..., Counter>>;
45
46 return inv_map_rank(std::integral_constant<size_t,Counter + 1>(), next_idx_seq_t(),
47 slices...);
48 }
49
50 // Helper for identifying strided_slice
51 template <class T> struct is_strided_slice : std::false_type {};
52
53 template <class OffsetType, class ExtentType, class StrideType>
54 struct is_strided_slice<
55 strided_slice<OffsetType, ExtentType, StrideType>> : std::true_type {};
56
57 // Helper for identifying valid pair like things
58 template <class T, class IndexType> struct index_pair_like : std::false_type {};
59
60 template <class IdxT1, class IdxT2, class IndexType>
61 struct index_pair_like<std::pair<IdxT1, IdxT2>, IndexType> {
62 static constexpr bool value = std::is_convertible_v<IdxT1, IndexType> &&
63 std::is_convertible_v<IdxT2, IndexType>;
64 };
65
66 template <class IdxT1, class IdxT2, class IndexType>
67 struct index_pair_like<std::tuple<IdxT1, IdxT2>, IndexType> {
68 static constexpr bool value = std::is_convertible_v<IdxT1, IndexType> &&
69 std::is_convertible_v<IdxT2, IndexType>;
70 };
71
72 template <class IdxT1, class IdxT2, class IndexType>
73 struct index_pair_like<tuple<IdxT1, IdxT2>, IndexType> {
74 static constexpr bool value = std::is_convertible_v<IdxT1, IndexType> &&
75 std::is_convertible_v<IdxT2, IndexType>;
76 };
77
78 template <class IdxT, class IndexType>
79 struct index_pair_like<std::complex<IdxT>, IndexType> {
80 static constexpr bool value = std::is_convertible_v<IdxT, IndexType>;
81 };
82
83 template <class IdxT, class IndexType>
84 struct index_pair_like<std::array<IdxT, 2>, IndexType> {
85 static constexpr bool value = std::is_convertible_v<IdxT, IndexType>;
86 };
87
88 // first_of(slice): getting begin of slice specifier range
89 MDSPAN_TEMPLATE_REQUIRES(
90 class Integral,
91 /* requires */(std::is_convertible_v<Integral, size_t>)
92 )
93 MDSPAN_INLINE_FUNCTION
94 constexpr Integral first_of(const Integral &i) {
95 return i;
96 }
97
98 template<class Integral, Integral v>
99 MDSPAN_INLINE_FUNCTION
100 constexpr Integral first_of(const std::integral_constant<Integral, v>&) {
101 return integral_constant<Integral, v>();
102 }
103
104 MDSPAN_INLINE_FUNCTION
105 constexpr integral_constant<size_t, 0>
106 first_of(const ::MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent_t &) {
107 return integral_constant<size_t, 0>();
108 }
109
110 MDSPAN_TEMPLATE_REQUIRES(
111 class Slice,
112 /* requires */(index_pair_like<Slice, size_t>::value)
113 )
114 MDSPAN_INLINE_FUNCTION
115 constexpr auto first_of(const Slice &i) {
116 return get<0>(i);
117 }
118
119 MDSPAN_TEMPLATE_REQUIRES(
120 class IdxT1, class IdxT2,
121 /* requires */ (index_pair_like<std::tuple<IdxT1, IdxT2>, size_t>::value)
122 )
123 constexpr auto first_of(const std::tuple<IdxT1, IdxT2>& i) {
124 return get<0>(i);
125 }
126
127 MDSPAN_TEMPLATE_REQUIRES(
128 class IdxT1, class IdxT2,
129 /* requires */ (index_pair_like<std::pair<IdxT1, IdxT2>, size_t>::value)
130 )
131 MDSPAN_INLINE_FUNCTION
132 constexpr auto first_of(const std::pair<IdxT1, IdxT2>& i) {
133 return i.first;
134 }
135
136 template<class T>
137 MDSPAN_INLINE_FUNCTION
138 constexpr auto first_of(const std::complex<T> &i) {
139 return i.real();
140 }
141
142 template <class OffsetType, class ExtentType, class StrideType>
143 MDSPAN_INLINE_FUNCTION
144 constexpr OffsetType
145 first_of(const strided_slice<OffsetType, ExtentType, StrideType> &r) {
146 return r.offset;
147 }
148
149 // last_of(slice): getting end of slice specifier range
150 // We need however not just the slice but also the extents
151 // of the original view and which rank from the extents.
152 // This is needed in the case of slice being full_extent_t.
153 MDSPAN_TEMPLATE_REQUIRES(
154 size_t k, class Extents, class Integral,
155 /* requires */(std::is_convertible_v<Integral, size_t>)
156 )
157 MDSPAN_INLINE_FUNCTION
158 constexpr Integral
159 last_of(std::integral_constant<size_t, k>, const Extents &, const Integral &i) {
160 return i;
161 }
162
163 MDSPAN_TEMPLATE_REQUIRES(
164 size_t k, class Extents, class Slice,
165 /* requires */(index_pair_like<Slice, size_t>::value)
166 )
167 MDSPAN_INLINE_FUNCTION
168 constexpr auto last_of(std::integral_constant<size_t, k>, const Extents &,
169 const Slice &i) {
170 return get<1>(i);
171 }
172
173 MDSPAN_TEMPLATE_REQUIRES(
174 size_t k, class Extents, class IdxT1, class IdxT2,
175 /* requires */ (index_pair_like<std::tuple<IdxT1, IdxT2>, size_t>::value)
176 )
177 constexpr auto last_of(std::integral_constant<size_t, k>, const Extents &, const std::tuple<IdxT1, IdxT2>& i) {
178 return get<1>(i);
179 }
180
181 MDSPAN_TEMPLATE_REQUIRES(
182 size_t k, class Extents, class IdxT1, class IdxT2,
183 /* requires */ (index_pair_like<std::pair<IdxT1, IdxT2>, size_t>::value)
184 )
185 MDSPAN_INLINE_FUNCTION
186 constexpr auto last_of(std::integral_constant<size_t, k>, const Extents &, const std::pair<IdxT1, IdxT2>& i) {
187 return i.second;
188 }
189
190 template<size_t k, class Extents, class T>
191 MDSPAN_INLINE_FUNCTION
192 constexpr auto last_of(std::integral_constant<size_t, k>, const Extents &, const std::complex<T> &i) {
193 return i.imag();
194 }
195
196 // Suppress spurious warning with NVCC about no return statement.
197 // This is a known issue in NVCC and NVC++
198 // Depending on the CUDA and GCC version we need both the builtin
199 // and the diagnostic push. I tried really hard to find something shorter
200 // but no luck ...
201 #if defined __NVCC__
202 #ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
203 #pragma nv_diagnostic push
204 #pragma nv_diag_suppress = implicit_return_from_non_void_function
205 #else
206 #ifdef __CUDA_ARCH__
207 #pragma diagnostic push
208 #pragma diag_suppress implicit_return_from_non_void_function
209 #endif
210 #endif
211 #elif defined __NVCOMPILER
212 #pragma diagnostic push
213 #pragma diag_suppress = implicit_return_from_non_void_function
214 #endif
215 template <size_t k, class Extents>
216 MDSPAN_INLINE_FUNCTION
217 constexpr auto last_of(std::integral_constant<size_t, k>, const Extents &ext,
218 ::MDSPAN_IMPL_STANDARD_NAMESPACE::full_extent_t) {
219 if constexpr (Extents::static_extent(k) == dynamic_extent) {
220 return ext.extent(k);
221 } else {
222 return integral_constant<size_t, Extents::static_extent(k)>();
223 }
224 #if defined(__NVCC__) && !defined(__CUDA_ARCH__) && defined(__GNUC__)
225 // Even with CUDA_ARCH protection this thing warns about calling host function
226 __builtin_unreachable();
227 #endif
228 }
229 #if defined __NVCC__
230 #ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
231 #pragma nv_diagnostic pop
232 #else
233 #ifdef __CUDA_ARCH__
234 #pragma diagnostic pop
235 #endif
236 #endif
237 #elif defined __NVCOMPILER
238 #pragma diagnostic pop
239 #endif
240
241 template <size_t k, class Extents, class OffsetType, class ExtentType,
242 class StrideType>
243 MDSPAN_INLINE_FUNCTION
244 constexpr OffsetType
245 last_of(std::integral_constant<size_t, k>, const Extents &,
246 const strided_slice<OffsetType, ExtentType, StrideType> &r) {
247 return r.extent;
248 }
249
250 // get stride of slices
251 template <class T>
252 MDSPAN_INLINE_FUNCTION
253 constexpr auto stride_of(const T &) {
254 return integral_constant<size_t, 1>();
255 }
256
257 template <class OffsetType, class ExtentType, class StrideType>
258 MDSPAN_INLINE_FUNCTION
259 constexpr auto
260 stride_of(const strided_slice<OffsetType, ExtentType, StrideType> &r) {
261 return r.stride;
262 }
263
264 // divide which can deal with integral constant preservation
265 template <class IndexT, class T0, class T1>
266 MDSPAN_INLINE_FUNCTION
267 constexpr auto divide(const T0 &v0, const T1 &v1) {
268 return IndexT(v0) / IndexT(v1);
269 }
270
271 template <class IndexT, class T0, T0 v0, class T1, T1 v1>
272 MDSPAN_INLINE_FUNCTION
273 constexpr auto divide(const std::integral_constant<T0, v0> &,
274 const std::integral_constant<T1, v1> &) {
275 // cutting short division by zero
276 // this is used for strided_slice with zero extent/stride
277 return integral_constant<IndexT, v0 == 0 ? 0 : v0 / v1>();
278 }
279
280 // multiply which can deal with integral constant preservation
281 template <class IndexT, class T0, class T1>
282 MDSPAN_INLINE_FUNCTION
283 constexpr auto multiply(const T0 &v0, const T1 &v1) {
284 return IndexT(v0) * IndexT(v1);
285 }
286
287 template <class IndexT, class T0, T0 v0, class T1, T1 v1>
288 MDSPAN_INLINE_FUNCTION
289 constexpr auto multiply(const std::integral_constant<T0, v0> &,
290 const std::integral_constant<T1, v1> &) {
291 return integral_constant<IndexT, v0 * v1>();
292 }
293
294 // compute new static extent from range, preserving static knowledge
295 template <class Arg0, class Arg1> struct StaticExtentFromRange {
296 constexpr static size_t value = dynamic_extent;
297 };
298
299 template <class Integral0, Integral0 val0, class Integral1, Integral1 val1>
300 struct StaticExtentFromRange<std::integral_constant<Integral0, val0>,
301 std::integral_constant<Integral1, val1>> {
302 constexpr static size_t value = val1 - val0;
303 };
304
305 template <class Integral0, Integral0 val0, class Integral1, Integral1 val1>
306 struct StaticExtentFromRange<integral_constant<Integral0, val0>,
307 integral_constant<Integral1, val1>> {
308 constexpr static size_t value = val1 - val0;
309 };
310
311 // compute new static extent from strided_slice, preserving static
312 // knowledge
313 template <class Arg0, class Arg1> struct StaticExtentFromStridedRange {
314 constexpr static size_t value = dynamic_extent;
315 };
316
317 template <class Integral0, Integral0 val0, class Integral1, Integral1 val1>
318 struct StaticExtentFromStridedRange<std::integral_constant<Integral0, val0>,
319 std::integral_constant<Integral1, val1>> {
320 constexpr static size_t value = val0 > 0 ? 1 + (val0 - 1) / val1 : 0;
321 };
322
323 template <class Integral0, Integral0 val0, class Integral1, Integral1 val1>
324 struct StaticExtentFromStridedRange<integral_constant<Integral0, val0>,
325 integral_constant<Integral1, val1>> {
326 constexpr static size_t value = val0 > 0 ? 1 + (val0 - 1) / val1 : 0;
327 };
328
329 // creates new extents through recursive calls to next_extent member function
330 // next_extent has different overloads for different types of stride specifiers
331 template <size_t K, class Extents, size_t... NewExtents>
332 struct extents_constructor {
333 MDSPAN_TEMPLATE_REQUIRES(
334 class Slice, class... SlicesAndExtents,
335 /* requires */(!std::is_convertible_v<Slice, size_t> &&
336 !is_strided_slice<Slice>::value)
337 )
338 MDSPAN_INLINE_FUNCTION
339 constexpr static auto next_extent(const Extents &ext, const Slice &sl,
340 SlicesAndExtents... slices_and_extents) {
341 constexpr size_t new_static_extent = StaticExtentFromRange<
342 decltype(first_of(std::declval<Slice>())),
343 decltype(last_of(std::integral_constant<size_t, Extents::rank() - K>(),
344 std::declval<Extents>(),
345 std::declval<Slice>()))>::value;
346
347 using next_t =
348 extents_constructor<K - 1, Extents, NewExtents..., new_static_extent>;
349 using index_t = typename Extents::index_type;
350 return next_t::next_extent(
351 ext, slices_and_extents...,
352 index_t(last_of(std::integral_constant<size_t, Extents::rank() - K>(), ext,
353 sl)) -
354 index_t(first_of(sl)));
355 }
356
357 MDSPAN_TEMPLATE_REQUIRES(
358 class Slice, class... SlicesAndExtents,
359 /* requires */ (std::is_convertible_v<Slice, size_t>)
360 )
361 MDSPAN_INLINE_FUNCTION
362 constexpr static auto next_extent(const Extents &ext, const Slice &,
363 SlicesAndExtents... slices_and_extents) {
364 using next_t = extents_constructor<K - 1, Extents, NewExtents...>;
365 return next_t::next_extent(ext, slices_and_extents...);
366 }
367
368 template <class OffsetType, class ExtentType, class StrideType,
369 class... SlicesAndExtents>
370 MDSPAN_INLINE_FUNCTION
371 constexpr static auto
372 next_extent(const Extents &ext,
373 const strided_slice<OffsetType, ExtentType, StrideType> &r,
374 SlicesAndExtents... slices_and_extents) {
375 using index_t = typename Extents::index_type;
376 using new_static_extent_t =
377 StaticExtentFromStridedRange<ExtentType, StrideType>;
378 if constexpr (new_static_extent_t::value == dynamic_extent) {
379 using next_t =
380 extents_constructor<K - 1, Extents, NewExtents..., dynamic_extent>;
381 return next_t::next_extent(
382 ext, slices_and_extents...,
383 r.extent > 0 ? 1 + divide<index_t>(r.extent - 1, r.stride) : 0);
384 } else {
385 constexpr size_t new_static_extent = new_static_extent_t::value;
386 using next_t =
387 extents_constructor<K - 1, Extents, NewExtents..., new_static_extent>;
388 return next_t::next_extent(
389 ext, slices_and_extents..., index_t(divide<index_t>(ExtentType(), StrideType())));
390 }
391 }
392 };
393
394 template <class Extents, size_t... NewStaticExtents>
395 struct extents_constructor<0, Extents, NewStaticExtents...> {
396
397 template <class... NewExtents>
398 MDSPAN_INLINE_FUNCTION
399 constexpr static auto next_extent(const Extents &, NewExtents... new_exts) {
400 return extents<typename Extents::index_type, NewStaticExtents...>(
401 new_exts...);
402 }
403 };
404
405 } // namespace detail
406
407 // submdspan_extents creates new extents given src extents and submdspan slice
408 // specifiers
409 template <class IndexType, size_t... Extents, class... SliceSpecifiers>
410 MDSPAN_INLINE_FUNCTION
411 constexpr auto submdspan_extents(const extents<IndexType, Extents...> &src_exts,
412 SliceSpecifiers... slices) {
413
414 using ext_t = extents<IndexType, Extents...>;
415 return detail::extents_constructor<ext_t::rank(), ext_t>::next_extent(
416 src_exts, slices...);
417 }
418 } // namespace MDSPAN_IMPL_STANDARD_NAMESPACE