Debian Codegraph @

src/atlas/functionspace/detail/StructuredColumns.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/StructuredColumns.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/library/FloatingPointExceptions.h"
31 #include "atlas/mesh/Mesh.h"
32 #include "atlas/parallel/Checksum.h"
33 #include "atlas/parallel/GatherScatter.h"
34 #include "atlas/parallel/HaloExchange.h"
35 #include "atlas/parallel/mpi/mpi.h"
36 #include "atlas/parallel/omp/fill.h"
37 #include "atlas/parallel/omp/omp.h"
38 #include "atlas/runtime/Exception.h"
39 #include "atlas/runtime/Trace.h"
40 #include "atlas/util/Checksum.h"
41 #include "atlas/util/CoordinateEnums.h"
42 #include "atlas/util/detail/Cache.h"
43
44 #if ATLAS_HAVE_FORTRAN
45 #define REMOTE_IDX_BASE 1
46 #else
47 #define REMOTE_IDX_BASE 0
48 #endif
49
50 namespace atlas {
51 namespace functionspace {
52 namespace detail {
53
54 namespace {
55
56 template <typename T, typename Field>
57 array::LocalView<T, 3> make_leveled_view(Field& field) {
58 using namespace array;
59 if (field.levels()) {
60 if (field.variables()) {
61 return make_view<T, 3>(field).slice(Range::all(), Range::all(), Range::all());
62 }
63 else {
64 return make_view<T, 2>(field).slice(Range::all(), Range::all(), Range::dummy());
65 }
66 }
67 else {
68 if (field.variables()) {
69 return make_view<T, 2>(field).slice(Range::all(), Range::dummy(), Range::all());
70 }
71 else {
72 return make_view<T, 1>(field).slice(Range::all(), Range::dummy(), Range::dummy());
73 }
74 }
75 }
76
77 template <typename T>
78 std::string checksum_3d_field(const parallel::Checksum& checksum, const Field& field) {
79 bool disabled_fpe_overflow = library::disable_floating_point_exception(FE_OVERFLOW);
80 bool disabled_fpe_invalid = library::disable_floating_point_exception(FE_INVALID);
81 auto values = make_leveled_view<const T>(field);
82 array::ArrayT<T> surface_field(values.shape(0), values.shape(2));
83 auto surface = array::make_view<T, 2>(surface_field);
84 const idx_t npts = values.shape(0);
85 atlas_omp_for(idx_t n = 0; n < npts; ++n) {
86 for (idx_t j = 0; j < surface.shape(1); ++j) {
87 surface(n, j) = 0.;
88 for (idx_t l = 0; l < values.shape(1); ++l) {
89 surface(n, j) += values(n, l, j);
90 }
91 }
92 }
93 if (disabled_fpe_overflow) {
94 library::enable_floating_point_exception(FE_OVERFLOW);
95 }
96 if (disabled_fpe_invalid) {
97 library::enable_floating_point_exception(FE_INVALID);
98 }
99 return checksum.execute(surface.data(), surface_field.stride(0));
100 }
101
102 } // namespace
103
104 class StructuredColumnsHaloExchangeCache : public util::Cache<std::string, parallel::HaloExchange>,
105 public grid::detail::grid::GridObserver {
106 private:
107 using Base = util::Cache<std::string, parallel::HaloExchange>;
108 StructuredColumnsHaloExchangeCache(): Base("StructuredColumnsHaloExchangeCache") {}
109
110 public:
111 static StructuredColumnsHaloExchangeCache& instance() {
112 static StructuredColumnsHaloExchangeCache inst;
113 return inst;
114 }
115 util::ObjectHandle<value_type> get_or_create(const detail::StructuredColumns& funcspace) {
116 registerGrid(*funcspace.grid().get());
117
118 creator_type creator = std::bind(&StructuredColumnsHaloExchangeCache::create, &funcspace);
119 return Base::get_or_create(key(funcspace), remove_key(funcspace), creator);
120 }
121 void onGridDestruction(grid::detail::grid::Grid& grid) override { remove(remove_key(grid)); }
122
123 private:
124 static Base::key_type key(const detail::StructuredColumns& funcspace) {
125 std::ostringstream key;
126 key << "grid[address=" << funcspace.grid().get() << ",halo=" << funcspace.halo()
127 << ",periodic_points=" << std::boolalpha << funcspace.periodic_points_
128 << ",distribution=" << funcspace.distribution() << "]";
129 return key.str();
130 }
131
132 static Base::key_type remove_key(const detail::StructuredColumns& funcspace) {
133 return remove_key(*funcspace.grid().get());
134 }
135
136 static Base::key_type remove_key(const grid::detail::grid::Grid& grid) {
137 std::ostringstream key;
138 key << "grid[address=" << &grid << "]";
139 return key.str();
140 }
141
142 static value_type* create(const detail::StructuredColumns* funcspace) {
143 value_type* value = new value_type();
144
145 value->setup(funcspace->mpi_comm(),
146 array::make_view<int, 1>(funcspace->partition()).data(),
147 array::make_view<idx_t, 1>(funcspace->remote_index()).data(), REMOTE_IDX_BASE,
148 funcspace->sizeHalo(), funcspace->sizeOwned());
149 return value;
150 }
151 ~StructuredColumnsHaloExchangeCache() override = default;
152 };
153
154 class StructuredColumnsGatherScatterCache : public util::Cache<std::string, parallel::GatherScatter>,
155 public grid::detail::grid::GridObserver {
156 private:
157 using Base = util::Cache<std::string, parallel::GatherScatter>;
158 StructuredColumnsGatherScatterCache(): Base("StructuredColumnsGatherScatterCache") {}
159
160 public:
161 static StructuredColumnsGatherScatterCache& instance() {
162 static StructuredColumnsGatherScatterCache inst;
163 return inst;
164 }
165 util::ObjectHandle<value_type> get_or_create(const detail::StructuredColumns& funcspace) {
166 registerGrid(*funcspace.grid().get());
167 creator_type creator = std::bind(&StructuredColumnsGatherScatterCache::create, &funcspace);
168 return Base::get_or_create(key(funcspace), remove_key(funcspace), creator);
169 }
170 void onGridDestruction(grid::detail::grid::Grid& grid) override { remove(remove_key(grid)); }
171
172 private:
173 static Base::key_type key(const detail::StructuredColumns& funcspace) {
174 std::ostringstream key;
175 key << "grid[address=" << funcspace.grid().get() << ",halo=" << funcspace.halo()
176 << ",periodic_points=" << std::boolalpha << funcspace.periodic_points_
177 << ",distribution=" << funcspace.distribution() << "]";
178 return key.str();
179 }
180
181 static Base::key_type remove_key(const detail::StructuredColumns& funcspace) {
182 return remove_key(*funcspace.grid().get());
183 }
184
185 static Base::key_type remove_key(const grid::detail::grid::Grid& grid) {
186 std::ostringstream key;
187 key << "grid[address=" << &grid << "]";
188 return key.str();
189 }
190
191 static value_type* create(const detail::StructuredColumns* funcspace) {
192 value_type* value = new value_type();
193
194 value->setup(funcspace->mpi_comm(),
195 array::make_view<int, 1>(funcspace->partition()).data(),
196 array::make_view<idx_t, 1>(funcspace->remote_index()).data(), REMOTE_IDX_BASE,
197 array::make_view<gidx_t, 1>(funcspace->global_index()).data(), funcspace->sizeOwned());
198 return value;
199 }
200 ~StructuredColumnsGatherScatterCache() override = default;
201 };
202
203 class StructuredColumnsChecksumCache : public util::Cache<std::string, parallel::Checksum>,
204 public grid::detail::grid::GridObserver {
205 private:
206 using Base = util::Cache<std::string, parallel::Checksum>;
207 StructuredColumnsChecksumCache(): Base("StructuredColumnsChecksumCache") {}
208
209 public:
210 static StructuredColumnsChecksumCache& instance() {
211 static StructuredColumnsChecksumCache inst;
212 return inst;
213 }
214 util::ObjectHandle<value_type> get_or_create(const detail::StructuredColumns& funcspace) {
215 registerGrid(*funcspace.grid().get());
216 creator_type creator = std::bind(&StructuredColumnsChecksumCache::create, &funcspace);
217 return Base::get_or_create(key(funcspace), remove_key(funcspace), creator);
218 }
219 void onGridDestruction(grid::detail::grid::Grid& grid) override { remove(remove_key(grid)); }
220
221 private:
222 static Base::key_type key(const detail::StructuredColumns& funcspace) {
223 std::ostringstream key;
224 key << "grid[address=" << funcspace.grid().get() << ",halo=" << funcspace.halo()
225 << ",periodic_points=" << std::boolalpha << funcspace.periodic_points_
226 << ",distribution=" << funcspace.distribution() << "]";
227 return key.str();
228 }
229
230 static Base::key_type remove_key(const detail::StructuredColumns& funcspace) {
231 return remove_key(*funcspace.grid().get());
232 }
233
234 static Base::key_type remove_key(const grid::detail::grid::Grid& grid) {
235 std::ostringstream key;
236 key << "grid[address=" << &grid << "]";
237 return key.str();
238 }
239
240 static value_type* create(const detail::StructuredColumns* funcspace) {
241 value_type* value = new value_type();
242 util::ObjectHandle<parallel::GatherScatter> gather(
243 StructuredColumnsGatherScatterCache::instance().get_or_create(*funcspace));
244 value->setup(gather);
245 return value;
246 }
247 ~StructuredColumnsChecksumCache() override = default;
248 };
249
250
251 const parallel::GatherScatter& StructuredColumns::gather() const {
252 if (gather_scatter_) {
253 return *gather_scatter_;
254 }
255 gather_scatter_ = StructuredColumnsGatherScatterCache::instance().get_or_create(*this);
256 return *gather_scatter_;
257 }
258
259 const parallel::GatherScatter& StructuredColumns::scatter() const {
260 if (gather_scatter_) {
261 return *gather_scatter_;
262 }
263 gather_scatter_ = StructuredColumnsGatherScatterCache::instance().get_or_create(*this);
264 return *gather_scatter_;
265 }
266
267 const parallel::Checksum& StructuredColumns::checksum() const {
268 if (checksum_) {
269 return *checksum_;
270 }
271 checksum_ = StructuredColumnsChecksumCache::instance().get_or_create(*this);
272 return *checksum_;
273 }
274
275 const parallel::HaloExchange& StructuredColumns::halo_exchange() const {
276 if (halo_exchange_) {
277 return *halo_exchange_;
278 }
279 halo_exchange_ = StructuredColumnsHaloExchangeCache::instance().get_or_create(*this);
280 return *halo_exchange_;
281 }
282
283 void StructuredColumns::set_field_metadata(const eckit::Configuration& config, Field& field) const {
284 field.set_functionspace(this);
285
286 bool global(false);
287 if (config.get("global", global)) {
288 if (global) {
289 idx_t owner(0);
290 config.get("owner", owner);
291 field.metadata().set("owner", owner);
292 }
293 }
294 field.metadata().set("global", global);
295
296 idx_t levels(nb_levels_);
297 config.get("levels", levels);
298 field.set_levels(levels);
299
300 idx_t variables(0);
301 config.get("variables", variables);
302 field.set_variables(variables);
303
304 if (config.has("type")) {
305 field.metadata().set("type", config.getString("type"));
306 }
307
308 if (config.has("vector_component")) {
309 field.metadata().set("vector_component", config.getSubConfiguration("vector_component"));
310 }
311 }
312
313 array::DataType StructuredColumns::config_datatype(const eckit::Configuration& config) const {
314 array::DataType::kind_t kind;
315 if (!config.get("datatype", kind)) {
316 throw_Exception("datatype missing", Here());
317 }
318 return array::DataType(kind);
319 }
320
321 std::string StructuredColumns::config_name(const eckit::Configuration& config) const {
322 std::string name;
323 config.get("name", name);
324 return name;
325 }
326
327 idx_t StructuredColumns::config_levels(const eckit::Configuration& config) const {
328 idx_t levels(nb_levels_);
329 config.get("levels", levels);
330 return levels;
331 }
332
333 array::ArrayShape StructuredColumns::config_shape(const eckit::Configuration& config) const {
334 array::ArrayShape shape;
335
336 shape.emplace_back(config_size(config));
337
338 idx_t levels(nb_levels_);
339 config.get("levels", levels);
340 if (levels > 0) {
341 shape.emplace_back(levels);
342 }
343
344 idx_t variables(0);
345 config.get("variables", variables);
346 if (variables > 0) {
347 shape.emplace_back(variables);
348 }
349
350 return shape;
351 }
352
353 array::ArrayAlignment StructuredColumns::config_alignment(const eckit::Configuration& config) const {
354 int alignment(1);
355 config.get("alignment", alignment);
356 return alignment;
357 }
358
359 array::ArraySpec StructuredColumns::config_spec(const eckit::Configuration& config) const {
360 return array::ArraySpec(config_shape(config), config_alignment(config));
361 }
362
363 size_t StructuredColumns::Map2to1::footprint() const {
364 size_t size = sizeof(*this);
365 size += data_.size() * sizeof(decltype(data_)::value_type);
366 return size;
367 }
368
369 void StructuredColumns::Map2to1::print(std::ostream& out) const {
370 for (idx_t j = j_min_; j <= j_max_; ++j) {
371 out << std::setw(4) << j << " : ";
372 for (idx_t i = i_min_; i <= i_max_; ++i) {
373 idx_t v = operator()(i, j);
374 if (v == missing()) {
375 out << std::setw(6) << "X";
376 }
377 else {
378 out << std::setw(6) << v;
379 }
380 }
381 out << '\n';
382 }
383 }
384
385 void StructuredColumns::IndexRange::print(std::ostream& out) const {
386 for (idx_t i = min_; i <= max_; ++i) {
387 idx_t v = operator()(i);
388 if (v == missing()) {
389 out << std::setw(4) << "X";
390 }
391 else {
392 out << std::setw(4) << v;
393 }
394 }
395 out << '\n';
396 }
397
398 idx_t StructuredColumns::config_size(const eckit::Configuration& config) const {
399 idx_t size = size_halo_;
400 bool global(false);
401 if (config.get("global", global)) {
402 if (global) {
403 idx_t owner(0);
404 config.get("owner", owner);
405 idx_t rank = mpi::comm(mpi_comm()).rank();
406 size = (rank == owner ? grid_->size() : 0);
407 }
408 }
409 return size;
410 }
411
412 std::string StructuredColumns::distribution() const {
413 return distribution_;
414 }
415
416 void StructuredColumns::throw_outofbounds(idx_t i, idx_t j) const {
417 std::stringstream ss;
418 if (j < j_begin_halo() || j >= j_end_halo()) {
419 ss << "OutofBounds: j out of range! : (i,j) = (" << i << "," << j << ") --- Expected: " << j_begin_halo()
420 << " <= j < " << j_end_halo();
421 }
422 if (i < i_begin_halo(j) || i >= i_end_halo(j)) {
423 ss << "OutofBounds: i out of range! : (i,j) = (" << i << "," << j << ") --- Expected: " << i_begin_halo(j)
424 << " <= i < " << i_end_halo(j);
425 }
426 throw_Exception(ss.str(), Here());
427 }
428
429 namespace {
430 static std::string extract_mpi_comm(const eckit::Configuration& config) {
431 if(config.has("mpi_comm")){
432 return config.getString("mpi_comm");
433 }
434 return mpi::comm().name();
435 }
436 }
437
438 // ----------------------------------------------------------------------------
439 // Constructor
440 // ----------------------------------------------------------------------------
441 StructuredColumns::StructuredColumns(const Grid& grid, const eckit::Configuration& config):
442 StructuredColumns::StructuredColumns(grid, grid::Partitioner(), config) {}
443
444 StructuredColumns::StructuredColumns(const Grid& grid, const grid::Partitioner& p, const eckit::Configuration& config):
445 StructuredColumns(grid, Vertical(config), p, config) {}
446
447 StructuredColumns::StructuredColumns(const Grid& grid, const grid::Distribution& distribution,
448 const eckit::Configuration& config):
449 StructuredColumns(grid, distribution, Vertical(config), config) {}
450
451 StructuredColumns::StructuredColumns(const Grid& grid, const grid::Distribution& distribution, const Vertical& vertical,
452 const eckit::Configuration& config):
453 vertical_(vertical), nb_levels_(vertical_.size()), grid_(new StructuredGrid(grid)), mpi_comm_(extract_mpi_comm(config)) {
454 setup(distribution, config);
455 }
456
457 StructuredColumns::StructuredColumns(const Grid& grid, const Vertical& vertical, const eckit::Configuration& config):
458 StructuredColumns(grid, vertical, grid::Partitioner(), config) {}
459
460 StructuredColumns::StructuredColumns(const Grid& grid, const Vertical& vertical, const grid::Partitioner& p,
461 const eckit::Configuration& config):
462 vertical_(vertical), nb_levels_(vertical_.size()), grid_(new StructuredGrid(grid)) {
463 ATLAS_TRACE("StructuredColumns constructor");
464
465 grid::Partitioner partitioner(p);
466
467 if (config.has("mpi_comm")) {
468 mpi_comm_ = config.getString("mpi_comm");
469 if (partitioner) {
470 ATLAS_ASSERT(partitioner.mpi_comm() == mpi_comm_);
471 }
472 if (config.has("partitioner.mpi_comm")) {
473 ATLAS_ASSERT(config.getString("partitioner.mpi_comm") == mpi_comm_);
474 }
475 }
476 else if (config.has("partitioner.mpi_comm")) {
477 mpi_comm_ = config.getString("partitioner.mpi_comm");
478 }
479 else if (partitioner) {
480 mpi_comm_ = partitioner.mpi_comm();
481 }
482 else {
483 mpi_comm_ = mpi::comm().name();
484 }
485
486 if (not partitioner) {
487 util::Config partitioner_config;
488 if (config.has("partitioner")) {
489 partitioner_config = config.getSubConfiguration("partitioner");
490 }
491 else {
492 if (grid_->domain().global()) {
493 partitioner_config.set("type", "equal_regions");
494 }
495 else {
496 partitioner_config.set("type", "checkerboard");
497 }
498 }
499 if (not partitioner_config.has("mpi_comm")) {
500 partitioner_config.set("mpi_comm", mpi_comm_);
501 }
502 partitioner = grid::Partitioner(partitioner_config);
503 }
504
505 grid::Distribution distribution;
506
507 {
508 mpi::Scope mpi_scope(mpi_comm_);
509 ATLAS_TRACE_SCOPE("Partitioning grid") { distribution = grid::Distribution(grid, partitioner); }
510 }
511
512 setup(distribution, config);
513 }
514
515 // ----------------------------------------------------------------------------
516
517 void StructuredColumns::compute_xy(idx_t i, idx_t j, PointXY& xy) const {
518 idx_t jj;
519 if (j < 0) {
520 jj = -j - 1 + north_pole_included_;
521 xy[YY] = 180. - grid_->y(jj);
522 }
523 else if (j >= ny_) {
524 jj = 2 * ny_ - j - 1 - south_pole_included_;
525 xy[YY] = -180. - grid_->y(jj);
526 }
527 else {
528 jj = j;
529 xy[YY] = grid_->y(jj);
530 }
531 xy[XX] = grid_->x(i, jj);
532 }
533
534
535 void StructuredColumns::Map2to1::resize(std::array<idx_t, 2> i_range, std::array<idx_t, 2> j_range) {
536 i_min_ = i_range[0];
537 i_max_ = i_range[1];
538 j_min_ = j_range[0];
539 j_max_ = j_range[1];
540 j_stride_ = (i_max_ - i_min_ + 1);
541 data_.resize((i_max_ - i_min_ + 1) * (j_max_ - j_min_ + 1));
542 atlas::omp::fill(data_.begin(), data_.end(), missing() + 1);
543 }
544
545 const util::PartitionPolygon& StructuredColumns::polygon(idx_t halo) const {
546 if (halo >= static_cast<idx_t>(polygons_.size())) {
547 polygons_.resize(halo + 1);
548 }
549 if (not polygons_[halo]) {
550 if (halo > this->halo()) {
551 throw_Exception("StructuredColumns does not contain a halo of size " + std::to_string(halo) + ".", Here());
552 }
553 polygons_[halo].reset(new grid::StructuredPartitionPolygon(*this, halo));
554 }
555 return *polygons_[halo];
556 }
557
558 const util::PartitionPolygons& StructuredColumns::polygons() const {
559 if (all_polygons_.size() == 0) {
560 polygon().allGather(all_polygons_);
561 }
562 return all_polygons_;
563 }
564
565 // ----------------------------------------------------------------------------
566 // Destructor
567 // ----------------------------------------------------------------------------
568 StructuredColumns::~StructuredColumns() {
569 delete grid_;
570 }
571 // ----------------------------------------------------------------------------
572
573 // ----------------------------------------------------------------------------
574 // Create Field
575 // ----------------------------------------------------------------------------
576 Field StructuredColumns::createField(const eckit::Configuration& options) const {
577 Field field(config_name(options), config_datatype(options), config_spec(options));
578 set_field_metadata(options, field);
579 return field;
580 }
581
582 Field StructuredColumns::createField(const Field& other, const eckit::Configuration& config) const {
583 return createField(option::datatype(other.datatype()) | option::levels(other.levels()) |
584 option::variables(other.variables()) |
585 option::type(other.metadata().getString("type", "scalar")) | config);
586 }
587 // ----------------------------------------------------------------------------
588
589 // ----------------------------------------------------------------------------
590 // Gather FieldSet
591 // ----------------------------------------------------------------------------
592 void StructuredColumns::gather(const FieldSet& local_fieldset, FieldSet& global_fieldset) const {
593 ATLAS_ASSERT(local_fieldset.size() == global_fieldset.size());
594
595 for (idx_t f = 0; f < local_fieldset.size(); ++f) {
596 const Field& loc = local_fieldset[f];
597 Field& glb = global_fieldset[f];
598 const idx_t nb_fields = 1;
599 idx_t root(0);
600 glb.metadata().get("owner", root);
601
602 if (loc.datatype() == array::DataType::kind<int>()) {
603 parallel::Field<int const> loc_field(make_leveled_view<const int>(loc));
604 parallel::Field<int> glb_field(make_leveled_view<int>(glb));
605 gather().gather(&loc_field, &glb_field, nb_fields, root);
606 }
607 else if (loc.datatype() == array::DataType::kind<long>()) {
608 parallel::Field<long const> loc_field(make_leveled_view<const long>(loc));
609 parallel::Field<long> glb_field(make_leveled_view<long>(glb));
610 gather().gather(&loc_field, &glb_field, nb_fields, root);
611 }
612 else if (loc.datatype() == array::DataType::kind<float>()) {
613 parallel::Field<float const> loc_field(make_leveled_view<const float>(loc));
614 parallel::Field<float> glb_field(make_leveled_view<float>(glb));
615 gather().gather(&loc_field, &glb_field, nb_fields, root);
616 }
617 else if (loc.datatype() == array::DataType::kind<double>()) {
618 parallel::Field<double const> loc_field(make_leveled_view<const double>(loc));
619 parallel::Field<double> glb_field(make_leveled_view<double>(glb));
620 gather().gather(&loc_field, &glb_field, nb_fields, root);
621 }
622 else {
623 throw_Exception("datatype not supported", Here());
624 }
625 }
626 }
627 // ----------------------------------------------------------------------------
628
629 // ----------------------------------------------------------------------------
630 // Gather Field
631 // ----------------------------------------------------------------------------
632 void StructuredColumns::gather(const Field& local, Field& global) const {
633 FieldSet local_fields;
634 FieldSet global_fields;
635 local_fields.add(local);
636 global_fields.add(global);
637 gather(local_fields, global_fields);
638 }
639 // ----------------------------------------------------------------------------
640
641 // ----------------------------------------------------------------------------
642 // Scatter FieldSet
643 // ----------------------------------------------------------------------------
644 void StructuredColumns::scatter(const FieldSet& global_fieldset, FieldSet& local_fieldset) const {
645 ATLAS_ASSERT(local_fieldset.size() == global_fieldset.size());
646
647 for (idx_t f = 0; f < local_fieldset.size(); ++f) {
648 const Field& glb = global_fieldset[f];
649 Field& loc = local_fieldset[f];
650 const idx_t nb_fields = 1;
651 idx_t root(0);
652 glb.metadata().get("owner", root);
653
654 if (loc.datatype() == array::DataType::kind<int>()) {
655 parallel::Field<int const> glb_field(make_leveled_view<const int>(glb));
656 parallel::Field<int> loc_field(make_leveled_view<int>(loc));
657 scatter().scatter(&glb_field, &loc_field, nb_fields, root);
658 }
659 else if (loc.datatype() == array::DataType::kind<long>()) {
660 parallel::Field<long const> glb_field(make_leveled_view<const long>(glb));
661 parallel::Field<long> loc_field(make_leveled_view<long>(loc));
662 scatter().scatter(&glb_field, &loc_field, nb_fields, root);
663 }
664 else if (loc.datatype() == array::DataType::kind<float>()) {
665 parallel::Field<float const> glb_field(make_leveled_view<const float>(glb));
666 parallel::Field<float> loc_field(make_leveled_view<float>(loc));
667 scatter().scatter(&glb_field, &loc_field, nb_fields, root);
668 }
669 else if (loc.datatype() == array::DataType::kind<double>()) {
670 parallel::Field<double const> glb_field(make_leveled_view<const double>(glb));
671 parallel::Field<double> loc_field(make_leveled_view<double>(loc));
672 scatter().scatter(&glb_field, &loc_field, nb_fields, root);
673 }
674 else {
675 throw_Exception("datatype not supported", Here());
676 }
677
678 glb.metadata().broadcast(loc.metadata(), root);
679 loc.metadata().set("global", false);
680 loc.set_dirty(true);
681 }
682 }
683 // ----------------------------------------------------------------------------
684
685 // ----------------------------------------------------------------------------
686 // Scatter Field
687 // ----------------------------------------------------------------------------
688 void StructuredColumns::scatter(const Field& global, Field& local) const {
689 FieldSet global_fields;
690 FieldSet local_fields;
691 global_fields.add(global);
692 local_fields.add(local);
693 scatter(global_fields, local_fields);
694 }
695 // ----------------------------------------------------------------------------
696
697 std::string StructuredColumns::checksum(const FieldSet& fieldset) const {
698 eckit::MD5 md5;
699 for (idx_t f = 0; f < fieldset.size(); ++f) {
700 const Field& field = fieldset[f];
701 if (field.datatype() == array::DataType::kind<int>()) {
702 md5 << checksum_3d_field<int>(checksum(), field);
703 }
704 else if (field.datatype() == array::DataType::kind<long>()) {
705 md5 << checksum_3d_field<long>(checksum(), field);
706 }
707 else if (field.datatype() == array::DataType::kind<float>()) {
708 md5 << checksum_3d_field<float>(checksum(), field);
709 }
710 else if (field.datatype() == array::DataType::kind<double>()) {
711 md5 << checksum_3d_field<double>(checksum(), field);
712 }
713 else {
714 throw_Exception("datatype not supported", Here());
715 }
716 }
717 return md5;
718 }
719 std::string StructuredColumns::checksum(const Field& field) const {
720 FieldSet fieldset;
721 fieldset.add(field);
722 return checksum(fieldset);
723 }
724
725 const StructuredGrid& StructuredColumns::grid() const {
726 return *grid_;
727 }
728
729 namespace {
730
731
732 template <int RANK>
733 struct FixupHaloForVectors {
734 FixupHaloForVectors(const StructuredColumns&) {}
735 template <typename DATATYPE>
736 void apply(Field& field) {
737 std::string type = field.metadata().getString("type", "scalar");
738 if (type == "vector") {
739 ATLAS_NOTIMPLEMENTED;
740 }
741 }
742 };
743
744 template <>
745 struct FixupHaloForVectors<2> {
746 static constexpr int RANK = 2;
747 const StructuredColumns& fs;
748 FixupHaloForVectors(const StructuredColumns& _fs): fs(_fs) {}
749
750 template <typename DATATYPE>
751 void apply(Field& field) {
752 std::string type = field.metadata().getString("type", "scalar");
753 if (type == "vector") {
754 auto array = array::make_view<DATATYPE, RANK>(field);
755 for (idx_t j = fs.j_begin_halo(); j < 0; ++j) {
756 for (idx_t i = fs.i_begin_halo(j); i < fs.i_end_halo(j); ++i) {
757 idx_t n = fs.index(i, j);
758 array(n, XX) = -array(n, XX);
759 array(n, YY) = -array(n, YY);
760 }
761 }
762 for (idx_t j = fs.grid().ny(); j < fs.j_end_halo(); ++j) {
763 for (idx_t i = fs.i_begin_halo(j); i < fs.i_end_halo(j); ++i) {
764 idx_t n = fs.index(i, j);
765 array(n, XX) = -array(n, XX);
766 array(n, YY) = -array(n, YY);
767 }
768 }
769 }
770 }
771 };
772
773 template <>
774 struct FixupHaloForVectors<3> {
775 static constexpr int RANK = 3;
776 const StructuredColumns& fs;
777 FixupHaloForVectors(const StructuredColumns& _fs): fs(_fs) {}
778
779 template <typename DATATYPE>
780 void apply(Field& field) {
781 std::string type = field.metadata().getString("type", "scalar");
782
783 // if levels not setup in functionspace use levels from field
784 idx_t k_end = (fs.k_begin() == 0) && (fs.k_end() == 0) ? field.levels() : fs.k_end();
785
786 if (type == "vector") {
787 auto array = array::make_view<DATATYPE, RANK>(field);
788 for (idx_t j = fs.j_begin_halo(); j < 0; ++j) {
789 for (idx_t i = fs.i_begin_halo(j); i < fs.i_end_halo(j); ++i) {
790 idx_t n = fs.index(i, j);
791 for (idx_t k = fs.k_begin(); k < k_end; ++k) {
792 array(n, k, XX) = -array(n, k, XX);
793 array(n, k, YY) = -array(n, k, YY);
794 }
795 }
796 }
797 for (idx_t j = fs.grid().ny(); j < fs.j_end_halo(); ++j) {
798 for (idx_t i = fs.i_begin_halo(j); i < fs.i_end_halo(j); ++i) {
799 idx_t n = fs.index(i, j);
800 for (idx_t k = fs.k_begin(); k < k_end; ++k) {
801 array(n, k, XX) = -array(n, k, XX);
802 array(n, k, YY) = -array(n, k, YY);
803 }
804 }
805 }
806 }
807 }
808 };
809
810
811 template <int RANK>
812 void dispatch_haloExchange(Field& field, bool on_device, const parallel::HaloExchange& halo_exchange, const StructuredColumns& fs) {
813 FixupHaloForVectors<RANK> fixup_halos(fs);
814 if (field.datatype() == array::DataType::kind<int>()) {
815 halo_exchange.template execute<int, RANK>(field.array(), on_device);
816 fixup_halos.template apply<int>(field);
817 }
818 else if (field.datatype() == array::DataType::kind<long>()) {
819 halo_exchange.template execute<long, RANK>(field.array(), on_device);
820 fixup_halos.template apply<long>(field);
821 }
822 else if (field.datatype() == array::DataType::kind<float>()) {
823 halo_exchange.template execute<float, RANK>(field.array(), on_device);
824 fixup_halos.template apply<float>(field);
825 }
826 else if (field.datatype() == array::DataType::kind<double>()) {
827 halo_exchange.template execute<double, RANK>(field.array(), on_device);
828 fixup_halos.template apply<double>(field);
829 }
830 else {
831 throw_Exception("datatype not supported", Here());
832 }
833 field.set_dirty(false);
834 }
835
836
837 template <int RANK>
838 void dispatch_adjointHaloExchange(Field& field, bool on_device, const parallel::HaloExchange& halo_exchange,
839 const StructuredColumns& fs) {
840 FixupHaloForVectors<RANK> fixup_halos(fs);
841 if (field.datatype() == array::DataType::kind<int>()) {
842 fixup_halos.template apply<int>(field);
843 halo_exchange.template execute_adjoint<int, RANK>(field.array(), on_device);
844 }
845 else if (field.datatype() == array::DataType::kind<long>()) {
846 fixup_halos.template apply<long>(field);
847 halo_exchange.template execute_adjoint<long, RANK>(field.array(), on_device);
848 }
849 else if (field.datatype() == array::DataType::kind<float>()) {
850 fixup_halos.template apply<float>(field);
851 halo_exchange.template execute_adjoint<float, RANK>(field.array(), on_device);
852 }
853 else if (field.datatype() == array::DataType::kind<double>()) {
854 fixup_halos.template apply<double>(field);
855 halo_exchange.template execute_adjoint<double, RANK>(field.array(), on_device);
856 }
857 else {
858 throw_Exception("datatype not supported", Here());
859 }
860 field.set_dirty(false);
861 }
862 } // namespace
863
864 void StructuredColumns::haloExchange(const FieldSet& fieldset, bool on_device) const {
865 for (idx_t f = 0; f < fieldset.size(); ++f) {
866 Field& field = const_cast<FieldSet&>(fieldset)[f];
867 switch (field.rank()) {
868 case 1:
869 dispatch_haloExchange<1>(field, on_device, halo_exchange(), *this);
870 break;
871 case 2:
872 dispatch_haloExchange<2>(field, on_device, halo_exchange(), *this);
873 break;
874 case 3:
875 dispatch_haloExchange<3>(field, on_device, halo_exchange(), *this);
876 break;
877 case 4:
878 dispatch_haloExchange<4>(field, on_device, halo_exchange(), *this);
879 break;
880 default:
881 throw_Exception("Rank not supported", Here());
882 }
883 }
884 }
885
886 void StructuredColumns::adjointHaloExchange(const FieldSet& fieldset, bool on_device) const {
887 for (idx_t f = 0; f < fieldset.size(); ++f) {
888 Field& field = const_cast<FieldSet&>(fieldset)[f];
889 switch (field.rank()) {
890 case 1:
891 dispatch_adjointHaloExchange<1>(field, on_device, halo_exchange(), *this);
892 break;
893 case 2:
894 dispatch_adjointHaloExchange<2>(field, on_device, halo_exchange(), *this);
895 break;
896 case 3:
897 dispatch_adjointHaloExchange<3>(field, on_device, halo_exchange(), *this);
898 break;
899 case 4:
900 dispatch_adjointHaloExchange<4>(field, on_device, halo_exchange(), *this);
901 break;
902 default:
903 throw_Exception("Rank not supported", Here());
904 }
905 }
906 }
907
908 void StructuredColumns::haloExchange(const Field& field, bool on_device) const {
909 FieldSet fieldset;
910 fieldset.add(field);
911 haloExchange(fieldset, on_device);
912 }
913
914 void StructuredColumns::adjointHaloExchange(const Field& field, bool on_device) const {
915 FieldSet fieldset;
916 fieldset.add(field);
917 adjointHaloExchange(fieldset, on_device);
918 }
919
920 size_t StructuredColumns::footprint() const {
921 size_t size = sizeof(*this);
922 size += ij2gp_.footprint();
923 if (field_xy_) {
924 size += field_xy_.footprint();
925 }
926 if (field_partition_) {
927 size += field_partition_.footprint();
928 }
929 if (field_global_index_) {
930 size += field_global_index_.footprint();
931 }
932 if (field_remote_index_) {
933 size += field_remote_index_.footprint();
934 }
935 if (field_index_i_) {
936 size += field_index_i_.footprint();
937 }
938 if (field_index_j_) {
939 size += field_index_j_.footprint();
940 }
941 return size;
942 }
943
944 // ----------------------------------------------------------------------------
945
946
947 } // namespace detail
948 } // namespace functionspace
949 } // namespace atlas