Debian Codegraph @

packages/kokkos/tpls/mdspan/include/experimental/__p1684_bits/mdarray.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 "../mdspan"
20 #include <cassert>
21 #include <vector>
22
23 namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
24 namespace MDSPAN_IMPL_PROPOSED_NAMESPACE {
25
26 namespace {
27 template<class Extents>
28 struct size_of_extents;
29
30 template<class IndexType, size_t ... Extents>
31 struct size_of_extents<extents<IndexType, Extents...>> {
32 constexpr static size_t value() {
33 size_t size = 1;
34 for(size_t r=0; r<extents<IndexType, Extents...>::rank(); r++)
35 size *= extents<IndexType, Extents...>::static_extent(r);
36 return size;
37 }
38 };
39 }
40
41 namespace {
42 template<class C>
43 struct container_is_array : std::false_type {
44 template<class M>
45 static constexpr C construct(const M& m) { return C(m.required_span_size()); }
46 };
47 template<class T, size_t N>
48 struct container_is_array<std::array<T,N>> : std::true_type {
49 template<class M>
50 static constexpr std::array<T,N> construct(const M&) { return std::array<T,N>(); }
51 };
52 }
53
54 template <
55 class ElementType,
56 class Extents,
57 class LayoutPolicy = layout_right,
58 class Container = std::vector<ElementType>
59 >
60 class mdarray {
61 private:
62 static_assert(::MDSPAN_IMPL_STANDARD_NAMESPACE::detail::__is_extents_v<Extents>,
63 MDSPAN_IMPL_PROPOSED_NAMESPACE_STRING "::mdspan's Extents template parameter must be a specialization of " MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::extents.");
64
65 public:
66
67 //--------------------------------------------------------------------------------
68 // Domain and codomain types
69
70 using extents_type = Extents;
71 using layout_type = LayoutPolicy;
72 using container_type = Container;
73 using mapping_type = typename layout_type::template mapping<extents_type>;
74 using element_type = ElementType;
75 using mdspan_type = mdspan<element_type, extents_type, layout_type>;
76 using const_mdspan_type = mdspan<const element_type, extents_type, layout_type>;
77 using value_type = std::remove_cv_t<element_type>;
78 using index_type = typename Extents::index_type;
79 using size_type = typename Extents::size_type;
80 using rank_type = typename Extents::rank_type;
81 using pointer = typename container_type::pointer;
82 using reference = typename container_type::reference;
83 using const_pointer = typename container_type::const_pointer;
84 using const_reference = typename container_type::const_reference;
85
86 public:
87
88 //--------------------------------------------------------------------------------
89 // [mdspan.basic.cons], mdspan constructors, assignment, and destructor
90
91 #if !(MDSPAN_HAS_CXX_20)
92 MDSPAN_FUNCTION_REQUIRES(
93 (MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr),
94 mdarray, (), ,
95 /* requires */ (extents_type::rank_dynamic()!=0)) {}
96 #else
97 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mdarray() requires(extents_type::rank_dynamic()!=0) = default;
98 #endif
99 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mdarray(const mdarray&) = default;
100 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mdarray(mdarray&&) = default;
101
102 // Constructors for container types constructible from a size
103 MDSPAN_TEMPLATE_REQUIRES(
104 class... SizeTypes,
105 /* requires */ (
106 (::MDSPAN_IMPL_STANDARD_NAMESPACE::detail::are_valid_indices<index_type, SizeTypes...>()) &&
107 _MDSPAN_TRAIT( std::is_constructible, extents_type, SizeTypes...) &&
108 _MDSPAN_TRAIT( std::is_constructible, mapping_type, extents_type) &&
109 (_MDSPAN_TRAIT( std::is_constructible, container_type, size_t) ||
110 container_is_array<container_type>::value) &&
111 (extents_type::rank()>0 || extents_type::rank_dynamic()==0)
112 )
113 )
114 MDSPAN_INLINE_FUNCTION
115 explicit constexpr mdarray(SizeTypes... dynamic_extents)
116 : map_(extents_type(dynamic_extents...)), ctr_(container_is_array<container_type>::construct(map_))
117 { }
118
119 MDSPAN_FUNCTION_REQUIRES(
120 (MDSPAN_INLINE_FUNCTION constexpr),
121 mdarray, (const extents_type& exts), ,
122 /* requires */ ((_MDSPAN_TRAIT( std::is_constructible, container_type, size_t) ||
123 container_is_array<container_type>::value) &&
124 _MDSPAN_TRAIT( std::is_constructible, mapping_type, extents_type))
125 ) : map_(exts), ctr_(container_is_array<container_type>::construct(map_))
126 { }
127
128 MDSPAN_FUNCTION_REQUIRES(
129 (MDSPAN_INLINE_FUNCTION constexpr),
130 mdarray, (const mapping_type& m), ,
131 /* requires */ (_MDSPAN_TRAIT( std::is_constructible, container_type, size_t) ||
132 container_is_array<container_type>::value)
133 ) : map_(m), ctr_(container_is_array<container_type>::construct(map_))
134 { }
135
136 MDSPAN_FUNCTION_REQUIRES(
137 (MDSPAN_INLINE_FUNCTION constexpr),
138 mdarray, (const extents_type& exts, const container_type& ctr), ,
139 /* requires */ (_MDSPAN_TRAIT( std::is_constructible, mapping_type, extents_type))
140 ) : map_(exts), ctr_(ctr)
141 { assert(ctr.size() >= static_cast<size_t>(map_.required_span_size())); }
142
143 constexpr mdarray(const mapping_type& m, const container_type& ctr)
144 : map_(m), ctr_(ctr)
145 { assert(ctr.size() >= static_cast<size_t>(map_.required_span_size())); }
146
147 MDSPAN_FUNCTION_REQUIRES(
148 (MDSPAN_INLINE_FUNCTION constexpr),
149 mdarray, (const extents_type& exts, container_type&& ctr), ,
150 /* requires */ (_MDSPAN_TRAIT( std::is_constructible, mapping_type, extents_type))
151 ) : map_(exts), ctr_(std::move(ctr))
152 { assert(ctr_.size() >= static_cast<size_t>(map_.required_span_size())); }
153
154 constexpr mdarray(const mapping_type& m, container_type&& ctr)
155 : map_(m), ctr_(std::move(ctr))
156 { assert(ctr_.size() >= static_cast<size_t>(map_.required_span_size())); }
157
158
159 MDSPAN_TEMPLATE_REQUIRES(
160 class OtherElementType, class OtherExtents, class OtherLayoutPolicy, class OtherContainer,
161 /* requires */ (
162 _MDSPAN_TRAIT( std::is_constructible, mapping_type, typename OtherLayoutPolicy::template mapping<OtherExtents>) &&
163 _MDSPAN_TRAIT( std::is_constructible, container_type, OtherContainer)
164 )
165 )
166 MDSPAN_INLINE_FUNCTION
167 constexpr mdarray(const mdarray<OtherElementType, OtherExtents, OtherLayoutPolicy, OtherContainer>& other)
168 : map_(other.mapping()), ctr_(other.container())
169 {
170 static_assert( std::is_constructible<extents_type, OtherExtents>::value, "");
171 }
172
173 // Constructors for container types constructible from a size and allocator
174 MDSPAN_TEMPLATE_REQUIRES(
175 class Alloc,
176 /* requires */ (_MDSPAN_TRAIT( std::is_constructible, container_type, size_t, Alloc) &&
177 _MDSPAN_TRAIT( std::is_constructible, mapping_type, extents_type))
178 )
179 MDSPAN_INLINE_FUNCTION
180 constexpr mdarray(const extents_type& exts, const Alloc& a)
181 : map_(exts), ctr_(map_.required_span_size(), a)
182 { }
183
184 MDSPAN_TEMPLATE_REQUIRES(
185 class Alloc,
186 /* requires */ (_MDSPAN_TRAIT( std::is_constructible, container_type, size_t, Alloc))
187 )
188 MDSPAN_INLINE_FUNCTION
189 constexpr mdarray(const mapping_type& map, const Alloc& a)
190 : map_(map), ctr_(map_.required_span_size(), a)
191 { }
192
193 // Constructors for container types constructible from a container and allocator
194 MDSPAN_TEMPLATE_REQUIRES(
195 class Alloc,
196 /* requires */ (_MDSPAN_TRAIT( std::is_constructible, container_type, container_type, Alloc) &&
197 _MDSPAN_TRAIT( std::is_constructible, mapping_type, extents_type))
198 )
199 MDSPAN_INLINE_FUNCTION
200 constexpr mdarray(const extents_type& exts, const container_type& ctr, const Alloc& a)
201 : map_(exts), ctr_(ctr, a)
202 { assert(ctr_.size() >= static_cast<size_t>(map_.required_span_size())); }
203
204 MDSPAN_TEMPLATE_REQUIRES(
205 class Alloc,
206 /* requires */ (_MDSPAN_TRAIT( std::is_constructible, container_type, size_t, Alloc))
207 )
208 MDSPAN_INLINE_FUNCTION
209 constexpr mdarray(const mapping_type& map, const container_type& ctr, const Alloc& a)
210 : map_(map), ctr_(ctr, a)
211 { assert(ctr_.size() >= static_cast<size_t>(map_.required_span_size())); }
212
213 MDSPAN_TEMPLATE_REQUIRES(
214 class Alloc,
215 /* requires */ (_MDSPAN_TRAIT( std::is_constructible, container_type, container_type, Alloc) &&
216 _MDSPAN_TRAIT( std::is_constructible, mapping_type, extents_type))
217 )
218 MDSPAN_INLINE_FUNCTION
219 constexpr mdarray(const extents_type& exts, container_type&& ctr, const Alloc& a)
220 : map_(exts), ctr_(std::move(ctr), a)
221 { assert(ctr_.size() >= static_cast<size_t>(map_.required_span_size())); }
222
223 MDSPAN_TEMPLATE_REQUIRES(
224 class Alloc,
225 /* requires */ (_MDSPAN_TRAIT( std::is_constructible, container_type, size_t, Alloc))
226 )
227 MDSPAN_INLINE_FUNCTION
228 constexpr mdarray(const mapping_type& map, container_type&& ctr, const Alloc& a)
229 : map_(map), ctr_(std::move(ctr), a)
230 { assert(ctr_.size() >= map_.required_span_size()); }
231
232 MDSPAN_TEMPLATE_REQUIRES(
233 class OtherElementType, class OtherExtents, class OtherLayoutPolicy, class OtherContainer, class Alloc,
234 /* requires */ (
235 _MDSPAN_TRAIT( std::is_constructible, mapping_type, typename OtherLayoutPolicy::template mapping<OtherExtents>) &&
236 _MDSPAN_TRAIT( std::is_constructible, container_type, OtherContainer, Alloc)
237 )
238 )
239 MDSPAN_INLINE_FUNCTION
240 constexpr mdarray(const mdarray<OtherElementType, OtherExtents, OtherLayoutPolicy, OtherContainer>& other, const Alloc& a)
241 : map_(other.mapping()), ctr_(other.container(), a)
242 {
243 static_assert( std::is_constructible<extents_type, OtherExtents>::value, "");
244 }
245
246 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mdarray& operator= (const mdarray&) = default;
247 MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mdarray& operator= (mdarray&&) = default;
248 MDSPAN_INLINE_FUNCTION_DEFAULTED
249 ~mdarray() = default;
250
251 //--------------------------------------------------------------------------------
252 // [mdspan.basic.mapping], mdspan mapping domain multidimensional index to access codomain element
253
254 #if MDSPAN_USE_BRACKET_OPERATOR
255 MDSPAN_TEMPLATE_REQUIRES(
256 class... SizeTypes,
257 /* requires */ (
258 _MDSPAN_FOLD_AND(_MDSPAN_TRAIT( std::is_convertible, SizeTypes, index_type) /* && ... */) &&
259 extents_type::rank() == sizeof...(SizeTypes)
260 )
261 )
262 MDSPAN_FORCE_INLINE_FUNCTION
263 constexpr const_reference operator[](SizeTypes... indices) const noexcept
264 {
265 return ctr_[map_(static_cast<index_type>(std::move(indices))...)];
266 }
267
268 MDSPAN_TEMPLATE_REQUIRES(
269 class... SizeTypes,
270 /* requires */ (
271 _MDSPAN_FOLD_AND(_MDSPAN_TRAIT( std::is_convertible, SizeTypes, index_type) /* && ... */) &&
272 extents_type::rank() == sizeof...(SizeTypes)
273 )
274 )
275 MDSPAN_FORCE_INLINE_FUNCTION
276 constexpr reference operator[](SizeTypes... indices) noexcept
277 {
278 return ctr_[map_(static_cast<index_type>(std::move(indices))...)];
279 }
280 #endif
281
282 #if 0
283 MDSPAN_TEMPLATE_REQUIRES(
284 class SizeType, size_t N,
285 /* requires */ (
286 _MDSPAN_TRAIT( std::is_convertible, SizeType, index_type) &&
287 N == extents_type::rank()
288 )
289 )
290 MDSPAN_FORCE_INLINE_FUNCTION
291 constexpr const_reference operator[](const std::array<SizeType, N>& indices) const noexcept
292 {
293 return __impl::template __callop<reference>(*this, indices);
294 }
295
296 MDSPAN_TEMPLATE_REQUIRES(
297 class SizeType, size_t N,
298 /* requires */ (
299 _MDSPAN_TRAIT( std::is_convertible, SizeType, index_type) &&
300 N == extents_type::rank()
301 )
302 )
303 MDSPAN_FORCE_INLINE_FUNCTION
304 constexpr reference operator[](const std::array<SizeType, N>& indices) noexcept
305 {
306 return __impl::template __callop<reference>(*this, indices);
307 }
308 #endif
309
310
311 #if MDSPAN_USE_PAREN_OPERATOR
312 MDSPAN_TEMPLATE_REQUIRES(
313 class... SizeTypes,
314 /* requires */ (
315 (::MDSPAN_IMPL_STANDARD_NAMESPACE::detail::are_valid_indices<index_type, SizeTypes...>()) &&
316 extents_type::rank() == sizeof...(SizeTypes)
317 )
318 )
319 MDSPAN_FORCE_INLINE_FUNCTION
320 constexpr const_reference operator()(SizeTypes... indices) const noexcept
321 {
322 return ctr_[map_(static_cast<index_type>(std::move(indices))...)];
323 }
324 MDSPAN_TEMPLATE_REQUIRES(
325 class... SizeTypes,
326 /* requires */ (
327 (::MDSPAN_IMPL_STANDARD_NAMESPACE::detail::are_valid_indices<index_type, SizeTypes...>()) &&
328 extents_type::rank() == sizeof...(SizeTypes)
329 )
330 )
331 MDSPAN_FORCE_INLINE_FUNCTION
332 constexpr reference operator()(SizeTypes... indices) noexcept
333 {
334 return ctr_[map_(static_cast<index_type>(std::move(indices))...)];
335 }
336
337 #if 0
338 MDSPAN_TEMPLATE_REQUIRES(
339 class SizeType, size_t N,
340 /* requires */ (
341 _MDSPAN_TRAIT( std::is_convertible, SizeType, index_type) &&
342 N == extents_type::rank()
343 )
344 )
345 MDSPAN_FORCE_INLINE_FUNCTION
346 constexpr const_reference operator()(const std::array<SizeType, N>& indices) const noexcept
347 {
348 return __impl::template __callop<reference>(*this, indices);
349 }
350
351 MDSPAN_TEMPLATE_REQUIRES(
352 class SizeType, size_t N,
353 /* requires */ (
354 _MDSPAN_TRAIT( std::is_convertible, SizeType, index_type) &&
355 N == extents_type::rank()
356 )
357 )
358 MDSPAN_FORCE_INLINE_FUNCTION
359 constexpr reference operator()(const std::array<SizeType, N>& indices) noexcept
360 {
361 return __impl::template __callop<reference>(*this, indices);
362 }
363 #endif
364 #endif
365
366 MDSPAN_INLINE_FUNCTION constexpr pointer data() noexcept { return ctr_.data(); };
367 MDSPAN_INLINE_FUNCTION constexpr const_pointer data() const noexcept { return ctr_.data(); };
368 MDSPAN_INLINE_FUNCTION constexpr container_type& container() noexcept { return ctr_; };
369 MDSPAN_INLINE_FUNCTION constexpr const container_type& container() const noexcept { return ctr_; };
370
371 //--------------------------------------------------------------------------------
372 // [mdspan.basic.domobs], mdspan observers of the domain multidimensional index space
373
374 MDSPAN_INLINE_FUNCTION static constexpr rank_type rank() noexcept { return extents_type::rank(); }
375 MDSPAN_INLINE_FUNCTION static constexpr rank_type rank_dynamic() noexcept { return extents_type::rank_dynamic(); }
376 MDSPAN_INLINE_FUNCTION static constexpr size_t static_extent(size_t r) noexcept { return extents_type::static_extent(r); }
377
378 MDSPAN_INLINE_FUNCTION constexpr const extents_type& extents() const noexcept { return map_.extents(); };
379 MDSPAN_INLINE_FUNCTION constexpr index_type extent(size_t r) const noexcept { return map_.extents().extent(r); };
380 MDSPAN_INLINE_FUNCTION constexpr index_type size() const noexcept {
381 // return __impl::__size(*this);
382 return ctr_.size();
383 };
384
385
386 //--------------------------------------------------------------------------------
387 // [mdspan.basic.obs], mdspan observers of the mapping
388
389 MDSPAN_INLINE_FUNCTION static constexpr bool is_always_unique() noexcept { return mapping_type::is_always_unique(); };
390 MDSPAN_INLINE_FUNCTION static constexpr bool is_always_exhaustive() noexcept { return mapping_type::is_always_exhaustive(); };
391 MDSPAN_INLINE_FUNCTION static constexpr bool is_always_strided() noexcept { return mapping_type::is_always_strided(); };
392
393 MDSPAN_INLINE_FUNCTION constexpr const mapping_type& mapping() const noexcept { return map_; };
394 MDSPAN_INLINE_FUNCTION constexpr bool is_unique() const noexcept { return map_.is_unique(); };
395 MDSPAN_INLINE_FUNCTION constexpr bool is_exhaustive() const noexcept { return map_.is_exhaustive(); };
396 MDSPAN_INLINE_FUNCTION constexpr bool is_strided() const noexcept { return map_.is_strided(); };
397 MDSPAN_INLINE_FUNCTION constexpr index_type stride(size_t r) const { return map_.stride(r); };
398
399 // Converstion to mdspan
400 MDSPAN_TEMPLATE_REQUIRES(
401 class OtherElementType, class OtherExtents,
402 class OtherLayoutType, class OtherAccessorType,
403 /* requires */ (
404 _MDSPAN_TRAIT(std::is_assignable,
405 mdspan<OtherElementType, OtherExtents, OtherLayoutType, OtherAccessorType>,
406 mdspan_type)
407 )
408 )
409 constexpr operator mdspan<OtherElementType, OtherExtents, OtherLayoutType, OtherAccessorType> () {
410 return mdspan_type(data(), map_);
411 }
412
413 MDSPAN_TEMPLATE_REQUIRES(
414 class OtherElementType, class OtherExtents,
415 class OtherLayoutType, class OtherAccessorType,
416 /* requires */ (
417 _MDSPAN_TRAIT(std::is_assignable,
418 mdspan<OtherElementType, OtherExtents, OtherLayoutType, OtherAccessorType>,
419 const_mdspan_type)
420 )
421 )
422 constexpr operator mdspan<OtherElementType, OtherExtents, OtherLayoutType, OtherAccessorType> () const {
423 return const_mdspan_type(data(), map_);
424 }
425
426 MDSPAN_TEMPLATE_REQUIRES(
427 class OtherAccessorType = default_accessor<element_type>,
428 /* requires */ (
429 _MDSPAN_TRAIT(std::is_assignable, mdspan_type,
430 mdspan<element_type, extents_type, layout_type, OtherAccessorType>)
431 )
432 )
433 constexpr mdspan<element_type, extents_type, layout_type, OtherAccessorType>
434 to_mdspan(const OtherAccessorType& a = default_accessor<element_type>()) {
435 return mdspan<element_type, extents_type, layout_type, OtherAccessorType>(data(), map_, a);
436 }
437
438 MDSPAN_TEMPLATE_REQUIRES(
439 class OtherAccessorType = default_accessor<const element_type>,
440 /* requires */ (
441 _MDSPAN_TRAIT(std::is_assignable, const_mdspan_type,
442 mdspan<const element_type, extents_type, layout_type, OtherAccessorType>)
443 )
444 )
445 constexpr mdspan<const element_type, extents_type, layout_type, OtherAccessorType>
446 to_mdspan(const OtherAccessorType& a = default_accessor<const element_type>()) const {
447 return mdspan<const element_type, extents_type, layout_type, OtherAccessorType>(data(), map_, a);
448 }
449
450 private:
451 mapping_type map_;
452 container_type ctr_;
453
454 template <class, class, class, class>
455 friend class mdarray;
456 };
457
458
459 } // end namespace MDSPAN_IMPL_PROPOSED_NAMESPACE
460 } // end namespace MDSPAN_IMPL_STANDARD_NAMESPACE