forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_View.hpp
2099 lines (1803 loc) · 82.5 KB
/
Kokkos_View.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
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//@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
#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
#include <Kokkos_Macros.hpp>
static_assert(false,
"Including non-public Kokkos header files is not allowed.");
#endif
#ifndef KOKKOS_VIEW_HPP
#define KOKKOS_VIEW_HPP
#include <type_traits>
#include <string>
#include <algorithm>
#include <initializer_list>
#include <Kokkos_Core_fwd.hpp>
#include <Kokkos_HostSpace.hpp>
#include <Kokkos_MemoryTraits.hpp>
#include <Kokkos_ExecPolicy.hpp>
#include <View/Hooks/Kokkos_ViewHooks.hpp>
#include <impl/Kokkos_Tools.hpp>
#include <impl/Kokkos_Utilities.hpp>
#ifdef KOKKOS_ENABLE_IMPL_MDSPAN
#include <View/MDSpan/Kokkos_MDSpan_Extents.hpp>
#include <View/MDSpan/Kokkos_MDSpan_Layout.hpp>
#endif
#include <Kokkos_MinMax.hpp>
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
namespace Kokkos {
namespace Impl {
template <class DataType>
struct ViewArrayAnalysis;
template <class DataType, class ArrayLayout,
typename ValueType =
typename ViewArrayAnalysis<DataType>::non_const_value_type>
struct ViewDataAnalysis;
template <class, class...>
class ViewMapping {
public:
enum : bool { is_assignable_data_type = false };
enum : bool { is_assignable = false };
};
template <typename IntType>
constexpr KOKKOS_INLINE_FUNCTION std::size_t count_valid_integers(
const IntType i0, const IntType i1, const IntType i2, const IntType i3,
const IntType i4, const IntType i5, const IntType i6, const IntType i7) {
static_assert(std::is_integral<IntType>::value,
"count_valid_integers() must have integer arguments.");
return (i0 != KOKKOS_INVALID_INDEX) + (i1 != KOKKOS_INVALID_INDEX) +
(i2 != KOKKOS_INVALID_INDEX) + (i3 != KOKKOS_INVALID_INDEX) +
(i4 != KOKKOS_INVALID_INDEX) + (i5 != KOKKOS_INVALID_INDEX) +
(i6 != KOKKOS_INVALID_INDEX) + (i7 != KOKKOS_INVALID_INDEX);
}
// FIXME Ideally, we would not instantiate this function for every possible View
// type. We should be able to only pass "extent" when we use mdspan.
template <typename View>
KOKKOS_INLINE_FUNCTION void runtime_check_rank(
const View&, const bool is_void_spec, const size_t i0, const size_t i1,
const size_t i2, const size_t i3, const size_t i4, const size_t i5,
const size_t i6, const size_t i7, const char* label) {
(void)(label);
if (is_void_spec) {
const size_t num_passed_args =
count_valid_integers(i0, i1, i2, i3, i4, i5, i6, i7);
// We either allow to pass as many extents as the dynamic rank is, or
// as many extents as the total rank is. In the latter case, the given
// extents for the static dimensions must match the
// compile-time extents.
constexpr int rank = View::rank();
constexpr int dyn_rank = View::rank_dynamic();
const bool n_args_is_dyn_rank = num_passed_args == dyn_rank;
const bool n_args_is_rank = num_passed_args == rank;
if constexpr (rank != dyn_rank) {
if (n_args_is_rank) {
size_t new_extents[8] = {i0, i1, i2, i3, i4, i5, i6, i7};
for (int i = dyn_rank; i < rank; ++i)
if (new_extents[i] != View::static_extent(i)) {
KOKKOS_IF_ON_HOST(
const std::string message =
"The specified run-time extent for Kokkos::View '" +
std::string(label) +
"' does not match the compile-time extent in dimension " +
std::to_string(i) + ". The given extent is " +
std::to_string(new_extents[i]) + " but should be " +
std::to_string(View::static_extent(i)) + ".\n";
Kokkos::abort(message.c_str());)
KOKKOS_IF_ON_DEVICE(
Kokkos::abort(
"The specified run-time extents for a Kokkos::View "
"do not match the compile-time extents.");)
}
}
}
if (!n_args_is_dyn_rank && !n_args_is_rank) {
KOKKOS_IF_ON_HOST(
const std::string message =
"Constructor for Kokkos::View '" + std::string(label) +
"' has mismatched number of arguments. The number "
"of arguments = " +
std::to_string(num_passed_args) +
" neither matches the dynamic rank = " +
std::to_string(dyn_rank) +
" nor the total rank = " + std::to_string(rank) + "\n";
Kokkos::abort(message.c_str());)
KOKKOS_IF_ON_DEVICE(Kokkos::abort("Constructor for Kokkos View has "
"mismatched number of arguments.");)
}
}
}
} /* namespace Impl */
} /* namespace Kokkos */
// Class to provide a uniform type
namespace Kokkos {
namespace Impl {
template <class ViewType, int Traits = 0>
struct ViewUniformType;
}
} // namespace Kokkos
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
namespace Kokkos {
/** \class ViewTraits
* \brief Traits class for accessing attributes of a View.
*
* This is an implementation detail of View. It is only of interest
* to developers implementing a new specialization of View.
*
* Template argument options:
* - View< DataType >
* - View< DataType , Space >
* - View< DataType , Space , MemoryTraits >
* - View< DataType , ArrayLayout >
* - View< DataType , ArrayLayout , Space >
* - View< DataType , ArrayLayout , MemoryTraits >
* - View< DataType , ArrayLayout , Space , MemoryTraits >
* - View< DataType , MemoryTraits >
*/
template <class DataType, class... Properties>
struct ViewTraits;
template <>
struct ViewTraits<void> {
using execution_space = void;
using memory_space = void;
using HostMirrorSpace = void;
using array_layout = void;
using memory_traits = void;
using specialize = void;
using hooks_policy = void;
};
template <class... Prop>
struct ViewTraits<void, void, Prop...> {
// Ignore an extraneous 'void'
using execution_space = typename ViewTraits<void, Prop...>::execution_space;
using memory_space = typename ViewTraits<void, Prop...>::memory_space;
using HostMirrorSpace = typename ViewTraits<void, Prop...>::HostMirrorSpace;
using array_layout = typename ViewTraits<void, Prop...>::array_layout;
using memory_traits = typename ViewTraits<void, Prop...>::memory_traits;
using specialize = typename ViewTraits<void, Prop...>::specialize;
using hooks_policy = typename ViewTraits<void, Prop...>::hooks_policy;
};
template <class HooksPolicy, class... Prop>
struct ViewTraits<
std::enable_if_t<Kokkos::Experimental::is_hooks_policy<HooksPolicy>::value>,
HooksPolicy, Prop...> {
using execution_space = typename ViewTraits<void, Prop...>::execution_space;
using memory_space = typename ViewTraits<void, Prop...>::memory_space;
using HostMirrorSpace = typename ViewTraits<void, Prop...>::HostMirrorSpace;
using array_layout = typename ViewTraits<void, Prop...>::array_layout;
using memory_traits = typename ViewTraits<void, Prop...>::memory_traits;
using specialize = typename ViewTraits<void, Prop...>::specialize;
using hooks_policy = HooksPolicy;
};
template <class ArrayLayout, class... Prop>
struct ViewTraits<std::enable_if_t<Kokkos::is_array_layout<ArrayLayout>::value>,
ArrayLayout, Prop...> {
// Specify layout, keep subsequent space and memory traits arguments
using execution_space = typename ViewTraits<void, Prop...>::execution_space;
using memory_space = typename ViewTraits<void, Prop...>::memory_space;
using HostMirrorSpace = typename ViewTraits<void, Prop...>::HostMirrorSpace;
using array_layout = ArrayLayout;
using memory_traits = typename ViewTraits<void, Prop...>::memory_traits;
using specialize = typename ViewTraits<void, Prop...>::specialize;
using hooks_policy = typename ViewTraits<void, Prop...>::hooks_policy;
};
template <class Space, class... Prop>
struct ViewTraits<std::enable_if_t<Kokkos::is_space<Space>::value>, Space,
Prop...> {
// Specify Space, memory traits should be the only subsequent argument.
static_assert(
std::is_same<typename ViewTraits<void, Prop...>::execution_space,
void>::value &&
std::is_same<typename ViewTraits<void, Prop...>::memory_space,
void>::value &&
std::is_same<typename ViewTraits<void, Prop...>::HostMirrorSpace,
void>::value &&
std::is_same<typename ViewTraits<void, Prop...>::array_layout,
void>::value,
"Only one View Execution or Memory Space template argument");
using execution_space = typename Space::execution_space;
using memory_space = typename Space::memory_space;
using HostMirrorSpace =
typename Kokkos::Impl::HostMirror<Space>::Space::memory_space;
using array_layout = typename execution_space::array_layout;
using memory_traits = typename ViewTraits<void, Prop...>::memory_traits;
using specialize = typename ViewTraits<void, Prop...>::specialize;
using hooks_policy = typename ViewTraits<void, Prop...>::hooks_policy;
};
template <class MemoryTraits, class... Prop>
struct ViewTraits<
std::enable_if_t<Kokkos::is_memory_traits<MemoryTraits>::value>,
MemoryTraits, Prop...> {
// Specify memory trait, should not be any subsequent arguments
static_assert(
std::is_same<typename ViewTraits<void, Prop...>::execution_space,
void>::value &&
std::is_same<typename ViewTraits<void, Prop...>::memory_space,
void>::value &&
std::is_same<typename ViewTraits<void, Prop...>::array_layout,
void>::value &&
std::is_same<typename ViewTraits<void, Prop...>::memory_traits,
void>::value &&
std::is_same<typename ViewTraits<void, Prop...>::hooks_policy,
void>::value,
"MemoryTrait is the final optional template argument for a View");
using execution_space = void;
using memory_space = void;
using HostMirrorSpace = void;
using array_layout = void;
using memory_traits = MemoryTraits;
using specialize = void;
using hooks_policy = void;
};
template <class DataType, class... Properties>
struct ViewTraits {
private:
// Unpack the properties arguments
using prop = ViewTraits<void, Properties...>;
using ExecutionSpace =
std::conditional_t<!std::is_void<typename prop::execution_space>::value,
typename prop::execution_space,
Kokkos::DefaultExecutionSpace>;
using MemorySpace =
std::conditional_t<!std::is_void<typename prop::memory_space>::value,
typename prop::memory_space,
typename ExecutionSpace::memory_space>;
using ArrayLayout =
std::conditional_t<!std::is_void<typename prop::array_layout>::value,
typename prop::array_layout,
typename ExecutionSpace::array_layout>;
using HostMirrorSpace = std::conditional_t<
!std::is_void<typename prop::HostMirrorSpace>::value,
typename prop::HostMirrorSpace,
typename Kokkos::Impl::HostMirror<ExecutionSpace>::Space>;
using MemoryTraits =
std::conditional_t<!std::is_void<typename prop::memory_traits>::value,
typename prop::memory_traits,
typename Kokkos::MemoryManaged>;
using HooksPolicy =
std::conditional_t<!std::is_void<typename prop::hooks_policy>::value,
typename prop::hooks_policy,
Kokkos::Experimental::DefaultViewHooks>;
// Analyze data type's properties,
// May be specialized based upon the layout and value type
using data_analysis = Kokkos::Impl::ViewDataAnalysis<DataType, ArrayLayout>;
public:
//------------------------------------
// Data type traits:
using data_type = typename data_analysis::type;
using const_data_type = typename data_analysis::const_type;
using non_const_data_type = typename data_analysis::non_const_type;
//------------------------------------
// Compatible array of trivial type traits:
using scalar_array_type = typename data_analysis::scalar_array_type;
using const_scalar_array_type =
typename data_analysis::const_scalar_array_type;
using non_const_scalar_array_type =
typename data_analysis::non_const_scalar_array_type;
//------------------------------------
// Value type traits:
using value_type = typename data_analysis::value_type;
using const_value_type = typename data_analysis::const_value_type;
using non_const_value_type = typename data_analysis::non_const_value_type;
//------------------------------------
// Mapping traits:
using array_layout = ArrayLayout;
using dimension = typename data_analysis::dimension;
using specialize = std::conditional_t<
std::is_void<typename data_analysis::specialize>::value,
typename prop::specialize,
typename data_analysis::specialize>; /* mapping specialization tag */
static constexpr unsigned rank = dimension::rank;
static constexpr unsigned rank_dynamic = dimension::rank_dynamic;
//------------------------------------
// Execution space, memory space, memory access traits, and host mirror space.
using execution_space = ExecutionSpace;
using memory_space = MemorySpace;
using device_type = Kokkos::Device<ExecutionSpace, MemorySpace>;
using memory_traits = MemoryTraits;
using host_mirror_space = HostMirrorSpace;
using hooks_policy = HooksPolicy;
using size_type = typename MemorySpace::size_type;
enum { is_hostspace = std::is_same<MemorySpace, HostSpace>::value };
enum { is_managed = MemoryTraits::is_unmanaged == 0 };
enum { is_random_access = MemoryTraits::is_random_access == 1 };
//------------------------------------
};
#ifdef KOKKOS_ENABLE_IMPL_MDSPAN
namespace Impl {
struct UnsupportedKokkosArrayLayout;
template <class Traits, class Enabled = void>
struct MDSpanViewTraits {
using mdspan_type = UnsupportedKokkosArrayLayout;
};
// "Natural" mdspan for a view if the View's ArrayLayout is supported.
template <class Traits>
struct MDSpanViewTraits<Traits,
std::void_t<typename Impl::LayoutFromArrayLayout<
typename Traits::array_layout>::type>> {
using index_type = std::size_t;
using extents_type =
typename Impl::ExtentsFromDataType<index_type,
typename Traits::data_type>::type;
using mdspan_layout_type =
typename Impl::LayoutFromArrayLayout<typename Traits::array_layout>::type;
using mdspan_type =
mdspan<typename Traits::value_type, extents_type, mdspan_layout_type>;
};
} // namespace Impl
#endif // KOKKOS_ENABLE_IMPL_MDSPAN
/** \class View
* \brief View to an array of data.
*
* A View represents an array of one or more dimensions.
* For details, please refer to Kokkos' tutorial materials.
*
* \section Kokkos_View_TemplateParameters Template parameters
*
* This class has both required and optional template parameters. The
* \c DataType parameter must always be provided, and must always be
* first. The parameters \c Arg1Type, \c Arg2Type, and \c Arg3Type are
* placeholders for different template parameters. The default value
* of the fifth template parameter \c Specialize suffices for most use
* cases. When explaining the template parameters, we won't refer to
* \c Arg1Type, \c Arg2Type, and \c Arg3Type; instead, we will refer
* to the valid categories of template parameters, in whatever order
* they may occur.
*
* Valid ways in which template arguments may be specified:
* - View< DataType >
* - View< DataType , Layout >
* - View< DataType , Layout , Space >
* - View< DataType , Layout , Space , MemoryTraits >
* - View< DataType , Space >
* - View< DataType , Space , MemoryTraits >
* - View< DataType , MemoryTraits >
*
* \tparam DataType (required) This indicates both the type of each
* entry of the array, and the combination of compile-time and
* run-time array dimension(s). For example, <tt>double*</tt>
* indicates a one-dimensional array of \c double with run-time
* dimension, and <tt>int*[3]</tt> a two-dimensional array of \c int
* with run-time first dimension and compile-time second dimension
* (of 3). In general, the run-time dimensions (if any) must go
* first, followed by zero or more compile-time dimensions. For
* more examples, please refer to the tutorial materials.
*
* \tparam Space (required) The memory space.
*
* \tparam Layout (optional) The array's layout in memory. For
* example, LayoutLeft indicates a column-major (Fortran style)
* layout, and LayoutRight a row-major (C style) layout. If not
* specified, this defaults to the preferred layout for the
* <tt>Space</tt>.
*
* \tparam MemoryTraits (optional) Assertion of the user's intended
* access behavior. For example, RandomAccess indicates read-only
* access with limited spatial locality, and Unmanaged lets users
* wrap externally allocated memory in a View without automatic
* deallocation.
*
* \section Kokkos_View_MT MemoryTraits discussion
*
* \subsection Kokkos_View_MT_Interp MemoryTraits interpretation depends on
* Space
*
* Some \c MemoryTraits options may have different interpretations for
* different \c Space types. For example, with the Cuda device,
* \c RandomAccess tells Kokkos to fetch the data through the texture
* cache, whereas the non-GPU devices have no such hardware construct.
*
* \subsection Kokkos_View_MT_PrefUse Preferred use of MemoryTraits
*
* Users should defer applying the optional \c MemoryTraits parameter
* until the point at which they actually plan to rely on it in a
* computational kernel. This minimizes the number of template
* parameters exposed in their code, which reduces the cost of
* compilation. Users may always assign a View without specified
* \c MemoryTraits to a compatible View with that specification.
* For example:
* \code
* // Pass in the simplest types of View possible.
* void
* doSomething (View<double*, Cuda> out,
* View<const double*, Cuda> in)
* {
* // Assign the "generic" View in to a RandomAccess View in_rr.
* // Note that RandomAccess View objects must have const data.
* View<const double*, Cuda, RandomAccess> in_rr = in;
* // ... do something with in_rr and out ...
* }
* \endcode
*/
} // namespace Kokkos
namespace Kokkos {
template <class T1, class T2>
struct is_always_assignable_impl;
template <class... ViewTDst, class... ViewTSrc>
struct is_always_assignable_impl<Kokkos::View<ViewTDst...>,
Kokkos::View<ViewTSrc...>> {
using mapping_type = Kokkos::Impl::ViewMapping<
typename Kokkos::View<ViewTDst...>::traits,
typename Kokkos::View<ViewTSrc...>::traits,
typename Kokkos::View<ViewTDst...>::traits::specialize>;
constexpr static bool value =
mapping_type::is_assignable &&
static_cast<int>(Kokkos::View<ViewTDst...>::rank_dynamic) >=
static_cast<int>(Kokkos::View<ViewTSrc...>::rank_dynamic);
};
template <class View1, class View2>
using is_always_assignable = is_always_assignable_impl<
std::remove_reference_t<View1>,
std::remove_const_t<std::remove_reference_t<View2>>>;
template <class T1, class T2>
inline constexpr bool is_always_assignable_v =
is_always_assignable<T1, T2>::value;
template <class... ViewTDst, class... ViewTSrc>
constexpr bool is_assignable(const Kokkos::View<ViewTDst...>& dst,
const Kokkos::View<ViewTSrc...>& src) {
using DstTraits = typename Kokkos::View<ViewTDst...>::traits;
using SrcTraits = typename Kokkos::View<ViewTSrc...>::traits;
using mapping_type =
Kokkos::Impl::ViewMapping<DstTraits, SrcTraits,
typename DstTraits::specialize>;
return is_always_assignable_v<Kokkos::View<ViewTDst...>,
Kokkos::View<ViewTSrc...>> ||
(mapping_type::is_assignable &&
((DstTraits::dimension::rank_dynamic >= 1) ||
(dst.static_extent(0) == src.extent(0))) &&
((DstTraits::dimension::rank_dynamic >= 2) ||
(dst.static_extent(1) == src.extent(1))) &&
((DstTraits::dimension::rank_dynamic >= 3) ||
(dst.static_extent(2) == src.extent(2))) &&
((DstTraits::dimension::rank_dynamic >= 4) ||
(dst.static_extent(3) == src.extent(3))) &&
((DstTraits::dimension::rank_dynamic >= 5) ||
(dst.static_extent(4) == src.extent(4))) &&
((DstTraits::dimension::rank_dynamic >= 6) ||
(dst.static_extent(5) == src.extent(5))) &&
((DstTraits::dimension::rank_dynamic >= 7) ||
(dst.static_extent(6) == src.extent(6))) &&
((DstTraits::dimension::rank_dynamic >= 8) ||
(dst.static_extent(7) == src.extent(7))));
}
} /* namespace Kokkos */
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include <impl/Kokkos_ViewMapping.hpp>
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
namespace Kokkos {
// FIXME_OPENMPTARGET - The `declare target` is needed for the Intel GPUs with
// the OpenMPTarget backend
#if defined(KOKKOS_ENABLE_OPENMPTARGET) && defined(KOKKOS_COMPILER_INTEL_LLVM)
#pragma omp declare target
#endif
inline constexpr Kokkos::ALL_t ALL{};
#if defined(KOKKOS_ENABLE_OPENMPTARGET) && defined(KOKKOS_COMPILER_INTEL_LLVM)
#pragma omp end declare target
#endif
inline constexpr Kokkos::Impl::WithoutInitializing_t WithoutInitializing{};
inline constexpr Kokkos::Impl::AllowPadding_t AllowPadding{};
/** \brief Create View allocation parameter bundle from argument list.
*
* Valid argument list members are:
* 1) label as a "string" or std::string
* 2) memory space instance of the View::memory_space type
* 3) execution space instance compatible with the View::memory_space
* 4) Kokkos::WithoutInitializing to bypass initialization
* 4) Kokkos::AllowPadding to allow allocation to pad dimensions for memory
* alignment
*/
template <class... Args>
inline Impl::ViewCtorProp<typename Impl::ViewCtorProp<void, Args>::type...>
view_alloc(Args const&... args) {
using return_type =
Impl::ViewCtorProp<typename Impl::ViewCtorProp<void, Args>::type...>;
static_assert(!return_type::has_pointer,
"Cannot give pointer-to-memory for view allocation");
return return_type(args...);
}
template <class... Args>
KOKKOS_INLINE_FUNCTION
Impl::ViewCtorProp<typename Impl::ViewCtorProp<void, Args>::type...>
view_wrap(Args const&... args) {
using return_type =
Impl::ViewCtorProp<typename Impl::ViewCtorProp<void, Args>::type...>;
static_assert(!return_type::has_memory_space &&
!return_type::has_execution_space &&
!return_type::has_label && return_type::has_pointer,
"Must only give pointer-to-memory for view wrapping");
return return_type(args...);
}
} /* namespace Kokkos */
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
namespace Kokkos {
template <class DataType, class... Properties>
class View;
template <class>
struct is_view : public std::false_type {};
template <class D, class... P>
struct is_view<View<D, P...>> : public std::true_type {};
template <class D, class... P>
struct is_view<const View<D, P...>> : public std::true_type {};
template <class T>
inline constexpr bool is_view_v = is_view<T>::value;
template <class DataType, class... Properties>
class View : public ViewTraits<DataType, Properties...> {
private:
template <class, class...>
friend class View;
template <class, class...>
friend class Kokkos::Impl::ViewMapping;
using view_tracker_type = Kokkos::Impl::ViewTracker<View>;
public:
using traits = ViewTraits<DataType, Properties...>;
private:
using map_type =
Kokkos::Impl::ViewMapping<traits, typename traits::specialize>;
template <typename V>
friend struct Kokkos::Impl::ViewTracker;
using hooks_policy = typename traits::hooks_policy;
view_tracker_type m_track;
map_type m_map;
public:
//----------------------------------------
/** \brief Compatible view of array of scalar types */
using array_type =
View<typename traits::scalar_array_type, typename traits::array_layout,
typename traits::device_type, typename traits::hooks_policy,
typename traits::memory_traits>;
/** \brief Compatible view of const data type */
using const_type =
View<typename traits::const_data_type, typename traits::array_layout,
typename traits::device_type, typename traits::hooks_policy,
typename traits::memory_traits>;
/** \brief Compatible view of non-const data type */
using non_const_type =
View<typename traits::non_const_data_type, typename traits::array_layout,
typename traits::device_type, typename traits::hooks_policy,
typename traits::memory_traits>;
/** \brief Compatible HostMirror view */
using HostMirror =
View<typename traits::non_const_data_type, typename traits::array_layout,
Device<DefaultHostExecutionSpace,
typename traits::host_mirror_space::memory_space>,
typename traits::hooks_policy>;
/** \brief Compatible HostMirror view */
using host_mirror_type =
View<typename traits::non_const_data_type, typename traits::array_layout,
typename traits::host_mirror_space, typename traits::hooks_policy>;
/** \brief Unified types */
using uniform_type = typename Impl::ViewUniformType<View, 0>::type;
using uniform_const_type =
typename Impl::ViewUniformType<View, 0>::const_type;
using uniform_runtime_type =
typename Impl::ViewUniformType<View, 0>::runtime_type;
using uniform_runtime_const_type =
typename Impl::ViewUniformType<View, 0>::runtime_const_type;
using uniform_nomemspace_type =
typename Impl::ViewUniformType<View, 0>::nomemspace_type;
using uniform_const_nomemspace_type =
typename Impl::ViewUniformType<View, 0>::const_nomemspace_type;
using uniform_runtime_nomemspace_type =
typename Impl::ViewUniformType<View, 0>::runtime_nomemspace_type;
using uniform_runtime_const_nomemspace_type =
typename Impl::ViewUniformType<View, 0>::runtime_const_nomemspace_type;
//----------------------------------------
// Domain rank and extents
static constexpr Impl::integral_constant<size_t, traits::dimension::rank>
rank = {};
static constexpr Impl::integral_constant<size_t,
traits::dimension::rank_dynamic>
rank_dynamic = {};
#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
enum {Rank KOKKOS_DEPRECATED_WITH_COMMENT("Use rank instead.") =
map_type::Rank};
#endif
template <typename iType>
KOKKOS_INLINE_FUNCTION constexpr std::enable_if_t<
std::is_integral<iType>::value, size_t>
extent(const iType& r) const noexcept {
return m_map.extent(r);
}
static KOKKOS_INLINE_FUNCTION constexpr size_t static_extent(
const unsigned r) noexcept {
return map_type::static_extent(r);
}
template <typename iType>
KOKKOS_INLINE_FUNCTION constexpr std::enable_if_t<
std::is_integral<iType>::value, int>
extent_int(const iType& r) const noexcept {
return static_cast<int>(m_map.extent(r));
}
KOKKOS_INLINE_FUNCTION constexpr typename traits::array_layout layout()
const {
return m_map.layout();
}
//----------------------------------------
/* Deprecate all 'dimension' functions in favor of
* ISO/C++ vocabulary 'extent'.
*/
KOKKOS_INLINE_FUNCTION constexpr size_t size() const {
return m_map.dimension_0() * m_map.dimension_1() * m_map.dimension_2() *
m_map.dimension_3() * m_map.dimension_4() * m_map.dimension_5() *
m_map.dimension_6() * m_map.dimension_7();
}
KOKKOS_INLINE_FUNCTION constexpr size_t stride_0() const {
return m_map.stride_0();
}
KOKKOS_INLINE_FUNCTION constexpr size_t stride_1() const {
return m_map.stride_1();
}
KOKKOS_INLINE_FUNCTION constexpr size_t stride_2() const {
return m_map.stride_2();
}
KOKKOS_INLINE_FUNCTION constexpr size_t stride_3() const {
return m_map.stride_3();
}
KOKKOS_INLINE_FUNCTION constexpr size_t stride_4() const {
return m_map.stride_4();
}
KOKKOS_INLINE_FUNCTION constexpr size_t stride_5() const {
return m_map.stride_5();
}
KOKKOS_INLINE_FUNCTION constexpr size_t stride_6() const {
return m_map.stride_6();
}
KOKKOS_INLINE_FUNCTION constexpr size_t stride_7() const {
return m_map.stride_7();
}
template <typename iType>
KOKKOS_INLINE_FUNCTION constexpr std::enable_if_t<
std::is_integral<iType>::value, size_t>
stride(iType r) const {
return (
r == 0
? m_map.stride_0()
: (r == 1
? m_map.stride_1()
: (r == 2
? m_map.stride_2()
: (r == 3
? m_map.stride_3()
: (r == 4
? m_map.stride_4()
: (r == 5
? m_map.stride_5()
: (r == 6
? m_map.stride_6()
: m_map.stride_7())))))));
}
template <typename iType>
KOKKOS_INLINE_FUNCTION void stride(iType* const s) const {
m_map.stride(s);
}
//----------------------------------------
// Range span is the span which contains all members.
using reference_type = typename map_type::reference_type;
using pointer_type = typename map_type::pointer_type;
enum {
reference_type_is_lvalue_reference =
std::is_lvalue_reference<reference_type>::value
};
KOKKOS_INLINE_FUNCTION constexpr size_t span() const { return m_map.span(); }
KOKKOS_INLINE_FUNCTION bool span_is_contiguous() const {
return m_map.span_is_contiguous();
}
KOKKOS_INLINE_FUNCTION constexpr bool is_allocated() const {
return m_map.data() != nullptr;
}
KOKKOS_INLINE_FUNCTION constexpr pointer_type data() const {
return m_map.data();
}
//----------------------------------------
// Allow specializations to query their specialized map
KOKKOS_INLINE_FUNCTION
const Kokkos::Impl::ViewMapping<traits, typename traits::specialize>&
impl_map() const {
return m_map;
}
KOKKOS_INLINE_FUNCTION
const Kokkos::Impl::SharedAllocationTracker& impl_track() const {
return m_track.m_tracker;
}
//----------------------------------------
private:
static constexpr bool is_layout_left =
std::is_same<typename traits::array_layout, Kokkos::LayoutLeft>::value;
static constexpr bool is_layout_right =
std::is_same<typename traits::array_layout, Kokkos::LayoutRight>::value;
static constexpr bool is_layout_stride =
std::is_same<typename traits::array_layout, Kokkos::LayoutStride>::value;
static constexpr bool is_default_map =
std::is_void<typename traits::specialize>::value &&
(is_layout_left || is_layout_right || is_layout_stride);
#if defined(KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK)
#define KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(...) \
Kokkos::Impl::runtime_check_memory_access_violation< \
typename traits::memory_space>( \
"Kokkos::View ERROR: attempt to access inaccessible memory space", \
__VA_ARGS__); \
Kokkos::Impl::view_verify_operator_bounds<typename traits::memory_space>( \
__VA_ARGS__);
#else
#define KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(...) \
Kokkos::Impl::runtime_check_memory_access_violation< \
typename traits::memory_space>( \
"Kokkos::View ERROR: attempt to access inaccessible memory space", \
__VA_ARGS__);
#endif
template <typename... Is>
static KOKKOS_FUNCTION void check_access_member_function_valid_args(Is...) {
static_assert(rank <= sizeof...(Is));
static_assert(sizeof...(Is) <= 8);
static_assert(Kokkos::Impl::are_integral<Is...>::value);
}
template <typename... Is>
static KOKKOS_FUNCTION void check_operator_parens_valid_args(Is...) {
static_assert(rank == sizeof...(Is));
static_assert(Kokkos::Impl::are_integral<Is...>::value);
}
public:
//------------------------------
// Rank 1 default map operator()
template <typename I0>
KOKKOS_FORCEINLINE_FUNCTION
std::enable_if_t<(Kokkos::Impl::always_true<I0>::value && //
(1 == rank) && is_default_map && !is_layout_stride),
reference_type>
operator()(I0 i0) const {
check_operator_parens_valid_args(i0);
KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(m_track, m_map, i0)
return m_map.m_impl_handle[i0];
}
template <typename I0>
KOKKOS_FORCEINLINE_FUNCTION
std::enable_if_t<(Kokkos::Impl::always_true<I0>::value && //
(1 == rank) && is_default_map && is_layout_stride),
reference_type>
operator()(I0 i0) const {
check_operator_parens_valid_args(i0);
KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(m_track, m_map, i0)
return m_map.m_impl_handle[m_map.m_impl_offset.m_stride.S0 * i0];
}
//------------------------------
// Rank 1 operator[]
template <typename I0>
KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t<
((1 == rank) && Kokkos::Impl::are_integral<I0>::value && !is_default_map),
reference_type>
operator[](I0 i0) const {
KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(m_track, m_map, i0)
return m_map.reference(i0);
}
template <typename I0>
KOKKOS_FORCEINLINE_FUNCTION
std::enable_if_t<((1 == rank) && Kokkos::Impl::are_integral<I0>::value &&
is_default_map && !is_layout_stride),
reference_type>
operator[](I0 i0) const {
KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(m_track, m_map, i0)
return m_map.m_impl_handle[i0];
}
template <typename I0>
KOKKOS_FORCEINLINE_FUNCTION
std::enable_if_t<((1 == rank) && Kokkos::Impl::are_integral<I0>::value &&
is_default_map && is_layout_stride),
reference_type>
operator[](I0 i0) const {
KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(m_track, m_map, i0)
return m_map.m_impl_handle[m_map.m_impl_offset.m_stride.S0 * i0];
}
//------------------------------
// Rank 2 default map operator()
template <typename I0, typename I1>
KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t<
(Kokkos::Impl::always_true<I0, I1>::value && //
(2 == rank) && is_default_map && is_layout_left && (rank_dynamic == 0)),
reference_type>
operator()(I0 i0, I1 i1) const {
check_operator_parens_valid_args(i0, i1);
KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(m_track, m_map, i0, i1)
return m_map.m_impl_handle[i0 + m_map.m_impl_offset.m_dim.N0 * i1];
}
template <typename I0, typename I1>
KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t<
(Kokkos::Impl::always_true<I0, I1>::value && //
(2 == rank) && is_default_map && is_layout_left && (rank_dynamic != 0)),
reference_type>
operator()(I0 i0, I1 i1) const {
check_operator_parens_valid_args(i0, i1);
KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(m_track, m_map, i0, i1)
return m_map.m_impl_handle[i0 + m_map.m_impl_offset.m_stride * i1];
}
template <typename I0, typename I1>
KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t<
(Kokkos::Impl::always_true<I0, I1>::value && //
(2 == rank) && is_default_map && is_layout_right && (rank_dynamic == 0)),
reference_type>
operator()(I0 i0, I1 i1) const {
check_operator_parens_valid_args(i0, i1);
KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(m_track, m_map, i0, i1)
return m_map.m_impl_handle[i1 + m_map.m_impl_offset.m_dim.N1 * i0];
}
template <typename I0, typename I1>
KOKKOS_FORCEINLINE_FUNCTION std::enable_if_t<
(Kokkos::Impl::always_true<I0, I1>::value && //
(2 == rank) && is_default_map && is_layout_right && (rank_dynamic != 0)),
reference_type>
operator()(I0 i0, I1 i1) const {
check_operator_parens_valid_args(i0, i1);
KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(m_track, m_map, i0, i1)
return m_map.m_impl_handle[i1 + m_map.m_impl_offset.m_stride * i0];
}
template <typename I0, typename I1>
KOKKOS_FORCEINLINE_FUNCTION
std::enable_if_t<(Kokkos::Impl::always_true<I0, I1>::value && //
(2 == rank) && is_default_map && is_layout_stride),
reference_type>
operator()(I0 i0, I1 i1) const {
check_operator_parens_valid_args(i0, i1);
KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(m_track, m_map, i0, i1)