Debian Codegraph @

src/atlas/linalg/Introspection.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 #include <type_traits>
14
15 #include "eckit/linalg/Matrix.h"
16 #include "eckit/linalg/Vector.h"
17 #include "eckit/linalg/types.h"
18
19 #include "atlas/library/config.h"
20
21 namespace atlas {
22 namespace linalg {
23 namespace introspection {
24
25
26 namespace detail {
27 // void_t is a C++17 feature
28 template <typename... Ts>
29 struct make_void {
30 typedef void type;
31 };
32 template <typename... Ts>
33 using void_t = typename make_void<Ts...>::type;
34 } // namespace detail
35
36 template <typename, typename = void>
37 struct has_contiguous : std::false_type {};
38
39 template <typename T>
40 struct has_contiguous<T, detail::void_t<decltype(&T::contiguous)>> : std::true_type {};
41
42 template <typename, typename = void>
43 struct has_rank : std::false_type {};
44
45 template <typename T>
46 struct has_rank<T, detail::void_t<decltype(&T::rank)>> : std::true_type {};
47
48 template <typename, typename = void>
49 struct has_RANK : std::false_type {};
50
51 template <typename T>
52 struct has_RANK<T, detail::void_t<decltype(&T::RANK)>> : std::true_type {};
53
54 template <typename, typename = void>
55 struct has_shape : std::false_type {};
56
57 template <typename T>
58 struct has_shape<T, detail::void_t<decltype(std::declval<T>().shape(0))>> : std::true_type {};
59
60 namespace detail {
61 template <typename View>
62 struct BaseType {
63 template <typename Base>
64 static constexpr int is_derived_from() {
65 return std::is_base_of<Base, View>::value;
66 }
67
68 using type = typename std::conditional<
69 is_derived_from<eckit::linalg::Matrix>(), eckit::linalg::Matrix,
70 typename std::conditional<is_derived_from<eckit::linalg::Vector>(), eckit::linalg::Vector,
71 typename std::remove_const<View>::type /*default*/
72 >::type>::type;
73 };
74 } // namespace detail
75
76 template <typename View>
77 using base_type = typename detail::BaseType<View>::type;
78
79 namespace detail {
80 template <typename T, typename>
81 struct ValueType {
82 using type = typename T::value_type;
83 };
84
85 template <typename T>
86 struct ValueType<T, eckit::linalg::Matrix> {
87 using type = eckit::linalg::Scalar;
88 };
89
90 template <typename T>
91 struct ValueType<T, eckit::linalg::Vector> {
92 using type = eckit::linalg::Scalar;
93 };
94
95 template <typename View, typename>
96 struct Introspect {
97 template <typename T, typename std::enable_if<has_contiguous<T>::value, T>::type* = nullptr>
98 static bool contiguous(const T& view) {
99 return view.contiguous();
100 }
101 template <typename T, typename std::enable_if<not has_contiguous<T>::value, T>::type* = nullptr>
102 static bool contiguous(const T&) {
103 return true;
104 }
105
106 template <typename T, typename std::enable_if<has_rank<T>::value, T>::type* = nullptr>
107 static constexpr int rank() {
108 static_assert(has_RANK<T>::value, "workaround for Cray 8.7: we need to use T::RANK instead of T::rank()");
109 return T::RANK;
110 }
111 template <typename T, typename std::enable_if<not has_rank<T>::value, T>::type* = nullptr>
112 static constexpr int rank() {
113 return 1;
114 }
115
116 static constexpr int rank() { return rank<View>(); }
117
118 template <typename T, typename std::enable_if<has_shape<T>::value, T>::type* = nullptr>
119 static idx_t outer_dimension(const T& view) {
120 return view.shape(0);
121 }
122 template <typename T, typename std::enable_if<not has_shape<T>::value && (rank<T>() == 1), T>::type* = nullptr>
123 static idx_t outer_dimension(const T& view) {
124 return view.size();
125 }
126
127 template <typename T, typename std::enable_if<has_shape<T>::value, T>::type* = nullptr>
128 static idx_t inner_dimension(const T& view) {
129 return view.shape(rank<T>() - 1);
130 }
131 template <typename T, typename std::enable_if<not has_shape<T>::value && (rank<T>() == 1), T>::type* = nullptr>
132 static idx_t inner_dimension(const T& view) {
133 return view.size();
134 }
135
136 template <int Dim, typename T, typename std::enable_if<not has_shape<T>::value, T>::type* = nullptr>
137 static idx_t shape(const T& view) {
138 //static_assert( Dim == 0, "Dim must be 0 if shape() is not there" );
139 return view.size();
140 }
141
142 template <int Dim, typename T, typename std::enable_if<has_shape<T>::value, T>::type* = nullptr>
143 static idx_t shape(const T& view) {
144 return view.shape(Dim);
145 }
146
147 static constexpr bool layout_left() { return true; }
148
149 using value_type = typename ValueType<View, base_type<View>>::type;
150 };
151
152 template <typename View>
153 struct Introspect<View, eckit::linalg::Matrix> {
154 using value_type = eckit::linalg::Scalar;
155 static constexpr int rank() { return 2; }
156 static bool contiguous(const eckit::linalg::Matrix&) { return true; }
157 static idx_t inner_dimension(const eckit::linalg::Matrix& m) { return static_cast<idx_t>(m.cols()); }
158 static idx_t outer_dimension(const eckit::linalg::Matrix& m) { return static_cast<idx_t>(m.rows()); }
159 template <int Dim>
160 static idx_t shape(const eckit::linalg::Matrix& m) {
161 static_assert(Dim <= 1, "Dim must be 0 or 1");
162 return (Dim == 0) ? outer_dimension(m) : inner_dimension(m);
163 }
164 static constexpr bool layout_left() { return false; }
165 };
166
167 template <typename T>
168 using Introspection = Introspect<T, base_type<T>>;
169
170 } // namespace detail
171
172 template <typename T>
173 using value_type = typename detail::Introspection<T>::value_type;
174
175 template <typename View>
176 using base_type = typename detail::BaseType<View>::type;
177
178 template <typename T>
179 static bool contiguous(const T& view) {
180 return detail::Introspection<T>::contiguous(view);
181 }
182 template <typename T>
183 static constexpr int rank() {
184 return detail::Introspection<T>::rank();
185 }
186 template <typename T>
187 static idx_t outer_dimension(const T& view) {
188 return detail::Introspection<T>::outer_dimension(view);
189 }
190 template <typename T>
191 static idx_t inner_dimension(const T& view) {
192 return detail::Introspection<T>::inner_dimension(view);
193 }
194
195 template <int Dim, typename T>
196 static idx_t shape(const T& view) {
197 return detail::Introspection<T>::template shape<Dim>(view);
198 }
199
200 template <typename T>
201 static constexpr bool layout_left(const T&) {
202 return detail::Introspection<T>::layout_left();
203 }
204 template <typename T>
205 static constexpr bool layout_right(const T&) {
206 return not detail::Introspection<T>::layout_left();
207 }
208
209 } // namespace introspection
210 } // namespace linalg
211 } // namespace atlas