forked from kokkos/mdspan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdspan.hpp
More file actions
757 lines (624 loc) · 25.3 KB
/
mdspan.hpp
File metadata and controls
757 lines (624 loc) · 25.3 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#if !defined(__circle_lang__) || (__circle_build__ < 146)
#error Must compile with Circle build 146 or later
#endif
#include <cassert>
#include <stdexcept>
#include <tuple>
#include <array>
#include <limits>
#include <type_traits>
namespace std {
namespace experimental {
using std::array;
constexpr size_t dynamic_extent = size_t.max;
// [mdspan.extents], class template extents
template<size_t... Extents>
class extents;
// Spam out dynamic_extent Rank times.
template<size_t Rank>
using dextents = extents<for i : Rank => dynamic_extent>;
template<typename>
constexpr size_t make_dynamic_extent() { return dynamic_extent; }
// The Index template parameter is required so that each data member is
// of a different type, allowing the [[no_unique_address]] attribute
// to map them all to the same struct offset.
template<typename Type>
concept SizeType = std::is_convertible_v<Type, size_t>;
template<size_t Index, size_t Extent>
struct _storage_t {
constexpr _storage_t(size_t value = Extent) noexcept {
assert(value == extent);
}
static constexpr size_t extent = Extent;
};
template<size_t Index>
struct _storage_t<Index, dynamic_extent> {
constexpr _storage_t(size_t value = 0) noexcept : extent(value) { }
size_t extent;
};
template<size_t... Extents>
struct extents {
using size_type = size_t;
static constexpr size_t rank() noexcept { return sizeof... Extents; }
static constexpr size_t rank_dynamic() noexcept {
return Extents.count(dynamic_extent);
}
static constexpr size_t static_extent(size_t n) noexcept {
return n == int... ...? Extents : 0;
}
// Set the runtime extents to 1.
constexpr extents() = default;
template<size_t... OtherExtents>
requires((... &&
(
Extents == dynamic_extent ||
OtherExtents == dynamic_extent ||
Extents == OtherExtents
)
))
explicit((... ||
(
Extents != dynamic_extent &&
OtherExtents == dynamic_extent
)
))
constexpr extents(const extents<OtherExtents...>& other) :
m(other. ...m.extent)... { }
// One index per extent.
template<SizeType... IndexTypes>
requires(sizeof...(IndexTypes) == rank())
explicit constexpr extents(IndexTypes... exts) noexcept :
m(exts)... { }
// Map index I to dynamic index J.
template<size_t I>
static constexpr size_t find_dynamic_index =
Extents...[:I].count(dynamic_extent);
// One index per dynamic extent.
template<SizeType... IndexTypes> requires(
sizeof...(IndexTypes) != rank() &&
sizeof...(IndexTypes) == rank_dynamic()
)
explicit constexpr extents(IndexTypes... exts) noexcept : m(
dynamic_extent == Extents ??
exts...[find_dynamic_index<int...>] :
Extents
)... { }
template<SizeType IndexType, size_t N>
requires(N == rank() || N == rank_dynamic())
explicit(N != rank_dynamic())
constexpr extents(const std::array<IndexType, N>& exts) noexcept :
extents(exts...) { }
template<size_t... Extents2>
friend constexpr bool operator==(const extents& lhs,
const extents<Extents2...>& rhs) noexcept {
return sizeof... Extents2 == rank() &&&
(... && (lhs.get<int...(rank())>() == rhs.template get<int...>()));
}
template<int I>
constexpr size_t get() const noexcept {
return m...[I].extent;
}
constexpr size_t extent(size_t n) const noexcept {
return n == int... ...? m.extent : 0;
}
// Create partially static storage for the dynamic extents.
[[no_unique_address]] _storage_t<int..., Extents> ...m;
};
template <typename... SizeTypes>
extents(SizeTypes...) -> extents<make_dynamic_extent<SizeTypes>()...>;
struct layout_left;
struct layout_right;
struct layout_stride;
struct layout_right {
template <class Extents>
class mapping {
private:
static_assert(extents == Extents.template,
"layout_right::mapping must be instantiated with extents");
template <class>
friend class mapping;
template<typename... Indices>
constexpr size_t compute_offset(Indices... indices) const noexcept {
// The right-most extent is the most quickly varying.
size_t x = 0;
@meta for(int i : sizeof... indices) {
@meta if(i != 0)
x *= _extents.template get<i>();
x += indices...[i];
}
return x;
}
public:
constexpr mapping() noexcept = default;
constexpr mapping(mapping const&) noexcept = default;
constexpr mapping(mapping&&) noexcept = default;
constexpr mapping& operator=(mapping const&) noexcept = default;
constexpr mapping& operator=(mapping&&) noexcept = default;
using layout_type = layout_right;
using extents_type = Extents;
using size_type = typename Extents::size_type;
constexpr mapping(const Extents& extents) noexcept
: _extents(extents) { }
template<typename OtherExtents>
requires(std::is_constructible_v<Extents, OtherExtents>)
explicit(!std::is_convertible_v<OtherExtents, Extents>)
constexpr mapping(const mapping<OtherExtents>& other) noexcept :
_extents(other.extents()) { }
// Convert from layout_left
template<typename OtherMapping>
requires(
std::is_constructible_v<Extents, typename OtherMapping::extents_type> &&
typename OtherMapping::layout_type == layout_left &&
Extents::rank() <= 1
)
explicit(!std::is_convertible_v<typename OtherMapping::extents_type, Extents>)
constexpr mapping(const OtherMapping& other) noexcept :
_extents(other.extents()) { }
// Convert from layout_stride
template<typename OtherMapping>
requires(
std::is_constructible_v<Extents, typename OtherMapping::extents_type> &&
typename OtherMapping::layout_type == layout_stride
)
explicit(Extents::rank() != 0)
constexpr mapping(const OtherMapping& other) noexcept :
_extents(other.extents()) {
size_t stride = 1;
for(size_t r = _extents.rank(); r > 0; --r) {
if(stride != other.stride(r - 1))
throw std::runtime_error(
"Assigning layout_stride to layout_right with invalid strides.");
stride *= _extents.extent(r - 1);
}
}
template<typename... Indices>
constexpr size_t operator()(Indices... idx) const noexcept {
return compute_offset(idx...);
}
constexpr Extents extents() const noexcept {
return _extents;
}
constexpr size_t stride(size_t i) const noexcept {
size_t value = 1;
for(size_t r = Extents::rank() - 1; r > i; --r)
value *= _extents.extent(r);
return value;
}
constexpr size_t required_span_size() const noexcept {
return (1 * ... * _extents.template get<int...(Extents::rank())>());
}
static constexpr bool is_always_unique() noexcept { return true; }
static constexpr bool is_always_contiguous() noexcept { return true; }
static constexpr bool is_always_strided() noexcept { return true; }
constexpr bool is_unique() const noexcept { return true; }
constexpr bool is_contiguous() const noexcept { return true; }
constexpr bool is_strided() const noexcept { return true; }
template<class OtherExtents>
friend constexpr bool operator==(const mapping& lhs,
const mapping<OtherExtents>& rhs) noexcept {
return lhs.extents() == rhs.extents();
}
private:
[[no_unique_address]] Extents _extents;
};
};
struct layout_left {
template <class Extents>
class mapping {
static_assert(Extents.template == extents,
"layout_left::mapping must be instantiated with extents.");
template <class>
friend class mapping;
template<typename... Indices>
constexpr size_t compute_offset(Indices... indices) const noexcept {
// The left-most extent is most quickly varying.
size_t x = 0;
@meta for(int i = sizeof... indices - 1; i >= 0; --i) {
if constexpr(i != sizeof... indices - 1)
x *= _extents.template get<i>();
x += indices...[i];
}
return x;
}
public:
constexpr mapping() noexcept = default;
constexpr mapping(mapping const&) noexcept = default;
constexpr mapping(mapping&&) noexcept = default;
mapping& operator=(mapping const&) noexcept = default;
mapping& operator=(mapping&&) noexcept = default;
using layout_type = layout_left;
using extents_type = Extents;
using size_type = typename Extents::size_type;
constexpr mapping(const Extents& exts) noexcept
: _extents(exts) { }
template<typename OtherExtents>
requires(std::is_constructible_v<Extents, OtherExtents>)
explicit(!std::is_convertible_v<OtherExtents, Extents>)
constexpr mapping(const mapping<OtherExtents>& other) noexcept :
_extents(other.extents()) { }
// Convert from layout_right.
template<typename OtherMapping>
requires(
std::is_constructible_v<Extents, typename OtherMapping::extents_type> &&
typename OtherMapping::layout_type == layout_right &&
Extents::rank() <= 1
)
explicit(!std::is_convertible_v<typename OtherMapping::extents_type, Extents>)
constexpr mapping(const OtherMapping& other) noexcept :
_extents(other.extents()) { }
// Convert from layout_stride
template<typename OtherMapping>
requires(
std::is_constructible_v<Extents, typename OtherMapping::extents_type> &&
typename OtherMapping::layout_type == layout_stride
)
explicit(Extents::rank() != 0)
constexpr mapping(const OtherMapping& other) noexcept :
_extents(other.extents()) {
size_t stride = 1;
for(size_type r = 0; r < _extents.rank(); ++r) {
if(stride != other.stride(r))
throw std::runtime_error(
"Assigning layout_stride to layout_left with invalid strides.");
stride *= _extents.extent(r);
}
}
template <typename... Indices>
constexpr size_type operator()(Indices... idxs) const noexcept {
return compute_offset(idxs...);
}
constexpr Extents extents() const noexcept {
return _extents;
}
constexpr size_type stride(size_t i) const noexcept {
size_t value = 1;
for(size_t r = 0; r < i; ++r)
value *= _extents.extent(r);
return value;
}
constexpr size_type required_span_size() const noexcept {
return (1 * ... * _extents.template get<int...(Extents::rank())>());
}
static constexpr bool is_always_unique() noexcept { return true; }
static constexpr bool is_always_contiguous() noexcept { return true; }
static constexpr bool is_always_strided() noexcept { return true; }
constexpr bool is_unique() const noexcept { return true; }
constexpr bool is_contiguous() const noexcept { return true; }
constexpr bool is_strided() const noexcept { return true; }
template<class OtherExtents>
friend constexpr bool operator==(const mapping& lhs,
const mapping<OtherExtents>& rhs) noexcept {
return lhs.extents() == rhs.extents();
}
private:
[[no_unique_address]] Extents _extents;
};
};
struct layout_stride {
template <class Extents>
class mapping {
public:
static_assert(Extents.template == extents,
"layout_string::mapping must be instantiated with extents.");
using size_type = typename Extents::size_type;
using extents_type = Extents;
using layout_type = layout_stride;
private:
//----------------------------------------------------------------------------
template <class>
friend class mapping;
template<typename... Indices>
constexpr size_t compute_index(Indices... indices) const noexcept {
return (0 + ... + (indices * _strides[int...]));
}
template<typename OtherMapping>
static constexpr std::array<size_t, Extents::rank()>
fill_storage(const OtherMapping& other) noexcept {
return { other.stride(int...(Extents::rank()))... };
}
public:
constexpr mapping() noexcept = default;
constexpr mapping(mapping const&) noexcept = default;
constexpr mapping(mapping&&) noexcept = default;
constexpr mapping& operator=(mapping const&) noexcept = default;
constexpr mapping& operator=(mapping&&) noexcept = default;
template<SizeType StrideType>
constexpr mapping(const Extents& exts,
const std::array<StrideType, Extents::rank()>& strides) noexcept :
_extents(exts), _strides { strides... } { }
template<typename OtherExtents>
explicit(!std::is_convertible_v<OtherExtents, Extents>)
constexpr mapping(const mapping<OtherExtents>& rhs) noexcept :
_extents(rhs.extents()), _strides(rhs._strides) { }
template<typename OtherMapping>
requires(
std::is_constructible_v<Extents, typename OtherMapping::extents_type> &&
typename OtherMapping::layout_type::template mapping<typename OtherMapping::extents_type> == OtherMapping,
OtherMapping::is_always_unique() &&
OtherMapping::is_always_strided()
)
explicit(!std::is_convertible_v<typename OtherMapping::extents_type, Extents>)
constexpr mapping(const OtherMapping& other) noexcept :
_extents(other.extents()), _strides(fill_storage(other)) { }
constexpr extents_type extents() const noexcept {
return _extents;
};
constexpr bool is_unique() const noexcept { return true; }
constexpr bool is_contiguous() const noexcept {
// TODO: Iplementat once I understand what this means.
return false;
}
constexpr bool is_strided() const noexcept { return true; }
static constexpr bool is_always_unique() noexcept { return true; }
static constexpr bool is_always_contiguous() noexcept {
return false;
}
static constexpr bool is_always_strided() noexcept { return true; }
template<SizeType... Indices>
requires(sizeof...(Indices) == Extents::rank())
constexpr size_t operator()(Indices... indices) const noexcept {
return compute_index(indices...);
}
constexpr size_t stride(size_t r) const noexcept {
return _strides[r];
}
constexpr const array<size_t, Extents::rank()>& strides() const noexcept {
return _strides;
}
constexpr size_t required_span_size() const noexcept {
// Return 0 if any extent is zero.
if((... || !_extents.template get<int...(Extents::rank())>()))
return 0;
// Return the largest stride * extent product.
return std::max({ 1zu, _extents.template get<int...>() * _strides.[:] ... });
}
template<class OtherExtents>
requires(OtherExtents::rank() == Extents::rank())
friend constexpr bool operator==(const mapping& lhs,
const mapping<OtherExtents>& rhs) noexcept {
return (... && (lhs.stride(int...(Extents::rank())) == rhs.stride(int...)));
}
[[no_unique_address]] Extents _extents;
[[no_unique_address]] std::array<size_t, Extents::rank()> _strides;
};
};
////////////////////////////////////////////////////////////////////////////////
template <class ElementType>
struct default_accessor {
using offset_policy = default_accessor;
using element_type = ElementType;
using reference = ElementType&;
using pointer = ElementType*;
constexpr default_accessor() noexcept = default;
template<typename OtherElementType>
requires(std::is_convertible_v<OtherElementType(*)[], element_type(*)[]>)
constexpr default_accessor(default_accessor<OtherElementType>) noexcept { }
constexpr pointer offset(pointer p, size_t i) const noexcept {
return p + i;
}
constexpr reference access(pointer p, size_t i) const noexcept {
return p[i];
}
};
////////////////////////////////////////////////////////////////////////////////
template <
class ElementType,
class Extents,
class Layout = layout_right,
class Accessor = default_accessor<ElementType>
>
class mdspan {
static_assert(extents == Extents.template,
"Extents parameter must be a specialization of extents");
public:
using extents_type = Extents;
using layout_type = Layout;
using accessor_type = Accessor;
using mapping_type = typename layout_type::template mapping<extents_type>;
using element_type = ElementType;
using value_type = element_type.remove_cv;
using size_type = size_t;
using difference_type = ptrdiff_t;
using pointer = typename accessor_type::pointer;
using reference = typename accessor_type::reference;
constexpr mdspan() = default;
constexpr mdspan(const mdspan&) = default;
constexpr mdspan(mdspan&&) = default;
template<SizeType... IndexTypes>
requires(
std::is_constructible_v<extents_type, IndexTypes...> &&
std::is_constructible_v<mapping_type, extents_type> &&
std::is_default_constructible_v<accessor_type>
)
explicit constexpr mdspan(pointer p, IndexTypes... dynamic_extents) :
_pointer(p), _mapping(extents_type(dynamic_extents...)) { }
template<SizeType IndexType, size_t N>
requires(
std::is_constructible_v<extents_type, array<IndexType, N> > &&
std::is_constructible_v<mapping_type, extents_type> &&
std::is_default_constructible_v<accessor_type>
)
explicit(N != extents_type::rank_dynamic())
constexpr mdspan(pointer p, const array<IndexType, N>& dynamic_extents)
: _pointer(p), _mapping(extents_type(dynamic_extents)) { }
constexpr mdspan(pointer p, const extents_type& exts) requires(
std::is_constructible<mapping_type, extents_type> &&
std::is_default_constructible_v<accessor_type>
) : _pointer(p), _mapping(exts) { }
constexpr mdspan(pointer p, const mapping_type& m)
requires(std::is_default_constructible_v<accessor_type>) :
_pointer(p), _mapping(m) { }
constexpr mdspan(pointer p, const mapping_type& m, const accessor_type& a)
: _pointer(p), _mapping(m), _accessor(a) { }
template<
typename ElementType2, typename Extents2,
typename Layout2, typename Accessor2
> requires(
std::is_constructible_v<mapping_type, typename Layout2::template mapping<Extents2> > &&
std::is_constructible_v<accessor_type, Accessor2> &&
std::is_constructible_v<pointer, typename Accessor2::pointer> &&
std::is_constructible_v<extents_type, Extents2>
)
constexpr mdspan(const mdspan<ElementType2, Extents2, Layout2, Accessor2>& other) :
_pointer(other._pointer), _mapping(other._mapping),
_accessor(other._accessor) { }
// Indexing.
template<SizeType... IndexTypes>
requires(extents_type::rank() == sizeof...(IndexTypes))
constexpr reference operator[](IndexTypes... indices) const noexcept {
return _accessor.access(_pointer, _mapping(indices...));
}
template<SizeType IndexType, size_t N>
requires(N == extents_type::rank())
constexpr reference operator[](const array<IndexType, N>& indices) const noexcept {
return _accessor.access(_pointer, _mapping(indices...));
}
template<SizeType... IndexTypes>
requires(extents_type::rank() == sizeof...(IndexTypes))
constexpr reference operator()(IndexTypes... indices) const noexcept {
return _accessor.access(_pointer, _mapping(indices...));
}
template<SizeType IndexType, size_t N>
requires(N == extents_type::rank())
constexpr reference operator()(const array<IndexType, N>& indices) const noexcept {
return _accessor.access(_pointer, _mapping(indices...));
}
constexpr accessor_type accessor() const { return _accessor; };
static constexpr size_t rank() noexcept { return extents_type::rank(); }
static constexpr size_t rank_dynamic() noexcept { return extents_type::rank_dynamic(); }
static constexpr size_type static_extent(size_t r) noexcept { return extents_type::static_extent(r); }
constexpr extents_type extents() const noexcept { return _mapping.extents(); };
constexpr size_type extent(size_t r) const noexcept { return _mapping.extents().extent(r); };
constexpr size_type size() const noexcept {
return (1 * ... * extents().template get<int...(extents_type::rank())>);
};
constexpr pointer data() const noexcept { return _pointer; };
static constexpr bool is_always_unique() noexcept { return mapping_type::is_always_unique(); };
static constexpr bool is_always_contiguous() noexcept { return mapping_type::is_always_contiguous(); };
static constexpr bool is_always_strided() noexcept { return mapping_type::is_always_strided(); };
constexpr mapping_type mapping() const noexcept { return _mapping; }
constexpr bool is_unique() const noexcept { return _mapping.is_unique(); };
constexpr bool is_contiguous() const noexcept { return _mapping.is_contiguous(); };
constexpr bool is_strided() const noexcept { return _mapping.is_strided(); };
constexpr size_type stride(size_t r) const { return _mapping.stride(r); };
private:
pointer _pointer;
[[no_unique_address]] mapping_type _mapping;
[[no_unique_address]] accessor_type _accessor;
template <class, class, class, class>
friend class mdspan;
};
template<typename ElementType, SizeType... IndexTypes>
mdspan(ElementType*, IndexTypes...)
-> mdspan<ElementType, extents<make_dynamic_extent<IndexTypes>()...>>;
template <class ElementType, typename IndexType, size_t N>
mdspan(ElementType*, const std::array<IndexType, N>&)
-> mdspan<ElementType, dextents<N>>;
template <class ElementType, size_t... ExtentsPack>
mdspan(ElementType*, const extents<ExtentsPack...>&)
-> mdspan<ElementType, extents<ExtentsPack...>>;
template <class ElementType, class MappingType>
mdspan(ElementType*, const MappingType&)
-> mdspan<ElementType, typename MappingType::extents_type, typename MappingType::layout_type>;
template <class MappingType, class AccessorType>
mdspan(const typename AccessorType::pointer, const MappingType&, const AccessorType&)
-> mdspan<typename AccessorType::element_type, typename MappingType::extents_type, typename MappingType::layout_type, AccessorType>;
struct full_extent_t { explicit full_extent_t() = default; };
inline constexpr auto full_extent = full_extent_t{ };
// submdspan
template<typename ET, size_t... Exts, typename LP, typename AP, typename... SliceSpecs>
requires(
(LP == layout_left || LP == layout_right || LP == layout_stride) && (
(... && (
std::is_convertible_v<SliceSpecs, size_t> ||
std::is_convertible_v<SliceSpecs, std::tuple<size_t, size_t>> ||
std::is_convertible_v<SliceSpecs, full_extent_t>
))
)
)
constexpr auto submdspan(const mdspan<ET, extents<Exts...>, LP, AP>& src,
SliceSpecs... slices) {
using tuple = std::tuple<size_t, size_t>;
constexpr size_t rank = src.rank();
// * For size_t slice, skip the extent and stride, but add an offset
// corresponding to the value.
// * For a std::full_extent, offset 0 and old extent.
// * For a std::tuple, add an offset and add a new dynamic extent
// (strides still preserved).
size_t offset = src.mapping()(
std::is_convertible_v<SliceSpecs, size_t> ??
(size_t)slices :
std::is_convertible_v<SliceSpecs, tuple> ??
std::get<0>((tuple)slices) :
0 ...
);
using Extents = extents<
if std::is_convertible_v<SliceSpecs, tuple> => dynamic_extent else
if std::is_convertible_v<SliceSpecs, full_extent_t> => Exts ...
>;
constexpr size_t rank2 = Extents::rank();
Extents sub_extents {
if std::is_convertible_v<SliceSpecs, tuple> =>
std::get<1>((tuple)slices) - std::get<0>((tuple)slices)
else if std::is_convertible_v<SliceSpecs, full_extent_t> =>
src.extent(int...) ...
};
// If LayoutPolicy is layout_left and sub.rank() > 0 is true, then:
// if is_convertible_v<Sk,full_extent_t> is true for all k in the range
// [0, sub.rank() - 1) and is_convertible_v<Sk,size_t> is false for k equal
// sub.rank()-1, then decltype(sub)::layout_type is layout_left.
constexpr bool is_layout_left = layout_left == LP &&& (!rank2 |||
(... && std::is_convertible_v<SliceSpecs...[0:rank2 - 1], full_extent_t>) &&&
!std::is_convertible_v<SliceSpecs...[rank2 - 1], size_t>
);
// If LayoutPolicy is layout_right and sub.rank() > 0 is true, then:
// if is_convertible_v<Sk,full_extent_t> is true for all k in the range
// [src.rank()-sub.rank()+1,src.rank()) and is_convertible_v<Sk,size_t>
// is false for k equal src.rank()-sub.rank(),
// then decltype(sub)::layout_type is layout_right.
constexpr size_t is_layout_right = layout_right == LP &&& (!rank2 |||
(... && std::is_convertible_v<SliceSpecs...[rank - rank2 + 1:rank], full_extent_t>) &&&
!std::is_convertible_v<SliceSpecs...[rank - rank2], size_t>
);
if constexpr(is_layout_left || is_layout_right) {
// Use the source's layout policy.
return mdspan<ET, Extents, LP, AP>(
src.data() + offset,
typename LP::template mapping<Extents>(sub_extents),
src.accessor()
);
} else {
// Use layout_stride policy.
std::array<size_t, Extents::rank()> strides {
if !std::is_convertible_v<SliceSpecs, size_t> => src.stride(int...) ...
};
return mdspan<ET, Extents, layout_stride, AP>(
src.data() + offset,
layout_stride::mapping<Extents>(sub_extents, strides),
src.accessor()
);
}
}
}
}
// Kokkos mdspan macros to let the Kokkos tests work.
#define _MDSPAN_INLINE_VARIABLE inline
#define MDSPAN_INLINE_FUNCTION inline
#define __MDSPAN_OP(mds,...) mds[__VA_ARGS__]
#define __MDSPAN_OP0(mds) mds[]
#define _MDSPAN_HOST_DEVICE
#define _MDSPAN_CPLUSPLUS __cplusplus
#define MDSPAN_CXX_STD_14 201402L
#define MDSPAN_CXX_STD_17 201703L
#define MDSPAN_CXX_STD_20 202002L
#define MDSPAN_HAS_CXX_14 1
#define MDSPAN_HAS_CXX_17 1
#define MDSPAN_HAS_CXX_20 1
#define _MDSPAN_USE_CONCEPTS 1
#define _MDSPAN_USE_FOLD_EXPRESSIONS 1
#define _MDSPAN_USE_INLINE_VARIABLES 1
#define _MDSPAN_USE_VARIABLE_TEMPLATES 1
#define _MDSPAN_USE_INTEGER_SEQUENCE 1
#define _MDSPAN_USE_RETURN_TYPE_DEDUCTION 1
#define _MDSPAN_USE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION 1
// #define _MDSPAN_USE_ALIAS_TEMPLATE_ARGUMENT_DEDUCTION 1