Debian Codegraph @

src/atlas/functionspace/detail/BlockStructuredColumns.cc

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 #include "atlas/functionspace/BlockStructuredColumns.h"
12
13 #include <fstream>
14 #include <iomanip>
15 #include <mutex>
16 #include <sstream>
17 #include <string>
18
19 #include "eckit/utils/MD5.h"
20
21 #include "atlas/array/Array.h"
22 #include "atlas/array/MakeView.h"
23 #include "atlas/domain.h"
24 #include "atlas/field/FieldSet.h"
25 #include "atlas/grid/Distribution.h"
26 #include "atlas/grid/Partitioner.h"
27 #include "atlas/grid/StructuredGrid.h"
28 #include "atlas/grid/StructuredPartitionPolygon.h"
29 #include "atlas/library/Library.h"
30 #include "atlas/mesh/Mesh.h"
31 #include "atlas/parallel/Checksum.h"
32 #include "atlas/parallel/mpi/mpi.h"
33 #include "atlas/parallel/omp/fill.h"
34 #include "atlas/parallel/omp/omp.h"
35 #include "atlas/runtime/Exception.h"
36 #include "atlas/runtime/Trace.h"
37 #include "atlas/util/Checksum.h"
38 #include "atlas/util/CoordinateEnums.h"
39 #include "atlas/util/detail/Cache.h"
40
41 namespace atlas {
42 namespace functionspace {
43 namespace detail {
44
45 namespace {
46
47 template <class ValueType>
48 void block_copy(const Field sloc, Field loc, const functionspace::detail::BlockStructuredColumns& fs) {
49 if (sloc.variables() and sloc.levels()) {
50 auto loc_v = array::make_view<ValueType, 4>(loc);
51 auto sloc_v = array::make_view<ValueType, 3>(sloc);
52 for (idx_t jblk = 0; jblk < fs.nblks(); ++jblk) {
53 const idx_t blk_size = fs.block_size(jblk);
54 const idx_t blk_begin = fs.block_begin(jblk);
55 for (idx_t jvar = 0; jvar < sloc.shape(2); ++jvar) {
56 for (idx_t jlev = 0; jlev < sloc.shape(1); ++jlev) {
57 for (idx_t jrof = 0; jrof < blk_size; ++jrof) {
58 loc_v(jblk, jvar, jlev, jrof) = sloc_v(blk_begin+jrof, jlev, jvar);
59 }
60 }
61 }
62 }
63 }
64 else if (not sloc.variables() and sloc.levels()) {
65 auto loc_v = array::make_view<ValueType, 3>(loc);
66 auto sloc_v = array::make_view<ValueType, 2>(sloc);
67 for (idx_t jblk = 0; jblk < fs.nblks(); ++jblk) {
68 const idx_t blk_size = fs.block_size(jblk);
69 const idx_t blk_begin = fs.block_begin(jblk);
70 for (idx_t jlev = 0; jlev < sloc.shape(1); ++jlev) {
71 for (idx_t jrof = 0; jrof < blk_size; ++jrof) {
72 loc_v(jblk, jlev, jrof) = sloc_v(blk_begin+jrof, jlev);
73 }
74 }
75 }
76 }
77 else if (sloc.variables() and not sloc.levels()) {
78 auto loc_v = array::make_view<ValueType, 3>(loc);
79 auto sloc_v = array::make_view<ValueType, 2>(sloc);
80 for (idx_t jblk = 0; jblk < fs.nblks(); ++jblk) {
81 const idx_t blk_size = fs.block_size(jblk);
82 const idx_t blk_begin = fs.block_begin(jblk);
83 for (idx_t jvar = 0; jvar < sloc.shape(1); ++jvar) {
84 for (idx_t jrof = 0; jrof < blk_size; ++jrof) {
85 loc_v(jblk, jvar, jrof) = sloc_v(blk_begin+jrof, jvar);
86 }
87 }
88 }
89 }
90 else {
91 auto loc_v = array::make_view<ValueType, 2>(loc);
92 auto sloc_v = array::make_view<ValueType, 1>(sloc);
93 for (idx_t jblk = 0; jblk < fs.nblks(); ++jblk) {
94 const idx_t blk_size = fs.block_size(jblk);
95 const idx_t blk_begin = fs.block_begin(jblk);
96 for (idx_t jrof = 0; jrof < blk_size; ++jrof) {
97 loc_v(jblk, jrof) = sloc_v(blk_begin+jrof);
98 }
99 }
100 }
101 }
102
103 template <class ValueType>
104 void rev_block_copy(const Field loc, Field sloc, const functionspace::detail::BlockStructuredColumns& fs) {
105 if (loc.variables() and loc.levels()) {
106 auto loc_v = array::make_view<ValueType, 4>(loc);
107 auto sloc_v = array::make_view<ValueType, 3>(sloc);
108 for (idx_t jblk = 0; jblk < fs.nblks(); ++jblk) {
109 const idx_t blk_size = fs.block_size(jblk);
110 const idx_t blk_begin = fs.block_begin(jblk);
111 for (idx_t jvar = 0; jvar < sloc.shape(2); ++jvar) {
112 for (idx_t jlev = 0; jlev < sloc.shape(1); ++jlev) {
113 for (idx_t jrof = 0; jrof < blk_size; ++jrof) {
114 sloc_v(blk_begin+jrof, jlev, jvar) = loc_v(jblk, jvar, jlev, jrof);
115 }
116 }
117 }
118 }
119 }
120 else if (not loc.variables() and loc.levels()) {
121 auto loc_v = array::make_view<ValueType, 3>(loc);
122 auto sloc_v = array::make_view<ValueType, 2>(sloc);
123 for (idx_t jblk = 0; jblk < fs.nblks(); ++jblk) {
124 const idx_t blk_size = fs.block_size(jblk);
125 const idx_t blk_begin = fs.block_begin(jblk);
126 for (idx_t jlev = 0; jlev < sloc.shape(1); ++jlev) {
127 for (idx_t jrof = 0; jrof < blk_size; ++jrof) {
128 sloc_v(blk_begin+jrof, jlev) = loc_v(jblk, jlev, jrof);
129 }
130 }
131 }
132 }
133 else if (loc.variables() and not loc.levels()) {
134 auto loc_v = array::make_view<ValueType, 3>(loc);
135 auto sloc_v = array::make_view<ValueType, 2>(sloc);
136 for (idx_t jblk = 0; jblk < fs.nblks(); ++jblk) {
137 const idx_t blk_size = fs.block_size(jblk);
138 const idx_t blk_begin = fs.block_begin(jblk);
139 for (idx_t jvar = 0; jvar < sloc.shape(1); ++jvar) {
140 for (idx_t jrof = 0; jrof < blk_size; ++jrof) {
141 sloc_v(blk_begin+jrof, jvar) = loc_v(jblk, jvar, jrof);
142 }
143 }
144 }
145 }
146 else {
147 auto loc_v = array::make_view<ValueType, 2>(loc);
148 auto sloc_v = array::make_view<ValueType, 1>(sloc);
149 for (idx_t jblk = 0; jblk < fs.nblks(); ++jblk) {
150 const idx_t blk_size = fs.block_size(jblk);
151 const idx_t blk_begin = fs.block_begin(jblk);
152 for (idx_t jrof = 0; jrof < blk_size; ++jrof) {
153 sloc_v(blk_begin+jrof) = loc_v(jblk, jrof);
154 }
155 }
156 }
157 }
158
159 void transpose_nonblocked_to_blocked(const Field& nonblocked, Field& blocked, const functionspace::detail::BlockStructuredColumns& fs) {
160 auto kind = nonblocked.datatype().kind();
161 if (kind == array::DataType::kind<int>()) {
162 block_copy<int>(nonblocked, blocked, fs);
163 }
164 else if (kind == array::DataType::kind<long>()) {
165 block_copy<long>(nonblocked, blocked, fs);
166 }
167 else if (kind == array::DataType::kind<float>()) {
168 block_copy<float>(nonblocked, blocked, fs);
169 }
170 else if (kind == array::DataType::kind<double>()) {
171 block_copy<double>(nonblocked, blocked, fs);
172 }
173 else {
174 throw_Exception("datatype not supported", Here());
175 }
176 }
177
178 void transpose_blocked_to_nonblocked(const Field& blocked, Field& nonblocked, const functionspace::detail::BlockStructuredColumns& fs) {
179 auto kind = blocked.datatype().kind();
180 if (kind == array::DataType::kind<int>()) {
181 rev_block_copy<int>(blocked, nonblocked, fs);
182 }
183 else if (kind == array::DataType::kind<long>()) {
184 rev_block_copy<long>(blocked, nonblocked, fs);
185 }
186 else if (kind == array::DataType::kind<float>()) {
187 rev_block_copy<float>(blocked, nonblocked, fs);
188 }
189 else if (kind == array::DataType::kind<double>()) {
190 rev_block_copy<double>(blocked, nonblocked, fs);
191 }
192 else {
193 throw_Exception("datatype not supported", Here());
194 }
195 }
196
197
198 }// namespace
199
200 array::ArrayShape BlockStructuredColumns::config_shape(const eckit::Configuration& config) const {
201 array::ArrayShape shape;
202
203 bool global = false;
204 config.get("global", global);
205 if (global) {
206 return structuredcolumns_->config_shape(config);
207 }
208 else {
209 shape.emplace_back(nblks_);
210 idx_t variables(0);
211 config.get("variables", variables);
212 if (variables > 0) {
213 shape.emplace_back(variables);
214 }
215 idx_t levels(structuredcolumns_->levels());
216 config.get("levels", levels);
217 if (levels > 0) {
218 shape.emplace_back(levels);
219 }
220 shape.emplace_back(nproma_);
221 }
222 return shape;
223 }
224
225 array::ArraySpec BlockStructuredColumns::config_spec(const eckit::Configuration& config) const {
226 return array::ArraySpec(config_shape(config), structuredcolumns_->config_alignment(config));
227 }
228
229 // ----------------------------------------------------------------------------
230 // Constructor
231 // ----------------------------------------------------------------------------
232 BlockStructuredColumns::BlockStructuredColumns(const Grid& grid, const eckit::Configuration& config):
233 BlockStructuredColumns::BlockStructuredColumns(grid, grid::Partitioner(), config) {}
234
235 BlockStructuredColumns::BlockStructuredColumns(const Grid& grid, const grid::Partitioner& p, const eckit::Configuration& config):
236 BlockStructuredColumns(grid, Vertical(config), p, config) {}
237
238 BlockStructuredColumns::BlockStructuredColumns(const Grid& grid, const grid::Distribution& distribution,
239 const eckit::Configuration& config):
240 BlockStructuredColumns(grid, distribution, Vertical(config), config) {}
241
242 BlockStructuredColumns::BlockStructuredColumns(const Grid& grid, const grid::Distribution& distribution, const Vertical& vertical,
243 const eckit::Configuration& config):
244 structuredcolumns_(new StructuredColumns(grid, distribution, vertical, config)),
245 structuredcolumns_handle_(structuredcolumns_){
246 setup(config);
247 }
248
249 BlockStructuredColumns::BlockStructuredColumns(const Grid& grid, const Vertical& vertical, const eckit::Configuration& config):
250 BlockStructuredColumns(grid, vertical, grid::Partitioner(), config) {}
251
252 BlockStructuredColumns::BlockStructuredColumns(const Grid& grid, const Vertical& vertical, const grid::Partitioner& p,
253 const eckit::Configuration& config):
254 structuredcolumns_(new StructuredColumns(grid, vertical, p, config)),
255 structuredcolumns_handle_(structuredcolumns_){
256 setup(config);
257 }
258
259 // ----------------------------------------------------------------------------
260 // Create Field
261 // ----------------------------------------------------------------------------
262 Field BlockStructuredColumns::createField(const eckit::Configuration& options) const {
263 Field field(structuredcolumns_->config_name(options), structuredcolumns_->config_datatype(options), config_spec(options));
264 structuredcolumns_->set_field_metadata(options, field);
265 field.set_functionspace(this);
266
267 bool global = false;
268 options.get("global", global);
269 if (not global) {
270 field.set_horizontal_dimension({0,field.rank()-1});
271 }
272
273 return field;
274 }
275
276 Field BlockStructuredColumns::createField(const Field& other, const eckit::Configuration& config) const {
277 return createField(option::name(other.name()) | option::datatype(other.datatype()) |
278 option::levels(other.levels()) |option::variables(other.variables()) |
279 option::type(other.metadata().getString("type", "scalar")) | config);
280 }
281 // ----------------------------------------------------------------------------
282
283 // ----------------------------------------------------------------------------
284 // Scatter FieldSet
285 // ----------------------------------------------------------------------------
286 void BlockStructuredColumns::scatter(const FieldSet& global_fieldset, FieldSet& local_fieldset) const {
287 ATLAS_ASSERT(local_fieldset.size() == global_fieldset.size());
288 for (idx_t f = 0; f < local_fieldset.size(); ++f) {
289 const Field& glb = global_fieldset[f];
290 Field& loc = local_fieldset[f];
291 auto sloc = structuredcolumns_->createField(glb, util::Config("global",false));
292 structuredcolumns_->scatter(glb, sloc);
293 loc.metadata() = sloc.metadata();
294 transpose_nonblocked_to_blocked(sloc, loc, *this);
295 }
296 }
297
298 // ----------------------------------------------------------------------------
299 // Scatter Field
300 // ----------------------------------------------------------------------------
301 void BlockStructuredColumns::scatter(const Field& global, Field& local) const {
302 FieldSet global_fields;
303 FieldSet local_fields;
304 global_fields.add(global);
305 local_fields.add(local);
306 scatter(global_fields, local_fields);
307 }
308
309 // ----------------------------------------------------------------------------
310 // Gather FieldSet
311 // ----------------------------------------------------------------------------
312 void BlockStructuredColumns::gather(const FieldSet& local_fieldset, FieldSet& global_fieldset) const {
313 ATLAS_ASSERT(local_fieldset.size() == global_fieldset.size());
314 for (idx_t f = 0; f < local_fieldset.size(); ++f) {
315 const Field& loc = local_fieldset[f];
316 Field& glb = global_fieldset[f];
317 auto sloc = structuredcolumns_->createField(loc, util::Config("global",false));
318 transpose_blocked_to_nonblocked(loc, sloc, *this);
319 structuredcolumns_->gather(sloc, glb);
320 glb.metadata() = loc.metadata();
321 glb.metadata().set("global", false);
322 }
323 }
324 // ----------------------------------------------------------------------------
325
326 void BlockStructuredColumns::setup(const eckit::Configuration &config) {
327 nproma_ = 1;
328 idx_t tmp_nproma;
329 if (config.get("nproma", tmp_nproma)) {
330 ATLAS_ASSERT(tmp_nproma > 0);
331 nproma_ = tmp_nproma;
332 }
333
334 nblks_ = std::floor( structuredcolumns_->size() / nproma_ );
335 endblk_size_ = nproma_;
336 if (structuredcolumns_->size() % nproma_ > 0) {
337 endblk_size_ = structuredcolumns_->size() - nblks_ * nproma_;
338 nblks_++;
339 }
340 }
341
342 // ----------------------------------------------------------------------------
343 // Gather Field
344 // ----------------------------------------------------------------------------
345 void BlockStructuredColumns::gather(const Field& local, Field& global) const {
346 FieldSet local_fields;
347 FieldSet global_fields;
348 local_fields.add(local);
349 global_fields.add(global);
350 gather(local_fields, global_fields);
351 }
352
353 // ----------------------------------------------------------------------------
354 // Checksum Field
355 // ----------------------------------------------------------------------------
356
357 std::string BlockStructuredColumns::checksum(const Field&) const {
358 ATLAS_NOTIMPLEMENTED;
359 }
360
361 std::string BlockStructuredColumns::checksum(const FieldSet&) const {
362 ATLAS_NOTIMPLEMENTED;
363 }
364
365
366 // ----------------------------------------------------------------------------
367
368
369 } // namespace detail
370 } // namespace functionspace
371 } // namespace atlas