Debian Codegraph @

src/atlas/array/helpers/ArrayInitializer.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 <sstream>
14 #include <string>
15 #include <vector>
16
17 #include "atlas/array.h"
18 #include "atlas/array/DataType.h"
19 #include "atlas/array_fwd.h"
20 #include "atlas/runtime/Exception.h"
21
22 //------------------------------------------------------------------------------
23
24 namespace atlas {
25 namespace array {
26 namespace helpers {
27
28 //------------------------------------------------------------------------------
29
30 struct array_initializer;
31
32 template <idx_t PartDim>
33 struct array_initializer_partitioned;
34
35 //------------------------------------------------------------------------------
36
37 template <typename Value, idx_t Rank, idx_t Dim>
38 struct array_initializer_impl {
39 static void apply(Array const& orig, Array& array_resized) {
40 array_initializer_impl<Value, Rank, Dim>::apply(make_view<Value, Rank>(orig),
41 make_view<Value, Rank>(array_resized));
42 }
43
44 template <typename... DimIndex>
45 static void apply(ArrayView<const Value, Rank> const&& orig, ArrayView<Value, Rank>&& array_resized,
46 DimIndex... idxs) {
47 const idx_t N = std::min(array_resized.shape(Dim), orig.shape(Dim));
48 for (idx_t i = 0; i < N; ++i) {
49 array_initializer_impl<Value, Rank, Dim + 1>::apply(std::move(orig), std::move(array_resized), idxs..., i);
50 }
51 }
52 };
53
54 //------------------------------------------------------------------------------
55
56 template <typename Value, idx_t Rank>
57 struct array_initializer_impl<Value, Rank, Rank> {
58 template <typename... DimIndex>
59 static void apply(ArrayView<const Value, Rank> const&& orig, ArrayView<Value, Rank>&& array_resized,
60 DimIndex... idxs) {
61 array_resized(idxs...) = orig(idxs...);
62 }
63 };
64
65 //------------------------------------------------------------------------------
66
67 struct array_initializer {
68
69 static void apply(Array const& from, Array& to) {
70 ATLAS_ASSERT(from.rank() == to.rank());
71 switch (from.rank()) {
72 case 1:
73 apply_rank<1>(from, to);
74 break;
75 case 2:
76 apply_rank<2>(from, to);
77 break;
78 case 3:
79 apply_rank<3>(from, to);
80 break;
81 case 4:
82 apply_rank<4>(from, to);
83 break;
84 case 5:
85 apply_rank<5>(from, to);
86 break;
87 case 6:
88 apply_rank<6>(from, to);
89 break;
90 case 7:
91 apply_rank<7>(from, to);
92 break;
93 case 8:
94 apply_rank<8>(from, to);
95 break;
96 case 9:
97 apply_rank<9>(from, to);
98 break;
99 default:
100 ATLAS_NOTIMPLEMENTED;
101 }
102 }
103
104 template <idx_t Rank>
105 static void apply_rank(Array const& orig, Array& array_resized) {
106 switch (orig.datatype().kind()) {
107 case DataType::KIND_REAL64:
108 return array_initializer_impl<double, Rank, 0>::apply(orig, array_resized);
109 case DataType::KIND_REAL32:
110 return array_initializer_impl<float, Rank, 0>::apply(orig, array_resized);
111 case DataType::KIND_INT32:
112 return array_initializer_impl<int, Rank, 0>::apply(orig, array_resized);
113 case DataType::KIND_INT64:
114 return array_initializer_impl<long, Rank, 0>::apply(orig, array_resized);
115 case DataType::KIND_UINT32:
116 return array_initializer_impl<unsigned int, Rank, 0>::apply(orig, array_resized);
117 case DataType::KIND_UINT64:
118 return array_initializer_impl<unsigned long, Rank, 0>::apply(orig, array_resized);
119 default: {
120 std::stringstream err;
121 err << "data kind " << orig.datatype().kind() << " not recognised.";
122 throw_NotImplemented(err.str(), Here());
123 }
124 }
125 }
126 };
127
128 //------------------------------------------------------------------------------
129
130 template <typename Value, idx_t Rank, idx_t Dim, idx_t PartDim>
131 struct array_initializer_partitioned_val_impl {
132 static void apply(Array const& orig, Array& dest, idx_t pos, idx_t offset) {
133 array_initializer_partitioned_val_impl<Value, Rank, Dim, PartDim>::apply(
134 make_view<const Value, Rank>(orig), make_view<Value, Rank>(dest), pos, offset);
135 }
136
137 template <typename... DimIndexPair>
138 static void apply(ArrayView<const Value, Rank>&& orig, ArrayView<Value, Rank>&& dest, idx_t pos, idx_t offset,
139 DimIndexPair... idxs) {
140 for (idx_t i = 0; i < orig.shape(Dim); ++i) {
141 idx_t displ = i;
142 if (Dim == PartDim && i >= pos) {
143 displ += offset;
144 }
145 std::pair<idx_t, idx_t> pair_idx{i, displ};
146 array_initializer_partitioned_val_impl<Value, Rank, Dim + 1, PartDim>::apply(
147 std::move(orig), std::move(dest), pos, offset, idxs..., pair_idx);
148 }
149 }
150 };
151
152 // template< typename stdarray >
153 // inline std::string print_array(const stdarray& v)
154 // {
155 // std::stringstream s;
156 // s << "[ ";
157 // for( int j=0; j<v.size(); ++ j ) {
158 // s << v[j];
159 // if( j != v.size()-1 ) s << " , ";
160 // }
161 // s << " ]";
162 // return s.str();
163 // }
164
165 //------------------------------------------------------------------------------
166
167 template <typename Value, idx_t Rank, idx_t PartDim>
168 struct array_initializer_partitioned_val_impl<Value, Rank, Rank, PartDim> {
169 template <typename... DimIndexPair>
170 static void apply(ArrayView<const Value, Rank>&& orig, ArrayView<Value, Rank>&& dest, idx_t /*pos*/,
171 idx_t /*offset*/, DimIndexPair... idxs) {
172 // Log::info() << print_array(std::array<int,Rank>{std::get<0>(idxs)...}) <<
173 // " --> " << print_array(std::array<int,Rank>{std::get<1>(idxs)...}) << "
174 // " << orig(std::get<0>(idxs)...) << std::endl;
175 dest(std::get<1>(idxs)...) = orig(std::get<0>(idxs)...);
176 }
177 };
178
179 //------------------------------------------------------------------------------
180
181 template <idx_t Rank, idx_t PartDim>
182 struct array_initializer_partitioned_impl {
183 static void apply(Array const& orig, Array& dest, idx_t pos, idx_t offset) {
184 switch (orig.datatype().kind()) {
185 case DataType::KIND_REAL64:
186 return array_initializer_partitioned_val_impl<double, Rank, 0, PartDim>::apply(orig, dest, pos, offset);
187 case DataType::KIND_REAL32:
188 return array_initializer_partitioned_val_impl<float, Rank, 0, PartDim>::apply(orig, dest, pos, offset);
189 case DataType::KIND_INT32:
190 return array_initializer_partitioned_val_impl<int, Rank, 0, PartDim>::apply(orig, dest, pos, offset);
191 case DataType::KIND_INT64:
192 return array_initializer_partitioned_val_impl<long, Rank, 0, PartDim>::apply(orig, dest, pos, offset);
193 case DataType::KIND_UINT32:
194 return array_initializer_partitioned_val_impl<unsigned int, Rank, 0, PartDim>::apply(orig, dest, pos,
195 offset);
196 case DataType::KIND_UINT64:
197 return array_initializer_partitioned_val_impl<unsigned long, Rank, 0, PartDim>::apply(orig, dest, pos,
198 offset);
199 default: {
200 std::stringstream err;
201 err << "data kind " << orig.datatype().kind() << " not recognised.";
202 throw_NotImplemented(err.str(), Here());
203 }
204 }
205 }
206 };
207
208 //------------------------------------------------------------------------------
209
210 template <idx_t PartDim>
211 struct array_initializer_partitioned {
212 static void apply(const Array& orig, Array& dest, idx_t pos, idx_t offset) {
213 switch (orig.rank()) {
214 case 1:
215 return array_initializer_partitioned_impl<1, PartDim>::apply(orig, dest, pos, offset);
216 case 2:
217 return array_initializer_partitioned_impl<2, PartDim>::apply(orig, dest, pos, offset);
218 case 3:
219 return array_initializer_partitioned_impl<3, PartDim>::apply(orig, dest, pos, offset);
220 case 4:
221 return array_initializer_partitioned_impl<4, PartDim>::apply(orig, dest, pos, offset);
222 case 5:
223 return array_initializer_partitioned_impl<5, PartDim>::apply(orig, dest, pos, offset);
224 case 6:
225 return array_initializer_partitioned_impl<6, PartDim>::apply(orig, dest, pos, offset);
226 case 7:
227 return array_initializer_partitioned_impl<7, PartDim>::apply(orig, dest, pos, offset);
228 case 8:
229 return array_initializer_partitioned_impl<8, PartDim>::apply(orig, dest, pos, offset);
230 case 9:
231 return array_initializer_partitioned_impl<9, PartDim>::apply(orig, dest, pos, offset);
232 default: {
233 std::stringstream err;
234 err << "too high Rank";
235 throw_NotImplemented(err.str(), Here());
236 }
237 }
238 }
239 };
240
241 //------------------------------------------------------------------------------
242
243 } // namespace helpers
244 } // namespace array
245 } // namespace atlas