Debian Codegraph @

src/atlas/functionspace/PointCloud.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF
3 * (C) Crown Copyright 2023 Met Office
4 *
5 * This software is licensed under the terms of the Apache Licence Version 2.0
6 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
7 * In applying this licence, ECMWF does not waive the privileges and immunities
8 * granted to it by virtue of its status as an intergovernmental organisation
9 * nor does it submit to any jurisdiction.
10 */
11
12
13 #include <string>
14 #include <vector>
15
16 #include "atlas/functionspace/PointCloud.h"
17 #include "atlas/array.h"
18 #include "atlas/field/Field.h"
19 #include "atlas/field/FieldSet.h"
20 #include "atlas/grid/Distribution.h"
21 #include "atlas/grid/Grid.h"
22 #include "atlas/grid/Iterator.h"
23 #include "atlas/grid/UnstructuredGrid.h"
24 #include "atlas/option/Options.h"
25 #include "atlas/parallel/HaloExchange.h"
26 #include "atlas/parallel/mpi/mpi.h"
27 #include "atlas/runtime/Exception.h"
28 #include "atlas/util/KDTree.h"
29 #include "atlas/util/CoordinateEnums.h"
30 #include "atlas/util/Metadata.h"
31 #include "atlas/util/Point.h"
32 #include "atlas/util/Unique.h"
33
34
35 #include "eckit/mpi/Comm.h"
36 #include "eckit/log/Bytes.h"
37
38 #if ATLAS_HAVE_FORTRAN
39 #define REMOTE_IDX_BASE 1
40 #else
41 #define REMOTE_IDX_BASE 0
42 #endif
43
44 namespace atlas {
45 namespace functionspace {
46
47 namespace detail {
48
49 namespace {
50
51 template <typename T, typename Field>
52 array::LocalView<T, 3> make_leveled_view(Field& field) {
53 using namespace array;
54 if (field.levels()) {
55 if (field.variables()) {
56 return make_view<T, 3>(field).slice(Range::all(), Range::all(), Range::all());
57 }
58 else {
59 return make_view<T, 2>(field).slice(Range::all(), Range::all(), Range::dummy());
60 }
61 }
62 else {
63 if (field.variables()) {
64 return make_view<T, 2>(field).slice(Range::all(), Range::dummy(), Range::all());
65 }
66 else {
67 return make_view<T, 1>(field).slice(Range::all(), Range::dummy(), Range::dummy());
68 }
69 }
70 }
71
72 } // namespace
73
74
75 static std::string get_mpi_comm(const eckit::Configuration& config) {
76 if(config.has("mpi_comm")) {
77 return config.getString("mpi_comm");
78 }
79 return mpi::comm().name();
80 }
81
82 template <>
83 PointCloud::PointCloud(const std::vector<PointXY>& points, const eckit::Configuration& config) {
84 ATLAS_TRACE("PointCloud(std::vector<PointXY>, eckit::Configuration)");
85 mpi_comm_ = get_mpi_comm(config);
86 lonlat_ = Field("lonlat", array::make_datatype<double>(), array::make_shape(points.size(), 2));
87 auto lonlat = array::make_view<double, 2>(lonlat_);
88 for (idx_t j = 0, size = points.size(); j < size; ++j) {
89 lonlat(j, 0) = points[j].x();
90 lonlat(j, 1) = points[j].y();
91 }
92 }
93
94 template <>
95 PointCloud::PointCloud(const std::vector<PointXYZ>& points, const eckit::Configuration& config) {
96 ATLAS_TRACE("PointCloud(std::vector<PointXYZ>, eckit::Configuration)");
97 mpi_comm_ = get_mpi_comm(config);
98 lonlat_ = Field("lonlat", array::make_datatype<double>(), array::make_shape(points.size(), 2));
99 vertical_ = Field("vertical", array::make_datatype<double>(), array::make_shape(points.size()));
100 auto lonlat = array::make_view<double, 2>(lonlat_);
101 auto vertical = array::make_view<double, 1>(vertical_);
102 for (idx_t j = 0, size = points.size(); j < size; ++j) {
103 lonlat(j, 0) = points[j].x();
104 lonlat(j, 1) = points[j].y();
105 vertical(j) = points[j].z();
106 }
107 }
108
109 PointCloud::PointCloud(const Field& lonlat, const eckit::Configuration& config): lonlat_(lonlat) {
110 ATLAS_TRACE("PointCloud(Field lonlat, eckit::Configuration)");
111 mpi_comm_ = get_mpi_comm(config);
112 }
113
114 PointCloud::PointCloud(const Field& lonlat, const Field& ghost, const eckit::Configuration& config): lonlat_(lonlat), ghost_(ghost) {
115 ATLAS_TRACE("PointCloud(Field lonlat, Field ghost, eckit::Configuration)");
116 mpi_comm_ = get_mpi_comm(config);
117 setupParallel();
118 }
119
120 PointCloud::PointCloud(const FieldSet& flds, const eckit::Configuration& config): lonlat_(flds["lonlat"]) {
121 ATLAS_TRACE("PointCloud(Fieldset, eckit::Configuration)");
122 mpi_comm_ = get_mpi_comm(config);
123 if (flds.has("ghost")) {
124 ghost_ = flds["ghost"];
125 }
126 if (flds.has("remote_index")) {
127 remote_index_ = flds["remote_index"];
128 }
129 if (flds.has("partition")) {
130 partition_ = flds["partition"];
131 }
132 if (flds.has("global_index")) {
133 global_index_ = flds["global_index"];
134 }
135 if( ghost_ && remote_index_ && partition_ ) {
136 setupParallel();
137 }
138 }
139
140 grid::Partitioner make_partitioner(const Grid& grid, const eckit::Configuration& config) {
141 auto mpi_comm = get_mpi_comm(config);
142 auto partitioner = grid.partitioner();
143 if( config.has("partitioner") ) {
144 partitioner.set("type",config.getString("partitioner"));
145 }
146 if( not partitioner.has("type") ) {
147 partitioner.set("type","equal_regions");
148 }
149 partitioner.set("mpi_comm",mpi_comm);
150 return grid::Partitioner(partitioner);
151 }
152
153 PointCloud::PointCloud(const Grid& grid, const eckit::Configuration& config) :
154 PointCloud(grid, make_partitioner(grid,config), config) {
155 }
156
157 PointCloud::PointCloud(const Grid& grid, const grid::Distribution& distribution, const eckit::Configuration& config) {
158 ATLAS_TRACE("PointCloud(grid,distribution,config)");
159 mpi_comm_ = get_mpi_comm(config);
160 auto& comm = mpi::comm(mpi_comm_);
161 double halo_radius;
162 config.get("halo_radius", halo_radius = 0.);
163 grid_ = grid;
164
165 part_ = comm.rank();
166
167 nb_partitions_ = distribution.nb_partitions();
168 auto size_owned = distribution.nb_pts()[part_];
169 size_owned_ = size_owned;
170
171 size_global_ = grid.size();
172
173 if (halo_radius == 0. || nb_partitions_ == 1) {
174 idx_t size_halo = size_owned;
175 ATLAS_ASSERT(size_owned > 0);
176 lonlat_ = Field("lonlat", array::make_datatype<double>(), array::make_shape(size_halo, 2));
177 ghost_ = Field("ghost", array::make_datatype<int>(), array::make_shape(size_halo));
178 global_index_ = Field("global_index", array::make_datatype<gidx_t>(), array::make_shape(size_halo));
179 partition_ = Field("partition", array::make_datatype<int>(), array::make_shape(size_halo));
180 remote_index_ = Field("remote_index", array::make_datatype<idx_t>(), array::make_shape(size_halo));
181
182 auto lonlat = array::make_view<double, 2>(lonlat_);
183 auto ridx = array::make_indexview<idx_t,1>(remote_index_);
184 auto gidx = array::make_view<gidx_t,1>(global_index_);
185 array::make_view<int,1>(ghost_).assign(0);
186 array::make_view<int,1>(partition_).assign(part_);
187
188 ATLAS_ASSERT(grid.size() == distribution.size());
189
190 idx_t j{0};
191 gidx_t g{0};
192 for (auto p : grid.lonlat()) {
193 if( distribution.partition(g) == part_ ) {
194 ATLAS_ASSERT(j < size_halo);
195 gidx(j) = g+1;
196 ridx(j) = j;
197 lonlat(j, 0) = p.lon();
198 lonlat(j, 1) = p.lat();
199 ++j;
200 }
201 ++g;
202 }
203 }
204 else {
205
206 std::vector<int> keep;
207
208 {
209 ATLAS_TRACE("Build list of points to keep");
210 std::vector<PointLonLat> owned_lonlat;
211 owned_lonlat.reserve(size_owned);
212
213 auto kdtree = util::IndexKDTree(config);
214 {
215 ATLAS_TRACE("build kdtree");
216 kdtree.reserve(grid.size());
217 idx_t j{0};
218 for (auto p : grid.lonlat()) {
219 if( distribution.partition(j) == part_ ) {
220 owned_lonlat.emplace_back(p);
221 }
222 kdtree.insert(p,j);
223 ++j;
224 }
225 kdtree.build();
226 }
227
228 keep.resize(grid.size());
229 {
230 ATLAS_TRACE("search kdtree");
231 for (idx_t j=0; j<size_owned; ++j) {
232 const auto& p = owned_lonlat[j];
233 auto points = kdtree.closestPointsWithinRadius(p,halo_radius).payloads();
234 for( idx_t jj: points ) {
235 keep[jj] = 1;
236 }
237 }
238 }
239 }
240
241 {
242 ATLAS_TRACE("create fields");
243
244 auto size_halo = std::accumulate(keep.begin(),keep.end(),0);
245
246 lonlat_ = Field("lonlat", array::make_datatype<double>(), array::make_shape(size_halo, 2));
247 partition_ = Field("partition", array::make_datatype<int>(), array::make_shape(size_halo));
248 ghost_ = Field("ghost", array::make_datatype<int>(), array::make_shape(size_halo));
249 global_index_ = Field("global_index", array::make_datatype<gidx_t>(), array::make_shape(size_halo));
250 max_glb_idx_ = grid.size();
251 auto lonlat = array::make_view<double,2>(lonlat_);
252 auto partition = array::make_view<int,1>(partition_);
253 auto ghost = array::make_view<int,1>(ghost_);
254 auto glb_idx = array::make_view<gidx_t,1>(global_index_);
255
256 gidx_t g=0;
257 idx_t j = 0;
258 for (auto p : grid.lonlat()) {
259 if (keep[g]) {
260 lonlat(j, 0) = p.lon();
261 lonlat(j, 1) = p.lat();
262 partition(j) = distribution.partition(g);
263 ghost(j) = partition(j) != part_;
264 glb_idx(j) = g+1;
265 ++j;
266 }
267 ++g;
268 }
269
270 }
271
272 }
273 setupParallel();
274 }
275
276 PointCloud::PointCloud(const Grid& grid, const grid::Partitioner& _partitioner, const eckit::Configuration& config):
277 PointCloud(
278 grid,
279 grid::Distribution{grid, (_partitioner) ? _partitioner :
280 grid::Partitioner{grid.partitioner() | util::Config("mpi_comm",get_mpi_comm(config))}
281 },
282 config) {
283 ATLAS_TRACE("PointCloud(grid,partitioner,config)");
284 }
285
286 Field PointCloud::ghost() const {
287 if (not ghost_) {
288 ghost_ = Field("ghost", array::make_datatype<int>(), array::make_shape(size()));
289 array::make_view<int, 1>(ghost_).assign(0);
290 }
291 return ghost_;
292 }
293
294 const Grid& PointCloud::grid() const {
295 if (grid_) {
296 return grid_;
297 }
298
299 std::vector<PointXY> points;
300 points.reserve(size_global());
301 if (nb_partitions_ == 1) {
302 for (const auto& point : iterate().xy()) {
303 points.push_back(point);
304 }
305 }
306 else {
307 std::vector<int> gidx;
308 gidx.reserve(size_global());
309 std::vector<double> x, y;
310 x.reserve(size_global());
311 y.reserve(size_global());
312 const auto gidxView = array::make_view<gidx_t, 1>(global_index_);
313 const auto ghostView = array::make_view<int, 1>(ghost_);
314 int i = 0;
315 for (const auto& point : iterate().xy()) {
316 if (ghostView(i) == 0) {
317 gidx.push_back(gidxView(i));
318 x.push_back(point.x());
319 y.push_back(point.y());
320 }
321 i++;
322 }
323 const auto& comm = mpi::comm(mpi_comm_);
324 eckit::mpi::Buffer<int> gidxBuffer(comm.size());
325 eckit::mpi::Buffer<double> xBuffer(comm.size());
326 eckit::mpi::Buffer<double> yBuffer(comm.size());
327 comm.allGatherv(gidx.begin(), gidx.end(), gidxBuffer);
328 comm.allGatherv(x.begin(), x.end(), xBuffer);
329 comm.allGatherv(y.begin(), y.end(), yBuffer);
330 for (auto i : gidxBuffer.buffer) {
331 points[i - 1] = atlas::PointXY{xBuffer.buffer[i - 1], yBuffer.buffer[i - 1]};
332 }
333 }
334 grid_ = UnstructuredGrid(points);
335 return grid_;
336 }
337
338
339 array::ArrayShape PointCloud::config_shape(const eckit::Configuration& config) const {
340 idx_t _size = size();
341 bool global(false);
342 if (config.get("global", global)) {
343 if (global) {
344 idx_t owner(0);
345 config.get("owner", owner);
346 idx_t rank = mpi::comm(mpi_comm()).rank();
347 _size = (rank == owner ? size_global() : 0);
348 }
349 }
350
351 array::ArrayShape shape;
352
353 shape.emplace_back(_size);
354
355 idx_t levels(levels_);
356 config.get("levels", levels);
357 if (levels > 0) {
358 shape.emplace_back(levels);
359 }
360
361 idx_t variables(0);
362 config.get("variables", variables);
363 if (variables > 0) {
364 shape.emplace_back(variables);
365 }
366
367 return shape;
368 }
369
370 array::ArrayAlignment PointCloud::config_alignment(const eckit::Configuration& config) const {
371 int alignment(1);
372 config.get("alignment", alignment);
373 return alignment;
374 }
375
376 array::ArraySpec PointCloud::config_spec(const eckit::Configuration& config) const {
377 return array::ArraySpec(config_shape(config), config_alignment(config));
378 }
379
380 array::DataType PointCloud::config_datatype(const eckit::Configuration& config) const {
381 array::DataType::kind_t kind;
382 if (!config.get("datatype", kind)) {
383 throw_Exception("datatype missing", Here());
384 }
385 return array::DataType(kind);
386 }
387
388 std::string PointCloud::config_name(const eckit::Configuration& config) const {
389 std::string name;
390 config.get("name", name);
391 return name;
392 }
393
394 const parallel::HaloExchange& PointCloud::halo_exchange() const {
395 if (halo_exchange_) {
396 return *halo_exchange_;
397 }
398 const_cast<PointCloud&>(*this).setupHaloExchange();
399 ATLAS_ASSERT(halo_exchange_);
400 return *halo_exchange_;
401 }
402
403 void PointCloud::gather(const FieldSet& local_fieldset, FieldSet& global_fieldset) const {
404 ATLAS_ASSERT(local_fieldset.size() == global_fieldset.size());
405
406 for (idx_t f = 0; f < local_fieldset.size(); ++f) {
407 const Field& loc = local_fieldset[f];
408 Field& glb = global_fieldset[f];
409 const idx_t nb_fields = 1;
410 idx_t root(0);
411 glb.metadata().get("owner", root);
412
413 if (loc.datatype() == array::DataType::kind<int>()) {
414 parallel::Field<int const> loc_field(make_leveled_view<const int>(loc));
415 parallel::Field<int> glb_field(make_leveled_view<int>(glb));
416 gather().gather(&loc_field, &glb_field, nb_fields, root);
417 }
418 else if (loc.datatype() == array::DataType::kind<long>()) {
419 parallel::Field<long const> loc_field(make_leveled_view<const long>(loc));
420 parallel::Field<long> glb_field(make_leveled_view<long>(glb));
421 gather().gather(&loc_field, &glb_field, nb_fields, root);
422 }
423 else if (loc.datatype() == array::DataType::kind<float>()) {
424 parallel::Field<float const> loc_field(make_leveled_view<const float>(loc));
425 parallel::Field<float> glb_field(make_leveled_view<float>(glb));
426 gather().gather(&loc_field, &glb_field, nb_fields, root);
427 }
428 else if (loc.datatype() == array::DataType::kind<double>()) {
429 parallel::Field<double const> loc_field(make_leveled_view<const double>(loc));
430 parallel::Field<double> glb_field(make_leveled_view<double>(glb));
431 gather().gather(&loc_field, &glb_field, nb_fields, root);
432 }
433 else {
434 throw_Exception("datatype not supported", Here());
435 }
436 }
437 }
438
439 void PointCloud::gather(const Field& local, Field& global) const {
440 FieldSet local_fields;
441 FieldSet global_fields;
442 local_fields.add(local);
443 global_fields.add(global);
444 gather(local_fields, global_fields);
445 }
446 const parallel::GatherScatter& PointCloud::gather() const {
447 if (gather_scatter_) {
448 return *gather_scatter_;
449 }
450 const_cast<PointCloud&>(*this).setupGatherScatter();
451 ATLAS_ASSERT(gather_scatter_);
452 return *gather_scatter_;
453 }
454 const parallel::GatherScatter& PointCloud::scatter() const {
455 if (gather_scatter_) {
456 return *gather_scatter_;
457 }
458 const_cast<PointCloud&>(*this).setupGatherScatter();
459 ATLAS_ASSERT(gather_scatter_);
460 return *gather_scatter_;
461 }
462
463 void PointCloud::scatter(const FieldSet& global_fieldset, FieldSet& local_fieldset) const {
464 ATLAS_ASSERT(local_fieldset.size() == global_fieldset.size());
465
466 for (idx_t f = 0; f < local_fieldset.size(); ++f) {
467 const Field& glb = global_fieldset[f];
468 Field& loc = local_fieldset[f];
469 const idx_t nb_fields = 1;
470 idx_t root(0);
471 glb.metadata().get("owner", root);
472
473 if (loc.datatype() == array::DataType::kind<int>()) {
474 parallel::Field<int const> glb_field(make_leveled_view<const int>(glb));
475 parallel::Field<int> loc_field(make_leveled_view<int>(loc));
476 scatter().scatter(&glb_field, &loc_field, nb_fields, root);
477 }
478 else if (loc.datatype() == array::DataType::kind<long>()) {
479 parallel::Field<long const> glb_field(make_leveled_view<const long>(glb));
480 parallel::Field<long> loc_field(make_leveled_view<long>(loc));
481 scatter().scatter(&glb_field, &loc_field, nb_fields, root);
482 }
483 else if (loc.datatype() == array::DataType::kind<float>()) {
484 parallel::Field<float const> glb_field(make_leveled_view<const float>(glb));
485 parallel::Field<float> loc_field(make_leveled_view<float>(loc));
486 scatter().scatter(&glb_field, &loc_field, nb_fields, root);
487 }
488 else if (loc.datatype() == array::DataType::kind<double>()) {
489 parallel::Field<double const> glb_field(make_leveled_view<const double>(glb));
490 parallel::Field<double> loc_field(make_leveled_view<double>(loc));
491 scatter().scatter(&glb_field, &loc_field, nb_fields, root);
492 }
493 else {
494 throw_Exception("datatype not supported", Here());
495 }
496
497 auto name = loc.name();
498 glb.metadata().broadcast(loc.metadata(), root);
499 loc.metadata().set("global", false);
500 if( !name.empty() ) {
501 loc.metadata().set("name", name);
502 }
503 loc.set_dirty(true);
504 }
505 }
506
507 void PointCloud::scatter(const Field& global, Field& local) const {
508 FieldSet global_fields;
509 FieldSet local_fields;
510 global_fields.add(global);
511 local_fields.add(local);
512 scatter(global_fields, local_fields);
513 }
514
515
516 void PointCloud::set_field_metadata(const eckit::Configuration& config, Field& field) const {
517 field.set_functionspace(this);
518
519 bool global(false);
520 if (config.get("global", global)) {
521 if (global) {
522 idx_t owner(0);
523 config.get("owner", owner);
524 field.metadata().set("owner", owner);
525 }
526 }
527 field.metadata().set("global", global);
528
529 idx_t levels(levels_);
530 config.get("levels", levels);
531 field.set_levels(levels);
532
533 idx_t variables(0);
534 config.get("variables", variables);
535 field.set_variables(variables);
536
537 if (config.has("type")) {
538 field.metadata().set("type", config.getString("type"));
539 }
540
541 if (config.has("vector_component")) {
542 field.metadata().set("vector_component", config.getSubConfiguration("vector_component"));
543 }
544 }
545
546 Field PointCloud::createField(const eckit::Configuration& options) const {
547 Field field(config_name(options), config_datatype(options), config_spec(options));
548 set_field_metadata(options, field);
549 return field;
550 }
551
552 Field PointCloud::createField(const Field& other, const eckit::Configuration& config) const {
553 return createField(option::datatype(other.datatype()) | option::levels(other.levels()) |
554 option::variables(other.variables()) |
555 option::type(other.metadata().getString("type", "scalar")) | config);
556 }
557
558 std::string PointCloud::distribution() const {
559 return (partition_ ? std::string("pointcloud") : std::string("serial"));
560 }
561
562 static const array::Array& get_dummy() {
563 static array::ArrayT<double> dummy_{1};
564 return dummy_;
565 }
566
567 template <typename Point>
568 PointCloud::IteratorT<Point>::IteratorT(const atlas::functionspace::detail::PointCloud& fs, bool begin):
569 fs_(fs),
570 xy_(array::make_view<const double, 2>(fs_.lonlat())),
571 z_(array::make_view<const double, 1>(bool(fs_.vertical()) ? fs_.vertical().array() : get_dummy())),
572 n_(begin ? 0 : fs_.size()),
573 size_(fs_.size()) {}
574
575 template <typename Point>
576 bool PointCloud::IteratorT<Point>::next(Point& p) {
577 if (n_ < size_) {
578 p[XX] = xy_(n_, XX);
579 p[YY] = xy_(n_, YY);
580 if (Point::DIMS == 3) {
581 p[ZZ] = z_(n_);
582 }
583 ++n_;
584 return true;
585 }
586 return false;
587 }
588
589 template <typename Point>
590 const Point PointCloud::IteratorT<Point>::operator*() const {
591 Point p;
592 p[XX] = xy_(n_, XX);
593 p[YY] = xy_(n_, YY);
594 if (Point::DIMS == 3) {
595 p[ZZ] = z_(n_);
596 }
597 return p;
598 }
599
600 template class PointCloud::IteratorT<PointXYZ>;
601 template class PointCloud::IteratorT<PointXY>;
602 template class PointCloud::IteratorT<PointLonLat>;
603
604
605 namespace {
606
607 template <int RANK>
608 void dispatch_haloExchange(Field& field, const parallel::HaloExchange& halo_exchange, bool on_device) {
609 if (field.datatype() == array::DataType::kind<int>()) {
610 halo_exchange.template execute<int, RANK>(field.array(), on_device);
611 }
612 else if (field.datatype() == array::DataType::kind<long>()) {
613 halo_exchange.template execute<long, RANK>(field.array(), on_device);
614 }
615 else if (field.datatype() == array::DataType::kind<float>()) {
616 halo_exchange.template execute<float, RANK>(field.array(), on_device);
617 }
618 else if (field.datatype() == array::DataType::kind<double>()) {
619 halo_exchange.template execute<double, RANK>(field.array(), on_device);
620 }
621 else {
622 throw_Exception("datatype not supported", Here());
623 }
624 field.set_dirty(false);
625 }
626
627 template <int RANK>
628 void dispatch_adjointHaloExchange(Field& field, const parallel::HaloExchange& halo_exchange, bool on_device) {
629 if (field.datatype() == array::DataType::kind<int>()) {
630 halo_exchange.template execute_adjoint<int, RANK>(field.array(), on_device);
631 }
632 else if (field.datatype() == array::DataType::kind<long>()) {
633 halo_exchange.template execute_adjoint<long, RANK>(field.array(), on_device);
634 }
635 else if (field.datatype() == array::DataType::kind<float>()) {
636 halo_exchange.template execute_adjoint<float, RANK>(field.array(), on_device);
637 }
638 else if (field.datatype() == array::DataType::kind<double>()) {
639 halo_exchange.template execute_adjoint<double, RANK>(field.array(), on_device);
640 }
641 else {
642 throw_Exception("datatype not supported", Here());
643 }
644 field.set_dirty(false);
645 }
646
647 } // namespace
648
649 void PointCloud::haloExchange(const FieldSet& fieldset, bool on_device) const {
650 if (parallel_) {
651 for (idx_t f = 0; f < fieldset.size(); ++f) {
652 Field& field = const_cast<FieldSet&>(fieldset)[f];
653 switch (field.rank()) {
654 case 1:
655 dispatch_haloExchange<1>(field, halo_exchange(), on_device);
656 break;
657 case 2:
658 dispatch_haloExchange<2>(field, halo_exchange(), on_device);
659 break;
660 case 3:
661 dispatch_haloExchange<3>(field, halo_exchange(), on_device);
662 break;
663 case 4:
664 dispatch_haloExchange<4>(field, halo_exchange(), on_device);
665 break;
666 default:
667 throw_Exception("Rank not supported", Here());
668 }
669 field.set_dirty(false);
670 }
671 }
672 }
673
674 void PointCloud::haloExchange(const Field& field, bool on_device) const {
675 FieldSet fieldset;
676 fieldset.add(field);
677 haloExchange(fieldset, on_device);
678 }
679
680 void PointCloud::create_remote_index() const {
681 ATLAS_TRACE();
682 const auto& comm = mpi::comm(mpi_comm_);
683 const int mpi_rank = comm.rank();
684 const int mpi_size = comm.size();
685 auto size_halo = lonlat_.shape(0);
686
687 remote_index_ = Field("remote_idx", array::make_datatype<idx_t>(), array::make_shape(size_halo));
688 auto remote_idx = array::make_indexview<idx_t, 1>(remote_index_);
689 auto ghost = array::make_view<int, 1>(ghost_);
690
691 atlas_omp_parallel_for(idx_t n = 0; n < size_halo; ++n) {
692 if(not ghost(n)) {
693 remote_idx(n) = n;
694 }
695 else {
696 remote_idx(n) = -1;
697 }
698 }
699
700 auto build_partition_graph = [this,mpi_size,mpi_rank,size_halo,&comm]() -> std::unique_ptr<Mesh::PartitionGraph> {
701
702 auto part = array::make_view<int, 1>(this->partition_);
703 auto ghost = array::make_view<int, 1>(this->ghost_);
704
705 std::vector<int> others_set(mpi_size, 0);
706 others_set[mpi_rank] = 1;
707 for( idx_t i = 0; i<size_halo; ++i) {
708 if (ghost(i)) {
709 others_set[part(i)] = 1; // present
710 }
711 }
712 std::vector<int> others;
713 others.reserve(mpi_size);
714 for (idx_t p = 0; p < mpi_size; ++p) {
715 if (others_set[p]) {
716 others.emplace_back(p);
717 }
718 }
719
720 eckit::mpi::Buffer<int> recv_others(mpi_size);
721
722 ATLAS_TRACE_MPI(ALLGATHER) { comm.allGatherv(others.begin(), others.end(), recv_others); }
723
724 std::vector<idx_t> counts(recv_others.counts.begin(), recv_others.counts.end());
725 std::vector<idx_t> displs(recv_others.displs.begin(), recv_others.displs.end());
726 std::vector<idx_t> values(recv_others.buffer.begin(), recv_others.buffer.end());
727 return std::unique_ptr<Mesh::PartitionGraph>(
728 new Mesh::PartitionGraph(values.data(), mpi_size, displs.data(), counts.data()));
729 };
730
731 std::unique_ptr<Mesh::PartitionGraph> graph_ptr;
732 ATLAS_TRACE_SCOPE("Building partition graph...") { graph_ptr = build_partition_graph(); }
733 const Mesh::PartitionGraph& graph = *graph_ptr;
734
735 ATLAS_TRACE_SCOPE("Setup remote_index fields...") {
736 auto p = array::make_view<int, 1>(partition_);
737 auto g = array::make_view<gidx_t, 1>(global_index_);
738
739 auto neighbours = graph.nearestNeighbours(mpi_rank);
740 const idx_t nb_neighbours = static_cast<idx_t>(neighbours.size());
741 std::unordered_map<int, idx_t> part_to_neighbour;
742 ATLAS_TRACE_SCOPE("part_to_neighbour") {
743 part_to_neighbour.reserve(nb_neighbours);
744 for (idx_t j = 0; j < nb_neighbours; ++j) {
745 part_to_neighbour[neighbours[j]] = j;
746 }
747 }
748 std::vector<idx_t> halo_per_neighbour(neighbours.size(), 0);
749 ATLAS_TRACE_SCOPE("set halo_per_neighbour")
750 for( idx_t i = 0; i<size_halo; ++i) {
751 if (ghost(i)) {
752 halo_per_neighbour[part_to_neighbour[p(i)]]++;
753 }
754 }
755
756
757 std::vector<std::vector<gidx_t>> g_per_neighbour(neighbours.size());
758 ATLAS_TRACE_SCOPE("assemble g_per_neighbour") {
759 for (idx_t j = 0; j < nb_neighbours; ++j) {
760 g_per_neighbour[j].reserve(halo_per_neighbour[j]);
761 }
762 for( idx_t j = 0; j<size_halo; ++j) {
763 if (ghost(j)) {
764 g_per_neighbour[part_to_neighbour[p(j)]].emplace_back(g(j));
765 }
766 }
767 }
768
769 std::vector<eckit::mpi::Request> send_requests(neighbours.size());
770 std::vector<eckit::mpi::Request> recv_requests(neighbours.size());
771
772 std::vector<idx_t> recv_size(neighbours.size());
773 std::vector<idx_t> send_size(neighbours.size());
774
775 int tag = 0;
776 ATLAS_TRACE_SCOPE("send-receive g_per_neighbour size") {
777 for (idx_t j = 0; j < nb_neighbours; ++j) {
778 send_size[j] = static_cast<idx_t>(g_per_neighbour[j].size());
779 send_requests[j] = comm.iSend(send_size[j], neighbours[j], tag);
780 recv_requests[j] = comm.iReceive(recv_size[j], neighbours[j], tag);
781 }
782
783 ATLAS_TRACE_MPI(WAIT) {
784 for (idx_t j = 0; j < nb_neighbours; ++j) {
785 comm.wait(send_requests[j]);
786 }
787
788 for (idx_t j = 0; j < nb_neighbours; ++j) {
789 comm.wait(recv_requests[j]);
790 }
791 }
792 }
793
794 std::vector<std::vector<gidx_t>> recv_g_per_neighbour(neighbours.size());
795 ATLAS_TRACE_SCOPE("send-receive g_per_neighbour")
796 for (idx_t j = 0; j < nb_neighbours; ++j) {
797 recv_g_per_neighbour[j].resize(recv_size[j]);
798
799 send_requests[j] = comm.iSend(g_per_neighbour[j].data(), g_per_neighbour[j].size(), neighbours[j], tag);
800 recv_requests[j] =
801 comm.iReceive(recv_g_per_neighbour[j].data(), recv_g_per_neighbour[j].size(), neighbours[j], tag);
802 }
803
804 std::vector<std::vector<idx_t>> send_r_per_neighbour(neighbours.size());
805
806 // Assemble "send_r_per_neighbour"
807 // Depending if we can afford memory for a globally sized array, we can have a
808 // much faster version of g_to_r map using std::vector.
809 // TODO: using c++14 we can create a polymorphic lambda to avoid duplicated
810 // code in the two branches of following if.
811
812 // size_t max_glb_idx = grid().size();
813 ATLAS_ASSERT(max_glb_idx_);
814
815 atlas::vector<idx_t> g_to_r_vector;
816 bool use_unordered_map_fallback = false;
817 try {
818 g_to_r_vector.resize(max_glb_idx_ + 1);
819 }
820 catch (std::bad_alloc& e) {
821 if (comm.size() > 1) {
822 Log::warning() << "Could not allocate " << eckit::Bytes((max_glb_idx_ + 1) * sizeof(idx_t)) << Here()
823 << "\n"
824 << "Using slower unordered_map fallback to map global to remote indices"
825 << std::endl;
826 use_unordered_map_fallback = true;
827 }
828 else {
829 throw_Exception(
830 "Could not allocate " + std::string(eckit::Bytes((max_glb_idx_ + 1) * sizeof(idx_t))), Here());
831 }
832 }
833 if (not use_unordered_map_fallback) {
834 auto& g_to_r = g_to_r_vector;
835 ATLAS_TRACE_SCOPE("g_to_r (using vector)") {
836 atlas_omp_parallel_for(idx_t j = 0; j < size_halo; ++j) {
837 if (not ghost(j)) {
838 // parallel omp possible for ``` g_to_r[g(j)] = j ``` as we only loop over size_owned,
839 // where global_index is such that race-conditions cannot occur
840 #if ATLAS_ARRAYVIEW_BOUNDS_CHECKING
841 if (g(j) >= max_glb_idx_ + 1) {
842 ATLAS_DEBUG_VAR(g(j));
843 throw_OutOfRange("g_to_r", g(j), max_glb_idx_ + 1, Here());
844 }
845 #endif
846 g_to_r[g(j)] = j;
847 }
848 }
849 }
850 for (idx_t j = 0; j < nb_neighbours; ++j) {
851 send_r_per_neighbour[j].reserve(recv_size[j]);
852
853 comm.wait(recv_requests[j]); // wait for recv_g_per_neighbour[j]
854 for (gidx_t gidx : recv_g_per_neighbour[j]) {
855 send_r_per_neighbour[j].emplace_back(g_to_r[gidx]);
856 }
857 }
858 }
859 else {
860 std::unordered_map<gidx_t, idx_t> g_to_r;
861 g_to_r.reserve(size_halo);
862
863 ATLAS_TRACE_SCOPE("g_to_r (using unordered set)") {
864 for (idx_t j = 0; j < size_halo; ++j) {
865 if (not ghost(j)) {
866 g_to_r[g(j)] = j;
867 }
868 }
869 }
870 ATLAS_TRACE_SCOPE("assemble r_per_neighbour")
871 for (idx_t j = 0; j < nb_neighbours; ++j) {
872 send_r_per_neighbour[j].reserve(recv_size[j]);
873
874 comm.wait(recv_requests[j]); // wait for recv_g_per_neighbour[j]
875 for (gidx_t gidx : recv_g_per_neighbour[j]) {
876 send_r_per_neighbour[j].emplace_back(g_to_r[gidx]);
877 }
878 }
879 }
880
881 std::vector<std::vector<idx_t>> r_per_neighbour(neighbours.size());
882
883 ATLAS_TRACE_SCOPE("send-receive r_per_neighbour") {
884 for (idx_t j = 0; j < nb_neighbours; ++j) {
885 r_per_neighbour[j].resize(halo_per_neighbour[j]);
886 }
887
888 ATLAS_TRACE_MPI(ALLTOALL) {
889 for (idx_t j = 0; j < nb_neighbours; ++j) {
890 comm.wait(send_requests[j]);
891 send_requests[j] = comm.iSend(send_r_per_neighbour[j].data(), send_r_per_neighbour[j].size(),
892 neighbours[j], tag);
893 recv_requests[j] =
894 comm.iReceive(r_per_neighbour[j].data(), r_per_neighbour[j].size(), neighbours[j], tag);
895 }
896 }
897 ATLAS_TRACE_MPI(WAIT) {
898 for (idx_t j = 0; j < nb_neighbours; ++j) {
899 comm.wait(recv_requests[j]);
900 }
901 }
902 }
903
904
905 std::vector<idx_t> counters(neighbours.size(), 0);
906 ATLAS_TRACE_SCOPE("set remote_idx")
907 for (idx_t j = 0; j < size_halo; ++j) {
908 if (ghost(j)) {
909 idx_t neighbour = part_to_neighbour[p(j)];
910 remote_idx(j) = r_per_neighbour[neighbour][counters[neighbour]++];
911 }
912 ATLAS_ASSERT(remote_idx(j) >= 0);
913 }
914
915 ATLAS_TRACE_MPI(WAIT) {
916 for (idx_t j = 0; j < nb_neighbours; ++j) {
917 comm.wait(send_requests[j]);
918 }
919 }
920 }
921 }
922
923 void PointCloud::setupParallel() {
924 ATLAS_TRACE();
925 if (ghost_ and partition_ and global_index_ and not remote_index_) {
926 create_remote_index();
927 }
928 else if (not partition_ or not remote_index_) {
929 ATLAS_TRACE("do setup");
930 const auto& comm = mpi::comm(mpi_comm_);
931 const int mpi_rank = comm.rank();
932 const int mpi_size = comm.size();
933
934 auto lonlat_v = array::make_view<double, 2>(lonlat_);
935 // data structure containing a flag to identify the 'ghost points';
936 // 0={is not a ghost point}, 1={is a ghost point}
937 auto is_ghost = array::make_view<int, 1>(ghost_);
938
939 std::vector<PointXY> opoints_local;
940 std::vector<PointXY> gpoints_local;
941 std::vector<uidx_t> lonlat_u;
942 std::vector<uidx_t> opoints_local_u;
943 lonlat_u.reserve(lonlat_v.shape(0));
944 opoints_local_u.reserve(is_ghost.shape(0));
945 gpoints_local.reserve(is_ghost.shape(0));
946 opoints_local.reserve(is_ghost.shape(0));
947
948 for (idx_t i = 0; i < lonlat_v.shape(0); ++i) {
949 lonlat_u.emplace_back(util::unique_lonlat(lonlat_v(i, XX), lonlat_v(i, YY)));
950 }
951
952 idx_t j{0};
953 for (idx_t i = 0; i < is_ghost.shape(0); ++i) {
954 PointXY loc(lonlat_v(j, XX), lonlat_v(j, YY));
955 if (is_ghost(i)) {
956 gpoints_local.emplace_back(loc);
957 }
958 else {
959 opoints_local.emplace_back(loc);
960 opoints_local_u.emplace_back(util::unique_lonlat(loc.x(), loc.y()));
961 }
962 ++j;
963 }
964
965 std::vector<double> coords_gp_local;
966 coords_gp_local.reserve(gpoints_local.size() * 2);
967
968 for (auto& gp : gpoints_local) {
969 coords_gp_local.push_back(gp[XX]);
970 coords_gp_local.push_back(gp[YY]);
971 }
972
973 eckit::mpi::Buffer<double> buffers_rec(mpi_size);
974
975 ATLAS_TRACE_SCOPE("mpi all gather") {
976 comm.allGatherv(coords_gp_local.begin(), coords_gp_local.end(), buffers_rec);
977 }
978
979 std::vector<PointXY> gpoints_global;
980
981 for (std::size_t pe = 0; pe < mpi_size; ++pe) {
982 for (std::size_t j = 0; j < buffers_rec.counts[pe] / 2; ++j) {
983 PointXY loc_gp(*(buffers_rec.begin() + buffers_rec.displs[pe] + 2 * j + XX),
984 *(buffers_rec.begin() + buffers_rec.displs[pe] + 2 * j + YY));
985 gpoints_global.emplace_back(loc_gp);
986 }
987 }
988
989 std::vector<uidx_t> gpoints_global_u;
990 gpoints_global_u.reserve(gpoints_global.size());
991 for (atlas::PointXY& loc : gpoints_global) {
992 gpoints_global_u.emplace_back(util::unique_lonlat(loc.x(), loc.y()));
993 }
994
995 std::vector<int> partition_ids_gp_global(gpoints_global.size(), -1);
996 std::vector<int> remote_index_gp_global(gpoints_global.size(), -1);
997
998 std::vector<uidx_t>::iterator iter_xy_gp_01;
999
1000 ATLAS_TRACE_SCOPE("find global")
1001 for (std::size_t idx = 0; idx < gpoints_global_u.size(); ++idx) {
1002 iter_xy_gp_01 = std::find(opoints_local_u.begin(), opoints_local_u.end(), gpoints_global_u.at(idx));
1003 if (iter_xy_gp_01 != opoints_local_u.end()) {
1004 std::size_t ridx = std::distance(opoints_local_u.begin(), iter_xy_gp_01);
1005 partition_ids_gp_global.at(idx) = mpi_rank;
1006 remote_index_gp_global.at(idx) = ridx;
1007 }
1008 }
1009
1010 ATLAS_TRACE_SCOPE("mpi all reduce") {
1011 comm.allReduceInPlace(partition_ids_gp_global.begin(), partition_ids_gp_global.end(), eckit::mpi::max());
1012 comm.allReduceInPlace(remote_index_gp_global.begin(), remote_index_gp_global.end(), eckit::mpi::max());
1013 }
1014
1015 std::vector<int> partition_ids_local(lonlat_v.shape(0), -1);
1016 std::vector<idx_t> remote_index_local(lonlat_v.shape(0), -1);
1017
1018 idx_t idx_loc{0};
1019 std::vector<uidx_t>::iterator iter_xy_gp_02;
1020
1021 ATLAS_TRACE_SCOPE("find local")
1022 for (idx_t i = 0; i < lonlat_v.shape(0); ++i) {
1023 iter_xy_gp_02 = std::find(gpoints_global_u.begin(), gpoints_global_u.end(), lonlat_u.at(i));
1024 if (iter_xy_gp_02 != gpoints_global_u.end()) {
1025 std::size_t idx_gp = std::distance(gpoints_global_u.begin(), iter_xy_gp_02);
1026 partition_ids_local[idx_loc] = partition_ids_gp_global[idx_gp];
1027 remote_index_local[idx_loc] = remote_index_gp_global[idx_gp];
1028 }
1029 else {
1030 partition_ids_local[idx_loc] = mpi_rank;
1031 remote_index_local[idx_loc] = idx_loc;
1032 }
1033 ++idx_loc;
1034 }
1035
1036 partition_ = Field("partition", array::make_datatype<int>(), array::make_shape(partition_ids_local.size()));
1037
1038 auto partitionv = array::make_view<int, 1>(partition_);
1039 for (idx_t i = 0; i < partitionv.shape(0); ++i) {
1040 partitionv(i) = partition_ids_local.at(i);
1041 }
1042
1043 remote_index_ =
1044 Field("remote_index", array::make_datatype<idx_t>(), array::make_shape(remote_index_local.size()));
1045
1046 auto remote_indexv = array::make_indexview<idx_t, 1>(remote_index_);
1047 for (idx_t i = 0; i < remote_indexv.shape(0); ++i) {
1048 remote_indexv(i) = remote_index_local.at(i);
1049 }
1050 }
1051
1052 ATLAS_ASSERT(partition_);
1053 ATLAS_ASSERT(ghost_);
1054 ATLAS_ASSERT(remote_index_);
1055 ATLAS_ASSERT(ghost_.size() == remote_index_.size());
1056 ATLAS_ASSERT(ghost_.size() == partition_.size());
1057 parallel_ = true;
1058 }
1059
1060 void PointCloud::setupHaloExchange() {
1061 ATLAS_TRACE();
1062 if (ghost_ and partition_ and remote_index_) {
1063 halo_exchange_.reset(new parallel::HaloExchange());
1064 halo_exchange_->setup(mpi_comm_,
1065 array::make_view<int, 1>(partition_).data(),
1066 array::make_view<idx_t, 1>(remote_index_).data(),
1067 REMOTE_IDX_BASE,
1068 ghost_.size());
1069 }
1070 }
1071
1072 void PointCloud::setupGatherScatter() {
1073 ATLAS_TRACE();
1074 if (ghost_ and partition_ and remote_index_ and global_index_) {
1075 gather_scatter_.reset(new parallel::GatherScatter());
1076 gather_scatter_->setup(mpi_comm_,
1077 array::make_view<int, 1>(partition_).data(),
1078 array::make_view<idx_t, 1>(remote_index_).data(),
1079 REMOTE_IDX_BASE,
1080 array::make_view<gidx_t, 1>(global_index_).data(),
1081 array::make_view<int, 1>(ghost_).data(),
1082 ghost_.size());
1083 }
1084 }
1085
1086 idx_t PointCloud::size_global() const {
1087 if (size_global_ == -1) {
1088 if (not parallel_) {
1089 size_global_ = lonlat().size();
1090 }
1091 else {
1092 if( !gather_scatter_ ) {
1093 const_cast<PointCloud&>(*this).setupGatherScatter();
1094 }
1095 if (gather_scatter_) {
1096 size_global_ = gather_scatter_->glb_dof();
1097 }
1098 }
1099 }
1100 return size_global_;
1101 }
1102
1103
1104 void PointCloud::adjointHaloExchange(const FieldSet& fieldset, bool on_device) const {
1105 if (parallel_) {
1106 for (idx_t f = 0; f < fieldset.size(); ++f) {
1107 Field& field = const_cast<FieldSet&>(fieldset)[f];
1108 switch (field.rank()) {
1109 case 1:
1110 dispatch_adjointHaloExchange<1>(field, halo_exchange(), on_device);
1111 break;
1112 case 2:
1113 dispatch_adjointHaloExchange<2>(field, halo_exchange(), on_device);
1114 break;
1115 case 3:
1116 dispatch_adjointHaloExchange<3>(field, halo_exchange(), on_device);
1117 break;
1118 case 4:
1119 dispatch_adjointHaloExchange<4>(field, halo_exchange(), on_device);
1120 break;
1121 default:
1122 throw_Exception("Rank not supported", Here());
1123 }
1124 }
1125 }
1126 }
1127
1128 void PointCloud::adjointHaloExchange(const Field& field, bool) const {
1129 FieldSet fieldset;
1130 fieldset.add(field);
1131 adjointHaloExchange(fieldset);
1132 }
1133
1134
1135 } // namespace detail
1136
1137 PointCloud::PointCloud(const FunctionSpace& functionspace):
1138 FunctionSpace(functionspace), functionspace_(dynamic_cast<const detail::PointCloud*>(get())) {}
1139
1140 PointCloud::PointCloud(const Field& field, const eckit::Configuration& config):
1141 FunctionSpace(new detail::PointCloud(field,config)), functionspace_(dynamic_cast<const detail::PointCloud*>(get())) {}
1142
1143 PointCloud::PointCloud(const Field& field1, const Field& field2, const eckit::Configuration& config):
1144 FunctionSpace(new detail::PointCloud(field1, field2, config)), functionspace_(dynamic_cast<const detail::PointCloud*>(get())) {}
1145
1146 PointCloud::PointCloud(const FieldSet& fset, const eckit::Configuration& config):
1147 FunctionSpace(new detail::PointCloud(fset, config)), functionspace_(dynamic_cast<const detail::PointCloud*>(get())) {}
1148
1149 PointCloud::PointCloud(const std::vector<PointXY>& points, const eckit::Configuration& config):
1150 FunctionSpace(new detail::PointCloud(points, config)), functionspace_(dynamic_cast<const detail::PointCloud*>(get())) {}
1151
1152 PointCloud::PointCloud(const std::vector<PointXYZ>& points, const eckit::Configuration& config):
1153 FunctionSpace(new detail::PointCloud(points, config)), functionspace_(dynamic_cast<const detail::PointCloud*>(get())) {}
1154
1155 PointCloud::PointCloud(const std::initializer_list<std::initializer_list<double>>& points, const eckit::Configuration& config):
1156 FunctionSpace((points.begin()->size() == 2
1157 ? new detail::PointCloud{std::vector<PointXY>(points.begin(), points.end()), config}
1158 : new detail::PointCloud{std::vector<PointXYZ>(points.begin(), points.end()), config})),
1159 functionspace_(dynamic_cast<const detail::PointCloud*>(get())) {}
1160
1161 PointCloud::PointCloud(const Grid& grid, const eckit::Configuration& config):
1162 FunctionSpace(new detail::PointCloud(grid, config)), functionspace_(dynamic_cast<const detail::PointCloud*>(get())) {}
1163
1164 PointCloud::PointCloud(const Grid& grid, const grid::Distribution& distribution, const eckit::Configuration& config):
1165 FunctionSpace(new detail::PointCloud(grid, distribution, config)), functionspace_(dynamic_cast<const detail::PointCloud*>(get())) {}
1166
1167 PointCloud::PointCloud(const Grid& grid, const grid::Partitioner& partitioner, const eckit::Configuration& config):
1168 FunctionSpace(new detail::PointCloud(grid, partitioner,config)), functionspace_(dynamic_cast<const detail::PointCloud*>(get())) {}
1169
1170
1171 } // namespace functionspace
1172 } // namespace atlas