Debian Codegraph @

src/atlas/functionspace/detail/StructuredColumns.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10 #pragma once
11
12 #include <array>
13 #include <functional>
14 #include <type_traits>
15
16 #include "atlas/array/DataType.h"
17 #include "atlas/field/Field.h"
18 #include "atlas/functionspace/detail/FunctionSpaceImpl.h"
19 #include "atlas/grid/StructuredGrid.h"
20 #include "atlas/grid/Vertical.h"
21 #include "atlas/library/config.h"
22 #include "atlas/option.h"
23 #include "atlas/parallel/mpi/mpi.h"
24 #include "atlas/parallel/omp/omp.h"
25 #include "atlas/runtime/Exception.h"
26 #include "atlas/util/Config.h"
27 #include "atlas/util/ObjectHandle.h"
28 #include "atlas/util/Point.h"
29 #include "atlas/util/Polygon.h"
30 #include "atlas/util/vector.h"
31
32 namespace atlas {
33 namespace parallel {
34 class GatherScatter;
35 class HaloExchange;
36 class Checksum;
37 } // namespace parallel
38 } // namespace atlas
39
40 namespace atlas {
41 class Field;
42 class FieldSet;
43 class Grid;
44 class StructuredGrid;
45 } // namespace atlas
46
47 namespace atlas {
48 namespace grid {
49 class Distribution;
50 class Partitioner;
51 } // namespace grid
52 } // namespace atlas
53
54 namespace atlas {
55 namespace functionspace {
56 namespace detail {
57
58 class StructuredColumnsHaloExchangeCache;
59 class StructuredColumnsGatherScatterCache;
60 class StructuredColumnsChecksumCache;
61
62
63 // -------------------------------------------------------------------
64
65 class StructuredColumns : public FunctionSpaceImpl {
66 public:
67 StructuredColumns(const Grid&, const eckit::Configuration& = util::NoConfig());
68
69 StructuredColumns(const Grid&, const grid::Partitioner&, const eckit::Configuration& = util::NoConfig());
70
71 StructuredColumns(const Grid&, const grid::Distribution&, const eckit::Configuration& = util::NoConfig());
72
73 StructuredColumns(const Grid&, const grid::Distribution&, const Vertical&,
74 const eckit::Configuration& = util::NoConfig());
75
76 StructuredColumns(const Grid&, const Vertical&, const eckit::Configuration& = util::NoConfig());
77
78 StructuredColumns(const Grid&, const Vertical&, const grid::Partitioner&,
79 const eckit::Configuration& = util::NoConfig());
80
81 ~StructuredColumns() override;
82
83 static std::string static_type() { return "StructuredColumns"; }
84 std::string type() const override { return static_type(); }
85 std::string distribution() const override;
86
87 /// @brief Create a Structured field
88 Field createField(const eckit::Configuration&) const override;
89
90 Field createField(const Field&, const eckit::Configuration&) const override;
91
92 void gather(const FieldSet&, FieldSet&) const override;
93 void gather(const Field&, Field&) const override;
94
95 void scatter(const FieldSet&, FieldSet&) const override;
96 void scatter(const Field&, Field&) const override;
97
98 void haloExchange(const FieldSet&, bool on_device = false) const override;
99 void haloExchange(const Field&, bool on_device = false) const override;
100
101 void adjointHaloExchange(const FieldSet&, bool on_device = false) const override;
102 void adjointHaloExchange(const Field&, bool on_device = false) const override;
103
104 idx_t sizeOwned() const { return size_owned_; }
105 idx_t sizeHalo() const { return size_halo_; }
106 idx_t size() const override { return size_halo_; }
107
108 idx_t levels() const { return nb_levels_; }
109
110 idx_t halo() const { return halo_; }
111
112 std::string checksum(const FieldSet&) const;
113 std::string checksum(const Field&) const;
114
115
116 const Vertical& vertical() const { return vertical_; }
117
118 const StructuredGrid& grid() const override;
119
120 const Projection& projection() const override { return grid().projection(); }
121
122 idx_t i_begin(idx_t j) const { return i_begin_[j]; }
123 idx_t i_end(idx_t j) const { return i_end_[j]; }
124
125 idx_t i_begin_halo(idx_t j) const { return i_begin_halo_[j]; }
126 idx_t i_end_halo(idx_t j) const { return i_end_halo_[j]; }
127
128 idx_t j_begin() const { return j_begin_; }
129 idx_t j_end() const { return j_end_; }
130
131 idx_t j_begin_halo() const { return j_begin_halo_; }
132 idx_t j_end_halo() const { return j_end_halo_; }
133
134 idx_t k_begin() const { return vertical_.k_begin(); }
135 idx_t k_end() const { return vertical_.k_end(); }
136
137 idx_t index(idx_t i, idx_t j) const {
138 check_bounds(i, j);
139 return ij2gp_(i, j);
140 }
141
142 Field lonlat() const override { return field_xy_; }
143 Field xy() const { return field_xy_; }
144 Field z() const { return field_z_; }
145 Field partition() const override { return field_partition_; }
146 Field global_index() const override { return field_global_index_; }
147 Field remote_index() const override {
148 if (not field_remote_index_) {
149 create_remote_index();
150 }
151 return field_remote_index_;
152 }
153 Field index_i() const { return field_index_i_; }
154 Field index_j() const { return field_index_j_; }
155 Field ghost() const override { return field_ghost_; }
156
157 void compute_xy(idx_t i, idx_t j, PointXY& xy) const;
158 PointXY compute_xy(idx_t i, idx_t j) const {
159 PointXY xy;
160 compute_xy(i, j, xy);
161 return xy;
162 }
163
164 virtual size_t footprint() const override;
165
166 const util::PartitionPolygon& polygon(idx_t halo = 0) const override;
167
168 const util::PartitionPolygons& polygons() const override;
169
170 idx_t part() const override { return part_; }
171 idx_t nb_parts() const override { return nb_partitions_; }
172 std::string mpi_comm() const override { return mpi_comm_; }
173
174 private: // methods
175 idx_t config_size(const eckit::Configuration& config) const;
176 array::DataType config_datatype(const eckit::Configuration&) const;
177 std::string config_name(const eckit::Configuration&) const;
178 idx_t config_levels(const eckit::Configuration&) const;
179 array::ArrayShape config_shape(const eckit::Configuration&) const;
180 array::ArrayAlignment config_alignment(const eckit::Configuration&) const;
181 array::ArraySpec config_spec(const eckit::Configuration&) const;
182 void set_field_metadata(const eckit::Configuration&, Field&) const;
183
184 void check_bounds(idx_t i, idx_t j) const {
185 #if ATLAS_ARRAYVIEW_BOUNDS_CHECKING
186 if (j < j_begin_halo() || j >= j_end_halo()) {
187 throw_outofbounds(i, j);
188 }
189 if (i < i_begin_halo(j) || i >= i_end_halo(j)) {
190 throw_outofbounds(i, j);
191 }
192 #endif
193 }
194 [[noreturn]] void throw_outofbounds(idx_t i, idx_t j) const;
195
196 const parallel::GatherScatter& gather() const override;
197 const parallel::GatherScatter& scatter() const override;
198 const parallel::Checksum& checksum() const;
199 const parallel::HaloExchange& halo_exchange() const;
200
201 void create_remote_index() const;
202
203 private: // data
204 std::string distribution_;
205
206 const Vertical vertical_;
207 idx_t nb_levels_;
208
209 idx_t size_owned_;
210 idx_t size_halo_;
211 idx_t halo_;
212
213 friend class StructuredColumnsHaloExchangeCache;
214 friend class StructuredColumnsGatherScatterCache;
215 friend class StructuredColumnsChecksumCache;
216 bool periodic_points_{false};
217
218 const StructuredGrid* grid_;
219 mutable util::ObjectHandle<parallel::GatherScatter> gather_scatter_;
220 mutable util::ObjectHandle<parallel::Checksum> checksum_;
221 mutable util::ObjectHandle<parallel::HaloExchange> halo_exchange_;
222 mutable std::vector<util::ObjectHandle<util::PartitionPolygon>> polygons_;
223 mutable util::PartitionPolygons all_polygons_;
224
225 Field field_xy_;
226 Field field_z_;
227 Field field_partition_;
228 Field field_global_index_;
229 mutable Field field_remote_index_;
230 Field field_index_i_;
231 Field field_index_j_;
232 Field field_ghost_;
233
234 class Map2to1 {
235 public:
236 Map2to1() { resize({1, 0}, {1, 0}); }
237
238 Map2to1(std::array<idx_t, 2> i_range, std::array<idx_t, 2> j_range) { resize(i_range, j_range); }
239
240 void resize(std::array<idx_t, 2> i_range, std::array<idx_t, 2> j_range);
241
242 atlas::vector<idx_t> data_;
243 idx_t i_min_;
244 idx_t i_max_;
245 idx_t j_min_;
246 idx_t j_max_;
247 idx_t j_stride_;
248
249 idx_t operator()(idx_t i, idx_t j) const { return data_[(i - i_min_) + (j - j_min_) * j_stride_] - 1; }
250
251 void set(idx_t i, idx_t j, idx_t n) { data_[(i - i_min_) + (j - j_min_) * j_stride_] = n + 1; }
252
253 static idx_t missing() { return std::numeric_limits<idx_t>::max() - 1; }
254
255 size_t footprint() const;
256
257 private:
258 void print(std::ostream&) const;
259
260 friend std::ostream& operator<<(std::ostream& s, const Map2to1& p) {
261 p.print(s);
262 return s;
263 }
264 };
265
266 class IndexRange {
267 public:
268 IndexRange() { resize(1, 0); }
269
270 IndexRange(idx_t min, idx_t max) { resize(min, max); }
271
272 std::vector<idx_t> data_;
273 idx_t min_;
274 idx_t max_;
275
276 idx_t operator()(idx_t i) const { return data_[i - min_]; }
277
278 idx_t& operator()(idx_t i) { return data_[i - min_]; }
279
280 idx_t operator[](idx_t i) const { return data_[i - min_]; }
281
282 idx_t& operator[](idx_t i) { return data_[i - min_]; }
283
284 idx_t missing() const { return std::numeric_limits<idx_t>::max() - 1; }
285
286 idx_t size() const { return idx_t(data_.size()); }
287
288 void resize(idx_t min, idx_t max) {
289 min_ = min;
290 max_ = max;
291 data_.resize(max_ - min_ + 1, missing() + 1);
292 }
293
294 private:
295 void print(std::ostream&) const;
296
297 friend std::ostream& operator<<(std::ostream& s, const IndexRange& p) {
298 p.print(s);
299 return s;
300 }
301 };
302
303 idx_t j_begin_;
304 idx_t j_end_;
305 std::vector<idx_t> i_begin_;
306 std::vector<idx_t> i_end_;
307 idx_t j_begin_halo_;
308 idx_t j_end_halo_;
309 IndexRange i_begin_halo_;
310 IndexRange i_end_halo_;
311
312 idx_t north_pole_included_;
313 idx_t south_pole_included_;
314 idx_t ny_;
315
316 idx_t part_;
317 idx_t nb_partitions_;
318 std::string mpi_comm_;
319
320 friend struct StructuredColumnsFortranAccess;
321 friend struct BlockStructuredColumnsFortranAccess;
322 Map2to1 ij2gp_;
323
324 friend class BlockStructuredColumns;
325 void setup(const grid::Distribution& distribution, const eckit::Configuration& config);
326
327 friend class BlockStructuredColumns;
328 };
329
330 // -------------------------------------------------------------------
331
332 } // namespace detail
333 } // namespace functionspace
334 } // namespace atlas