Debian Codegraph @

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