Debian Codegraph @

src/atlas/array/helpers/ArraySlicer.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 <array>
14
15 #include "atlas/array/ArrayViewDefs.h"
16 #include "atlas/array/Range.h"
17 #include "atlas/library/config.h"
18
19 namespace atlas {
20 namespace array {
21
22 template <typename Value, int Rank>
23 class LocalView;
24
25 template <typename Value>
26 struct Reference {
27 Value& value;
28 operator Value&() { return value; }
29 operator const Value&() const { return value; }
30 template <typename T>
31 void operator=(const T a) {
32 value = a;
33 }
34 template <typename T>
35 Reference<Value>& operator+(const T a) {
36 value += a;
37 return *this;
38 }
39 template <typename T>
40 Reference<Value>& operator-(const T a) {
41 value -= a;
42 return *this;
43 }
44 Reference<Value>& operator--() {
45 --value;
46 return *this;
47 }
48 Reference<Value>& operator++() {
49 ++value;
50 return *this;
51 }
52 template <typename T>
53 bool operator==(const T a) {
54 return value == a;
55 }
56 template <typename T>
57 bool operator!=(const T a) {
58 return value != a;
59 }
60 friend std::ostream& operator<<(std::ostream& out, const Reference& ref) {
61 out << ref.value;
62 return out;
63 }
64 constexpr int shape(idx_t) { return 0; }
65 constexpr int stride(idx_t) { return 0; }
66 constexpr int rank() { return 0; }
67 };
68
69 template <typename Value, int Rank>
70 struct get_slice_type {
71 using type = typename std::conditional<(Rank == 0), Reference<Value>, LocalView<Value, Rank>>::type;
72 };
73
74 //------------------------------------------------------------------------------
75
76 namespace helpers {
77
78 template <int Dim>
79 struct deduce_slice_rank;
80
81 template <int Rank, typename... Args>
82 struct SliceRank_impl;
83
84 template <>
85 struct deduce_slice_rank<1> {
86 template <typename Last>
87 static constexpr int apply() {
88 return std::is_base_of<RangeBase, Last>::value;
89 }
90 };
91 template <typename... Args>
92 struct SliceRank_impl<1, Args...> {
93 static constexpr int value{deduce_slice_rank<1>::apply<Args...>()};
94 };
95
96 #define ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(RANK) \
97 template <> \
98 struct deduce_slice_rank<RANK> { \
99 template <typename First, typename... Args> \
100 static constexpr int apply() { \
101 return std::is_base_of<RangeBase, First>::value + deduce_slice_rank<RANK - 1>::apply<Args...>(); \
102 } \
103 }; \
104 template <typename... Args> \
105 struct SliceRank_impl<RANK, Args...> { \
106 static constexpr int value{deduce_slice_rank<RANK>::apply<Args...>()}; \
107 }
108
109 ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(2);
110 ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(3);
111 ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(4);
112 ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(5);
113 ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(6);
114 ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(7);
115 ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(8);
116 ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION(9);
117 #undef ATLAS_ARRAY_SLICER_EXPLICIT_TEMPLATE_SPECIALISATION
118
119 template <typename... Args>
120 struct SliceRank {
121 static constexpr int value{SliceRank_impl<sizeof...(Args), Args...>::value};
122 };
123
124 // template <typename Value, int Rank, Intent AccessMode>
125 template <typename View>
126 class ArraySlicer {
127 public:
128 ArraySlicer(View& view): view_(view) {}
129
130 template <typename... Args>
131 struct Slice {
132 using type = typename get_slice_type<typename View::value_type, SliceRank<Args...>::value>::type;
133 };
134
135 template <typename... Args>
136 typename Slice<Args...>::type apply(const Args... args) const {
137 using slicer_t = Slicer<typename Slice<Args...>::type, (SliceRank<Args...>::value == 0)>;
138 static_assert(
139 View::RANK <= sizeof...(Args),
140 "Not enough arguments passed to slice() function. Pehaps you forgot to add a array::Range::all()");
141 return slicer_t::apply(view_, args...);
142 }
143
144 private:
145 template <typename... Args>
146 struct array {
147 using type = typename std::array<idx_t, SliceRank<Args...>::value>;
148 };
149
150 template <typename ReturnType, bool ToScalar = false>
151 struct Slicer {
152 template <typename... Args>
153 static ReturnType apply(View& view, const Args... args) {
154 return ReturnType(view.data() + offset(view, args...), shape(view, args...).data(),
155 strides(view, args...).data());
156 }
157 };
158
159 template <typename ReturnType>
160 struct Slicer<ReturnType, true> {
161 template <typename... Args>
162 static ReturnType apply(View& view, const Args... args) {
163 return ReturnType{*(view.data() + offset(view, args...))};
164 }
165 };
166
167 template <typename Int>
168 static idx_t offset_part(View& view, int& i_view, Int idx) {
169 return idx * view.stride(i_view++);
170 }
171
172 static idx_t offset_part(View& view, int& i_view, Range range) { return range.start() * view.stride(i_view++); }
173
174 static idx_t offset_part(View& view, int& i_view, RangeAll range) { return range.start() * view.stride(i_view++); }
175
176 static idx_t offset_part(View& view, int& i_view, RangeTo range) { return range.start() * view.stride(i_view++); }
177
178 static idx_t offset_part(View& view, int& i_view, RangeFrom range) { return range.start() * view.stride(i_view++); }
179
180 static idx_t offset_part(View&, int& /*i_view*/, RangeDummy) { return 0; }
181
182 template <int Dim, typename Int, typename... Ints>
183 static idx_t offset_remaining(View& view, int& i_view, const Int idx, const Ints... next_idx) {
184 const idx_t p = offset_part(view, i_view, idx);
185 return p + offset_remaining<Dim + 1>(view, i_view, next_idx...);
186 }
187
188 template <int Dim, typename Int>
189 static idx_t offset_remaining(View& view, int& i_view, const Int last_idx) {
190 return offset_part(view, i_view, last_idx);
191 }
192
193 template <typename... Args>
194 static idx_t offset(View& view, const Args... args) {
195 int i_view(0);
196 return offset_remaining<0>(view, i_view, args...);
197 }
198
199 template <int Dim, typename Shape, typename Int>
200 static void update_shape(View&, Shape&, int& i_view, int& /*i_slice*/, const Int& /*index*/) {
201 // do nothing
202 ++i_view;
203 }
204 template <int Dim, typename Shape>
205 static void update_shape(View&, Shape& shape, int& i_view, int& i_slice, const Range range) {
206 shape[i_slice] = range.end() - range.start();
207 ++i_slice;
208 ++i_view;
209 }
210 template <int Dim, typename Shape>
211 static void update_shape(View& view, Shape& shape, int& i_view, int& i_slice, const RangeAll range) {
212 shape[i_slice] = range.end(view, i_view) - range.start();
213 ++i_slice;
214 ++i_view;
215 }
216 template <int Dim, typename Shape>
217 static void update_shape(View& view, Shape& shape, int& i_view, int& i_slice, const RangeFrom range) {
218 shape[i_slice] = range.end(view, i_view) - range.start();
219 ++i_slice;
220 ++i_view;
221 }
222 template <int Dim, typename Shape>
223 static void update_shape(View&, Shape& shape, int& i_view, int& i_slice, const RangeTo range) {
224 shape[i_slice] = range.end() - range.start();
225 ++i_slice;
226 ++i_view;
227 }
228
229 template <int Dim, typename Shape>
230 static void update_shape(View&, Shape& shape, int& /*i_view*/, int& i_slice, const RangeDummy) {
231 shape[i_slice] = 1;
232 ++i_slice;
233 // no update of i_view for dummy-dimension
234 }
235
236 template <int Dim, typename Shape, typename Int, typename... Ints>
237 static void shape_part(View& view, Shape& shape, int& i_view, int& i_slice, const Int idx, const Ints... next_idx) {
238 update_shape<Dim>(view, shape, i_view, i_slice, idx);
239 shape_part<Dim + 1>(view, shape, i_view, i_slice, next_idx...);
240 }
241
242 template <int Dim, typename Shape, typename Int>
243 static void shape_part(View& view, Shape& shape, int& i_view, int& i_slice, const Int idx) {
244 update_shape<Dim>(view, shape, i_view, i_slice, idx);
245 }
246
247 template <typename... Args>
248 static typename array<Args...>::type shape(View& view, const Args... args) {
249 typename array<Args...>::type result;
250 int i_slice(0);
251 int i_view(0);
252 shape_part<0>(view, result, i_view, i_slice, args...);
253 return result;
254 }
255
256 template <int Dim, typename Strides, typename Int>
257 static void update_strides(View&, Strides&, int& i_view, int& /*i_slice*/, const Int& /*idx*/) {
258 // do nothing
259 ++i_view;
260 }
261 template <int Dim, typename Strides>
262 static void update_strides(View& view, Strides& strides, int& i_view, int& i_slice, const Range& /*range*/) {
263 strides[i_slice] = view.stride(i_view);
264 ++i_slice;
265 ++i_view;
266 }
267 template <int Dim, typename Strides>
268 static void update_strides(View& view, Strides& strides, int& i_view, int& i_slice, const RangeFrom& /*range*/) {
269 strides[i_slice] = view.stride(i_view);
270 ++i_slice;
271 ++i_view;
272 }
273 template <int Dim, typename Strides>
274 static void update_strides(View& view, Strides& strides, int& i_view, int& i_slice, const RangeTo& /*range*/) {
275 strides[i_slice] = view.stride(i_view);
276 ++i_slice;
277 ++i_view;
278 }
279 template <int Dim, typename Strides>
280 static void update_strides(View& view, Strides& strides, int& i_view, int& i_slice, const RangeAll& /*range*/) {
281 strides[i_slice] = view.stride(i_view);
282 ++i_slice;
283 ++i_view;
284 }
285 template <int Dim, typename Strides>
286 static void update_strides(View& /*view*/, Strides& strides, int& /*i_view*/, int& i_slice,
287 const RangeDummy& /*range*/) {
288 strides[i_slice] = 0;
289 ++i_slice;
290 }
291
292 template <int Dim, typename Strides, typename Int, typename... Ints>
293 static void strides_part(View& view, Strides& strides, int& i_view, int& i_slice, const Int idx,
294 const Ints... next_idx) {
295 update_strides<Dim>(view, strides, i_view, i_slice, idx);
296 strides_part<Dim + 1>(view, strides, i_view, i_slice, next_idx...);
297 }
298
299 template <int Dim, typename Strides, typename Int>
300 static void strides_part(View& view, Strides& strides, int& i_view, int& i_slice, const Int idx) {
301 update_strides<Dim>(view, strides, i_view, i_slice, idx);
302 }
303
304 template <typename... Args>
305 static typename array<Args...>::type strides(View& view, const Args... args) {
306 typename array<Args...>::type result;
307 int i_slice(0);
308 int i_view(0);
309 strides_part<0>(view, result, i_view, i_slice, args...);
310 return result;
311 }
312
313 private:
314 View& view_;
315 };
316
317 //------------------------------------------------------------------------------
318
319 } // namespace helpers
320 } // namespace array
321 } // namespace atlas