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