forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kokkos_MDSpan_Extents.hpp
122 lines (100 loc) · 3.96 KB
/
Kokkos_MDSpan_Extents.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
//@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
static_assert(false,
"Including non-public Kokkos header files is not allowed.");
#endif
#ifndef KOKKOS_EXPERIMENTAL_MDSPAN_EXTENTS_HPP
#define KOKKOS_EXPERIMENTAL_MDSPAN_EXTENTS_HPP
#include "Kokkos_MDSpan_Header.hpp"
namespace Kokkos::Impl {
// Forward declarations from impl/Kokkos_ViewMapping.hpp
// We cannot include directly since ViewMapping is used elsewhere in View.
// After View is fully moved to mdspan we can include it only from here.
template <class DataType>
struct ViewArrayAnalysis;
template <std::size_t... Vals>
struct ViewDimension;
template <class T, class Dim>
struct ViewDataType;
// A few things to note --
// - mdspan allows for 0-rank extents similarly to View, so we don't need
// special handling of this case
// - View dynamic dimensions must be appear before static dimensions. This isn't
// a requirement in mdspan but won't cause an issue here
template <std::size_t N>
struct ExtentFromDimension {
static constexpr std::size_t value = N;
};
// Kokkos uses a dimension of '0' to denote a dynamic dimension.
template <>
struct ExtentFromDimension<std::size_t{0}> {
static constexpr std::size_t value = dynamic_extent;
};
template <std::size_t N>
struct DimensionFromExtent {
static constexpr std::size_t value = N;
};
template <>
struct DimensionFromExtent<dynamic_extent> {
static constexpr std::size_t value = std::size_t{0};
};
template <class IndexType, class Dimension, class Indices>
struct ExtentsFromDimension;
template <class IndexType, class Dimension, std::size_t... Indices>
struct ExtentsFromDimension<IndexType, Dimension,
std::index_sequence<Indices...>> {
using type =
extents<IndexType,
ExtentFromDimension<Dimension::static_extent(Indices)>::value...>;
};
template <class Extents, class Indices>
struct DimensionsFromExtent;
template <class Extents, std::size_t... Indices>
struct DimensionsFromExtent<Extents, std::index_sequence<Indices...>> {
using type = ::Kokkos::Impl::ViewDimension<
DimensionFromExtent<Extents::static_extent(Indices)>::value...>;
};
template <class IndexType, class DataType>
struct ExtentsFromDataType {
using array_analysis = ::Kokkos::Impl::ViewArrayAnalysis<DataType>;
using dimension_type = typename array_analysis::dimension;
using type = typename ExtentsFromDimension<
IndexType, dimension_type,
std::make_index_sequence<dimension_type::rank>>::type;
};
template <class T, class Extents>
struct DataTypeFromExtents {
using extents_type = Extents;
using dimension_type = typename DimensionsFromExtent<
Extents, std::make_index_sequence<extents_type::rank()>>::type;
// Will cause a compile error if it is malformed (i.e. dynamic after static)
using type = typename ::Kokkos::Impl::ViewDataType<T, dimension_type>::type;
};
template <class Extents, class VM, std::size_t... Indices>
constexpr KOKKOS_INLINE_FUNCTION auto extents_from_view_mapping_impl(
const VM &view_mapping, std::index_sequence<Indices...>) {
return Extents{view_mapping.extent(Indices)...};
}
template <class Extents, class VM>
constexpr KOKKOS_INLINE_FUNCTION auto extents_from_view_mapping(
const VM &view_mapping) {
static_assert(Extents::rank() == VM::Rank);
return extents_from_view_mapping_impl<Extents>(
view_mapping, std::make_index_sequence<Extents::rank()>{});
}
} // namespace Kokkos::Impl
#endif // KOKKOS_EXPERIMENTAL_MDSPAN_EXTENTS_HPP