Debian Codegraph @

src/atlas/array/native/NativeIndexView.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 /// @file IndexView.h
12 /// This file contains the IndexView class, a class that allows to wrap any
13 /// contiguous raw data into
14 /// a view which is accessible with multiple indices.
15 /// This view is intended to work with Connectivity Tables storing Fortran
16 /// Numbering internally
17 /// All it needs is the strides for each index, and the shape of each index.
18 /// ATTENTION: The last index is stride 1
19 ///
20 /// Bounds-checking can be turned ON by defining
21 /// "ATLAS_INDEXVIEW_BOUNDS_CHECKING"
22 /// before including this header.
23 ///
24 /// Example:
25 /// int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
26 /// int[2] strides = { 3, 1 };
27 /// int[2] shape = { 3, 3 };
28 /// IndexView<int,2> matrix( array, strides, shape );
29 /// for( size_t i=0; i<matrix.shape(0); ++i ) {
30 /// for( size_t j=0; j<matrix.shape(1); ++j ) {
31 /// matrix(i,j) *= 10;
32 /// }
33 /// }
34 ///
35 /// There is also an easier way to wrap Field and Array classes:
36 /// IndexView<int,3> fieldview( Field );
37 /// IndexView<int,2> INDEXVIEW( Array );
38
39 #pragma once
40
41 #include <initializer_list>
42 #include <iosfwd>
43 #include <type_traits>
44 #include <utility>
45
46 #include "atlas/array/ArrayDataStore.h"
47 #include "atlas/library/config.h"
48 #include "atlas/mdspan.h"
49
50 //------------------------------------------------------------------------------------------------------
51
52 namespace atlas {
53 namespace array {
54
55 //------------------------------------------------------------------------------------------------------
56
57 #if ATLAS_HAVE_FORTRAN
58 #define INDEX_REF Index
59 #define FROM_FORTRAN -1
60 #else
61 #define INDEX_REF *
62 #define FROM_FORTRAN
63 #endif
64
65 #define ENABLE_IF_NON_CONST \
66 template <bool EnableBool = true, \
67 typename std::enable_if<(!std::is_const<Value>::value && EnableBool), int>::type* = nullptr>
68
69 //------------------------------------------------------------------------------------------------------
70
71 ///@brief Multidimensional access to Array or Field objects containing index fields
72 /// that are compatible with Fortran indexing
73 ///
74 /// Index fields that are compatible with Fortran are 1-based: a value `1` corresponds
75 /// to the first index in a Fortran array. In C++ the first index would of course correspond to
76 /// the value `0`. To stay compatible, we created this class IndexView that subtracts `1`
77 /// when an index value is accessed, and adds `1` when a value is set.
78 ///
79 /// If Atlas is compiled without the FORTRAN feature (ATLAS_HAVE_FORTRAN==0), then the addition
80 /// and substraction of `1` is compiled out, which may slightly improve performance.
81 template <typename Value, int Rank>
82 class IndexView {
83 public:
84 using value_type = typename remove_const<Value>::type;
85 using accessor_type = index_accessor<Value,ATLAS_HAVE_FORTRAN>;
86 using reference = typename accessor_type::reference;
87
88 private:
89 using mdspan_extents_type = dextents<size_t,Rank>;
90 using mdspan_strides_type = std::array<size_t,Rank>;
91 using mdspan_type = mdspan<Value, mdspan_extents_type, layout_stride, index_accessor<Value,ATLAS_HAVE_FORTRAN>>;
92 using const_mdspan_type = mdspan<const Value, mdspan_extents_type, layout_stride, index_accessor<const Value,ATLAS_HAVE_FORTRAN>>;
93
94 public:
95 #ifndef DOXYGEN_SHOULD_SKIP_THIS
96 IndexView(Value* data, const idx_t shape[Rank]);
97
98 IndexView(Value* data, const idx_t shape[Rank], const idx_t strides[Rank]);
99 #endif
100
101 template <typename T, typename E, typename L, typename A, typename = std::enable_if_t<std::is_convertible_v<typename A::data_handle_type, Value*> && E::rank() == Rank>>
102 IndexView(mdspan<T,E,L,A>& other) :
103 data_(other.data_handle()) {
104 for (int j = 0; j < Rank; ++j) {
105 shape_[j] = other.extent(j);
106 strides_[j] = other.stride(j);
107 }
108 }
109
110 // -- Access methods
111
112 /// @brief Multidimensional index operator: view(i,j,k,...)
113 template <typename... Idx>
114 reference operator()(Idx... idx) {
115 check_bounds(idx...);
116 return accessor_.access(data_, index(idx...));
117 }
118
119 template <typename... Idx>
120 value_type operator()(Idx... idx) const {
121 check_bounds(idx...);
122 return data_[index(idx...)] FROM_FORTRAN;
123 }
124
125 ENABLE_IF_NON_CONST
126 void assign(const std::initializer_list<value_type>& list);
127
128 template <typename Int>
129 idx_t shape(Int idx) const {
130 return shape_[idx];
131 }
132
133 mdspan_type as_mdspan() {
134 return mdspan_type{data_, {mdspan_extents(), mdspan_strides()}};
135 }
136
137 const_mdspan_type as_mdspan() const {
138 return const_mdspan_type{data_, {mdspan_extents(), mdspan_strides()}};
139 }
140
141 private:
142 // -- Private methods
143
144 template <int Dim, typename Int, typename... Ints>
145 constexpr idx_t index_part(Int idx, Ints... next_idx) const {
146 return idx * strides_[Dim] + index_part<Dim + 1>(next_idx...);
147 }
148
149 template <int Dim, typename Int>
150 constexpr idx_t index_part(Int last_idx) const {
151 return last_idx * strides_[Dim];
152 }
153
154 template <typename... Ints>
155 constexpr idx_t index(Ints... idx) const {
156 return index_part<0>(idx...);
157 }
158
159 #if ATLAS_INDEXVIEW_BOUNDS_CHECKING
160 template <typename... Ints>
161 void check_bounds(Ints... idx) const {
162 static_assert(sizeof...(idx) == Rank, "Expected number of indices is different from rank of array");
163 return check_bounds_part<0>(idx...);
164 }
165 #else
166 template <typename... Ints>
167 void check_bounds(Ints...) const {}
168 #endif
169
170 template <typename... Ints>
171 void check_bounds_force(Ints... idx) const {
172 static_assert(sizeof...(idx) == Rank, "Expected number of indices is different from rank of array");
173 return check_bounds_part<0>(idx...);
174 }
175
176 template <int Dim, typename Int, typename... Ints>
177 void check_bounds_part(Int idx, Ints... next_idx) const {
178 if (idx_t(idx) >= shape_[Dim]) {
179 throw_OutOfRange("IndexView", array_dim<Dim>(), idx, shape_[Dim]);
180 }
181 check_bounds_part<Dim + 1>(next_idx...);
182 }
183
184 template <int Dim, typename Int>
185 void check_bounds_part(Int last_idx) const {
186 if (idx_t(last_idx) >= shape_[Dim]) {
187 throw_OutOfRange("IndexView", array_dim<Dim>(), last_idx, shape_[Dim]);
188 }
189 }
190
191 idx_t size() const { return shape_[0]; }
192
193 void dump(std::ostream& os) const;
194
195 template<int... i>
196 mdspan_extents_type _get_mdspan_extents(std::integer_sequence<int, i...> = {}) const {
197 return mdspan_extents_type{(shape_[i])...};
198 }
199
200 mdspan_extents_type mdspan_extents() const {
201 return _get_mdspan_extents(std::make_integer_sequence<int,Rank>{});
202 }
203
204 template<int... i>
205 mdspan_strides_type _get_mdspan_strides(std::integer_sequence<int, i...> = {}) const {
206 return mdspan_strides_type{(static_cast<typename mdspan_strides_type::value_type>(strides_[i]))...};
207 }
208
209 mdspan_strides_type mdspan_strides() const {
210 return _get_mdspan_strides(std::make_integer_sequence<int,Rank>{});
211 }
212
213 private:
214 Value* data_;
215 idx_t strides_[Rank];
216 idx_t shape_[Rank];
217 static constexpr accessor_type accessor_{};
218 };
219
220 template <typename Value, int Rank>
221 class LocalIndexView : public IndexView<Value, Rank> {
222 using Base = IndexView<Value, Rank>;
223
224 public:
225 using Base::Base;
226 };
227
228 #undef INDEX_REF
229 #undef FROM_FORTRAN
230 #undef ENABLE_IF_NON_CONST
231
232 //------------------------------------------------------------------------------------------------------
233
234 } // namespace array
235 } // namespace atlas