Debian Codegraph @

src/atlas/functionspace/detail/StructuredColumns_setup.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 <functional>
14 #include <iomanip>
15 #include <numeric>
16 #include <sstream>
17 #include <string>
18
19 #include "atlas/array/MakeView.h"
20 #include "atlas/field/FieldSet.h"
21 #include "atlas/grid/Distribution.h"
22 #include "atlas/grid/Partitioner.h"
23 #include "atlas/grid/StructuredGrid.h"
24 #include "atlas/library/Library.h"
25 #include "atlas/parallel/mpi/mpi.h"
26 #include "atlas/parallel/omp/omp.h"
27 #include "atlas/runtime/Exception.h"
28 #include "atlas/runtime/Trace.h"
29 #include "atlas/util/CoordinateEnums.h"
30
31 namespace atlas {
32 namespace functionspace {
33 namespace detail {
34
35 namespace {
36
37 struct GridPoint {
38 public:
39 idx_t i, j;
40 idx_t r;
41
42 GridPoint() = default;
43 GridPoint(idx_t _i, idx_t _j): i(_i), j(_j) {}
44 GridPoint(idx_t _i, idx_t _j, idx_t _r): i(_i), j(_j), r(_r) {}
45
46 /* No longer used:
47
48 bool operator<( const GridPoint& other ) const {
49 if ( j < other.j ) return true;
50 if ( j == other.j ) return i < other.i;
51 return false;
52 }
53
54 bool operator==( const GridPoint& other ) const { return ( j == other.j && i == other.i ); }
55
56 */
57 };
58
59
60 struct GridPointSet {
61 public:
62 GridPointSet() = default;
63
64 atlas::vector<GridPoint> gp_;
65
66 void reserve(size_t size) { gp_.reserve(size); }
67 void resize(size_t size) { gp_.resize(size); }
68 void set(idx_t i, idx_t j, idx_t r) {
69 gp_[r].i = i;
70 gp_[r].j = j;
71 gp_[r].r = r;
72 }
73
74 idx_t size() const { return static_cast<idx_t>(gp_.size()); }
75 // idx_t capacity() const { return static_cast<idx_t>(gp_.capacity()); }
76
77 const GridPoint& operator[](idx_t i) const { return gp_[i]; }
78
79 // unused:
80 //using const_iterator = decltype( gp_ )::const_iterator;
81 //const_iterator begin() const { return gp_.begin(); }
82 //const_iterator end() const { return gp_.end(); }
83 };
84
85 } // namespace
86
87
88 void StructuredColumns::setup(const grid::Distribution& distribution, const eckit::Configuration& config) {
89 config.get("periodic_points", periodic_points_);
90 if (not(*grid_)) {
91 throw_Exception("Grid is not a grid::Structured type", Here());
92 }
93
94 bool periodic_x = false, periodic_y = false;
95 config.get("periodic_x", periodic_x);
96 config.get("periodic_y", periodic_y);
97
98 bool regional = (!periodic_x && !periodic_y && !grid_->domain().global());
99
100 const double eps = 1.e-12;
101
102 ny_ = grid_->ny();
103 north_pole_included_ = 90. - grid_->y(0) == 0.;
104 south_pole_included_ = 90. + grid_->y(ny_ - 1) == 0;
105
106 distribution_ = distribution.type();
107
108 part_ = mpi::comm(mpi_comm()).rank();
109 nb_partitions_ = distribution.nb_partitions();
110
111 // We initialize j_begin_ and j_end_ to large integers, but we avoid using
112 // the max/min (as in numeric_limits<idx_t>::max()/min()) values because we
113 // need the difference `j_end_ - j_begin_` to be representable as an idx_t.
114 // This is to avoid miscompilation of OpenMP for loops below when using the
115 // gcc compiler, as described in https://github.com/ecmwf/atlas/issues/323
116 const idx_t max_over_two = std::numeric_limits<idx_t>::max() / 2;
117 j_begin_ = max_over_two;
118 j_end_ = -max_over_two;
119 // The logic around i_begin_, i_end_ doesn't miscompile in the same way so
120 // we initialize to the max/min values.
121 i_begin_.resize(grid_->ny(), std::numeric_limits<idx_t>::max());
122 i_end_.resize(grid_->ny(), std::numeric_limits<idx_t>::min());
123 idx_t owned(0);
124
125 ATLAS_TRACE_SCOPE("Compute bounds owned") {
126 if (nb_partitions_ == 1) {
127 j_begin_ = 0;
128 j_end_ = grid_->ny();
129 for (idx_t j = j_begin_; j < j_end_; ++j) {
130 i_begin_[j] = 0;
131 i_end_[j] = grid_->nx(j);
132 }
133 owned = grid_->size();
134 }
135 else {
136 size_t num_threads = atlas_omp_get_max_threads();
137 if (num_threads == 1) {
138 idx_t c(0);
139 for (idx_t j = 0; j < grid_->ny(); ++j) {
140 for (idx_t i = 0; i < grid_->nx(j); ++i, ++c) {
141 if (distribution.partition(c) == part_) {
142 j_begin_ = std::min<idx_t>(j_begin_, j);
143 j_end_ = std::max<idx_t>(j_end_, j + 1);
144 i_begin_[j] = std::min<idx_t>(i_begin_[j], i);
145 i_end_[j] = std::max<idx_t>(i_end_[j], i + 1);
146 ++owned;
147 }
148 }
149 }
150 }
151 else {
152 std::vector<idx_t> thread_reduce_j_begin(num_threads, std::numeric_limits<idx_t>::max());
153 std::vector<idx_t> thread_reduce_j_end(num_threads, std::numeric_limits<idx_t>::min());
154 std::vector<std::vector<idx_t>> thread_reduce_i_begin(
155 grid_->ny(), std::vector<idx_t>(num_threads, std::numeric_limits<idx_t>::max()));
156 std::vector<std::vector<idx_t>> thread_reduce_i_end(
157 grid_->ny(), std::vector<idx_t>(num_threads, std::numeric_limits<idx_t>::min()));
158 std::vector<idx_t> thread_reduce_owned(num_threads, 0);
159 atlas_omp_parallel {
160 const idx_t thread_num = atlas_omp_get_thread_num();
161 const idx_t begin =
162 static_cast<int>(size_t(thread_num) * size_t(grid_->size()) / size_t(num_threads));
163 const idx_t end =
164 static_cast<int>(size_t(thread_num + 1) * size_t(grid_->size()) / size_t(num_threads));
165 idx_t thread_j_begin = 0;
166 std::vector<idx_t> thread_i_begin(grid_->ny());
167 std::vector<idx_t> thread_i_end(grid_->ny());
168 idx_t n = 0;
169 for (idx_t j = 0; j < grid_->ny(); ++j) {
170 if (n + grid_->nx(j) > begin) {
171 thread_j_begin = j;
172 thread_i_begin[j] = begin - n;
173 break;
174 }
175 n += grid_->nx(j);
176 }
177 idx_t thread_j_end{thread_j_begin};
178 for (idx_t j = thread_j_begin; j < grid_->ny(); ++j) {
179 idx_t i_end = end - n;
180 if (j > thread_j_begin) {
181 thread_i_begin[j] = 0;
182 }
183 if (i_end > grid_->nx(j)) {
184 thread_i_end[j] = grid_->nx(j);
185 n += grid_->nx(j);
186 }
187 else {
188 thread_i_end[j] = i_end;
189 thread_j_end = j + 1;
190 break;
191 }
192 }
193 auto& __j_begin = thread_reduce_j_begin[thread_num];
194 auto& __j_end = thread_reduce_j_end[thread_num];
195 auto& __owned = thread_reduce_owned[thread_num];
196 idx_t c = begin;
197
198 for (idx_t j = thread_j_begin; j < thread_j_end; ++j) {
199 auto& __i_begin = thread_reduce_i_begin[j][thread_num];
200 auto& __i_end = thread_reduce_i_end[j][thread_num];
201 bool j_in_partition{false};
202 for (idx_t i = thread_i_begin[j]; i < thread_i_end[j]; ++i, ++c) {
203 if (distribution.partition(c) == part_) {
204 j_in_partition = true;
205 __i_begin = std::min<idx_t>(__i_begin, i);
206 __i_end = std::max<idx_t>(__i_end, i + 1);
207 ++__owned;
208 }
209 }
210 if (j_in_partition) {
211 __j_begin = std::min<idx_t>(__j_begin, j);
212 __j_end = std::max<idx_t>(__j_end, j + 1);
213 }
214 }
215 ATLAS_ASSERT(c == end);
216 }
217 owned = std::accumulate(thread_reduce_owned.begin(), thread_reduce_owned.end(), 0);
218 j_begin_ = *std::min_element(thread_reduce_j_begin.begin(), thread_reduce_j_begin.end());
219 j_end_ = *std::max_element(thread_reduce_j_end.begin(), thread_reduce_j_end.end());
220 atlas_omp_parallel_for(idx_t j = j_begin_; j < j_end_; ++j) {
221 i_begin_[j] = *std::min_element(thread_reduce_i_begin[j].begin(), thread_reduce_i_begin[j].end());
222 i_end_[j] = *std::max_element(thread_reduce_i_end[j].begin(), thread_reduce_i_end[j].end());
223 }
224 }
225 }
226 }
227
228 size_owned_ = owned;
229
230 int halo = config.getInt("halo", 0);
231 halo_ = halo;
232
233 j_begin_halo_ = j_begin_ - halo;
234 j_end_halo_ = j_end_ + halo;
235 i_begin_halo_.resize(-halo, grid_->ny() - 1 + halo);
236 i_end_halo_.resize(-halo, grid_->ny() - 1 + halo);
237 if (regional) {
238 j_begin_halo_ = std::max(j_begin_halo_, idx_t{0});
239 j_end_halo_ = std::min(j_end_halo_, grid_->ny());
240 }
241
242 auto compute_i = [this](idx_t i, idx_t j) -> idx_t {
243 const idx_t nx = grid_->nx(j);
244 while (i >= nx) {
245 i -= nx;
246 }
247 while (i < 0) {
248 i += nx;
249 }
250 return i;
251 };
252
253 auto compute_i_fast = [](idx_t i, const idx_t nx) -> idx_t {
254 while (i >= nx) {
255 i -= nx;
256 }
257 while (i < 0) {
258 i += nx;
259 }
260 return i;
261 };
262
263 std::function<idx_t(idx_t)> compute_j;
264 compute_j = [this, &compute_j, &periodic_y](idx_t j) -> idx_t {
265 if (periodic_y) {
266 const idx_t ny = grid_->ny();
267 while (j < 0) {
268 j += ny;
269 }
270 while (j >= ny) {
271 j -= ny;
272 }
273 }
274 else {
275 if (j < 0) {
276 j = (grid_->y(0) == 90.) ? -j : -j - 1;
277 }
278 else if (j >= grid_->ny()) {
279 idx_t jlast = grid_->ny() - 1;
280 j = (grid_->y(jlast) == -90.) ? jlast - 1 - (j - grid_->ny()) : jlast - (j - grid_->ny());
281 }
282 if (j < 0 or j >= grid_->ny()) {
283 j = compute_j(j);
284 }
285 }
286 return j;
287 };
288
289 auto compute_x = [this, &compute_i, &compute_j](idx_t i, idx_t j) -> double {
290 const idx_t jj = compute_j(j);
291 const idx_t ii = compute_i(i, jj); // guaranteed between 0 and nx(jj)
292 const idx_t nx = grid_->nx(jj);
293 const double a = (ii - i) / nx;
294 const double x = grid_->x(ii, jj) - a * grid_->x(nx, jj);
295 return x;
296 };
297
298 auto compute_x_fast = [this, &compute_i_fast](idx_t i, idx_t jj, idx_t nx) -> double {
299 const idx_t ii = compute_i_fast(i, nx); // guaranteed between 0 and nx(jj)
300 const double a = (ii - i) / nx;
301 const double x = grid_->x(ii, jj) - a * (grid_->x(nx, jj) - grid_->x(0, jj));
302 return x;
303 };
304
305 auto compute_i_less_equal_x = [this, &eps](const double& x, idx_t j) -> idx_t {
306 const double dx = grid_->dx(j);
307 idx_t i = idx_t(std::floor((x + eps - grid_->xmin(j)) / dx));
308 return i;
309 };
310
311 auto compute_y = [this, &compute_j, &periodic_y](idx_t j) -> double {
312 double y;
313 idx_t jj;
314 jj = compute_j(j);
315 if (periodic_y) {
316 y = grid_->y(jj);
317 }
318 else {
319 y = (j < 0) ? 90. + (90. - grid_->y(jj)) : (j >= grid_->ny()) ? -90. + (-90. - grid_->y(jj)) : grid_->y(jj);
320 }
321 return y;
322 };
323
324 std::vector<gidx_t> global_offsets(grid_->ny());
325 idx_t grid_idx = 0;
326 for (idx_t j = 0; j < grid_->ny(); ++j) {
327 global_offsets[j] = grid_idx;
328 grid_idx += grid_->nx(j);
329 }
330
331 auto compute_g = [this, &global_offsets, &compute_i, &compute_j, &periodic_y](idx_t i, idx_t j) -> gidx_t {
332 idx_t ii, jj;
333 gidx_t g;
334 jj = compute_j(j);
335 ii = compute_i(i, jj);
336 if (!periodic_y) {
337 if (jj != j) {
338 if (grid_->nx(jj) % 2 == 0) {
339 ii = (ii < grid_->nx(jj) / 2) ? ii + grid_->nx(jj) / 2
340 : (ii >= grid_->nx(jj) / 2) ? ii - grid_->nx(jj) / 2
341 : ii;
342 }
343 else {
344 if (ii < grid_->nx(jj)/2 + 1) {
345 ii += grid_->nx(jj) / 2 + 1;
346 }
347 else if (ii >= grid_->nx(jj)/2 + 1) {
348 ii -= grid_->nx(jj) / 2 + 1;
349 }
350 }
351 }
352 }
353 g = global_offsets[jj] + ii + 1;
354 return g;
355 };
356
357 auto compute_p = [&compute_g, &distribution](idx_t i, idx_t j) -> int {
358 return distribution.partition(compute_g(i, j) - 1);
359 };
360
361 GridPointSet gridpoints;
362
363 ATLAS_TRACE_SCOPE("Compute mapping") {
364 idx_t imin = std::numeric_limits<idx_t>::max();
365 idx_t imax = -std::numeric_limits<idx_t>::max();
366 idx_t jmin = std::numeric_limits<idx_t>::max();
367 idx_t jmax = -std::numeric_limits<idx_t>::max();
368
369 ATLAS_TRACE_SCOPE("Compute bounds halo") {
370 for (idx_t j = j_begin_ - halo; j < j_end_ + halo; ++j) {
371 i_begin_halo_(j) = imin;
372 i_end_halo_(j) = imax;
373 }
374
375 // Following cannot be multithreaded in current form due to race-conditions related to index jj
376 for (idx_t j = j_begin_; j < j_end_; ++j) {
377 for (idx_t i : {i_begin_[j], i_end_[j] - 1}) {
378 // Following line only, increases periodic halo on the east side by 1
379 if (periodic_points_ && i == grid_->nx(j) - 1) {
380 ++i;
381 }
382
383 double x = grid_->x(i, j);
384
385 double x_next = grid_->x(i + idx_t{1}, j);
386 double x_prev = grid_->x(i - idx_t{1}, j);
387 idx_t jj_min = j - halo;
388 idx_t jj_max = j + halo;
389 if (regional) {
390 jj_min = std::max(jj_min, idx_t{0});
391 jj_max = std::min(jj_max, grid_->ny() - idx_t{1});
392 }
393 for (idx_t jj = jj_min; jj <= jj_max; ++jj) {
394 idx_t jjj = compute_j(jj);
395 idx_t nx_jjj = grid_->nx(jjj);
396 idx_t last = grid_->nx(jjj) - idx_t{1};
397 if (i == grid_->nx(j)) {
398 ++last;
399 }
400
401 jmin = std::min(jmin, jj);
402 jmax = std::max(jmax, jj);
403 // Compute ii as index less-equal of x
404 //
405 // x(i,j)
406 // |-----|-----|-----|-----|
407 // ii-halo ii
408 //
409 // x(i,j)
410 // |-----|-----|--+--|-----|
411 // ii-halo ii
412
413 // idx_t ii = -halo;
414 // while ( compute_x_fast( ii, jjj, nx_jjj ) < x - eps ) {
415 // ii++;
416 // }
417 // Question: is following implementation reproducible with above original while loop?
418 idx_t ii = compute_i_less_equal_x(x, jjj);
419
420 // ATLAS-186 workaround
421 // This while should not have to be there, but is here because of
422 // the MatchingMeshDomainDecomposition algorithm. that may lead to points
423 // left of the point ii.
424 while (compute_x_fast(ii - 1, jjj, nx_jjj) > x_prev + eps) {
425 --ii;
426 }
427
428 idx_t i_minus_halo = ii - halo;
429
430 // Compute iii as index less-equal of x_next
431 //
432 // x(i,j) x_next(i,j)
433 // |-----|-----|-+---|-+---|-----|
434 // ii-halo iii iii+halo
435 //
436 idx_t iii = ii;
437 while (compute_x_fast(iii + 1, jjj, nx_jjj) < x_next - eps) {
438 ++iii;
439 }
440 iii = std::min(iii, last);
441 idx_t i_plus_halo = iii + halo;
442
443 if (regional) {
444 i_minus_halo = std::max(i_minus_halo, idx_t{0});
445 i_plus_halo = std::min(i_plus_halo, grid_->nx(jj) - idx_t{1});
446 }
447
448 imin = std::min(imin, i_minus_halo);
449 imax = std::max(imax, i_plus_halo);
450 i_begin_halo_(jj) = std::min(i_begin_halo_(jj), i_minus_halo);
451 i_end_halo_(jj) = std::max(i_end_halo_(jj), i_plus_halo + idx_t{1});
452 }
453 }
454 }
455 }
456
457 int extra_halo{0};
458 for (idx_t j = j_begin_halo_; j < j_begin_; ++j) {
459 extra_halo += i_end_halo_(j) - i_begin_halo_(j);
460 }
461 for (idx_t j = j_begin_; j < j_end_; ++j) {
462 extra_halo += i_begin_[j] - i_begin_halo_(j);
463 extra_halo += i_end_halo_(j) - i_end_[j];
464 }
465 for (idx_t j = j_end_; j < j_end_halo_; ++j) {
466 extra_halo += i_end_halo_(j) - i_begin_halo_(j);
467 }
468
469 ATLAS_TRACE_SCOPE("Assemble gridpoints") {
470 gridpoints.reserve(owned + extra_halo);
471 gridpoints.resize(owned);
472
473 if (atlas_omp_get_max_threads() == 1) {
474 idx_t r = 0;
475 for (idx_t j = j_begin_; j < j_end_; ++j) {
476 for (idx_t i = i_begin_[j]; i < i_end_[j]; ++i, ++r) {
477 gridpoints.set(i, j, r);
478 }
479 }
480 ATLAS_ASSERT(r == owned);
481 }
482 else {
483 atlas_omp_parallel {
484 const size_t num_threads = atlas_omp_get_num_threads();
485 const size_t thread_num = atlas_omp_get_thread_num();
486
487 auto chunksize = size_t(owned) / num_threads;
488 size_t begin = chunksize * thread_num;
489 size_t end = (thread_num == num_threads - 1) ? owned : begin + chunksize;
490
491 idx_t thread_j_begin = 0;
492 std::vector<idx_t> thread_i_begin(grid_->ny());
493 std::vector<idx_t> thread_i_end(grid_->ny());
494 size_t n = 0;
495 for (idx_t j = j_begin_; j < j_end_; ++j) {
496 size_t j_size = static_cast<size_t>(i_end_[j] - i_begin_[j]);
497 if (n + j_size > begin) {
498 thread_j_begin = j;
499 thread_i_begin[j] = i_begin_[j] + static_cast<idx_t>(begin - n);
500 break;
501 }
502 n += j_size;
503 }
504 idx_t thread_j_end{thread_j_begin};
505 for (idx_t j = thread_j_begin; j < j_end_; ++j) {
506 if (j > thread_j_begin) {
507 thread_i_begin[j] = i_begin_[j];
508 }
509 idx_t j_size = (i_end_[j] - i_begin_[j]);
510
511 idx_t remaining = static_cast<idx_t>(end - n);
512 if (j_size <= remaining) {
513 thread_i_end[j] = i_end_[j];
514 n += j_size;
515 if (n == end) {
516 goto stop;
517 }
518 }
519 else {
520 thread_i_end[j] = i_begin_[j] + remaining;
521 goto stop;
522 }
523
524 continue;
525
526 stop:
527 thread_j_end = j + 1;
528 break;
529 }
530
531 idx_t r = idx_t(begin);
532 for (idx_t j = thread_j_begin; j < thread_j_end; ++j) {
533 for (idx_t i = thread_i_begin[j]; i < thread_i_end[j]; ++i, ++r) {
534 gridpoints.set(i, j, r);
535 }
536 }
537 if (r != idx_t(end)) {
538 ATLAS_DEBUG_VAR(thread_num);
539 ATLAS_DEBUG_VAR(begin);
540 ATLAS_DEBUG_VAR(end);
541 ATLAS_DEBUG_VAR(r);
542 }
543 ATLAS_ASSERT(r == idx_t(end));
544 }
545 }
546
547 ATLAS_ASSERT(gridpoints.size() == owned);
548
549 gridpoints.resize(owned + extra_halo);
550 idx_t r = owned;
551 for (idx_t j = j_begin_halo_; j < j_begin_; ++j) {
552 for (idx_t i = i_begin_halo_(j); i < i_end_halo_(j); ++i, ++r) {
553 gridpoints.set(i, j, r);
554 }
555 }
556 for (idx_t j = j_begin_; j < j_end_; ++j) {
557 for (idx_t i = i_begin_halo_(j); i < i_begin_[j]; ++i, ++r) {
558 gridpoints.set(i, j, r);
559 }
560 for (idx_t i = i_end_[j]; i < i_end_halo_(j); ++i, ++r) {
561 gridpoints.set(i, j, r);
562 }
563 }
564 for (idx_t j = j_end_; j < j_end_halo_; ++j) {
565 for (idx_t i = i_begin_halo_(j); i < i_end_halo_(j); ++i, ++r) {
566 gridpoints.set(i, j, r);
567 }
568 }
569
570 ATLAS_ASSERT(gridpoints.size() == owned + extra_halo);
571 }
572
573 ATLAS_TRACE_SCOPE("Fill in ij2gp ") {
574 ij2gp_.resize({imin, imax}, {jmin, jmax});
575
576 atlas_omp_parallel_for(idx_t n = 0; n < gridpoints.size(); ++n) {
577 const GridPoint& gp = gridpoints[n];
578 ij2gp_.set(gp.i, gp.j, gp.r);
579 }
580 }
581 }
582
583 ATLAS_TRACE_SCOPE("Create fields") {
584 size_halo_ = gridpoints.size();
585 field_partition_ = Field("partition", array::make_datatype<int>(), array::make_shape(size_halo_));
586 field_ghost_ = Field("ghost", array::make_datatype<int>(), array::make_shape(size_halo_));
587 field_global_index_ = Field("glb_idx", array::make_datatype<gidx_t>(), array::make_shape(size_halo_));
588 field_index_i_ = Field("index_i", array::make_datatype<idx_t>(), array::make_shape(size_halo_));
589 field_index_j_ = Field("index_j", array::make_datatype<idx_t>(), array::make_shape(size_halo_));
590 field_xy_ = Field("xy", array::make_datatype<double>(), array::make_shape(size_halo_, 2));
591
592 field_z_ = Field("z", array::make_datatype<double>(), array::make_shape(vertical_.size()));
593 {
594 auto z = array::make_view<double, 1>(field_z_);
595 for (idx_t k = 0; k < z.size(); ++k) {
596 z(k) = vertical_[k];
597 }
598 }
599 auto xy = array::make_view<double, 2>(field_xy_);
600 auto part = array::make_view<int, 1>(field_partition_);
601 auto ghost = array::make_view<int, 1>(field_ghost_);
602 auto global_idx = array::make_view<gidx_t, 1>(field_global_index_);
603 auto index_i = array::make_indexview<idx_t, 1>(field_index_i_);
604 auto index_j = array::make_indexview<idx_t, 1>(field_index_j_);
605
606 atlas_omp_parallel_for(idx_t n = 0; n < gridpoints.size(); ++n) {
607 const GridPoint& gp = gridpoints[n];
608 if (regional) {
609 std::array<double,2> lonlatVec;
610 grid_->lonlat(gp.i, gp.j, lonlatVec.data());
611 xy(gp.r, XX) = lonlatVec[0];
612 xy(gp.r, YY) = lonlatVec[1];
613 } else {
614 if (gp.j >= 0 && gp.j < grid_->ny()) {
615 xy(gp.r, XX) = grid_->x(gp.i, gp.j);
616 xy(gp.r, YY) = grid_->y(gp.j);
617 }
618 else {
619 xy(gp.r, XX) = compute_x(gp.i, gp.j);
620 xy(gp.r, YY) = compute_y(gp.j);
621 }
622 }
623
624 bool in_domain(false);
625 if (gp.j >= 0 && gp.j < grid_->ny()) {
626 if (gp.i >= 0 && gp.i < grid_->nx(gp.j)) {
627 in_domain = true;
628 gidx_t k = global_offsets[gp.j] + gp.i;
629 part(gp.r) = distribution.partition(k);
630 global_idx(gp.r) = k + 1;
631 }
632 }
633 if (not in_domain) {
634 global_idx(gp.r) = compute_g(gp.i, gp.j);
635 part(gp.r) = compute_p(gp.i, gp.j);
636 }
637 index_i(gp.r) = gp.i;
638 index_j(gp.r) = gp.j;
639 ghost(gp.r) = 0;
640 }
641
642 // Following short loops are not parallelized with OpenMP
643
644 for (idx_t j = j_begin_halo_; j < j_begin_; ++j) {
645 for (idx_t i = i_begin_halo_(j); i < i_end_halo_(j); ++i) {
646 ghost(index(i, j)) = 1;
647 }
648 }
649 atlas_omp_parallel_for(idx_t j = j_begin_; j < j_end_; ++j) {
650 for (idx_t i = i_begin_halo_(j); i < i_begin_[j]; ++i) {
651 ghost(index(i, j)) = 1;
652 }
653 for (idx_t i = i_end_[j]; i < i_end_halo_(j); ++i) {
654 ghost(index(i, j)) = 1;
655 }
656 }
657 for (idx_t j = j_end_; j < j_end_halo_; ++j) {
658 for (idx_t i = i_begin_halo_(j); i < i_end_halo_(j); ++i) {
659 ghost(index(i, j)) = 1;
660 }
661 }
662 }
663 }
664
665 } // namespace detail
666
667 // ----------------------------------------------------------------------------
668
669 } // namespace functionspace
670 } // namespace atlas