23 #ifndef KOKKOS_DYNRANKVIEW_HPP
24 #define KOKKOS_DYNRANKVIEW_HPP
25 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
26 #define KOKKOS_IMPL_PUBLIC_INCLUDE
27 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_DYNRANKVIEW
30 #include <Kokkos_Core.hpp>
31 #include <impl/Kokkos_Error.hpp>
32 #include <type_traits>
36 template <
typename DataType,
class... Properties>
41 template <
class T,
size_t Rank>
42 struct ViewDataTypeFromRank {
43 using type =
typename ViewDataTypeFromRank<T, Rank - 1>::type*;
47 struct ViewDataTypeFromRank<T, 0> {
51 template <
unsigned N,
typename T,
typename... Args>
52 KOKKOS_FUNCTION View<typename ViewDataTypeFromRank<T, N>::type, Args...>
54 DynRankView<T, Args...> v,
55 std::enable_if_t<std::is_same_v<
typename ViewTraits<T, Args...>::specialize,
58 template <
typename Specialize>
59 struct DynRankDimTraits {
60 enum :
size_t { unspecified = KOKKOS_INVALID_INDEX };
63 KOKKOS_INLINE_FUNCTION
64 static size_t computeRank(
const size_t N0,
const size_t N1,
const size_t N2,
65 const size_t N3,
const size_t N4,
const size_t N5,
66 const size_t N6,
const size_t ) {
68 (N6 == unspecified && N5 == unspecified && N4 == unspecified &&
69 N3 == unspecified && N2 == unspecified && N1 == unspecified &&
72 : ((N6 == unspecified && N5 == unspecified && N4 == unspecified &&
73 N3 == unspecified && N2 == unspecified && N1 == unspecified)
75 : ((N6 == unspecified && N5 == unspecified &&
76 N4 == unspecified && N3 == unspecified &&
79 : ((N6 == unspecified && N5 == unspecified &&
80 N4 == unspecified && N3 == unspecified)
82 : ((N6 == unspecified && N5 == unspecified &&
85 : ((N6 == unspecified &&
88 : ((N6 == unspecified)
94 template <
typename Layout>
95 KOKKOS_INLINE_FUNCTION
static size_t computeRank(
const Layout& layout) {
96 return computeRank(layout.dimension[0], layout.dimension[1],
97 layout.dimension[2], layout.dimension[3],
98 layout.dimension[4], layout.dimension[5],
99 layout.dimension[6], layout.dimension[7]);
103 template <
typename Layout,
typename... P>
104 KOKKOS_INLINE_FUNCTION
static size_t computeRank(
105 const Kokkos::Impl::ViewCtorProp<P...>& ,
106 const Layout& layout) {
107 return computeRank(layout);
115 template <
typename Layout>
116 KOKKOS_INLINE_FUNCTION
static std::enable_if_t<
117 (std::is_same_v<Layout, Kokkos::LayoutRight> ||
118 std::is_same_v<Layout, Kokkos::LayoutLeft>),
120 createLayout(
const Layout& layout) {
122 layout.dimension[0] != unspecified ? layout.dimension[0] : 1,
123 layout.dimension[1] != unspecified ? layout.dimension[1] : 1,
124 layout.dimension[2] != unspecified ? layout.dimension[2] : 1,
125 layout.dimension[3] != unspecified ? layout.dimension[3] : 1,
126 layout.dimension[4] != unspecified ? layout.dimension[4] : 1,
127 layout.dimension[5] != unspecified ? layout.dimension[5] : 1,
128 layout.dimension[6] != unspecified ? layout.dimension[6] : 1,
129 layout.dimension[7] != unspecified ? layout.dimension[7] : unspecified);
130 new_layout.stride = layout.stride;
135 template <
typename Layout>
136 KOKKOS_INLINE_FUNCTION
static std::enable_if_t<
137 (std::is_same_v<Layout, Kokkos::LayoutStride>), Layout>
138 createLayout(
const Layout& layout) {
140 layout.dimension[0] != unspecified ? layout.dimension[0] : 1,
142 layout.dimension[1] != unspecified ? layout.dimension[1] : 1,
144 layout.dimension[2] != unspecified ? layout.dimension[2] : 1,
146 layout.dimension[3] != unspecified ? layout.dimension[3] : 1,
148 layout.dimension[4] != unspecified ? layout.dimension[4] : 1,
150 layout.dimension[5] != unspecified ? layout.dimension[5] : 1,
152 layout.dimension[6] != unspecified ? layout.dimension[6] : 1,
154 layout.dimension[7] != unspecified ? layout.dimension[7] : unspecified,
159 template <
typename Traits,
typename... P>
160 KOKKOS_INLINE_FUNCTION
static std::enable_if_t<
161 (std::is_same_v<typename Traits::array_layout, Kokkos::LayoutRight> ||
162 std::is_same_v<typename Traits::array_layout, Kokkos::LayoutLeft> ||
163 std::is_same_v<typename Traits::array_layout, Kokkos::LayoutStride>),
164 typename Traits::array_layout>
165 createLayout(
const Kokkos::Impl::ViewCtorProp<P...>& ,
166 const typename Traits::array_layout& layout) {
167 return createLayout(layout);
174 template <
typename ViewType,
typename ViewArg>
175 static ViewType createView(
const ViewArg& arg,
const size_t N0,
176 const size_t N1,
const size_t N2,
const size_t N3,
177 const size_t N4,
const size_t N5,
const size_t N6,
179 return ViewType(arg, N0 != unspecified ? N0 : 1, N1 != unspecified ? N1 : 1,
180 N2 != unspecified ? N2 : 1, N3 != unspecified ? N3 : 1,
181 N4 != unspecified ? N4 : 1, N5 != unspecified ? N5 : 1,
182 N6 != unspecified ? N6 : 1, N7 != unspecified ? N7 : 1);
187 template <
typename Layout,
typename iType>
188 KOKKOS_INLINE_FUNCTION
static std::enable_if_t<
189 (std::is_same_v<Layout, Kokkos::LayoutRight> ||
190 std::is_same_v<Layout, Kokkos::LayoutLeft>)&&std::is_integral_v<iType>,
192 reconstructLayout(
const Layout& layout, iType dynrank) {
193 return Layout(dynrank > 0 ? layout.dimension[0] : KOKKOS_INVALID_INDEX,
194 dynrank > 1 ? layout.dimension[1] : KOKKOS_INVALID_INDEX,
195 dynrank > 2 ? layout.dimension[2] : KOKKOS_INVALID_INDEX,
196 dynrank > 3 ? layout.dimension[3] : KOKKOS_INVALID_INDEX,
197 dynrank > 4 ? layout.dimension[4] : KOKKOS_INVALID_INDEX,
198 dynrank > 5 ? layout.dimension[5] : KOKKOS_INVALID_INDEX,
199 dynrank > 6 ? layout.dimension[6] : KOKKOS_INVALID_INDEX,
200 dynrank > 7 ? layout.dimension[7] : KOKKOS_INVALID_INDEX);
204 template <
typename Layout,
typename iType>
205 KOKKOS_INLINE_FUNCTION
static std::enable_if_t<
206 (std::is_same_v<Layout, Kokkos::LayoutStride>)&&std::is_integral_v<iType>,
208 reconstructLayout(
const Layout& layout, iType dynrank) {
209 return Layout(dynrank > 0 ? layout.dimension[0] : KOKKOS_INVALID_INDEX,
210 dynrank > 0 ? layout.stride[0] : (0),
211 dynrank > 1 ? layout.dimension[1] : KOKKOS_INVALID_INDEX,
212 dynrank > 1 ? layout.stride[1] : (0),
213 dynrank > 2 ? layout.dimension[2] : KOKKOS_INVALID_INDEX,
214 dynrank > 2 ? layout.stride[2] : (0),
215 dynrank > 3 ? layout.dimension[3] : KOKKOS_INVALID_INDEX,
216 dynrank > 3 ? layout.stride[3] : (0),
217 dynrank > 4 ? layout.dimension[4] : KOKKOS_INVALID_INDEX,
218 dynrank > 4 ? layout.stride[4] : (0),
219 dynrank > 5 ? layout.dimension[5] : KOKKOS_INVALID_INDEX,
220 dynrank > 5 ? layout.stride[5] : (0),
221 dynrank > 6 ? layout.dimension[6] : KOKKOS_INVALID_INDEX,
222 dynrank > 6 ? layout.stride[6] : (0),
223 dynrank > 7 ? layout.dimension[7] : KOKKOS_INVALID_INDEX,
224 dynrank > 7 ? layout.stride[7] : (0));
230 template <
unsigned,
typename iType0,
class MapType>
232 const iType0&,
const MapType&) {
236 template <
unsigned R,
typename iType0,
class MapType,
typename iType1,
239 const iType0& rank,
const MapType& map,
const iType1& i, Args... args) {
240 if (static_cast<iType0>(R) < rank) {
241 return (
size_t(i) < map.extent(R)) &&
242 dyn_rank_view_verify_operator_bounds<R + 1>(rank, map, args...);
245 "DynRankView Debug Bounds Checking Error: at rank %u\n Extra "
246 "arguments beyond the rank must be zero \n",
249 dyn_rank_view_verify_operator_bounds<R + 1>(rank, map, args...);
252 dyn_rank_view_verify_operator_bounds<R + 1>(rank, map, args...);
256 template <
unsigned,
class MapType>
257 inline void dyn_rank_view_error_operator_bounds(
char*,
int,
const MapType&) {}
259 template <
unsigned R,
class MapType,
class iType,
class... Args>
260 inline void dyn_rank_view_error_operator_bounds(
char* buf,
int len,
262 const iType& i, Args... args) {
263 const int n = snprintf(
264 buf, len,
" %ld < %ld %c", static_cast<unsigned long>(i),
265 static_cast<unsigned long>(map.extent(R)), (
sizeof...(Args) ?
',' :
')'));
266 dyn_rank_view_error_operator_bounds<R + 1>(buf + n, len - n, map, args...);
270 template <
typename MemorySpace,
typename iType0,
typename iType1,
class MapType,
273 const iType0& op_rank,
const iType1& rank,
274 const Kokkos::Impl::SharedAllocationTracker& tracker,
const MapType& map,
276 if (static_cast<iType0>(rank) > op_rank) {
278 "DynRankView Bounds Checking Error: Need at least rank arguments to "
282 if (!dyn_rank_view_verify_operator_bounds<0>(rank, map, args...)) {
284 (
enum {LEN = 1024};
char buffer[LEN];
285 const std::string label = tracker.template get_label<MemorySpace>();
286 int n = snprintf(buffer, LEN,
"DynRankView bounds error of view %s (",
288 dyn_rank_view_error_operator_bounds<0>(buffer + n, LEN - n, map,
290 Kokkos::Impl::throw_runtime_exception(std::string(buffer));))
293 ((
void)tracker; Kokkos::abort("DynRankView bounds error");))
304 template <
class DstTraits,
class SrcTraits>
306 DstTraits, SrcTraits,
308 (std::is_same_v<typename DstTraits::memory_space,
309 typename SrcTraits::memory_space> &&
310 std::is_void_v<typename DstTraits::specialize> &&
311 std::is_void_v<typename SrcTraits::specialize> &&
312 (std::is_same_v<typename DstTraits::array_layout,
313 typename SrcTraits::array_layout> ||
314 ((std::is_same_v<typename DstTraits::array_layout,
315 Kokkos::LayoutLeft> ||
316 std::is_same_v<typename DstTraits::array_layout,
317 Kokkos::LayoutRight> ||
319 typename DstTraits::array_layout,
320 Kokkos::LayoutStride>)&&(std::is_same_v<typename SrcTraits::
322 Kokkos::LayoutLeft> ||
324 typename SrcTraits::array_layout,
325 Kokkos::LayoutRight> ||
327 typename SrcTraits::array_layout,
328 Kokkos::LayoutStride>)))),
329 Kokkos::Impl::ViewToDynRankViewTag>> {
332 is_assignable_value_type =
333 std::is_same_v<
typename DstTraits::value_type,
334 typename SrcTraits::value_type> ||
335 std::is_same_v<
typename DstTraits::value_type,
336 typename SrcTraits::const_value_type>
340 is_assignable_layout =
341 std::is_same_v<
typename DstTraits::array_layout,
342 typename SrcTraits::array_layout> ||
343 std::is_same_v<typename DstTraits::array_layout, Kokkos::LayoutStride>
347 enum { is_assignable = is_assignable_value_type && is_assignable_layout };
349 using DstType = ViewMapping<DstTraits, typename DstTraits::specialize>;
350 using SrcType = ViewMapping<SrcTraits, typename SrcTraits::specialize>;
352 template <
typename DT,
typename... DP,
typename ST,
typename... SP>
353 KOKKOS_INLINE_FUNCTION
static void assign(
354 Kokkos::DynRankView<DT, DP...>& dst,
const Kokkos::View<ST, SP...>& src) {
356 is_assignable_value_type,
357 "View assignment must have same value type or const = non-const");
360 is_assignable_layout,
361 "View assignment must have compatible layout or have rank <= 1");
365 using dst_offset_type =
typename DstType::offset_type;
366 dst.m_map.m_impl_offset = dst_offset_type(
367 std::integral_constant<unsigned, 0>(),
369 dst.m_map.m_impl_handle = Kokkos::Impl::ViewDataHandle<DstTraits>::assign(
370 src.m_map.m_impl_handle, src.m_track.m_tracker);
371 dst.m_track.m_tracker.assign(src.m_track.m_tracker, DstTraits::is_managed);
372 dst.m_rank = Kokkos::View<ST, SP...>::rank();
395 struct is_dyn_rank_view :
public std::false_type {};
397 template <
class D,
class... P>
398 struct is_dyn_rank_view<Kokkos::DynRankView<D, P...>> :
public std::true_type {
402 inline constexpr
bool is_dyn_rank_view_v = is_dyn_rank_view<T>::value;
406 template <
typename DataType,
class... Properties>
407 class DynRankView :
private View<DataType*******, Properties...> {
408 static_assert(!std::is_array_v<DataType> && !std::is_pointer_v<DataType>,
409 "Cannot template DynRankView with array or pointer datatype - "
413 template <
class,
class...>
414 friend class DynRankView;
415 template <
class,
class...>
416 friend class Kokkos::Impl::ViewMapping;
421 using drvtraits = ViewTraits<DataType, Properties...>;
423 using view_type =
View<DataType*******, Properties...>;
426 using drdtraits = Impl::DynRankDimTraits<typename view_type::specialize>;
430 using data_type =
typename drvtraits::data_type;
431 using const_data_type =
typename drvtraits::const_data_type;
432 using non_const_data_type =
typename drvtraits::non_const_data_type;
435 using value_type =
typename view_type::value_type;
436 using const_value_type =
typename view_type::const_value_type;
437 using non_const_value_type =
typename view_type::non_const_value_type;
438 using traits =
typename view_type::traits;
439 using array_layout =
typename view_type::array_layout;
441 using execution_space =
typename view_type::execution_space;
442 using memory_space =
typename view_type::memory_space;
443 using device_type =
typename view_type::device_type;
445 using memory_traits =
typename view_type::memory_traits;
446 using host_mirror_space =
typename view_type::host_mirror_space;
447 using size_type =
typename view_type::size_type;
449 using reference_type =
typename view_type::reference_type;
450 using pointer_type =
typename view_type::pointer_type;
452 using scalar_array_type = value_type;
453 using const_scalar_array_type = const_value_type;
454 using non_const_scalar_array_type = non_const_value_type;
455 using specialize =
typename view_type::specialize;
460 using index_type =
typename view_type::index_type;
461 using element_type =
typename view_type::element_type;
462 using rank_type =
typename view_type::rank_type;
463 using reference = reference_type;
464 using data_handle_type = pointer_type;
467 view_type& DownCast()
const {
return (view_type&)(*this); }
472 const view_type& ConstDownCast()
const {
return (
const view_type&)(*this); }
482 using array_type = DynRankView<
483 typename drvtraits::scalar_array_type,
typename drvtraits::array_layout,
484 typename drvtraits::device_type,
typename drvtraits::memory_traits>;
487 using const_type = DynRankView<
488 typename drvtraits::const_data_type,
typename drvtraits::array_layout,
489 typename drvtraits::device_type,
typename drvtraits::memory_traits>;
492 using non_const_type = DynRankView<
493 typename drvtraits::non_const_data_type,
typename drvtraits::array_layout,
494 typename drvtraits::device_type,
typename drvtraits::memory_traits>;
497 using HostMirror = DynRankView<
typename drvtraits::non_const_data_type,
498 typename drvtraits::array_layout,
499 typename drvtraits::host_mirror_space>;
501 using host_mirror_type = HostMirror;
518 std::is_same_v<typename traits::array_layout, Kokkos::LayoutLeft>,
521 std::is_same_v<typename traits::array_layout, Kokkos::LayoutRight>,
524 std::is_same_v<typename traits::array_layout, Kokkos::LayoutStride>,
526 is_default_map = std::is_void_v<typename traits::specialize> &&
527 (is_layout_left || is_layout_right || is_layout_stride),
530 is_default_map && std::is_same_v<reference_type, element_type&>
534 #if defined(KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK)
537 #define KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(ARG) \
538 Kokkos::Impl::runtime_check_memory_access_violation< \
539 typename traits::memory_space>( \
540 "Kokkos::DynRankView ERROR: attempt to access inaccessible memory " \
542 Kokkos::Impl::dyn_rank_view_verify_operator_bounds< \
543 typename traits::memory_space> \
548 #define KOKKOS_IMPL_VIEW_OPERATOR_VERIFY(ARG) \
549 Kokkos::Impl::runtime_check_memory_access_violation< \
550 typename traits::memory_space>( \
551 "Kokkos::DynRankView ERROR: attempt to access inaccessible memory " \
558 constexpr
unsigned rank()
const {
return m_rank; }
560 using view_type::data;
561 using view_type::extent;
562 using view_type::extent_int;
563 using view_type::impl_map;
564 using view_type::is_allocated;
565 using view_type::label;
566 using view_type::size;
567 using view_type::span;
568 using view_type::span_is_contiguous;
569 using view_type::stride;
570 using view_type::stride_0;
571 using view_type::stride_1;
572 using view_type::stride_2;
573 using view_type::stride_3;
574 using view_type::stride_4;
575 using view_type::stride_5;
576 using view_type::stride_6;
577 using view_type::stride_7;
578 using view_type::use_count;
580 #ifdef KOKKOS_ENABLE_CUDA
581 KOKKOS_FUNCTION reference_type
582 operator()(index_type i0 = 0, index_type i1 = 0, index_type i2 = 0,
583 index_type i3 = 0, index_type i4 = 0, index_type i5 = 0,
584 index_type i6 = 0)
const {
585 return view_type::operator()(i0, i1, i2, i3, i4, i5, i6);
595 KOKKOS_FUNCTION reference_type operator()()
const {
596 #ifdef KOKKOS_ENABLE_DEBUG
599 "DynRankView rank 0 operator() called with invalid number of "
602 #ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
603 if constexpr (is_default_access) {
604 return view_type::data()[0];
607 return view_type::operator()(0, 0, 0, 0, 0, 0, 0);
610 KOKKOS_FUNCTION reference_type operator()(index_type i0)
const {
611 #ifdef KOKKOS_ENABLE_DEBUG
616 "DynRankView rank 1 operator() called with invalid number of "
619 #ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
620 if constexpr (is_default_access) {
621 if constexpr (is_layout_stride) {
622 return view_type::data()[i0 * view_type::stride(0)];
624 return view_type::data()[i0];
628 return view_type::operator()(i0, 0, 0, 0, 0, 0, 0);
629 #if defined KOKKOS_COMPILER_INTEL || \
630 (defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
631 !defined(KOKKOS_COMPILER_MSVC))
632 __builtin_unreachable();
636 KOKKOS_FUNCTION reference_type operator()(index_type i0,
637 index_type i1)
const {
638 #ifdef KOKKOS_ENABLE_DEBUG
643 "DynRankView rank 2 operator() called with invalid number of "
646 #ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
647 if constexpr (is_default_access) {
648 if constexpr (is_layout_left) {
649 return view_type::data()[i0 + i1 * view_type::stride(1)];
650 }
else if constexpr (is_layout_right) {
651 return view_type::data()[i0 * view_type::extent(1) + i1];
653 return view_type::data()[i0 * view_type::stride(0) +
654 i1 * view_type::stride(1)];
658 return view_type::operator()(i0, i1, 0, 0, 0, 0, 0);
659 #if defined KOKKOS_COMPILER_INTEL || \
660 (defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
661 !defined(KOKKOS_COMPILER_MSVC))
662 __builtin_unreachable();
666 KOKKOS_FUNCTION reference_type operator()(index_type i0, index_type i1,
667 index_type i2)
const {
668 #ifdef KOKKOS_ENABLE_DEBUG
673 "DynRankView rank 3 operator() called with invalid number of "
676 #ifndef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
677 if constexpr (is_default_access) {
678 if constexpr (is_layout_left) {
679 return view_type::data()[i0 + view_type::stride(1) *
680 (i1 + i2 * view_type::extent(1))];
681 }
else if constexpr (is_layout_right) {
682 return view_type::data()[(i0 * view_type::extent(1) + i1) *
683 view_type::extent(2) +
686 return view_type::data()[i0 * view_type::stride(0) +
687 i1 * view_type::stride(1) +
688 i2 * view_type::stride(2)];
692 return view_type::operator()(i0, i1, i2, 0, 0, 0, 0);
693 #if defined KOKKOS_COMPILER_INTEL || \
694 (defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
695 !defined(KOKKOS_COMPILER_MSVC))
696 __builtin_unreachable();
700 KOKKOS_FUNCTION reference_type operator()(index_type i0, index_type i1,
701 index_type i2, index_type i3,
704 index_type i6 = 0)
const {
705 return view_type::operator()(i0, i1, i2, i3, i4, i5, i6);
711 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
712 KOKKOS_FUNCTION reference_type operator[](index_type i0)
const {
713 if constexpr (std::is_same_v<
typename drvtraits::value_type,
714 typename drvtraits::scalar_array_type>) {
715 return view_type::data()[i0];
717 const size_t dim_scalar = view_type::impl_map().dimension_scalar();
718 const size_t bytes = view_type::span() / dim_scalar;
720 using tmp_view_type =
721 Kokkos::View<DataType*,
typename traits::array_layout,
722 typename traits::device_type,
723 Kokkos::MemoryTraits<traits::memory_traits::impl_value |
724 unsigned(Kokkos::Unmanaged)>>;
725 tmp_view_type rankone_view(view_type::data(), bytes, dim_scalar);
726 return rankone_view(i0);
730 KOKKOS_FUNCTION reference_type operator[](index_type i0)
const {
731 #ifdef KOKKOS_ENABLE_DEBUG
733 Kokkos::abort(
"DynRankView operator[] can only be used for rank-1");
735 return view_type::operator()(i0, 0, 0, 0, 0, 0, 0);
739 KOKKOS_FUNCTION reference_type access(index_type i0 = 0, index_type i1 = 0,
740 index_type i2 = 0, index_type i3 = 0,
741 index_type i4 = 0, index_type i5 = 0,
742 index_type i6 = 0)
const {
743 return view_type::operator()(i0, i1, i2, i3, i4, i5, i6);
749 KOKKOS_DEFAULTED_FUNCTION
750 ~DynRankView() =
default;
752 KOKKOS_DEFAULTED_FUNCTION DynRankView() =
default;
758 template <
class RT,
class... RP>
759 KOKKOS_FUNCTION DynRankView(
const DynRankView<RT, RP...>& rhs)
760 : view_type(rhs), m_rank(rhs.m_rank) {}
762 template <
class RT,
class... RP>
763 KOKKOS_FUNCTION DynRankView& operator=(
const DynRankView<RT, RP...>& rhs) {
764 view_type::operator=(rhs);
769 #if 0 // TODO: this will later be swapped in depending on whether the new View
773 KOKKOS_FUNCTION
typename view_type::extents_type create_rank7_extents(
775 return typename view_type::extents_type(
776 ext.rank() > 0 ? ext.extent(0) : 1, ext.rank() > 1 ? ext.extent(1) : 1,
777 ext.rank() > 2 ? ext.extent(2) : 1, ext.rank() > 3 ? ext.extent(3) : 1,
778 ext.rank() > 4 ? ext.extent(4) : 1, ext.rank() > 5 ? ext.extent(5) : 1,
779 ext.rank() > 6 ? ext.extent(6) : 1);
784 template <
class RT,
class... RP>
785 KOKKOS_INLINE_FUNCTION DynRankView(
const View<RT, RP...>& rhs,
787 : view_type(rhs.data_handle(), drdtraits::createLayout(rhs.layout())),
789 if (new_rank > rhs.rank())
791 "Attempting to construct DynRankView from View and new rank, with "
792 "the new rank being too large.");
795 template <
class RT,
class... RP>
796 KOKKOS_INLINE_FUNCTION DynRankView& operator=(
const View<RT, RP...>& rhs) {
797 view_type::operator=(view_type(
799 typename view_type::mapping_type(create_rank7_extents(rhs.extents())),
805 template <
class RT,
class... RP>
806 KOKKOS_FUNCTION DynRankView(
const View<RT, RP...>& rhs,
size_t new_rank) {
807 using SrcTraits =
typename View<RT, RP...>::traits;
809 Kokkos::Impl::ViewMapping<traits, SrcTraits,
811 static_assert(Mapping::is_assignable,
812 "Incompatible View to DynRankView copy assignment");
813 if (new_rank > View<RT, RP...>::rank())
815 "Attempting to construct DynRankView from View and new rank, with "
816 "the new rank being too large.");
817 Mapping::assign(*
this, rhs);
821 template <
class RT,
class... RP>
822 KOKKOS_FUNCTION DynRankView& operator=(
const View<RT, RP...>& rhs) {
823 using SrcTraits =
typename View<RT, RP...>::traits;
825 Kokkos::Impl::ViewMapping<traits, SrcTraits,
826 Kokkos::Impl::ViewToDynRankViewTag>;
827 static_assert(Mapping::is_assignable,
828 "Incompatible View to DynRankView copy assignment");
829 Mapping::assign(*
this, rhs);
830 m_rank = View<RT, RP...>::rank();
835 template <
class RT,
class... RP>
836 KOKKOS_FUNCTION DynRankView(
const View<RT, RP...>& rhs)
837 : DynRankView(rhs,
View<RT, RP...>::rank()) {}
848 template <
class... P>
849 explicit KOKKOS_FUNCTION DynRankView(
850 const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
851 std::enable_if_t<Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
852 typename traits::array_layout
const&>
854 : view_type(arg_prop, drdtraits::template createLayout<traits, P...>(
855 arg_prop, arg_layout)),
856 m_rank(drdtraits::computeRank(arg_prop, arg_layout)) {}
858 template <
class... P>
859 explicit DynRankView(
860 const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
861 std::enable_if_t<!Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
862 typename traits::array_layout
const&>
864 : view_type(arg_prop, drdtraits::template createLayout<traits, P...>(
865 arg_prop, arg_layout)),
866 m_rank(drdtraits::computeRank(arg_prop, arg_layout)) {}
874 template <
class... P>
875 explicit KOKKOS_FUNCTION DynRankView(
876 const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
877 std::enable_if_t<Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
879 arg_N0 = KOKKOS_INVALID_INDEX,
880 const size_t arg_N1 = KOKKOS_INVALID_INDEX,
881 const size_t arg_N2 = KOKKOS_INVALID_INDEX,
882 const size_t arg_N3 = KOKKOS_INVALID_INDEX,
883 const size_t arg_N4 = KOKKOS_INVALID_INDEX,
884 const size_t arg_N5 = KOKKOS_INVALID_INDEX,
885 const size_t arg_N6 = KOKKOS_INVALID_INDEX,
886 const size_t arg_N7 = KOKKOS_INVALID_INDEX)
887 : DynRankView(arg_prop, typename traits::array_layout(
888 arg_N0, arg_N1, arg_N2, arg_N3, arg_N4,
889 arg_N5, arg_N6, arg_N7)) {}
891 template <
class... P>
892 explicit DynRankView(
893 const Kokkos::Impl::ViewCtorProp<P...>& arg_prop,
894 std::enable_if_t<!Kokkos::Impl::ViewCtorProp<P...>::has_pointer,
896 arg_N0 = KOKKOS_INVALID_INDEX,
897 const size_t arg_N1 = KOKKOS_INVALID_INDEX,
898 const size_t arg_N2 = KOKKOS_INVALID_INDEX,
899 const size_t arg_N3 = KOKKOS_INVALID_INDEX,
900 const size_t arg_N4 = KOKKOS_INVALID_INDEX,
901 const size_t arg_N5 = KOKKOS_INVALID_INDEX,
902 const size_t arg_N6 = KOKKOS_INVALID_INDEX,
903 const size_t arg_N7 = KOKKOS_INVALID_INDEX)
904 : DynRankView(arg_prop, typename traits::array_layout(
905 arg_N0, arg_N1, arg_N2, arg_N3, arg_N4,
906 arg_N5, arg_N6, arg_N7)) {}
909 template <
typename Label>
910 explicit inline DynRankView(
911 const Label& arg_label,
912 std::enable_if_t<Kokkos::Impl::is_view_label<Label>::value,
913 typename traits::array_layout>
const& arg_layout)
914 : DynRankView(Kokkos::Impl::ViewCtorProp<std::string>(arg_label),
918 template <
typename Label>
919 explicit inline DynRankView(
920 const Label& arg_label,
921 std::enable_if_t<Kokkos::Impl::is_view_label<Label>::value,
const size_t>
922 arg_N0 = KOKKOS_INVALID_INDEX,
923 const size_t arg_N1 = KOKKOS_INVALID_INDEX,
924 const size_t arg_N2 = KOKKOS_INVALID_INDEX,
925 const size_t arg_N3 = KOKKOS_INVALID_INDEX,
926 const size_t arg_N4 = KOKKOS_INVALID_INDEX,
927 const size_t arg_N5 = KOKKOS_INVALID_INDEX,
928 const size_t arg_N6 = KOKKOS_INVALID_INDEX,
929 const size_t arg_N7 = KOKKOS_INVALID_INDEX)
931 Kokkos::Impl::ViewCtorProp<std::string>(arg_label),
932 typename traits::array_layout(arg_N0, arg_N1, arg_N2, arg_N3,
933 arg_N4, arg_N5, arg_N6, arg_N7)) {}
938 static constexpr
size_t required_allocation_size(
939 const size_t arg_N0 = 1,
const size_t arg_N1 = 1,
const size_t arg_N2 = 1,
940 const size_t arg_N3 = 1,
const size_t arg_N4 = 1,
const size_t arg_N5 = 1,
941 const size_t arg_N6 = 1,
942 [[maybe_unused]]
const size_t arg_N7 = KOKKOS_INVALID_INDEX) {
944 return view_type::required_allocation_size(arg_N0, arg_N1, arg_N2, arg_N3,
945 arg_N4, arg_N5, arg_N6);
948 explicit KOKKOS_FUNCTION DynRankView(
949 typename view_type::pointer_type arg_ptr,
950 const size_t arg_N0 = KOKKOS_INVALID_INDEX,
951 const size_t arg_N1 = KOKKOS_INVALID_INDEX,
952 const size_t arg_N2 = KOKKOS_INVALID_INDEX,
953 const size_t arg_N3 = KOKKOS_INVALID_INDEX,
954 const size_t arg_N4 = KOKKOS_INVALID_INDEX,
955 const size_t arg_N5 = KOKKOS_INVALID_INDEX,
956 const size_t arg_N6 = KOKKOS_INVALID_INDEX,
957 const size_t arg_N7 = KOKKOS_INVALID_INDEX)
959 Kokkos::Impl::ViewCtorProp<typename view_type::pointer_type>(
961 arg_N0, arg_N1, arg_N2, arg_N3, arg_N4, arg_N5, arg_N6, arg_N7) {}
963 explicit KOKKOS_FUNCTION DynRankView(
964 typename view_type::pointer_type arg_ptr,
965 typename traits::array_layout& arg_layout)
967 Kokkos::Impl::ViewCtorProp<typename view_type::pointer_type>(
975 static inline size_t shmem_size(
976 const size_t arg_N0 = 1,
const size_t arg_N1 = 1,
const size_t arg_N2 = 1,
977 const size_t arg_N3 = 1,
const size_t arg_N4 = 1,
const size_t arg_N5 = 1,
978 const size_t arg_N6 = 1,
const size_t arg_N7 = KOKKOS_INVALID_INDEX) {
979 return view_type::shmem_size(arg_N0, arg_N1, arg_N2, arg_N3, arg_N4, arg_N5,
983 explicit KOKKOS_FUNCTION DynRankView(
984 const typename traits::execution_space::scratch_memory_space& arg_space,
985 const typename traits::array_layout& arg_layout)
986 : view_type(arg_space, drdtraits::createLayout(arg_layout)),
987 m_rank(drdtraits::computeRank(arg_layout)) {}
989 explicit KOKKOS_FUNCTION DynRankView(
990 const typename traits::execution_space::scratch_memory_space& arg_space,
991 const size_t arg_N0 = KOKKOS_INVALID_INDEX,
992 const size_t arg_N1 = KOKKOS_INVALID_INDEX,
993 const size_t arg_N2 = KOKKOS_INVALID_INDEX,
994 const size_t arg_N3 = KOKKOS_INVALID_INDEX,
995 const size_t arg_N4 = KOKKOS_INVALID_INDEX,
996 const size_t arg_N5 = KOKKOS_INVALID_INDEX,
997 const size_t arg_N6 = KOKKOS_INVALID_INDEX,
998 const size_t arg_N7 = KOKKOS_INVALID_INDEX)
1000 : DynRankView(arg_space, typename traits::array_layout(
1001 arg_N0, arg_N1, arg_N2, arg_N3, arg_N4,
1002 arg_N5, arg_N6, arg_N7)) {}
1004 KOKKOS_FUNCTION constexpr
auto layout()
const {
1006 case 0:
return Impl::as_view_of_rank_n<0>(*this).layout();
1007 case 1:
return Impl::as_view_of_rank_n<1>(*this).layout();
1008 case 2:
return Impl::as_view_of_rank_n<2>(*this).layout();
1009 case 3:
return Impl::as_view_of_rank_n<3>(*this).layout();
1010 case 4:
return Impl::as_view_of_rank_n<4>(*this).layout();
1011 case 5:
return Impl::as_view_of_rank_n<5>(*this).layout();
1012 case 6:
return Impl::as_view_of_rank_n<6>(*this).layout();
1013 case 7:
return Impl::as_view_of_rank_n<7>(*this).layout();
1018 "Calling DynRankView::layout on DRV of unexpected rank " +
1019 std::to_string(rank()))
1021 KOKKOS_IF_ON_DEVICE(
1023 "Calling DynRankView::layout on DRV of unexpected rank");)
1026 return view_type::layout();
1030 template <
typename D,
class... P>
1031 KOKKOS_FUNCTION constexpr
unsigned rank(
const DynRankView<D, P...>& DRV) {
1042 struct DynRankSubviewTag {};
1046 template <
class V,
class... Args>
1047 using Subdynrankview =
1048 typename Kokkos::Impl::ViewMapping<Kokkos::Impl::DynRankSubviewTag, V,
1051 template <
class... DRVArgs,
class SubArg0 = int,
class SubArg1 = int,
1052 class SubArg2 = int,
class SubArg3 = int,
class SubArg4 = int,
1053 class SubArg5 = int,
class SubArg6 =
int>
1054 KOKKOS_INLINE_FUNCTION
auto subdynrankview(
1055 const DynRankView<DRVArgs...>& drv, SubArg0 arg0 = SubArg0{},
1056 SubArg1 arg1 = SubArg1{}, SubArg2 arg2 = SubArg2{},
1057 SubArg3 arg3 = SubArg3{}, SubArg4 arg4 = SubArg4{},
1058 SubArg5 arg5 = SubArg5{}, SubArg6 arg6 = SubArg6{}) {
1059 auto sub = subview(drv.DownCast(), arg0, arg1, arg2, arg3, arg4, arg5, arg6);
1060 using sub_t = decltype(sub);
1061 size_t new_rank = (drv.rank() > 0 && !std::is_integral_v<SubArg0> ? 1 : 0) +
1062 (drv.rank() > 1 && !std::is_integral_v<SubArg1> ? 1 : 0) +
1063 (drv.rank() > 2 && !std::is_integral_v<SubArg2> ? 1 : 0) +
1064 (drv.rank() > 3 && !std::is_integral_v<SubArg3> ? 1 : 0) +
1065 (drv.rank() > 4 && !std::is_integral_v<SubArg4> ? 1 : 0) +
1066 (drv.rank() > 5 && !std::is_integral_v<SubArg5> ? 1 : 0) +
1067 (drv.rank() > 6 && !std::is_integral_v<SubArg6> ? 1 : 0);
1071 typename sub_t::device_type,
typename sub_t::memory_traits>;
1072 return static_cast<return_type
>(
1073 DynRankView<
typename sub_t::value_type,
typename sub_t::array_layout,
1074 typename sub_t::device_type,
typename sub_t::memory_traits>(
1077 template <
class... DRVArgs,
class SubArg0 = int,
class SubArg1 = int,
1078 class SubArg2 = int,
class SubArg3 = int,
class SubArg4 = int,
1079 class SubArg5 = int,
class SubArg6 =
int>
1080 KOKKOS_INLINE_FUNCTION
auto subview(
1081 const DynRankView<DRVArgs...>& drv, SubArg0 arg0 = SubArg0{},
1082 SubArg1 arg1 = SubArg1{}, SubArg2 arg2 = SubArg2{},
1083 SubArg3 arg3 = SubArg3{}, SubArg4 arg4 = SubArg4{},
1084 SubArg5 arg5 = SubArg5{}, SubArg6 arg6 = SubArg6{}) {
1085 return subdynrankview(drv, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
1093 template <
class LT,
class... LP,
class RT,
class... RP>
1094 KOKKOS_INLINE_FUNCTION
bool operator==(
const DynRankView<LT, LP...>& lhs,
1095 const DynRankView<RT, RP...>& rhs) {
1097 using lhs_traits = ViewTraits<LT, LP...>;
1098 using rhs_traits = ViewTraits<RT, RP...>;
1100 return std::is_same_v<
typename lhs_traits::const_value_type,
1101 typename rhs_traits::const_value_type> &&
1102 std::is_same_v<
typename lhs_traits::array_layout,
1103 typename rhs_traits::array_layout> &&
1104 std::is_same_v<
typename lhs_traits::memory_space,
1105 typename rhs_traits::memory_space> &&
1106 lhs.rank() == rhs.rank() && lhs.data() == rhs.data() &&
1107 lhs.span() == rhs.span() && lhs.extent(0) == rhs.extent(0) &&
1108 lhs.extent(1) == rhs.extent(1) && lhs.extent(2) == rhs.extent(2) &&
1109 lhs.extent(3) == rhs.extent(3) && lhs.extent(4) == rhs.extent(4) &&
1110 lhs.extent(5) == rhs.extent(5) && lhs.extent(6) == rhs.extent(6) &&
1111 lhs.extent(7) == rhs.extent(7);
1114 template <
class LT,
class... LP,
class RT,
class... RP>
1115 KOKKOS_INLINE_FUNCTION
bool operator!=(
const DynRankView<LT, LP...>& lhs,
1116 const DynRankView<RT, RP...>& rhs) {
1117 return !(operator==(lhs, rhs));
1127 template <
class OutputView,
class Enable =
void>
1128 struct DynRankViewFill {
1129 using const_value_type =
typename OutputView::traits::const_value_type;
1131 const OutputView output;
1132 const_value_type input;
1134 KOKKOS_INLINE_FUNCTION
1135 void operator()(
const size_t i0)
const {
1136 const size_t n1 = output.extent(1);
1137 const size_t n2 = output.extent(2);
1138 const size_t n3 = output.extent(3);
1139 const size_t n4 = output.extent(4);
1140 const size_t n5 = output.extent(5);
1141 const size_t n6 = output.extent(6);
1143 for (
size_t i1 = 0; i1 < n1; ++i1) {
1144 for (
size_t i2 = 0; i2 < n2; ++i2) {
1145 for (
size_t i3 = 0; i3 < n3; ++i3) {
1146 for (
size_t i4 = 0; i4 < n4; ++i4) {
1147 for (
size_t i5 = 0; i5 < n5; ++i5) {
1148 for (
size_t i6 = 0; i6 < n6; ++i6) {
1149 output.access(i0, i1, i2, i3, i4, i5, i6) = input;
1158 DynRankViewFill(
const OutputView& arg_out, const_value_type& arg_in)
1159 : output(arg_out), input(arg_in) {
1160 using execution_space =
typename OutputView::execution_space;
1163 Kokkos::parallel_for(
"Kokkos::DynRankViewFill", Policy(0, output.extent(0)),
1168 template <
class OutputView>
1169 struct DynRankViewFill<OutputView, std::enable_if_t<OutputView::rank == 0>> {
1170 DynRankViewFill(
const OutputView& dst,
1171 const typename OutputView::const_value_type& src) {
1172 Kokkos::Impl::DeepCopy<
typename OutputView::memory_space,
1174 dst.data(), &src,
sizeof(
typename OutputView::const_value_type));
1178 template <
class OutputView,
class InputView,
1179 class ExecSpace =
typename OutputView::execution_space>
1180 struct DynRankViewRemap {
1181 const OutputView output;
1182 const InputView input;
1192 DynRankViewRemap(
const ExecSpace& exec_space,
const OutputView& arg_out,
1193 const InputView& arg_in)
1196 n0(std::min((size_t)arg_out.extent(0), (size_t)arg_in.extent(0))),
1197 n1(std::min((size_t)arg_out.extent(1), (size_t)arg_in.extent(1))),
1198 n2(std::min((size_t)arg_out.extent(2), (size_t)arg_in.extent(2))),
1199 n3(std::min((size_t)arg_out.extent(3), (size_t)arg_in.extent(3))),
1200 n4(std::min((size_t)arg_out.extent(4), (size_t)arg_in.extent(4))),
1201 n5(std::min((size_t)arg_out.extent(5), (size_t)arg_in.extent(5))),
1202 n6(std::min((size_t)arg_out.extent(6), (size_t)arg_in.extent(6))),
1203 n7(std::min((size_t)arg_out.extent(7), (size_t)arg_in.extent(7))) {
1206 Kokkos::parallel_for(
"Kokkos::DynRankViewRemap", Policy(exec_space, 0, n0),
1210 DynRankViewRemap(
const OutputView& arg_out,
const InputView& arg_in)
1213 n0(std::min((size_t)arg_out.extent(0), (size_t)arg_in.extent(0))),
1214 n1(std::min((size_t)arg_out.extent(1), (size_t)arg_in.extent(1))),
1215 n2(std::min((size_t)arg_out.extent(2), (size_t)arg_in.extent(2))),
1216 n3(std::min((size_t)arg_out.extent(3), (size_t)arg_in.extent(3))),
1217 n4(std::min((size_t)arg_out.extent(4), (size_t)arg_in.extent(4))),
1218 n5(std::min((size_t)arg_out.extent(5), (size_t)arg_in.extent(5))),
1219 n6(std::min((size_t)arg_out.extent(6), (size_t)arg_in.extent(6))),
1220 n7(std::min((size_t)arg_out.extent(7), (size_t)arg_in.extent(7))) {
1223 Kokkos::parallel_for(
"Kokkos::DynRankViewRemap", Policy(0, n0), *
this);
1226 KOKKOS_INLINE_FUNCTION
1227 void operator()(
const size_t i0)
const {
1228 for (
size_t i1 = 0; i1 < n1; ++i1) {
1229 for (
size_t i2 = 0; i2 < n2; ++i2) {
1230 for (
size_t i3 = 0; i3 < n3; ++i3) {
1231 for (
size_t i4 = 0; i4 < n4; ++i4) {
1232 for (
size_t i5 = 0; i5 < n5; ++i5) {
1233 for (
size_t i6 = 0; i6 < n6; ++i6) {
1234 output.access(i0, i1, i2, i3, i4, i5, i6) =
1235 input.access(i0, i1, i2, i3, i4, i5, i6);
1255 template <
unsigned N,
typename T,
typename... Args>
1256 KOKKOS_FUNCTION View<typename ViewDataTypeFromRank<T, N>::type, Args...>
1258 DynRankView<T, Args...> v,
1260 std::is_same_v<
typename ViewTraits<T, Args...>::specialize,
void>>*) {
1261 if (v.rank() != N) {
1263 const std::string message =
1264 "Converting DynRankView of rank " + std::to_string(v.rank()) +
1265 " to a View of mis-matched rank " + std::to_string(N) +
"!";
1266 Kokkos::abort(message.c_str());)
1267 KOKKOS_IF_ON_DEVICE(
1268 Kokkos::abort(
"Converting DynRankView to a View of mis-matched rank!");)
1271 auto layout = v.DownCast().layout();
1276 for (
int i = N; i < 7; ++i)
1277 layout.dimension[i] = KOKKOS_IMPL_CTOR_DEFAULT_ARG;
1280 return View<typename RankDataType<T, N>::type, Args...>(v.data(), layout);
1283 template <
typename Function,
typename... Args>
1284 void apply_to_view_of_static_rank(Function&& f, DynRankView<Args...> a) {
1286 case 0: f(as_view_of_rank_n<0>(a));
break;
1287 case 1: f(as_view_of_rank_n<1>(a));
break;
1288 case 2: f(as_view_of_rank_n<2>(a));
break;
1289 case 3: f(as_view_of_rank_n<3>(a));
break;
1290 case 4: f(as_view_of_rank_n<4>(a));
break;
1291 case 5: f(as_view_of_rank_n<5>(a));
break;
1292 case 6: f(as_view_of_rank_n<6>(a));
break;
1293 case 7: f(as_view_of_rank_n<7>(a));
break;
1298 "Trying to apply a function to a view of unexpected rank " +
1299 std::to_string(rank(a)))
1301 KOKKOS_IF_ON_DEVICE(
1303 "Trying to apply a function to a view of unexpected rank");)
1310 template <class ExecSpace, class DT, class... DP>
1311 inline void deep_copy(
1312 const ExecSpace& e,
const DynRankView<DT, DP...>& dst,
1313 typename ViewTraits<DT, DP...>::const_value_type& value,
1314 std::enable_if_t<std::is_same_v<
typename ViewTraits<DT, DP...>::specialize,
1315 void>>* =
nullptr) {
1317 std::is_same_v<
typename ViewTraits<DT, DP...>::non_const_value_type,
1318 typename ViewTraits<DT, DP...>::value_type>,
1319 "deep_copy requires non-const type");
1321 Impl::apply_to_view_of_static_rank(
1322 [=](
auto view) { deep_copy(e, view, value); }, dst);
1325 template <
class DT,
class... DP>
1326 inline void deep_copy(
1327 const DynRankView<DT, DP...>& dst,
1328 typename ViewTraits<DT, DP...>::const_value_type& value,
1329 std::enable_if_t<std::is_same_v<
typename ViewTraits<DT, DP...>::specialize,
1330 void>>* =
nullptr) {
1331 Impl::apply_to_view_of_static_rank([=](
auto view) { deep_copy(view, value); },
1336 template <
class ExecSpace,
class ST,
class... SP>
1337 inline void deep_copy(
1339 typename ViewTraits<ST, SP...>::non_const_value_type& dst,
1340 const DynRankView<ST, SP...>& src,
1341 std::enable_if_t<std::is_same_v<
typename ViewTraits<ST, SP...>::specialize,
1343 deep_copy(e, dst, Impl::as_view_of_rank_n<0>(src));
1346 template <
class ST,
class... SP>
1347 inline void deep_copy(
1348 typename ViewTraits<ST, SP...>::non_const_value_type& dst,
1349 const DynRankView<ST, SP...>& src,
1350 std::enable_if_t<std::is_same_v<
typename ViewTraits<ST, SP...>::specialize,
1352 deep_copy(dst, Impl::as_view_of_rank_n<0>(src));
1361 template <
class ExecSpace,
class DstType,
class SrcType>
1362 inline void deep_copy(
1363 const ExecSpace& exec_space,
const DstType& dst,
const SrcType& src,
1364 std::enable_if_t<(std::is_void_v<typename DstType::traits::specialize> &&
1365 std::is_void_v<typename SrcType::traits::specialize> &&
1366 (Kokkos::is_dyn_rank_view<DstType>::value ||
1367 Kokkos::is_dyn_rank_view<SrcType>::value))>* =
nullptr) {
1368 static_assert(std::is_same_v<
typename DstType::traits::value_type,
1369 typename DstType::traits::non_const_value_type>,
1370 "deep_copy requires non-const destination type");
1372 switch (rank(dst)) {
1374 deep_copy(exec_space, Impl::as_view_of_rank_n<0>(dst),
1375 Impl::as_view_of_rank_n<0>(src));
1378 deep_copy(exec_space, Impl::as_view_of_rank_n<1>(dst),
1379 Impl::as_view_of_rank_n<1>(src));
1382 deep_copy(exec_space, Impl::as_view_of_rank_n<2>(dst),
1383 Impl::as_view_of_rank_n<2>(src));
1386 deep_copy(exec_space, Impl::as_view_of_rank_n<3>(dst),
1387 Impl::as_view_of_rank_n<3>(src));
1390 deep_copy(exec_space, Impl::as_view_of_rank_n<4>(dst),
1391 Impl::as_view_of_rank_n<4>(src));
1394 deep_copy(exec_space, Impl::as_view_of_rank_n<5>(dst),
1395 Impl::as_view_of_rank_n<5>(src));
1398 deep_copy(exec_space, Impl::as_view_of_rank_n<6>(dst),
1399 Impl::as_view_of_rank_n<6>(src));
1402 deep_copy(exec_space, Impl::as_view_of_rank_n<7>(dst),
1403 Impl::as_view_of_rank_n<7>(src));
1406 Kokkos::Impl::throw_runtime_exception(
1407 "Calling DynRankView deep_copy with a view of unexpected rank " +
1408 std::to_string(rank(dst)));
1412 template <
class DstType,
class SrcType>
1413 inline void deep_copy(
1414 const DstType& dst,
const SrcType& src,
1415 std::enable_if_t<(std::is_void_v<typename DstType::traits::specialize> &&
1416 std::is_void_v<typename SrcType::traits::specialize> &&
1417 (Kokkos::is_dyn_rank_view<DstType>::value ||
1418 Kokkos::is_dyn_rank_view<SrcType>::value))>* =
nullptr) {
1419 static_assert(std::is_same_v<
typename DstType::traits::value_type,
1420 typename DstType::traits::non_const_value_type>,
1421 "deep_copy requires non-const destination type");
1423 switch (rank(dst)) {
1425 deep_copy(Impl::as_view_of_rank_n<0>(dst),
1426 Impl::as_view_of_rank_n<0>(src));
1429 deep_copy(Impl::as_view_of_rank_n<1>(dst),
1430 Impl::as_view_of_rank_n<1>(src));
1433 deep_copy(Impl::as_view_of_rank_n<2>(dst),
1434 Impl::as_view_of_rank_n<2>(src));
1437 deep_copy(Impl::as_view_of_rank_n<3>(dst),
1438 Impl::as_view_of_rank_n<3>(src));
1441 deep_copy(Impl::as_view_of_rank_n<4>(dst),
1442 Impl::as_view_of_rank_n<4>(src));
1445 deep_copy(Impl::as_view_of_rank_n<5>(dst),
1446 Impl::as_view_of_rank_n<5>(src));
1449 deep_copy(Impl::as_view_of_rank_n<6>(dst),
1450 Impl::as_view_of_rank_n<6>(src));
1453 deep_copy(Impl::as_view_of_rank_n<7>(dst),
1454 Impl::as_view_of_rank_n<7>(src));
1457 Kokkos::Impl::throw_runtime_exception(
1458 "Calling DynRankView deep_copy with a view of unexpected rank " +
1459 std::to_string(rank(dst)));
1472 template <
class Space,
class T,
class... P>
1473 struct MirrorDRViewType {
1475 using src_view_type =
typename Kokkos::DynRankView<T, P...>;
1477 using memory_space =
typename Space::memory_space;
1481 std::is_same_v<memory_space, typename src_view_type::memory_space>
1484 using array_layout =
typename src_view_type::array_layout;
1487 using data_type =
typename src_view_type::non_const_data_type;
1489 using dest_view_type = Kokkos::DynRankView<data_type, array_layout, Space>;
1493 std::conditional_t<is_same_memspace, src_view_type, dest_view_type>;
1503 template <
class T,
class... P,
class... ViewCtorArgs>
1504 inline auto create_mirror(
const DynRankView<T, P...>& src,
1505 const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop) {
1506 check_view_ctor_args_create_mirror<ViewCtorArgs...>();
1508 auto prop_copy = Impl::with_properties_if_unset(
1509 arg_prop, std::string(src.label()).append(
"_mirror"));
1511 if constexpr (Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space) {
1512 using dst_type =
typename Impl::MirrorDRViewType<
1513 typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
1514 P...>::dest_view_type;
1515 return dst_type(prop_copy,
1516 Impl::reconstructLayout(src.layout(), src.rank()));
1518 using src_type = DynRankView<T, P...>;
1519 using dst_type =
typename src_type::HostMirror;
1521 return dst_type(prop_copy,
1522 Impl::reconstructLayout(src.layout(), src.rank()));
1524 #if defined(KOKKOS_COMPILER_INTEL) || \
1525 (defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
1526 !defined(KOKKOS_COMPILER_MSVC))
1527 __builtin_unreachable();
1534 template <
class T,
class... P,
1535 class Enable = std::enable_if_t<
1536 std::is_void_v<
typename ViewTraits<T, P...>::specialize>>>
1537 inline auto create_mirror(
const DynRankView<T, P...>& src) {
1538 return Impl::create_mirror(src, Kokkos::view_alloc());
1542 template <
class T,
class... P,
1543 class Enable = std::enable_if_t<
1544 std::is_void_v<
typename ViewTraits<T, P...>::specialize>>>
1545 inline auto create_mirror(Kokkos::Impl::WithoutInitializing_t wi,
1546 const DynRankView<T, P...>& src) {
1547 return Impl::create_mirror(src, Kokkos::view_alloc(wi));
1551 template <
class Space,
class T,
class... P,
1552 class Enable = std::enable_if_t<
1553 Kokkos::is_space<Space>::value &&
1554 std::is_void_v<
typename ViewTraits<T, P...>::specialize>>>
1555 inline auto create_mirror(
const Space&,
1556 const Kokkos::DynRankView<T, P...>& src) {
1557 return Impl::create_mirror(
1558 src, Kokkos::view_alloc(
typename Space::memory_space{}));
1562 template <
class Space,
class T,
class... P,
1563 class Enable = std::enable_if_t<
1564 Kokkos::is_space<Space>::value &&
1565 std::is_void_v<
typename ViewTraits<T, P...>::specialize>>>
1566 inline auto create_mirror(Kokkos::Impl::WithoutInitializing_t wi,
const Space&,
1567 const Kokkos::DynRankView<T, P...>& src) {
1568 return Impl::create_mirror(
1569 src, Kokkos::view_alloc(wi,
typename Space::memory_space{}));
1574 template <
class T,
class... P,
class... ViewCtorArgs,
1575 typename Enable = std::enable_if_t<
1576 std::is_void_v<
typename ViewTraits<T, P...>::specialize>>>
1577 inline auto create_mirror(
const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1578 const DynRankView<T, P...>& src) {
1579 return Impl::create_mirror(src, arg_prop);
1587 template <
class T,
class... P,
class... ViewCtorArgs>
1588 inline auto create_mirror_view(
1589 const DynRankView<T, P...>& src,
1590 [[maybe_unused]]
const typename Impl::ViewCtorProp<ViewCtorArgs...>&
1592 if constexpr (!Impl::ViewCtorProp<ViewCtorArgs...>::has_memory_space) {
1593 if constexpr (std::is_same_v<
typename DynRankView<T, P...>::memory_space,
1594 typename DynRankView<
1595 T, P...>::HostMirror::memory_space> &&
1597 typename DynRankView<T, P...>::data_type,
1598 typename DynRankView<T, P...>::HostMirror::data_type>) {
1599 return typename DynRankView<T, P...>::HostMirror(src);
1601 return Kokkos::Impl::choose_create_mirror(src, arg_prop);
1604 if constexpr (Impl::MirrorDRViewType<
typename Impl::ViewCtorProp<
1605 ViewCtorArgs...>::memory_space,
1606 T, P...>::is_same_memspace) {
1607 return typename Impl::MirrorDRViewType<
1608 typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space, T,
1609 P...>::view_type(src);
1611 return Kokkos::Impl::choose_create_mirror(src, arg_prop);
1614 #if defined(KOKKOS_COMPILER_INTEL) || \
1615 (defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
1616 !defined(KOKKOS_COMPILER_MSVC))
1617 __builtin_unreachable();
1624 template <
class T,
class... P>
1625 inline auto create_mirror_view(
const Kokkos::DynRankView<T, P...>& src) {
1626 return Impl::create_mirror_view(src, Kokkos::view_alloc());
1630 template <
class T,
class... P>
1631 inline auto create_mirror_view(Kokkos::Impl::WithoutInitializing_t wi,
1632 const DynRankView<T, P...>& src) {
1633 return Impl::create_mirror_view(src, Kokkos::view_alloc(wi));
1637 template <
class Space,
class T,
class... P,
1638 class Enable = std::enable_if_t<Kokkos::is_space<Space>::value>>
1639 inline auto create_mirror_view(
const Space&,
1640 const Kokkos::DynRankView<T, P...>& src) {
1641 return Impl::create_mirror_view(
1642 src, Kokkos::view_alloc(
typename Space::memory_space()));
1646 template <
class Space,
class T,
class... P,
1647 typename Enable = std::enable_if_t<Kokkos::is_space<Space>::value>>
1648 inline auto create_mirror_view(Kokkos::Impl::WithoutInitializing_t wi,
1650 const Kokkos::DynRankView<T, P...>& src) {
1651 return Impl::create_mirror_view(
1652 src, Kokkos::view_alloc(
typename Space::memory_space{}, wi));
1657 template <
class T,
class... P,
class... ViewCtorArgs>
1658 inline auto create_mirror_view(
1659 const typename Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1660 const Kokkos::DynRankView<T, P...>& src) {
1661 return Impl::create_mirror_view(src, arg_prop);
1667 template <
class... ViewCtorArgs,
class T,
class... P,
1668 class Enable = std::enable_if_t<
1669 std::is_void_v<
typename ViewTraits<T, P...>::specialize>>>
1670 auto create_mirror_view_and_copy(
1671 [[maybe_unused]]
const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1672 const Kokkos::DynRankView<T, P...>& src) {
1673 using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
1675 Impl::check_view_ctor_args_create_mirror_view_and_copy<ViewCtorArgs...>();
1677 if constexpr (Impl::MirrorDRViewType<
1678 typename Impl::ViewCtorProp<ViewCtorArgs...>::memory_space,
1679 T, P...>::is_same_memspace) {
1681 if constexpr (!alloc_prop_input::has_execution_space)
1683 "Kokkos::create_mirror_view_and_copy: fence before returning src "
1687 using Space =
typename alloc_prop_input::memory_space;
1688 using Mirror =
typename Impl::MirrorDRViewType<Space, T, P...>::view_type;
1690 auto arg_prop_copy = Impl::with_properties_if_unset(
1691 arg_prop, std::string{}, WithoutInitializing,
1692 typename Space::execution_space{});
1694 std::string& label = Impl::get_property<Impl::LabelTag>(arg_prop_copy);
1695 if (label.empty()) label = src.label();
1696 auto mirror =
typename Mirror::non_const_type{
1697 arg_prop_copy, Impl::reconstructLayout(src.layout(), src.rank())};
1698 if constexpr (alloc_prop_input::has_execution_space) {
1699 deep_copy(Impl::get_property<Impl::ExecutionSpaceTag>(arg_prop_copy),
1702 deep_copy(mirror, src);
1705 #if defined(KOKKOS_COMPILER_NVCC) && KOKKOS_COMPILER_NVCC >= 1130 && \
1706 !defined(KOKKOS_COMPILER_MSVC)
1707 __builtin_unreachable();
1711 template <
class Space,
class T,
class... P>
1712 auto create_mirror_view_and_copy(
const Space&,
1713 const Kokkos::DynRankView<T, P...>& src,
1714 std::string
const& name =
"") {
1715 return create_mirror_view_and_copy(
1716 Kokkos::view_alloc(
typename Space::memory_space{}, name), src);
1727 template <
class... ViewCtorArgs,
class T,
class... P>
1728 inline void impl_resize(
const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1729 DynRankView<T, P...>& v,
const size_t n0,
1730 const size_t n1,
const size_t n2,
const size_t n3,
1731 const size_t n4,
const size_t n5,
const size_t n6,
1733 using drview_type = DynRankView<T, P...>;
1734 using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
1736 static_assert(Kokkos::ViewTraits<T, P...>::is_managed,
1737 "Can only resize managed views");
1738 static_assert(!alloc_prop_input::has_label,
1739 "The view constructor arguments passed to Kokkos::resize "
1740 "must not include a label!");
1741 static_assert(!alloc_prop_input::has_pointer,
1742 "The view constructor arguments passed to Kokkos::resize must "
1743 "not include a pointer!");
1744 static_assert(!alloc_prop_input::has_memory_space,
1745 "The view constructor arguments passed to Kokkos::resize must "
1746 "not include a memory space instance!");
1748 auto prop_copy = Impl::with_properties_if_unset(
1749 arg_prop, v.label(),
typename drview_type::execution_space{});
1751 drview_type v_resized(prop_copy, n0, n1, n2, n3, n4, n5, n6, n7);
1753 if constexpr (alloc_prop_input::has_execution_space)
1754 Kokkos::Impl::DynRankViewRemap<drview_type, drview_type>(
1755 Impl::get_property<Impl::ExecutionSpaceTag>(prop_copy), v_resized, v);
1757 Kokkos::Impl::DynRankViewRemap<drview_type, drview_type>(v_resized, v);
1758 Kokkos::fence(
"Kokkos::resize(DynRankView)");
1763 template <
class T,
class... P>
1764 inline void resize(DynRankView<T, P...>& v,
1765 const size_t n0 = KOKKOS_INVALID_INDEX,
1766 const size_t n1 = KOKKOS_INVALID_INDEX,
1767 const size_t n2 = KOKKOS_INVALID_INDEX,
1768 const size_t n3 = KOKKOS_INVALID_INDEX,
1769 const size_t n4 = KOKKOS_INVALID_INDEX,
1770 const size_t n5 = KOKKOS_INVALID_INDEX,
1771 const size_t n6 = KOKKOS_INVALID_INDEX,
1772 const size_t n7 = KOKKOS_INVALID_INDEX) {
1773 impl_resize(Impl::ViewCtorProp<>{}, v, n0, n1, n2, n3, n4, n5, n6, n7);
1776 template <
class... ViewCtorArgs,
class T,
class... P>
1777 void resize(
const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1778 DynRankView<T, P...>& v,
1779 const size_t n0 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1780 const size_t n1 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1781 const size_t n2 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1782 const size_t n3 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1783 const size_t n4 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1784 const size_t n5 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1785 const size_t n6 = KOKKOS_IMPL_CTOR_DEFAULT_ARG,
1786 const size_t n7 = KOKKOS_IMPL_CTOR_DEFAULT_ARG) {
1787 impl_resize(arg_prop, v, n0, n1, n2, n3, n4, n5, n6, n7);
1790 template <
class I,
class T,
class... P>
1791 inline std::enable_if_t<Impl::is_view_ctor_property<I>::value> resize(
1792 const I& arg_prop, DynRankView<T, P...>& v,
1793 const size_t n0 = KOKKOS_INVALID_INDEX,
1794 const size_t n1 = KOKKOS_INVALID_INDEX,
1795 const size_t n2 = KOKKOS_INVALID_INDEX,
1796 const size_t n3 = KOKKOS_INVALID_INDEX,
1797 const size_t n4 = KOKKOS_INVALID_INDEX,
1798 const size_t n5 = KOKKOS_INVALID_INDEX,
1799 const size_t n6 = KOKKOS_INVALID_INDEX,
1800 const size_t n7 = KOKKOS_INVALID_INDEX) {
1801 impl_resize(Kokkos::view_alloc(arg_prop), v, n0, n1, n2, n3, n4, n5, n6, n7);
1806 template <
class... ViewCtorArgs,
class T,
class... P>
1807 inline void impl_realloc(DynRankView<T, P...>& v,
const size_t n0,
1808 const size_t n1,
const size_t n2,
const size_t n3,
1809 const size_t n4,
const size_t n5,
const size_t n6,
1811 const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop) {
1812 using drview_type = DynRankView<T, P...>;
1813 using alloc_prop_input = Impl::ViewCtorProp<ViewCtorArgs...>;
1815 static_assert(Kokkos::ViewTraits<T, P...>::is_managed,
1816 "Can only realloc managed views");
1817 static_assert(!alloc_prop_input::has_label,
1818 "The view constructor arguments passed to Kokkos::realloc must "
1819 "not include a label!");
1820 static_assert(!alloc_prop_input::has_pointer,
1821 "The view constructor arguments passed to Kokkos::realloc must "
1822 "not include a pointer!");
1823 static_assert(!alloc_prop_input::has_memory_space,
1824 "The view constructor arguments passed to Kokkos::realloc must "
1825 "not include a memory space instance!");
1827 auto arg_prop_copy = Impl::with_properties_if_unset(arg_prop, v.label());
1830 v = drview_type(arg_prop_copy, n0, n1, n2, n3, n4, n5, n6, n7);
1833 template <
class T,
class... P,
class... ViewCtorArgs>
1834 inline void realloc(
const Impl::ViewCtorProp<ViewCtorArgs...>& arg_prop,
1835 DynRankView<T, P...>& v,
1836 const size_t n0 = KOKKOS_INVALID_INDEX,
1837 const size_t n1 = KOKKOS_INVALID_INDEX,
1838 const size_t n2 = KOKKOS_INVALID_INDEX,
1839 const size_t n3 = KOKKOS_INVALID_INDEX,
1840 const size_t n4 = KOKKOS_INVALID_INDEX,
1841 const size_t n5 = KOKKOS_INVALID_INDEX,
1842 const size_t n6 = KOKKOS_INVALID_INDEX,
1843 const size_t n7 = KOKKOS_INVALID_INDEX) {
1844 impl_realloc(v, n0, n1, n2, n3, n4, n5, n6, n7, arg_prop);
1847 template <
class T,
class... P>
1848 inline void realloc(DynRankView<T, P...>& v,
1849 const size_t n0 = KOKKOS_INVALID_INDEX,
1850 const size_t n1 = KOKKOS_INVALID_INDEX,
1851 const size_t n2 = KOKKOS_INVALID_INDEX,
1852 const size_t n3 = KOKKOS_INVALID_INDEX,
1853 const size_t n4 = KOKKOS_INVALID_INDEX,
1854 const size_t n5 = KOKKOS_INVALID_INDEX,
1855 const size_t n6 = KOKKOS_INVALID_INDEX,
1856 const size_t n7 = KOKKOS_INVALID_INDEX) {
1857 impl_realloc(v, n0, n1, n2, n3, n4, n5, n6, n7, Impl::ViewCtorProp<>{});
1860 template <
class I,
class T,
class... P>
1861 inline std::enable_if_t<Impl::is_view_ctor_property<I>::value> realloc(
1862 const I& arg_prop, DynRankView<T, P...>& v,
1863 const size_t n0 = KOKKOS_INVALID_INDEX,
1864 const size_t n1 = KOKKOS_INVALID_INDEX,
1865 const size_t n2 = KOKKOS_INVALID_INDEX,
1866 const size_t n3 = KOKKOS_INVALID_INDEX,
1867 const size_t n4 = KOKKOS_INVALID_INDEX,
1868 const size_t n5 = KOKKOS_INVALID_INDEX,
1869 const size_t n6 = KOKKOS_INVALID_INDEX,
1870 const size_t n7 = KOKKOS_INVALID_INDEX) {
1871 impl_realloc(v, n0, n1, n2, n3, n4, n5, n6, n7, Kokkos::view_alloc(arg_prop));
1876 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_DYNRANKVIEW
1877 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
1878 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_DYNRANKVIEW
Memory layout tag indicating left-to-right (Fortran scheme) striding of multi-indices.
KOKKOS_INLINE_FUNCTION bool dyn_rank_view_verify_operator_bounds(const iType0 &, const MapType &)
Debug bounds-checking routines.
Memory layout tag indicated arbitrarily strided multi-index mapping into contiguous memory...
Memory management for host memory.
Memory layout tag indicating right-to-left (C or lexigraphical scheme) striding of multi-indices...
Assign compatible default mappings.
Execution policy for work over a range of an integral type.