Debian Codegraph @

packages/kokkos/tpls/mdspan/include/experimental/__p0009_bits/layout_left.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 #pragma once
17
18 #include "macros.hpp"
19 #include "trait_backports.hpp"
20 #include "extents.hpp"
21 #include "layout_stride.hpp"
22 #include "utility.hpp"
23 #if MDSPAN_HAS_CXX_17
24 #include "../__p2642_bits/layout_padded_fwd.hpp"
25 #endif
26 #include <type_traits>
27
28 namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
29
30 //==============================================================================
31
32 template <class Extents>
33 class layout_left::mapping {
34 public:
35 using extents_type = Extents;
36 using index_type = typename extents_type::index_type;
37 using size_type = typename extents_type::size_type;
38 using rank_type = typename extents_type::rank_type;
39 using layout_type = layout_left;
40 private:
41
42 static_assert(detail::__is_extents_v<extents_type>,
43 MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::layout_left::mapping must be instantiated with a specialization of " MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::extents.");
44
45 template <class>
46 friend class mapping;
47
48 // i0+(i1 + E(1)*(i2 + E(2)*i3))
49 template <size_t r, size_t Rank>
50 struct __rank_count {};
51
52 template <size_t r, size_t Rank, class I, class... Indices>
53 _MDSPAN_HOST_DEVICE
54 constexpr index_type __compute_offset(
55 __rank_count<r,Rank>, const I& i, Indices... idx) const {
56 return __compute_offset(__rank_count<r+1,Rank>(), idx...) *
57 __extents.extent(r) + i;
58 }
59
60 template<class I>
61 _MDSPAN_HOST_DEVICE
62 constexpr index_type __compute_offset(
63 __rank_count<extents_type::rank()-1,extents_type::rank()>, const I& i) const {
64 return i;
65 }
66
67 _MDSPAN_HOST_DEVICE
68 constexpr index_type __compute_offset(__rank_count<0,0>) const { return 0; }
69
70 public:
71
72 //--------------------------------------------------------------------------------
73
74 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mapping() noexcept = default;
75 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mapping(mapping const&) noexcept = default;
76
77 _MDSPAN_HOST_DEVICE
78 constexpr mapping(extents_type const& __exts) noexcept
79 :__extents(__exts)
80 { }
81
82 MDSPAN_TEMPLATE_REQUIRES(
83 class OtherExtents,
84 /* requires */ (
85 _MDSPAN_TRAIT(std::is_constructible, extents_type, OtherExtents)
86 )
87 )
88 MDSPAN_CONDITIONAL_EXPLICIT((!std::is_convertible<OtherExtents, extents_type>::value)) // needs two () due to comma
89 MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14
90 mapping(mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor)
91 :__extents(other.extents())
92 {
93 /*
94 * TODO: check precondition
95 * other.required_span_size() is a representable value of type index_type
96 */
97 }
98
99 MDSPAN_TEMPLATE_REQUIRES(
100 class OtherExtents,
101 /* requires */ (
102 _MDSPAN_TRAIT(std::is_constructible, extents_type, OtherExtents) &&
103 (extents_type::rank() <= 1)
104 )
105 )
106 MDSPAN_CONDITIONAL_EXPLICIT((!std::is_convertible<OtherExtents, extents_type>::value)) // needs two () due to comma
107 MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14
108 mapping(layout_right::mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor)
109 :__extents(other.extents())
110 {
111 /*
112 * TODO: check precondition
113 * other.required_span_size() is a representable value of type index_type
114 */
115 }
116
117 #if MDSPAN_HAS_CXX_17
118 /**
119 * Converting constructor from `layout_left_padded::mapping`.
120 *
121 * This overload participates in overload resolution only if _Mapping is a layout_left_padded mapping and
122 * extents_type is constructible from _Mapping::extents_type.
123 *
124 * \note There is currently a difference from p2642r2, where this function is specified as taking
125 * `layout_left_padded< padding_value >::mapping< Extents>`. However, this makes `padding_value` non-deducible.
126 */
127 MDSPAN_TEMPLATE_REQUIRES(
128 class _Mapping,
129 /* requires */ (
130 MDSPAN_IMPL_PROPOSED_NAMESPACE::detail::is_layout_left_padded_mapping<_Mapping>::value
131 && std::is_constructible_v<extents_type, typename _Mapping::extents_type>
132 )
133 )
134 MDSPAN_CONDITIONAL_EXPLICIT((!std::is_convertible_v<typename _Mapping::extents_type, extents_type>))
135 mapping(const _Mapping& __other) noexcept
136 : __extents(__other.extents())
137 {
138 MDSPAN_IMPL_PROPOSED_NAMESPACE::detail::
139 check_padded_layout_converting_constructor_mandates<
140 extents_type, _Mapping>(detail::with_rank<extents_type::rank()>{});
141 MDSPAN_IMPL_PROPOSED_NAMESPACE::detail::
142 check_padded_layout_converting_constructor_preconditions<
143 extents_type>(detail::with_rank<extents_type::rank()>{}, __other);
144 }
145 #endif
146
147 MDSPAN_TEMPLATE_REQUIRES(
148 class OtherExtents,
149 /* requires */ (
150 _MDSPAN_TRAIT(std::is_constructible, extents_type, OtherExtents)
151 )
152 )
153 MDSPAN_CONDITIONAL_EXPLICIT((extents_type::rank() > 0))
154 MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14
155 mapping(layout_stride::mapping<OtherExtents> const& other) noexcept // NOLINT(google-explicit-constructor)
156 :__extents(other.extents())
157 {
158 /*
159 * TODO: check precondition
160 * other.required_span_size() is a representable value of type index_type
161 */
162 detail::validate_strides(detail::with_rank<extents_type::rank()>{}, layout_left{}, __extents, other);
163 }
164
165 MDSPAN_INLINE_FUNCTION_DEFAULTED _MDSPAN_CONSTEXPR_14_DEFAULTED mapping& operator=(mapping const&) noexcept = default;
166
167 MDSPAN_INLINE_FUNCTION
168 constexpr const extents_type& extents() const noexcept {
169 return __extents;
170 }
171
172 MDSPAN_INLINE_FUNCTION
173 constexpr index_type required_span_size() const noexcept {
174 index_type value = 1;
175 for(rank_type r=0; r<extents_type::rank(); r++) value*=__extents.extent(r);
176 return value;
177 }
178
179 //--------------------------------------------------------------------------------
180
181 MDSPAN_TEMPLATE_REQUIRES(
182 class... Indices,
183 /* requires */ (
184 (sizeof...(Indices) == extents_type::rank()) &&
185 (detail::are_valid_indices<index_type, Indices...>())
186 )
187 )
188 _MDSPAN_HOST_DEVICE
189 constexpr index_type operator()(Indices... idxs) const noexcept {
190 #if ! defined(NDEBUG)
191 detail::check_all_indices(this->extents(), idxs...);
192 #endif // ! NDEBUG
193 return __compute_offset(__rank_count<0, extents_type::rank()>(), static_cast<index_type>(idxs)...);
194 }
195
196
197
198 MDSPAN_INLINE_FUNCTION static constexpr bool is_always_unique() noexcept { return true; }
199 MDSPAN_INLINE_FUNCTION static constexpr bool is_always_exhaustive() noexcept { return true; }
200 MDSPAN_INLINE_FUNCTION static constexpr bool is_always_strided() noexcept { return true; }
201
202 MDSPAN_INLINE_FUNCTION static constexpr bool is_unique() noexcept { return true; }
203 MDSPAN_INLINE_FUNCTION static constexpr bool is_exhaustive() noexcept { return true; }
204 MDSPAN_INLINE_FUNCTION static constexpr bool is_strided() noexcept { return true; }
205
206 MDSPAN_INLINE_FUNCTION
207 constexpr index_type stride(rank_type i) const noexcept
208 #if MDSPAN_HAS_CXX_20
209 requires ( Extents::rank() > 0 )
210 #endif
211 {
212 index_type value = 1;
213 for(rank_type r=0; r<i; r++) value*=__extents.extent(r);
214 return value;
215 }
216
217 MDSPAN_TEMPLATE_REQUIRES(
218 class OtherExtents,
219 /* requires */ ( Extents::rank() == OtherExtents::rank())
220 )
221 MDSPAN_INLINE_FUNCTION
222 friend constexpr bool operator==(mapping const& lhs, mapping<OtherExtents> const& rhs) noexcept {
223 return lhs.extents() == rhs.extents();
224 }
225
226 // In C++ 20 the not equal exists if equal is found
227 #if !(MDSPAN_HAS_CXX_20)
228 MDSPAN_TEMPLATE_REQUIRES(
229 class OtherExtents,
230 /* requires */ ( Extents::rank() == OtherExtents::rank())
231 )
232 MDSPAN_INLINE_FUNCTION
233 friend constexpr bool operator!=(mapping const& lhs, mapping<OtherExtents> const& rhs) noexcept {
234 return lhs.extents() != rhs.extents();
235 }
236 #endif
237
238 // Not really public, but currently needed to implement fully constexpr useable submdspan:
239 template<size_t N, class SizeType, size_t ... E, size_t ... Idx>
240 MDSPAN_INLINE_FUNCTION
241 constexpr index_type __get_stride(MDSPAN_IMPL_STANDARD_NAMESPACE::extents<SizeType, E...>,std::integer_sequence<size_t, Idx...>) const {
242 return _MDSPAN_FOLD_TIMES_RIGHT((Idx<N? __extents.template __extent<Idx>():1),1);
243 }
244 template<size_t N>
245 MDSPAN_INLINE_FUNCTION
246 constexpr index_type __stride() const noexcept {
247 return __get_stride<N>(__extents, std::make_index_sequence<extents_type::rank()>());
248 }
249
250 private:
251 _MDSPAN_NO_UNIQUE_ADDRESS extents_type __extents{};
252
253 // [mdspan.submdspan.mapping], submdspan mapping specialization
254 template<class... SliceSpecifiers>
255 MDSPAN_INLINE_FUNCTION
256 constexpr auto submdspan_mapping_impl(
257 SliceSpecifiers... slices) const;
258
259 template<class... SliceSpecifiers>
260 MDSPAN_INLINE_FUNCTION
261 friend constexpr auto submdspan_mapping(
262 const mapping& src, SliceSpecifiers... slices) {
263 return src.submdspan_mapping_impl(slices...);
264 }
265 };
266
267
268 } // end namespace MDSPAN_IMPL_STANDARD_NAMESPACE
269