Debian Codegraph @

packages/kokkos/tpls/mdspan/include/experimental/__p0009_bits/mdspan.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 "default_accessor.hpp"
20 #include "layout_right.hpp"
21 #include "extents.hpp"
22 #include "trait_backports.hpp"
23 #include "compressed_pair.hpp"
24
25 namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
26 template <
27 class ElementType,
28 class Extents,
29 class LayoutPolicy = layout_right,
30 class AccessorPolicy = default_accessor<ElementType>
31 >
32 class mdspan
33 {
34 private:
35 static_assert(detail::__is_extents_v<Extents>,
36 MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::mdspan's Extents template parameter must be a specialization of " MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::extents.");
37 static_assert(std::is_same<ElementType, typename AccessorPolicy::element_type>::value,
38 MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::mdspan's ElementType template parameter must be the same as its AccessorPolicy::element_type.");
39
40 // Workaround for non-deducibility of the index sequence template parameter if it's given at the top level
41 template <class>
42 struct __deduction_workaround;
43
44 template <size_t... Idxs>
45 struct __deduction_workaround<std::index_sequence<Idxs...>>
46 {
47 MDSPAN_FORCE_INLINE_FUNCTION static constexpr
48 size_t __size(mdspan const& __self) noexcept {
49 return _MDSPAN_FOLD_TIMES_RIGHT((__self.__mapping_ref().extents().extent(Idxs)), /* * ... * */ size_t(1));
50 }
51 MDSPAN_FORCE_INLINE_FUNCTION static constexpr
52 bool __empty(mdspan const& __self) noexcept {
53 return (__self.rank()>0) && _MDSPAN_FOLD_OR((__self.__mapping_ref().extents().extent(Idxs)==index_type(0)));
54 }
55 template <class ReferenceType, class SizeType, size_t N>
56 MDSPAN_FORCE_INLINE_FUNCTION static constexpr
57 ReferenceType __callop(mdspan const& __self, const std::array<SizeType, N>& indices) noexcept {
58 return __self.__accessor_ref().access(__self.__ptr_ref(), __self.__mapping_ref()(indices[Idxs]...));
59 }
60 #ifdef __cpp_lib_span
61 template <class ReferenceType, class SizeType, size_t N>
62 MDSPAN_FORCE_INLINE_FUNCTION static constexpr
63 ReferenceType __callop(mdspan const& __self, const std::span<SizeType, N>& indices) noexcept {
64 return __self.__accessor_ref().access(__self.__ptr_ref(), __self.__mapping_ref()(indices[Idxs]...));
65 }
66 #endif
67 };
68
69 public:
70
71 //--------------------------------------------------------------------------------
72 // Domain and codomain types
73
74 using extents_type = Extents;
75 using layout_type = LayoutPolicy;
76 using accessor_type = AccessorPolicy;
77 using mapping_type = typename layout_type::template mapping<extents_type>;
78 using element_type = ElementType;
79 using value_type = std::remove_cv_t<element_type>;
80 using index_type = typename extents_type::index_type;
81 using size_type = typename extents_type::size_type;
82 using rank_type = typename extents_type::rank_type;
83 using data_handle_type = typename accessor_type::data_handle_type;
84 using reference = typename accessor_type::reference;
85
86 MDSPAN_INLINE_FUNCTION static constexpr size_t rank() noexcept { return extents_type::rank(); }
87 MDSPAN_INLINE_FUNCTION static constexpr size_t rank_dynamic() noexcept { return extents_type::rank_dynamic(); }
88 MDSPAN_INLINE_FUNCTION static constexpr size_t static_extent(size_t r) noexcept { return extents_type::static_extent(r); }
89 MDSPAN_INLINE_FUNCTION constexpr index_type extent(size_t r) const noexcept { return __mapping_ref().extents().extent(r); };
90
91 private:
92
93 // Can't use defaulted parameter in the __deduction_workaround template because of a bug in MSVC warning C4348.
94 using __impl = __deduction_workaround<std::make_index_sequence<extents_type::rank()>>;
95
96 using __map_acc_pair_t = detail::__compressed_pair<mapping_type, accessor_type>;
97
98 public:
99
100 //--------------------------------------------------------------------------------
101 // [mdspan.basic.cons], mdspan constructors, assignment, and destructor
102
103 #if !MDSPAN_HAS_CXX_20
104 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mdspan() = default;
105 #else
106 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mdspan()
107 requires(
108 // nvhpc has a bug where using just rank_dynamic() here doesn't work ...
109 (extents_type::rank_dynamic() > 0) &&
110 _MDSPAN_TRAIT(std::is_default_constructible, data_handle_type) &&
111 _MDSPAN_TRAIT(std::is_default_constructible, mapping_type) &&
112 _MDSPAN_TRAIT(std::is_default_constructible, accessor_type)
113 ) = default;
114 #endif
115 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mdspan(const mdspan&) = default;
116 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mdspan(mdspan&&) = default;
117
118 MDSPAN_TEMPLATE_REQUIRES(
119 class... SizeTypes,
120 /* requires */ (
121 ((sizeof...(SizeTypes) == rank()) || (sizeof...(SizeTypes) == rank_dynamic())) &&
122 (detail::are_valid_indices<index_type, SizeTypes...>()) &&
123 _MDSPAN_TRAIT(std::is_constructible, mapping_type, extents_type) &&
124 _MDSPAN_TRAIT(std::is_default_constructible, accessor_type)
125 )
126 )
127 MDSPAN_INLINE_FUNCTION
128 explicit constexpr mdspan(data_handle_type p, SizeTypes... dynamic_extents)
129 // TODO @proposal-bug shouldn't I be allowed to do `move(p)` here?
130 : __members(std::move(p), __map_acc_pair_t(mapping_type(extents_type(static_cast<index_type>(std::move(dynamic_extents))...)), accessor_type()))
131 { }
132
133 MDSPAN_TEMPLATE_REQUIRES(
134 class SizeType, size_t N,
135 /* requires */ (
136 _MDSPAN_TRAIT(std::is_convertible, const SizeType&, index_type) &&
137 _MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, const SizeType&) &&
138 ((N == rank()) || (N == rank_dynamic())) &&
139 _MDSPAN_TRAIT(std::is_constructible, mapping_type, extents_type) &&
140 _MDSPAN_TRAIT(std::is_default_constructible, accessor_type)
141 )
142 )
143 MDSPAN_CONDITIONAL_EXPLICIT(N != rank_dynamic())
144 MDSPAN_INLINE_FUNCTION
145 constexpr mdspan(data_handle_type p, const std::array<SizeType, N>& dynamic_extents)
146 : __members(std::move(p), __map_acc_pair_t(mapping_type(extents_type(dynamic_extents)), accessor_type()))
147 { }
148
149 #ifdef __cpp_lib_span
150 MDSPAN_TEMPLATE_REQUIRES(
151 class SizeType, size_t N,
152 /* requires */ (
153 _MDSPAN_TRAIT(std::is_convertible, const SizeType&, index_type) &&
154 _MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, const SizeType&) &&
155 ((N == rank()) || (N == rank_dynamic())) &&
156 _MDSPAN_TRAIT(std::is_constructible, mapping_type, extents_type) &&
157 _MDSPAN_TRAIT(std::is_default_constructible, accessor_type)
158 )
159 )
160 MDSPAN_CONDITIONAL_EXPLICIT(N != rank_dynamic())
161 MDSPAN_INLINE_FUNCTION
162 constexpr mdspan(data_handle_type p, std::span<SizeType, N> dynamic_extents)
163 : __members(std::move(p), __map_acc_pair_t(mapping_type(extents_type(as_const(dynamic_extents))), accessor_type()))
164 { }
165 #endif
166
167 MDSPAN_FUNCTION_REQUIRES(
168 (MDSPAN_INLINE_FUNCTION constexpr),
169 mdspan, (data_handle_type p, const extents_type& exts), ,
170 /* requires */ (_MDSPAN_TRAIT(std::is_default_constructible, accessor_type) &&
171 _MDSPAN_TRAIT(std::is_constructible, mapping_type, const extents_type&))
172 ) : __members(std::move(p), __map_acc_pair_t(mapping_type(exts), accessor_type()))
173 { }
174
175 MDSPAN_FUNCTION_REQUIRES(
176 (MDSPAN_INLINE_FUNCTION constexpr),
177 mdspan, (data_handle_type p, const mapping_type& m), ,
178 /* requires */ (_MDSPAN_TRAIT(std::is_default_constructible, accessor_type))
179 ) : __members(std::move(p), __map_acc_pair_t(m, accessor_type()))
180 { }
181
182 MDSPAN_INLINE_FUNCTION
183 constexpr mdspan(data_handle_type p, const mapping_type& m, const accessor_type& a)
184 : __members(std::move(p), __map_acc_pair_t(m, a))
185 { }
186
187 MDSPAN_TEMPLATE_REQUIRES(
188 class OtherElementType, class OtherExtents, class OtherLayoutPolicy, class OtherAccessor,
189 /* requires */ (
190 _MDSPAN_TRAIT(std::is_constructible, mapping_type, const typename OtherLayoutPolicy::template mapping<OtherExtents>&) &&
191 _MDSPAN_TRAIT(std::is_constructible, accessor_type, const OtherAccessor&)
192 )
193 )
194 MDSPAN_CONDITIONAL_EXPLICIT(
195 !_MDSPAN_TRAIT(std::is_convertible, const typename OtherLayoutPolicy::template mapping<OtherExtents>&, mapping_type) ||
196 !_MDSPAN_TRAIT(std::is_convertible, const OtherAccessor&, accessor_type)
197 )
198 MDSPAN_INLINE_FUNCTION
199 constexpr mdspan(const mdspan<OtherElementType, OtherExtents, OtherLayoutPolicy, OtherAccessor>& other)
200 : __members(other.__ptr_ref(), __map_acc_pair_t(other.__mapping_ref(), other.__accessor_ref()))
201 {
202 static_assert(_MDSPAN_TRAIT(std::is_constructible, data_handle_type, typename OtherAccessor::data_handle_type),"Incompatible data_handle_type for mdspan construction");
203 static_assert(_MDSPAN_TRAIT(std::is_constructible, extents_type, OtherExtents),"Incompatible extents for mdspan construction");
204 /*
205 * TODO: Check precondition
206 * For each rank index r of extents_type, static_extent(r) == dynamic_extent || static_extent(r) == other.extent(r) is true.
207 */
208 }
209
210 /* Might need this on NVIDIA?
211 MDSPAN_INLINE_FUNCTION_DEFAULTED
212 ~mdspan() = default;
213 */
214
215 MDSPAN_INLINE_FUNCTION_DEFAULTED _MDSPAN_CONSTEXPR_14_DEFAULTED mdspan& operator=(const mdspan&) = default;
216 MDSPAN_INLINE_FUNCTION_DEFAULTED _MDSPAN_CONSTEXPR_14_DEFAULTED mdspan& operator=(mdspan&&) = default;
217
218
219 //--------------------------------------------------------------------------------
220 // [mdspan.basic.mapping], mdspan mapping domain multidimensional index to access codomain element
221
222 #if MDSPAN_USE_BRACKET_OPERATOR
223 MDSPAN_TEMPLATE_REQUIRES(
224 class... SizeTypes,
225 /* requires */ (
226 _MDSPAN_FOLD_AND(_MDSPAN_TRAIT(std::is_convertible, SizeTypes, index_type) /* && ... */) &&
227 _MDSPAN_FOLD_AND(_MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, SizeTypes) /* && ... */) &&
228 (rank() == sizeof...(SizeTypes))
229 )
230 )
231 MDSPAN_FORCE_INLINE_FUNCTION
232 constexpr reference operator[](SizeTypes... indices) const
233 {
234 return __accessor_ref().access(__ptr_ref(), __mapping_ref()(static_cast<index_type>(std::move(indices))...));
235 }
236 #endif
237
238 MDSPAN_TEMPLATE_REQUIRES(
239 class SizeType,
240 /* requires */ (
241 _MDSPAN_TRAIT(std::is_convertible, const SizeType&, index_type) &&
242 _MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, const SizeType&)
243 )
244 )
245 MDSPAN_FORCE_INLINE_FUNCTION
246 constexpr reference operator[](const std::array< SizeType, rank()>& indices) const
247 {
248 return __impl::template __callop<reference>(*this, indices);
249 }
250
251 #ifdef __cpp_lib_span
252 MDSPAN_TEMPLATE_REQUIRES(
253 class SizeType,
254 /* requires */ (
255 _MDSPAN_TRAIT(std::is_convertible, const SizeType&, index_type) &&
256 _MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, const SizeType&)
257 )
258 )
259 MDSPAN_FORCE_INLINE_FUNCTION
260 constexpr reference operator[](std::span<SizeType, rank()> indices) const
261 {
262 return __impl::template __callop<reference>(*this, indices);
263 }
264 #endif // __cpp_lib_span
265
266 #if !MDSPAN_USE_BRACKET_OPERATOR
267 MDSPAN_TEMPLATE_REQUIRES(
268 class Index,
269 /* requires */ (
270 _MDSPAN_TRAIT(std::is_convertible, Index, index_type) &&
271 _MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, Index) &&
272 extents_type::rank() == 1
273 )
274 )
275 MDSPAN_FORCE_INLINE_FUNCTION
276 constexpr reference operator[](Index idx) const
277 {
278 return __accessor_ref().access(__ptr_ref(), __mapping_ref()(static_cast<index_type>(std::move(idx))));
279 }
280 #endif
281
282 #if MDSPAN_USE_PAREN_OPERATOR
283 MDSPAN_TEMPLATE_REQUIRES(
284 class... SizeTypes,
285 /* requires */ (
286 extents_type::rank() == sizeof...(SizeTypes) &&
287 (detail::are_valid_indices<index_type, SizeTypes...>())
288 )
289 )
290 MDSPAN_FORCE_INLINE_FUNCTION
291 constexpr reference operator()(SizeTypes... indices) const
292 {
293 return __accessor_ref().access(__ptr_ref(), __mapping_ref()(static_cast<index_type>(std::move(indices))...));
294 }
295
296 MDSPAN_TEMPLATE_REQUIRES(
297 class SizeType,
298 /* requires */ (
299 _MDSPAN_TRAIT(std::is_convertible, const SizeType&, index_type) &&
300 _MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, const SizeType&)
301 )
302 )
303 MDSPAN_FORCE_INLINE_FUNCTION
304 constexpr reference operator()(const std::array<SizeType, rank()>& indices) const
305 {
306 return __impl::template __callop<reference>(*this, indices);
307 }
308
309 #ifdef __cpp_lib_span
310 MDSPAN_TEMPLATE_REQUIRES(
311 class SizeType,
312 /* requires */ (
313 _MDSPAN_TRAIT(std::is_convertible, const SizeType&, index_type) &&
314 _MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, const SizeType&)
315 )
316 )
317 MDSPAN_FORCE_INLINE_FUNCTION
318 constexpr reference operator()(std::span<SizeType, rank()> indices) const
319 {
320 return __impl::template __callop<reference>(*this, indices);
321 }
322 #endif // __cpp_lib_span
323 #endif // MDSPAN_USE_PAREN_OPERATOR
324
325 MDSPAN_INLINE_FUNCTION constexpr size_type size() const noexcept {
326 return static_cast<size_type>(__impl::__size(*this));
327 };
328
329 MDSPAN_INLINE_FUNCTION constexpr bool empty() const noexcept {
330 return __impl::__empty(*this);
331 };
332
333 MDSPAN_INLINE_FUNCTION
334 friend constexpr void swap(mdspan& x, mdspan& y) noexcept {
335 // can't call the std::swap inside on HIP
336 #if !defined(_MDSPAN_HAS_HIP) && !defined(_MDSPAN_HAS_CUDA)
337 using std::swap;
338 swap(x.__ptr_ref(), y.__ptr_ref());
339 swap(x.__mapping_ref(), y.__mapping_ref());
340 swap(x.__accessor_ref(), y.__accessor_ref());
341 #else
342 mdspan tmp = y;
343 y = x;
344 x = tmp;
345 #endif
346 }
347
348 //--------------------------------------------------------------------------------
349 // [mdspan.basic.domobs], mdspan observers of the domain multidimensional index space
350
351
352 MDSPAN_INLINE_FUNCTION constexpr const extents_type& extents() const noexcept { return __mapping_ref().extents(); };
353 MDSPAN_INLINE_FUNCTION constexpr const data_handle_type& data_handle() const noexcept { return __ptr_ref(); };
354 MDSPAN_INLINE_FUNCTION constexpr const mapping_type& mapping() const noexcept { return __mapping_ref(); };
355 MDSPAN_INLINE_FUNCTION constexpr const accessor_type& accessor() const noexcept { return __accessor_ref(); };
356
357 //--------------------------------------------------------------------------------
358 // [mdspan.basic.obs], mdspan observers of the mapping
359
360 MDSPAN_INLINE_FUNCTION static constexpr bool is_always_unique() { return mapping_type::is_always_unique(); };
361 MDSPAN_INLINE_FUNCTION static constexpr bool is_always_exhaustive() { return mapping_type::is_always_exhaustive(); };
362 MDSPAN_INLINE_FUNCTION static constexpr bool is_always_strided() { return mapping_type::is_always_strided(); };
363
364 MDSPAN_INLINE_FUNCTION constexpr bool is_unique() const { return __mapping_ref().is_unique(); };
365 MDSPAN_INLINE_FUNCTION constexpr bool is_exhaustive() const { return __mapping_ref().is_exhaustive(); };
366 MDSPAN_INLINE_FUNCTION constexpr bool is_strided() const { return __mapping_ref().is_strided(); };
367 MDSPAN_INLINE_FUNCTION constexpr index_type stride(size_t r) const { return __mapping_ref().stride(r); };
368
369 private:
370
371 detail::__compressed_pair<data_handle_type, __map_acc_pair_t> __members{};
372
373 MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 data_handle_type& __ptr_ref() noexcept { return __members.__first(); }
374 MDSPAN_FORCE_INLINE_FUNCTION constexpr data_handle_type const& __ptr_ref() const noexcept { return __members.__first(); }
375 MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 mapping_type& __mapping_ref() noexcept { return __members.__second().__first(); }
376 MDSPAN_FORCE_INLINE_FUNCTION constexpr mapping_type const& __mapping_ref() const noexcept { return __members.__second().__first(); }
377 MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 accessor_type& __accessor_ref() noexcept { return __members.__second().__second(); }
378 MDSPAN_FORCE_INLINE_FUNCTION constexpr accessor_type const& __accessor_ref() const noexcept { return __members.__second().__second(); }
379
380 template <class, class, class, class>
381 friend class mdspan;
382
383 };
384
385 #if defined(_MDSPAN_USE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION)
386 MDSPAN_TEMPLATE_REQUIRES(
387 class ElementType, class... SizeTypes,
388 /* requires */ _MDSPAN_FOLD_AND(_MDSPAN_TRAIT(std::is_convertible, SizeTypes, size_t) /* && ... */) &&
389 (sizeof...(SizeTypes) > 0)
390 )
391 MDSPAN_DEDUCTION_GUIDE explicit mdspan(ElementType*, SizeTypes...)
392 -> mdspan<ElementType, ::MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<size_t, sizeof...(SizeTypes)>>;
393
394 MDSPAN_TEMPLATE_REQUIRES(
395 class Pointer,
396 (_MDSPAN_TRAIT(std::is_pointer, std::remove_reference_t<Pointer>))
397 )
398 MDSPAN_DEDUCTION_GUIDE mdspan(Pointer&&) -> mdspan<std::remove_pointer_t<std::remove_reference_t<Pointer>>, extents<size_t>>;
399
400 MDSPAN_TEMPLATE_REQUIRES(
401 class CArray,
402 (_MDSPAN_TRAIT(std::is_array, CArray) && (std::rank_v<CArray> == 1))
403 )
404 MDSPAN_DEDUCTION_GUIDE mdspan(CArray&) -> mdspan<std::remove_all_extents_t<CArray>, extents<size_t, ::std::extent_v<CArray,0>>>;
405
406 template <class ElementType, class SizeType, size_t N>
407 MDSPAN_DEDUCTION_GUIDE mdspan(ElementType*, const ::std::array<SizeType, N>&)
408 -> mdspan<ElementType, ::MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<size_t, N>>;
409
410 #ifdef __cpp_lib_span
411 template <class ElementType, class SizeType, size_t N>
412 MDSPAN_DEDUCTION_GUIDE mdspan(ElementType*, ::std::span<SizeType, N>)
413 -> mdspan<ElementType, ::MDSPAN_IMPL_STANDARD_NAMESPACE::dextents<size_t, N>>;
414 #endif
415
416 // This one is necessary because all the constructors take `data_handle_type`s, not
417 // `ElementType*`s, and `data_handle_type` is taken from `accessor_type::data_handle_type`, which
418 // seems to throw off automatic deduction guides.
419 template <class ElementType, class SizeType, size_t... ExtentsPack>
420 MDSPAN_DEDUCTION_GUIDE mdspan(ElementType*, const extents<SizeType, ExtentsPack...>&)
421 -> mdspan<ElementType, ::MDSPAN_IMPL_STANDARD_NAMESPACE::extents<SizeType, ExtentsPack...>>;
422
423 template <class ElementType, class MappingType>
424 MDSPAN_DEDUCTION_GUIDE mdspan(ElementType*, const MappingType&)
425 -> mdspan<ElementType, typename MappingType::extents_type, typename MappingType::layout_type>;
426
427 template <class MappingType, class AccessorType>
428 MDSPAN_DEDUCTION_GUIDE mdspan(const typename AccessorType::data_handle_type, const MappingType&, const AccessorType&)
429 -> mdspan<typename AccessorType::element_type, typename MappingType::extents_type, typename MappingType::layout_type, AccessorType>;
430 #endif
431
432 } // end namespace MDSPAN_IMPL_STANDARD_NAMESPACE