| 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 <cmath> |
| 12 |
#include <iomanip> |
| 13 |
#include <iostream> |
| 14 |
#include <limits> |
| 15 |
#include <numeric> |
| 16 |
#include <stdexcept> |
| 17 |
#include <unordered_map> |
| 18 |
#include <unordered_set> |
| 19 |
|
| 20 |
#include "atlas/array.h" |
| 21 |
#include "atlas/array/IndexView.h" |
| 22 |
#include "atlas/field/Field.h" |
| 23 |
#include "atlas/library/config.h" |
| 24 |
#include "atlas/mesh/Elements.h" |
| 25 |
#include "atlas/mesh/HybridElements.h" |
| 26 |
#include "atlas/mesh/Mesh.h" |
| 27 |
#include "atlas/mesh/Nodes.h" |
| 28 |
#include "atlas/mesh/actions/BuildHalo.h" |
| 29 |
#include "atlas/mesh/actions/BuildParallelFields.h" |
| 30 |
#include "atlas/mesh/detail/AccumulateFacets.h" |
| 31 |
#include "atlas/parallel/mpi/Buffer.h" |
| 32 |
#include "atlas/parallel/mpi/mpi.h" |
| 33 |
#include "atlas/parallel/omp/omp.h" |
| 34 |
#include "atlas/parallel/omp/sort.h" |
| 35 |
#include "atlas/runtime/Exception.h" |
| 36 |
#include "atlas/runtime/Log.h" |
| 37 |
#include "atlas/runtime/Trace.h" |
| 38 |
#include "atlas/util/CoordinateEnums.h" |
| 39 |
#include "atlas/util/LonLatMicroDeg.h" |
| 40 |
#include "atlas/util/MicroDeg.h" |
| 41 |
#include "atlas/util/PeriodicTransform.h" |
| 42 |
#include "atlas/util/Unique.h" |
| 43 |
|
| 44 |
//#define DEBUG_OUTPUT |
| 45 |
#ifdef DEBUG_OUTPUT |
| 46 |
#include "atlas/mesh/actions/BuildParallelFields.h" |
| 47 |
#include "atlas/mesh/actions/BuildXYZField.h" |
| 48 |
#include "atlas/output/Gmsh.h" |
| 49 |
#endif |
| 50 |
|
| 51 |
// #define ATLAS_103 |
| 52 |
// #define ATLAS_103_SORT |
| 53 |
|
| 54 |
using atlas::mesh::detail::accumulate_facets; |
| 55 |
using atlas::util::LonLatMicroDeg; |
| 56 |
using atlas::util::microdeg; |
| 57 |
using atlas::util::PeriodicTransform; |
| 58 |
using atlas::util::UniqueLonLat; |
| 59 |
using Topology = atlas::mesh::Nodes::Topology; |
| 60 |
|
| 61 |
namespace atlas { |
| 62 |
namespace mesh { |
| 63 |
namespace actions { |
| 64 |
|
| 65 |
struct Entity { |
| 66 |
Entity(gidx_t gid, idx_t idx) { |
| 67 |
g = gid; |
| 68 |
i = idx; |
| 69 |
} |
| 70 |
gidx_t g; |
| 71 |
idx_t i; |
| 72 |
bool operator<(const Entity& other) const { return (g < other.g); } |
| 73 |
}; |
| 74 |
|
| 75 |
void make_nodes_global_index_human_readable(const mesh::actions::BuildHalo& build_halo, mesh::Nodes& nodes, |
| 76 |
bool do_all) { |
| 77 |
ATLAS_TRACE(); |
| 78 |
// TODO: ATLAS-14: fix renumbering of EAST periodic boundary points |
| 79 |
// --> Those specific periodic points at the EAST boundary are not checked for |
| 80 |
// uid, |
| 81 |
// and could receive different gidx for different tasks |
| 82 |
|
| 83 |
// unused // int mypart = mpi::rank(); |
| 84 |
int nparts = mpi::size(); |
| 85 |
size_t root = 0; |
| 86 |
|
| 87 |
array::ArrayView<gidx_t, 1> nodes_glb_idx = array::make_view<gidx_t, 1>(nodes.global_index()); |
| 88 |
// nodes_glb_idx.dump( Log::info() ); |
| 89 |
// ATLAS_DEBUG( "min = " << nodes.global_index().metadata().getLong("min") ); |
| 90 |
// ATLAS_DEBUG( "max = " << nodes.global_index().metadata().getLong("max") ); |
| 91 |
// ATLAS_DEBUG( "human_readable = " << |
| 92 |
// nodes.global_index().metadata().getBool("human_readable") ); |
| 93 |
gidx_t glb_idx_max = 0; |
| 94 |
|
| 95 |
std::vector<int> points_to_edit; |
| 96 |
|
| 97 |
if (do_all) { |
| 98 |
points_to_edit.resize(nodes_glb_idx.size()); |
| 99 |
for (idx_t i = 0; i < nodes_glb_idx.size(); ++i) { |
| 100 |
points_to_edit[i] = i; |
| 101 |
} |
| 102 |
} |
| 103 |
else { |
| 104 |
glb_idx_max = nodes.global_index().metadata().getLong("max", 0); |
| 105 |
points_to_edit.resize(build_halo.periodic_points_local_index_.size()); |
| 106 |
for (size_t i = 0; i < points_to_edit.size(); ++i) { |
| 107 |
points_to_edit[i] = build_halo.periodic_points_local_index_[i]; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
std::vector<gidx_t> glb_idx(points_to_edit.size()); |
| 112 |
int nb_nodes = static_cast<int>(glb_idx.size()); |
| 113 |
for (idx_t i = 0; i < nb_nodes; ++i) { |
| 114 |
glb_idx[i] = nodes_glb_idx(points_to_edit[i]); |
| 115 |
} |
| 116 |
|
| 117 |
// ATLAS_DEBUG_VAR( points_to_edit ); |
| 118 |
// ATLAS_DEBUG_VAR( points_to_edit.size() ); |
| 119 |
|
| 120 |
/* |
| 121 |
* Sorting following gidx will define global order of |
| 122 |
* gathered fields. Special care needs to be taken for |
| 123 |
* pole edges, as their centroid might coincide with |
| 124 |
* other edges |
| 125 |
*/ |
| 126 |
// Try to recover |
| 127 |
// { |
| 128 |
// UniqueLonLat compute_uid(nodes); |
| 129 |
// for( int jnode=0; jnode<nb_nodes; ++jnode ) { |
| 130 |
// if( glb_idx(jnode) <= 0 ) { |
| 131 |
// glb_idx(jnode) = compute_uid(jnode); |
| 132 |
// } |
| 133 |
// } |
| 134 |
// } |
| 135 |
|
| 136 |
// 1) Gather all global indices, together with location |
| 137 |
|
| 138 |
std::vector<int> recvcounts(mpi::size()); |
| 139 |
std::vector<int> recvdispls(mpi::size()); |
| 140 |
|
| 141 |
ATLAS_TRACE_MPI(GATHER) { mpi::comm().gather(nb_nodes, recvcounts, root); } |
| 142 |
idx_t glb_nb_nodes = std::accumulate(recvcounts.begin(), recvcounts.end(), 0); |
| 143 |
|
| 144 |
recvdispls[0] = 0; |
| 145 |
for (int jpart = 1; jpart < nparts; ++jpart) { // start at 1 |
| 146 |
recvdispls[jpart] = recvcounts[jpart - 1] + recvdispls[jpart - 1]; |
| 147 |
} |
| 148 |
|
| 149 |
std::vector<gidx_t> glb_idx_gathered(glb_nb_nodes); |
| 150 |
ATLAS_TRACE_MPI(GATHER) { |
| 151 |
mpi::comm().gatherv(glb_idx.data(), glb_idx.size(), glb_idx_gathered.data(), recvcounts.data(), |
| 152 |
recvdispls.data(), root); |
| 153 |
} |
| 154 |
|
| 155 |
// 2) Sort all global indices, and renumber from 1 to glb_nb_edges |
| 156 |
std::vector<Entity> node_sort; |
| 157 |
node_sort.reserve(glb_nb_nodes); |
| 158 |
const idx_t nb_glb_idx_gathered = static_cast<idx_t>(glb_idx_gathered.size()); |
| 159 |
for (idx_t jnode = 0; jnode < nb_glb_idx_gathered; ++jnode) { |
| 160 |
node_sort.emplace_back(glb_idx_gathered[jnode], jnode); |
| 161 |
} |
| 162 |
|
| 163 |
ATLAS_TRACE_SCOPE("sort on rank 0") { std::sort(node_sort.begin(), node_sort.end()); } |
| 164 |
|
| 165 |
gidx_t gid = glb_idx_max + 1; |
| 166 |
const idx_t nb_node_sort = static_cast<idx_t>(node_sort.size()); |
| 167 |
for (idx_t jnode = 0; jnode < nb_node_sort; ++jnode) { |
| 168 |
if (jnode > 0 && node_sort[jnode].g != node_sort[jnode - 1].g) { |
| 169 |
++gid; |
| 170 |
} |
| 171 |
int inode = node_sort[jnode].i; |
| 172 |
glb_idx_gathered[inode] = gid; |
| 173 |
} |
| 174 |
|
| 175 |
// 3) Scatter renumbered back |
| 176 |
ATLAS_TRACE_MPI(SCATTER) { |
| 177 |
mpi::comm().scatterv(glb_idx_gathered.data(), recvcounts.data(), recvdispls.data(), glb_idx.data(), |
| 178 |
static_cast<int>(glb_idx.size()), root); |
| 179 |
} |
| 180 |
|
| 181 |
for (int jnode = 0; jnode < nb_nodes; ++jnode) { |
| 182 |
nodes_glb_idx(points_to_edit[jnode]) = glb_idx[jnode]; |
| 183 |
} |
| 184 |
|
| 185 |
// nodes_glb_idx.dump( Log::info() ); |
| 186 |
// Log::info() << std::endl; |
| 187 |
nodes.global_index().metadata().set("human_readable", true); |
| 188 |
} |
| 189 |
|
| 190 |
// ------------------------------------------------------------------------------------- |
| 191 |
|
| 192 |
void make_cells_global_index_human_readable(const mesh::actions::BuildHalo& build_halo, mesh::Cells& cells, |
| 193 |
bool do_all) { |
| 194 |
ATLAS_TRACE(); |
| 195 |
|
| 196 |
int nparts = static_cast<idx_t>(mpi::size()); |
| 197 |
idx_t root = 0; |
| 198 |
|
| 199 |
array::ArrayView<gidx_t, 1> cells_glb_idx = array::make_view<gidx_t, 1>(cells.global_index()); |
| 200 |
// ATLAS_DEBUG( "min = " << cells.global_index().metadata().getLong("min") ); |
| 201 |
// ATLAS_DEBUG( "max = " << cells.global_index().metadata().getLong("max") ); |
| 202 |
// ATLAS_DEBUG( "human_readable = " << |
| 203 |
// cells.global_index().metadata().getBool("human_readable") ); |
| 204 |
gidx_t glb_idx_max = 0; |
| 205 |
|
| 206 |
std::vector<int> cells_to_edit; |
| 207 |
|
| 208 |
if (do_all) { |
| 209 |
cells_to_edit.resize(cells_glb_idx.size()); |
| 210 |
for (idx_t i = 0; i < cells_glb_idx.size(); ++i) { |
| 211 |
cells_to_edit[i] = i; |
| 212 |
} |
| 213 |
} |
| 214 |
else { |
| 215 |
idx_t nb_cells_to_edit(0); |
| 216 |
for (const auto& new_cells : build_halo.periodic_cells_local_index_) { |
| 217 |
nb_cells_to_edit += new_cells.size(); |
| 218 |
} |
| 219 |
cells_to_edit.resize(nb_cells_to_edit); |
| 220 |
int c{0}; |
| 221 |
int i{0}; |
| 222 |
for (int t = 0; t < cells.nb_types(); ++t) { |
| 223 |
for (idx_t p : build_halo.periodic_cells_local_index_[t]) { |
| 224 |
cells_to_edit[i++] = c + p; |
| 225 |
} |
| 226 |
c += cells.elements(t).size(); |
| 227 |
} |
| 228 |
glb_idx_max = cells.global_index().metadata().getLong("max", 0); |
| 229 |
} |
| 230 |
|
| 231 |
std::vector<gidx_t> glb_idx(cells_to_edit.size()); |
| 232 |
const int nb_cells = static_cast<int>(glb_idx.size()); |
| 233 |
for (int i = 0; i < nb_cells; ++i) { |
| 234 |
glb_idx[i] = cells_glb_idx(cells_to_edit[i]); |
| 235 |
} |
| 236 |
|
| 237 |
// 1) Gather all global indices, together with location |
| 238 |
|
| 239 |
std::vector<int> recvcounts(mpi::size()); |
| 240 |
std::vector<int> recvdispls(mpi::size()); |
| 241 |
|
| 242 |
ATLAS_TRACE_MPI(GATHER) { mpi::comm().gather(nb_cells, recvcounts, root); } |
| 243 |
idx_t glb_nb_cells = std::accumulate(recvcounts.begin(), recvcounts.end(), 0); |
| 244 |
|
| 245 |
recvdispls[0] = 0; |
| 246 |
for (int jpart = 1; jpart < nparts; ++jpart) { // start at 1 |
| 247 |
recvdispls[jpart] = recvcounts[jpart - 1] + recvdispls[jpart - 1]; |
| 248 |
} |
| 249 |
|
| 250 |
std::vector<gidx_t> glb_idx_gathered(glb_nb_cells); |
| 251 |
ATLAS_TRACE_MPI(GATHER) { |
| 252 |
mpi::comm().gatherv(glb_idx.data(), glb_idx.size(), glb_idx_gathered.data(), recvcounts.data(), |
| 253 |
recvdispls.data(), root); |
| 254 |
} |
| 255 |
|
| 256 |
// 2) Sort all global indices, and renumber from 1 to glb_nb_edges |
| 257 |
std::vector<Entity> cell_sort; |
| 258 |
cell_sort.reserve(glb_nb_cells); |
| 259 |
for (idx_t jcell = 0; jcell < glb_nb_cells; ++jcell) { |
| 260 |
cell_sort.emplace_back(glb_idx_gathered[jcell], jcell); |
| 261 |
} |
| 262 |
|
| 263 |
ATLAS_TRACE_SCOPE("sort on rank 0") { std::sort(cell_sort.begin(), cell_sort.end()); } |
| 264 |
|
| 265 |
gidx_t gid = glb_idx_max + 1; |
| 266 |
for (idx_t jcell = 0; jcell < glb_nb_cells; ++jcell) { |
| 267 |
if (jcell > 0 && cell_sort[jcell].g != cell_sort[jcell - 1].g) { |
| 268 |
++gid; |
| 269 |
} |
| 270 |
idx_t icell = cell_sort[jcell].i; |
| 271 |
glb_idx_gathered[icell] = gid; |
| 272 |
} |
| 273 |
|
| 274 |
// 3) Scatter renumbered back |
| 275 |
ATLAS_TRACE_MPI(SCATTER) { |
| 276 |
mpi::comm().scatterv(glb_idx_gathered.data(), recvcounts.data(), recvdispls.data(), glb_idx.data(), |
| 277 |
static_cast<int>(glb_idx.size()), root); |
| 278 |
} |
| 279 |
|
| 280 |
for (int jcell = 0; jcell < nb_cells; ++jcell) { |
| 281 |
cells_glb_idx(cells_to_edit[jcell]) = glb_idx[jcell]; |
| 282 |
} |
| 283 |
|
| 284 |
// nodes_glb_idx.dump( Log::info() ); |
| 285 |
// Log::info() << std::endl; |
| 286 |
cells.global_index().metadata().set("human_readable", true); |
| 287 |
} |
| 288 |
|
| 289 |
// ------------------------------------------------------------------------------------- |
| 290 |
|
| 291 |
using uid_t = gidx_t; |
| 292 |
|
| 293 |
// ------------------------------------------------------------------ |
| 294 |
class BuildHaloHelper; |
| 295 |
|
| 296 |
void increase_halo(Mesh& mesh); |
| 297 |
void increase_halo_interior(BuildHaloHelper&); |
| 298 |
|
| 299 |
class EastWest : public util::PeriodicTransform { |
| 300 |
public: |
| 301 |
EastWest() { x_translation_ = -360.; } |
| 302 |
}; |
| 303 |
|
| 304 |
class WestEast : public util::PeriodicTransform { |
| 305 |
public: |
| 306 |
WestEast() { x_translation_ = 360.; } |
| 307 |
}; |
| 308 |
|
| 309 |
using Node2Elem = std::vector<std::vector<idx_t>>; |
| 310 |
|
| 311 |
void build_lookup_node2elem(const Mesh& mesh, Node2Elem& node2elem) { |
| 312 |
ATLAS_TRACE(); |
| 313 |
|
| 314 |
const mesh::Nodes& nodes = mesh.nodes(); |
| 315 |
|
| 316 |
node2elem.resize(nodes.size()); |
| 317 |
for (idx_t jnode = 0; jnode < nodes.size(); ++jnode) { |
| 318 |
node2elem[jnode].clear(); |
| 319 |
node2elem[jnode].reserve(12); |
| 320 |
} |
| 321 |
|
| 322 |
const mesh::HybridElements::Connectivity& elem_nodes = mesh.cells().node_connectivity(); |
| 323 |
auto field_flags = array::make_view<int, 1>(mesh.cells().flags()); |
| 324 |
auto patched = [&field_flags](idx_t e) { |
| 325 |
using Topology = atlas::mesh::Nodes::Topology; |
| 326 |
return Topology::check(field_flags(e), Topology::PATCH); |
| 327 |
}; |
| 328 |
|
| 329 |
idx_t nb_elems = mesh.cells().size(); |
| 330 |
for (idx_t elem = 0; elem < nb_elems; ++elem) { |
| 331 |
if (not patched(elem)) { |
| 332 |
for (idx_t n = 0; n < elem_nodes.cols(elem); ++n) { |
| 333 |
int node = elem_nodes(elem, n); |
| 334 |
node2elem[node].push_back(elem); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
void accumulate_partition_bdry_nodes_old(Mesh& mesh, std::vector<idx_t>& bdry_nodes) { |
| 341 |
ATLAS_TRACE(); |
| 342 |
|
| 343 |
std::set<idx_t> bdry_nodes_set; |
| 344 |
|
| 345 |
std::vector<idx_t> facet_nodes; |
| 346 |
std::vector<idx_t> connectivity_facet_to_elem; |
| 347 |
|
| 348 |
facet_nodes.reserve(mesh.nodes().size() * 4); |
| 349 |
connectivity_facet_to_elem.reserve(facet_nodes.capacity() * 2); |
| 350 |
|
| 351 |
idx_t nb_facets(0); |
| 352 |
idx_t nb_inner_facets(0); |
| 353 |
idx_t missing_value; |
| 354 |
accumulate_facets( |
| 355 |
/*in*/ mesh.cells(), |
| 356 |
/*in*/ mesh.nodes(), |
| 357 |
/*out*/ facet_nodes, // shape(nb_facets,nb_nodes_per_facet) |
| 358 |
/*out*/ connectivity_facet_to_elem, |
| 359 |
/*out*/ nb_facets, |
| 360 |
/*out*/ nb_inner_facets, |
| 361 |
/*out*/ missing_value); |
| 362 |
|
| 363 |
for (idx_t jface = 0; jface < nb_facets; ++jface) { |
| 364 |
if (connectivity_facet_to_elem[jface * 2 + 1] == missing_value) { |
| 365 |
for (idx_t jnode = 0; jnode < 2; ++jnode) // 2 nodes per face |
| 366 |
{ |
| 367 |
bdry_nodes_set.insert(facet_nodes[jface * 2 + jnode]); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
bdry_nodes = std::vector<idx_t>(bdry_nodes_set.begin(), bdry_nodes_set.end()); |
| 372 |
} |
| 373 |
|
| 374 |
void accumulate_partition_bdry_nodes(Mesh& mesh, idx_t halo, std::vector<idx_t>& bdry_nodes) { |
| 375 |
#ifndef ATLAS_103 |
| 376 |
/* deprecated */ |
| 377 |
accumulate_partition_bdry_nodes_old(mesh, bdry_nodes); |
| 378 |
#else |
| 379 |
ATLAS_TRACE(); |
| 380 |
const Mesh::Polygon& polygon = mesh.polygon(halo); |
| 381 |
bdry_nodes = std::vector<int>(polygon.begin(), polygon.end()); |
| 382 |
#endif |
| 383 |
|
| 384 |
#ifdef ATLAS_103_SORT |
| 385 |
/* not required */ |
| 386 |
std::sort(bdry_nodes.begin(), bdry_nodes.end()); |
| 387 |
#endif |
| 388 |
} |
| 389 |
|
| 390 |
template <typename Predicate> |
| 391 |
std::vector<idx_t> filter_nodes(std::vector<idx_t> nodes, const Predicate& predicate) { |
| 392 |
std::vector<idx_t> filtered; |
| 393 |
filtered.reserve(nodes.size()); |
| 394 |
for (idx_t inode : nodes) { |
| 395 |
if (predicate(inode)) { |
| 396 |
filtered.push_back(inode); |
| 397 |
} |
| 398 |
} |
| 399 |
return filtered; |
| 400 |
} |
| 401 |
|
| 402 |
class Notification { |
| 403 |
public: |
| 404 |
void add_error(const std::string& note, const eckit::CodeLocation& loc) { |
| 405 |
notes.push_back(note + " @ " + std::string(loc)); |
| 406 |
} |
| 407 |
void add_error(const std::string& note) { notes.push_back(note); } |
| 408 |
|
| 409 |
bool error() const { return notes.size() > 0; } |
| 410 |
void reset() { notes.clear(); } |
| 411 |
|
| 412 |
std::string str() const { |
| 413 |
std::stringstream stream; |
| 414 |
for (size_t jnote = 0; jnote < notes.size(); ++jnote) { |
| 415 |
if (jnote > 0) { |
| 416 |
stream << "\n"; |
| 417 |
} |
| 418 |
stream << notes[jnote]; |
| 419 |
} |
| 420 |
return stream.str(); |
| 421 |
} |
| 422 |
|
| 423 |
operator std::string() const { return str(); } |
| 424 |
|
| 425 |
private: |
| 426 |
friend std::ostream& operator<<(std::ostream& s, const Notification& notes) { |
| 427 |
s << notes.str(); |
| 428 |
return s; |
| 429 |
} |
| 430 |
|
| 431 |
private: |
| 432 |
std::vector<std::string> notes; |
| 433 |
}; |
| 434 |
|
| 435 |
using Uid2Node = std::unordered_map<uid_t, idx_t>; |
| 436 |
|
| 437 |
|
| 438 |
void build_lookup_uid2node(Mesh& mesh, Uid2Node& uid2node) { |
| 439 |
ATLAS_TRACE(); |
| 440 |
Notification notes; |
| 441 |
mesh::Nodes& nodes = mesh.nodes(); |
| 442 |
array::ArrayView<double, 2> xy = array::make_view<double, 2>(nodes.xy()); |
| 443 |
array::ArrayView<gidx_t, 1> glb_idx = array::make_view<gidx_t, 1>(nodes.global_index()); |
| 444 |
idx_t nb_nodes = nodes.size(); |
| 445 |
|
| 446 |
UniqueLonLat compute_uid(mesh); |
| 447 |
|
| 448 |
uid2node.clear(); |
| 449 |
for (idx_t jnode = 0; jnode < nb_nodes; ++jnode) { |
| 450 |
uid_t uid = compute_uid(jnode); |
| 451 |
bool inserted = uid2node.insert(std::make_pair(uid, jnode)).second; |
| 452 |
if (not inserted) { |
| 453 |
int other = uid2node[uid]; |
| 454 |
std::stringstream msg; |
| 455 |
msg << std::setprecision(10) << std::fixed << "Node uid: " << uid << " " << glb_idx(jnode) << " xy(" |
| 456 |
<< xy(jnode, XX) << "," << xy(jnode, YY) << ")"; |
| 457 |
if (nodes.has_field("ij")) { |
| 458 |
auto ij = array::make_view<idx_t, 2>(nodes.field("ij")); |
| 459 |
msg << " ij(" << ij(jnode, XX) << "," << ij(jnode, YY) << ")"; |
| 460 |
} |
| 461 |
msg << " has already been added as node " << glb_idx(other) << " (" << xy(other, XX) << "," << xy(other, YY) |
| 462 |
<< ")"; |
| 463 |
if (nodes.has_field("ij")) { |
| 464 |
auto ij = array::make_view<idx_t, 2>(nodes.field("ij")); |
| 465 |
msg << " ij(" << ij(other, XX) << "," << ij(other, YY) << ")"; |
| 466 |
} |
| 467 |
notes.add_error(msg.str()); |
| 468 |
} |
| 469 |
} |
| 470 |
if (notes.error()) { |
| 471 |
throw_Exception(notes.str(), Here()); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
/// For given nodes, find new connected elements |
| 476 |
void accumulate_elements(const Mesh& mesh, const mpi::BufferView<uid_t>& request_node_uid, const Uid2Node& uid2node, |
| 477 |
const Node2Elem& node2elem, std::vector<idx_t>& found_elements, |
| 478 |
std::set<uid_t>& new_nodes_uid) { |
| 479 |
|
| 480 |
ATLAS_TRACE(); |
| 481 |
const mesh::HybridElements::Connectivity& elem_nodes = mesh.cells().node_connectivity(); |
| 482 |
const auto elem_part = array::make_view<int, 1>(mesh.cells().partition()); |
| 483 |
|
| 484 |
const idx_t nb_nodes = mesh.nodes().size(); |
| 485 |
const idx_t nb_request_nodes = static_cast<idx_t>(request_node_uid.size()); |
| 486 |
const int mpi_rank = static_cast<int>(mpi::rank()); |
| 487 |
|
| 488 |
std::unordered_set<idx_t> found_elements_set; |
| 489 |
found_elements_set.reserve(nb_request_nodes*2); |
| 490 |
|
| 491 |
for (idx_t jnode = 0; jnode < nb_request_nodes; ++jnode) { |
| 492 |
uid_t uid = request_node_uid(jnode); |
| 493 |
|
| 494 |
idx_t inode = -1; |
| 495 |
// search and get node index for uid |
| 496 |
auto found = uid2node.find(uid); |
| 497 |
if (found != uid2node.end()) { |
| 498 |
inode = found->second; |
| 499 |
} |
| 500 |
if (inode != -1 && inode < nb_nodes) { |
| 501 |
for (const idx_t e : node2elem[inode]) { |
| 502 |
if (elem_part(e) == mpi_rank) { |
| 503 |
found_elements_set.insert(e); |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
// found_bdry_elements_set now contains elements for the nodes |
| 510 |
found_elements = std::vector<idx_t>(found_elements_set.begin(), found_elements_set.end()); |
| 511 |
|
| 512 |
UniqueLonLat compute_uid(mesh); |
| 513 |
|
| 514 |
// Collect all nodes |
| 515 |
new_nodes_uid.clear(); |
| 516 |
for (const idx_t e : found_elements) { |
| 517 |
idx_t nb_elem_nodes = elem_nodes.cols(e); |
| 518 |
for (idx_t n = 0; n < nb_elem_nodes; ++n) { |
| 519 |
new_nodes_uid.insert(compute_uid(elem_nodes(e, n))); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
// Remove nodes we already have in the request-buffer |
| 524 |
for (idx_t jnode = 0; jnode < nb_request_nodes; ++jnode) { |
| 525 |
new_nodes_uid.erase(request_node_uid(jnode)); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
class BuildHaloHelper { |
| 530 |
public: |
| 531 |
struct Buffers { |
| 532 |
std::vector<std::vector<int>> node_part; |
| 533 |
|
| 534 |
std::vector<std::vector<int>> node_ridx; |
| 535 |
|
| 536 |
std::vector<std::vector<int>> node_flags; |
| 537 |
|
| 538 |
std::vector<std::vector<uid_t>> node_glb_idx; |
| 539 |
|
| 540 |
std::vector<std::vector<double>> node_xy; |
| 541 |
|
| 542 |
std::vector<std::vector<uid_t>> elem_glb_idx; |
| 543 |
|
| 544 |
std::vector<std::vector<uid_t>> elem_nodes_id; |
| 545 |
|
| 546 |
std::vector<std::vector<int>> elem_nodes_displs; |
| 547 |
|
| 548 |
std::vector<std::vector<int>> elem_part; |
| 549 |
|
| 550 |
std::vector<std::vector<int>> elem_ridx; |
| 551 |
|
| 552 |
std::vector<std::vector<int>> elem_flags; |
| 553 |
|
| 554 |
std::vector<std::vector<int>> elem_type; |
| 555 |
|
| 556 |
Buffers(Mesh&) { |
| 557 |
const idx_t mpi_size = static_cast<idx_t>(mpi::size()); |
| 558 |
|
| 559 |
node_part.resize(mpi_size); |
| 560 |
node_ridx.resize(mpi_size); |
| 561 |
node_flags.resize(mpi_size); |
| 562 |
node_glb_idx.resize(mpi_size); |
| 563 |
node_xy.resize(mpi_size); |
| 564 |
elem_glb_idx.resize(mpi_size); |
| 565 |
elem_nodes_id.resize(mpi_size); |
| 566 |
elem_nodes_displs.resize(mpi_size); |
| 567 |
elem_part.resize(mpi_size); |
| 568 |
elem_ridx.resize(mpi_size); |
| 569 |
elem_flags.resize(mpi_size); |
| 570 |
elem_type.resize(mpi_size); |
| 571 |
} |
| 572 |
|
| 573 |
void print(std::ostream& os) const { |
| 574 |
const idx_t mpi_size = static_cast<idx_t>(mpi::size()); |
| 575 |
os << "Nodes\n" |
| 576 |
<< "-----\n"; |
| 577 |
idx_t n(0); |
| 578 |
for (idx_t jpart = 0; jpart < mpi_size; ++jpart) { |
| 579 |
for (idx_t jnode = 0; jnode < node_glb_idx[jpart].size(); ++jnode ) { |
| 580 |
auto g = node_glb_idx[jpart][jnode]; |
| 581 |
auto p = node_part[jpart][jnode]; |
| 582 |
os << std::setw(4) << n++ << " : [ p" << p << " ] " << g << "\n"; |
| 583 |
} |
| 584 |
} |
| 585 |
os << std::flush; |
| 586 |
os << "Cells\n" |
| 587 |
<< "-----\n"; |
| 588 |
idx_t e(0); |
| 589 |
for (idx_t jpart = 0; jpart < mpi_size; ++jpart) { |
| 590 |
const idx_t nb_elem = static_cast<idx_t>(elem_glb_idx[jpart].size()); |
| 591 |
for (idx_t jelem = 0; jelem < nb_elem; ++jelem) { |
| 592 |
os << std::setw(4) << e++ << " : [ t" << elem_type[jpart][jelem] << " -- p" |
| 593 |
<< elem_part[jpart][jelem] << "] " << elem_glb_idx[jpart][jelem] << "\n"; |
| 594 |
} |
| 595 |
} |
| 596 |
os << std::flush; |
| 597 |
} |
| 598 |
friend std::ostream& operator<<(std::ostream& out, const Buffers& b) { |
| 599 |
b.print(out); |
| 600 |
return out; |
| 601 |
} |
| 602 |
}; |
| 603 |
|
| 604 |
static void all_to_all(Buffers& send, Buffers& recv) { |
| 605 |
ATLAS_TRACE(); |
| 606 |
const eckit::mpi::Comm& comm = mpi::comm(); |
| 607 |
|
| 608 |
ATLAS_TRACE_MPI(ALLTOALL) { |
| 609 |
comm.allToAll(send.node_glb_idx, recv.node_glb_idx); |
| 610 |
comm.allToAll(send.node_part, recv.node_part); |
| 611 |
comm.allToAll(send.node_ridx, recv.node_ridx); |
| 612 |
comm.allToAll(send.node_flags, recv.node_flags); |
| 613 |
comm.allToAll(send.node_xy, recv.node_xy); |
| 614 |
comm.allToAll(send.elem_glb_idx, recv.elem_glb_idx); |
| 615 |
comm.allToAll(send.elem_nodes_id, recv.elem_nodes_id); |
| 616 |
comm.allToAll(send.elem_part, recv.elem_part); |
| 617 |
comm.allToAll(send.elem_ridx, recv.elem_ridx); |
| 618 |
comm.allToAll(send.elem_type, recv.elem_type); |
| 619 |
comm.allToAll(send.elem_flags, recv.elem_flags); |
| 620 |
comm.allToAll(send.elem_nodes_displs, recv.elem_nodes_displs); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
struct Status { |
| 625 |
std::vector<idx_t> new_periodic_ghost_points; |
| 626 |
std::vector<std::vector<idx_t>> new_periodic_ghost_cells; |
| 627 |
} status; |
| 628 |
|
| 629 |
public: |
| 630 |
BuildHalo& builder_; |
| 631 |
Mesh& mesh; |
| 632 |
array::ArrayView<double, 2> xy; |
| 633 |
array::ArrayView<double, 2> lonlat; |
| 634 |
array::ArrayView<gidx_t, 1> glb_idx; |
| 635 |
array::ArrayView<int, 1> part; |
| 636 |
array::IndexView<idx_t, 1> ridx; |
| 637 |
array::ArrayView<int, 1> flags; |
| 638 |
array::ArrayView<int, 1> halo; |
| 639 |
array::ArrayView<int, 1> ghost; |
| 640 |
mesh::HybridElements::Connectivity* elem_nodes; |
| 641 |
array::ArrayView<int, 1> elem_part; |
| 642 |
array::IndexView<idx_t, 1> elem_ridx; |
| 643 |
array::ArrayView<int, 1> elem_flags; |
| 644 |
array::ArrayView<gidx_t, 1> elem_glb_idx; |
| 645 |
|
| 646 |
std::vector<idx_t> bdry_nodes; |
| 647 |
Node2Elem node_to_elem; |
| 648 |
Uid2Node uid2node; |
| 649 |
UniqueLonLat compute_uid; |
| 650 |
idx_t halosize; |
| 651 |
|
| 652 |
public: |
| 653 |
BuildHaloHelper(BuildHalo& builder, Mesh& _mesh): |
| 654 |
builder_(builder), |
| 655 |
mesh(_mesh), |
| 656 |
xy(array::make_view<double, 2>(mesh.nodes().xy())), |
| 657 |
lonlat(array::make_view<double, 2>(mesh.nodes().lonlat())), |
| 658 |
glb_idx(array::make_view<gidx_t, 1>(mesh.nodes().global_index())), |
| 659 |
part(array::make_view<int, 1>(mesh.nodes().partition())), |
| 660 |
ridx(array::make_indexview<idx_t, 1>(mesh.nodes().remote_index())), |
| 661 |
flags(array::make_view<int, 1>(mesh.nodes().flags())), |
| 662 |
halo(array::make_view<int, 1>(mesh.nodes().halo())), |
| 663 |
ghost(array::make_view<int, 1>(mesh.nodes().ghost())), |
| 664 |
elem_nodes(&mesh.cells().node_connectivity()), |
| 665 |
elem_part(array::make_view<int, 1>(mesh.cells().partition())), |
| 666 |
elem_ridx(array::make_indexview<idx_t, 1>(mesh.cells().remote_index())), |
| 667 |
elem_flags(array::make_view<int, 1>(mesh.cells().flags())), |
| 668 |
elem_glb_idx(array::make_view<gidx_t, 1>(mesh.cells().global_index())), |
| 669 |
compute_uid(mesh) { |
| 670 |
halosize = 0; |
| 671 |
mesh.metadata().get("halo", halosize); |
| 672 |
// update(); |
| 673 |
} |
| 674 |
|
| 675 |
void update() { |
| 676 |
compute_uid.update(); |
| 677 |
mesh::Nodes& nodes = mesh.nodes(); |
| 678 |
xy = array::make_view<double, 2>(nodes.xy()); |
| 679 |
lonlat = array::make_view<double, 2>(nodes.lonlat()); |
| 680 |
glb_idx = array::make_view<gidx_t, 1>(nodes.global_index()); |
| 681 |
part = array::make_view<int, 1>(nodes.partition()); |
| 682 |
ridx = array::make_indexview<idx_t, 1>(nodes.remote_index()); |
| 683 |
flags = array::make_view<int, 1>(nodes.flags()); |
| 684 |
ghost = array::make_view<int, 1>(nodes.ghost()); |
| 685 |
halo = array::make_view<int, 1>(nodes.halo()); |
| 686 |
|
| 687 |
elem_nodes = &mesh.cells().node_connectivity(); |
| 688 |
elem_part = array::make_view<int, 1>(mesh.cells().partition()); |
| 689 |
elem_ridx = array::make_indexview<idx_t, 1>(mesh.cells().remote_index()); |
| 690 |
elem_flags = array::make_view<int, 1>(mesh.cells().flags()); |
| 691 |
elem_glb_idx = array::make_view<gidx_t, 1>(mesh.cells().global_index()); |
| 692 |
} |
| 693 |
|
| 694 |
template <typename NodeContainer, typename ElementContainer> |
| 695 |
void fill_sendbuffer(Buffers& buf, const NodeContainer& nodes_uid, const ElementContainer& elems, const int p) { |
| 696 |
ATLAS_TRACE(); |
| 697 |
|
| 698 |
idx_t nb_nodes = static_cast<idx_t>(nodes_uid.size()); |
| 699 |
buf.node_glb_idx[p].resize(nb_nodes); |
| 700 |
buf.node_part[p].resize(nb_nodes); |
| 701 |
buf.node_ridx[p].resize(nb_nodes); |
| 702 |
buf.node_flags[p].resize(nb_nodes, Topology::NONE); |
| 703 |
buf.node_xy[p].resize(2 * nb_nodes); |
| 704 |
|
| 705 |
idx_t jnode = 0; |
| 706 |
typename NodeContainer::const_iterator it; |
| 707 |
for (it = nodes_uid.begin(); it != nodes_uid.end(); ++it, ++jnode) { |
| 708 |
uid_t uid = *it; |
| 709 |
auto found = uid2node.find(uid); |
| 710 |
if (found != uid2node.end()) // Point exists inside domain |
| 711 |
{ |
| 712 |
idx_t node = found->second; |
| 713 |
buf.node_glb_idx[p][jnode] = glb_idx(node); |
| 714 |
buf.node_part[p][jnode] = part(node); |
| 715 |
buf.node_ridx[p][jnode] = ridx(node); |
| 716 |
buf.node_xy[p][jnode * 2 + XX] = xy(node, XX); |
| 717 |
buf.node_xy[p][jnode * 2 + YY] = xy(node, YY); |
| 718 |
Topology::set(buf.node_flags[p][jnode], flags(node) | Topology::GHOST); |
| 719 |
} |
| 720 |
else { |
| 721 |
Log::warning() << "Node with uid " << uid << " needed by [" << p << "] was not found in [" |
| 722 |
<< mpi::rank() << "]." << std::endl; |
| 723 |
ATLAS_ASSERT(false); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
idx_t nb_elems = static_cast<idx_t>(elems.size()); |
| 728 |
|
| 729 |
idx_t nb_elem_nodes(0); |
| 730 |
for (idx_t jelem = 0; jelem < nb_elems; ++jelem) { |
| 731 |
idx_t ielem = elems[jelem]; |
| 732 |
nb_elem_nodes += elem_nodes->cols(ielem); |
| 733 |
} |
| 734 |
|
| 735 |
buf.elem_glb_idx[p].resize(nb_elems); |
| 736 |
buf.elem_part[p].resize(nb_elems); |
| 737 |
buf.elem_ridx[p].resize(nb_elems); |
| 738 |
buf.elem_flags[p].resize(nb_elems, Topology::NONE); |
| 739 |
buf.elem_type[p].resize(nb_elems); |
| 740 |
buf.elem_nodes_id[p].resize(nb_elem_nodes); |
| 741 |
buf.elem_nodes_displs[p].resize(nb_elems); |
| 742 |
idx_t jelemnode(0); |
| 743 |
for (idx_t jelem = 0; jelem < nb_elems; ++jelem) { |
| 744 |
buf.elem_nodes_displs[p][jelem] = jelemnode; |
| 745 |
idx_t ielem = elems[jelem]; |
| 746 |
|
| 747 |
buf.elem_glb_idx[p][jelem] = elem_glb_idx(ielem); |
| 748 |
buf.elem_part[p][jelem] = elem_part(ielem); |
| 749 |
buf.elem_ridx[p][jelem] = elem_ridx(ielem); |
| 750 |
Topology::set(buf.elem_flags[p][jelem], elem_flags(ielem)); |
| 751 |
buf.elem_type[p][jelem] = mesh.cells().type_idx(ielem); |
| 752 |
for (idx_t jnode = 0; jnode < elem_nodes->cols(ielem); ++jnode) { |
| 753 |
buf.elem_nodes_id[p][jelemnode++] = compute_uid((*elem_nodes)(ielem, jnode)); |
| 754 |
} |
| 755 |
Topology::set(buf.elem_flags[p][jelem], Topology::GHOST); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
template <typename NodeContainer, typename ElementContainer> |
| 760 |
void fill_sendbuffer(Buffers& buf, const NodeContainer& nodes_uid, const ElementContainer& elems, |
| 761 |
const util::PeriodicTransform& transform, int newflags, const int p) { |
| 762 |
// ATLAS_TRACE(); |
| 763 |
|
| 764 |
idx_t nb_nodes = static_cast<idx_t>(nodes_uid.size()); |
| 765 |
buf.node_glb_idx[p].resize(nb_nodes); |
| 766 |
buf.node_part[p].resize(nb_nodes); |
| 767 |
buf.node_ridx[p].resize(nb_nodes); |
| 768 |
buf.node_flags[p].resize(nb_nodes, Topology::NONE); |
| 769 |
buf.node_xy[p].resize(2 * nb_nodes); |
| 770 |
|
| 771 |
int jnode = 0; |
| 772 |
typename NodeContainer::const_iterator it; |
| 773 |
for (it = nodes_uid.begin(); it != nodes_uid.end(); ++it, ++jnode) { |
| 774 |
uid_t uid = *it; |
| 775 |
|
| 776 |
auto found = uid2node.find(uid); |
| 777 |
if (found != uid2node.end()) // Point exists inside domain |
| 778 |
{ |
| 779 |
int node = found->second; |
| 780 |
buf.node_part[p][jnode] = part(node); |
| 781 |
buf.node_ridx[p][jnode] = ridx(node); |
| 782 |
buf.node_xy[p][jnode * 2 + XX] = xy(node, XX); |
| 783 |
buf.node_xy[p][jnode * 2 + YY] = xy(node, YY); |
| 784 |
transform(&buf.node_xy[p][jnode * 2], -1); |
| 785 |
// Global index of node is based on UID of destination |
| 786 |
buf.node_glb_idx[p][jnode] = util::unique_lonlat(&buf.node_xy[p][jnode * 2]); |
| 787 |
Topology::set(buf.node_flags[p][jnode], newflags); |
| 788 |
} |
| 789 |
else { |
| 790 |
Log::warning() << "Node with uid " << uid << " needed by [" << p << "] was not found in [" |
| 791 |
<< mpi::rank() << "]." << std::endl; |
| 792 |
ATLAS_ASSERT(false); |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
idx_t nb_elems = static_cast<idx_t>(elems.size()); |
| 797 |
|
| 798 |
idx_t nb_elem_nodes(0); |
| 799 |
for (idx_t jelem = 0; jelem < nb_elems; ++jelem) { |
| 800 |
idx_t ielem = elems[jelem]; |
| 801 |
nb_elem_nodes += elem_nodes->cols(ielem); |
| 802 |
} |
| 803 |
|
| 804 |
buf.elem_glb_idx[p].resize(nb_elems); |
| 805 |
buf.elem_part[p].resize(nb_elems); |
| 806 |
buf.elem_ridx[p].resize(nb_elems); |
| 807 |
buf.elem_flags[p].resize(nb_elems, Topology::NONE); |
| 808 |
buf.elem_type[p].resize(nb_elems); |
| 809 |
buf.elem_nodes_id[p].resize(nb_elem_nodes); |
| 810 |
buf.elem_nodes_displs[p].resize(nb_elems); |
| 811 |
idx_t jelemnode(0); |
| 812 |
for (idx_t jelem = 0; jelem < nb_elems; ++jelem) { |
| 813 |
buf.elem_nodes_displs[p][jelem] = jelemnode; |
| 814 |
idx_t ielem = elems[jelem]; |
| 815 |
buf.elem_part[p][jelem] = elem_part(ielem); |
| 816 |
buf.elem_ridx[p][jelem] = elem_ridx(ielem); |
| 817 |
Topology::set(buf.elem_flags[p][jelem], elem_flags(ielem) | newflags); |
| 818 |
buf.elem_type[p][jelem] = mesh.cells().type_idx(ielem); |
| 819 |
std::vector<double> crds(elem_nodes->cols(ielem) * 2); |
| 820 |
for (idx_t jnode = 0; jnode < elem_nodes->cols(ielem); ++jnode) { |
| 821 |
double crd[] = {xy((*elem_nodes)(ielem, jnode), XX), xy((*elem_nodes)(ielem, jnode), YY)}; |
| 822 |
transform(crd, -1); |
| 823 |
buf.elem_nodes_id[p][jelemnode++] = util::unique_lonlat(crd); |
| 824 |
crds[jnode * 2 + XX] = crd[XX]; |
| 825 |
crds[jnode * 2 + YY] = crd[YY]; |
| 826 |
} |
| 827 |
// Global index of element is based on UID of destination |
| 828 |
|
| 829 |
buf.elem_glb_idx[p][jelem] = -util::unique_lonlat(crds.data(), elem_nodes->cols(ielem)); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
void add_nodes(Buffers& buf) { |
| 834 |
ATLAS_TRACE(); |
| 835 |
|
| 836 |
const idx_t mpi_size = static_cast<idx_t>(mpi::size()); |
| 837 |
|
| 838 |
mesh::Nodes& nodes = mesh.nodes(); |
| 839 |
int nb_nodes = nodes.size(); |
| 840 |
|
| 841 |
// Nodes might be duplicated from different Tasks. We need to identify |
| 842 |
// unique entries |
| 843 |
std::vector<uid_t> node_uid(nb_nodes); |
| 844 |
std::set<uid_t> new_node_uid; |
| 845 |
{ |
| 846 |
ATLAS_TRACE("compute node_uid"); |
| 847 |
for (int jnode = 0; jnode < nb_nodes; ++jnode) { |
| 848 |
node_uid[jnode] = compute_uid(jnode); |
| 849 |
} |
| 850 |
std::sort(node_uid.begin(), node_uid.end()); |
| 851 |
} |
| 852 |
auto node_already_exists = [&node_uid, &new_node_uid](uid_t uid) { |
| 853 |
std::vector<uid_t>::iterator it = std::lower_bound(node_uid.begin(), node_uid.end(), uid); |
| 854 |
bool not_found = (it == node_uid.end() || uid < *it); |
| 855 |
if (not_found) { |
| 856 |
bool inserted = new_node_uid.insert(uid).second; |
| 857 |
return not inserted; |
| 858 |
} |
| 859 |
else { |
| 860 |
return true; |
| 861 |
} |
| 862 |
}; |
| 863 |
|
| 864 |
std::vector<std::vector<int>> rfn_idx(mpi_size); |
| 865 |
for (idx_t jpart = 0; jpart < mpi_size; ++jpart) { |
| 866 |
rfn_idx[jpart].reserve(buf.node_glb_idx[jpart].size()); |
| 867 |
} |
| 868 |
|
| 869 |
int nb_new_nodes = 0; |
| 870 |
for (idx_t jpart = 0; jpart < mpi_size; ++jpart) { |
| 871 |
const idx_t nb_nodes_on_part = static_cast<idx_t>(buf.node_glb_idx[jpart].size()); |
| 872 |
for (idx_t n = 0; n < nb_nodes_on_part; ++n) { |
| 873 |
std::array<double,2> crd{buf.node_xy[jpart][n * 2 + XX], buf.node_xy[jpart][n * 2 + YY]}; |
| 874 |
mesh.projection().xy2lonlat(crd.data()); |
| 875 |
|
| 876 |
if (not node_already_exists(util::unique_lonlat(crd))) { |
| 877 |
rfn_idx[jpart].push_back(n); |
| 878 |
} |
| 879 |
} |
| 880 |
nb_new_nodes += rfn_idx[jpart].size(); |
| 881 |
} |
| 882 |
|
| 883 |
// Resize nodes |
| 884 |
// ------------ |
| 885 |
nodes.resize(nb_nodes + nb_new_nodes); |
| 886 |
flags = array::make_view<int, 1>(nodes.flags()); |
| 887 |
halo = array::make_view<int, 1>(nodes.halo()); |
| 888 |
glb_idx = array::make_view<gidx_t, 1>(nodes.global_index()); |
| 889 |
part = array::make_view<int, 1>(nodes.partition()); |
| 890 |
ridx = array::make_indexview<idx_t, 1>(nodes.remote_index()); |
| 891 |
xy = array::make_view<double, 2>(nodes.xy()); |
| 892 |
lonlat = array::make_view<double, 2>(nodes.lonlat()); |
| 893 |
ghost = array::make_view<int, 1>(nodes.ghost()); |
| 894 |
|
| 895 |
compute_uid.update(); |
| 896 |
|
| 897 |
// Add new nodes |
| 898 |
// ------------- |
| 899 |
int new_node = 0; |
| 900 |
for (idx_t jpart = 0; jpart < mpi_size; ++jpart) { |
| 901 |
for (size_t n = 0; n < rfn_idx[jpart].size(); ++n) { |
| 902 |
int loc_idx = nb_nodes + new_node; |
| 903 |
halo(loc_idx) = halosize + 1; |
| 904 |
Topology::reset(flags(loc_idx), buf.node_flags[jpart][rfn_idx[jpart][n]]); |
| 905 |
ghost(loc_idx) = Topology::check(flags(loc_idx), Topology::GHOST); |
| 906 |
glb_idx(loc_idx) = buf.node_glb_idx[jpart][rfn_idx[jpart][n]]; |
| 907 |
part(loc_idx) = buf.node_part[jpart][rfn_idx[jpart][n]]; |
| 908 |
ridx(loc_idx) = buf.node_ridx[jpart][rfn_idx[jpart][n]]; |
| 909 |
PointXY pxy(&buf.node_xy[jpart][rfn_idx[jpart][n] * 2]); |
| 910 |
xy(loc_idx, XX) = pxy.x(); |
| 911 |
xy(loc_idx, YY) = pxy.y(); |
| 912 |
PointLonLat pll = mesh.projection().lonlat(pxy); |
| 913 |
lonlat(loc_idx, XX) = pll.lon(); |
| 914 |
lonlat(loc_idx, YY) = pll.lat(); |
| 915 |
|
| 916 |
if (Topology::check(flags(loc_idx), Topology::PERIODIC) and |
| 917 |
not Topology::check(flags(loc_idx), Topology::BC)) { |
| 918 |
status.new_periodic_ghost_points.push_back(loc_idx); |
| 919 |
} |
| 920 |
|
| 921 |
// make sure new node was not already there |
| 922 |
{ |
| 923 |
uid_t uid = compute_uid(loc_idx); |
| 924 |
auto found = uid2node.find(uid); |
| 925 |
if (found != uid2node.end()) { |
| 926 |
int other = found->second; |
| 927 |
std::stringstream msg; |
| 928 |
msg << "New node loc " << loc_idx << " with uid " << uid << ":\n" |
| 929 |
<< glb_idx(loc_idx) << "(" << xy(loc_idx, XX) << "," << xy(loc_idx, YY) << ")\n"; |
| 930 |
msg << "Existing already loc " << other << " : " << glb_idx(other) << "(" << xy(other, XX) |
| 931 |
<< "," << xy(other, YY) << ")\n"; |
| 932 |
throw_Exception(msg.str(), Here()); |
| 933 |
} |
| 934 |
uid2node[uid] = nb_nodes + new_node; |
| 935 |
} |
| 936 |
++new_node; |
| 937 |
} |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
void add_elements(Buffers& buf) { |
| 942 |
ATLAS_TRACE(); |
| 943 |
|
| 944 |
const idx_t mpi_size = static_cast<idx_t>(mpi::size()); |
| 945 |
auto cell_gidx = array::make_view<gidx_t, 1>(mesh.cells().global_index()); |
| 946 |
// Elements might be duplicated from different Tasks. We need to identify |
| 947 |
// unique entries |
| 948 |
int nb_elems = mesh.cells().size(); |
| 949 |
// std::set<uid_t> elem_uid; |
| 950 |
std::vector<uid_t> elem_uid(2 * nb_elems); |
| 951 |
std::set<uid_t> new_elem_uid; |
| 952 |
{ |
| 953 |
ATLAS_TRACE("compute elem_uid"); |
| 954 |
atlas_omp_parallel_for (int jelem = 0; jelem < nb_elems; ++jelem) { |
| 955 |
elem_uid[jelem * 2 + 0] = -compute_uid(elem_nodes->row(jelem)); |
| 956 |
elem_uid[jelem * 2 + 1] = cell_gidx(jelem); |
| 957 |
} |
| 958 |
omp::sort(elem_uid.begin(), elem_uid.end()); |
| 959 |
} |
| 960 |
auto element_already_exists = [&elem_uid, &new_elem_uid](uid_t uid) -> bool { |
| 961 |
std::vector<uid_t>::iterator it = std::lower_bound(elem_uid.begin(), elem_uid.end(), uid); |
| 962 |
bool not_found = (it == elem_uid.end() || uid < *it); |
| 963 |
if (not_found) { |
| 964 |
bool inserted = new_elem_uid.insert(uid).second; |
| 965 |
return not inserted; |
| 966 |
} |
| 967 |
else { |
| 968 |
return true; |
| 969 |
} |
| 970 |
}; |
| 971 |
|
| 972 |
if (not status.new_periodic_ghost_cells.size()) { |
| 973 |
status.new_periodic_ghost_cells.resize(mesh.cells().nb_types()); |
| 974 |
} |
| 975 |
|
| 976 |
std::vector<std::vector<idx_t>> received_new_elems(mpi_size); |
| 977 |
for (idx_t jpart = 0; jpart < mpi_size; ++jpart) { |
| 978 |
received_new_elems[jpart].reserve(buf.elem_glb_idx[jpart].size()); |
| 979 |
} |
| 980 |
|
| 981 |
for (idx_t jpart = 0; jpart < mpi_size; ++jpart) { |
| 982 |
const idx_t nb_elems_from_part = static_cast<idx_t>(buf.elem_glb_idx[jpart].size()); |
| 983 |
for (idx_t e = 0; e < nb_elems_from_part; ++e) { |
| 984 |
if (element_already_exists(buf.elem_glb_idx[jpart][e]) == false) { |
| 985 |
received_new_elems[jpart].emplace_back(e); |
| 986 |
} |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
std::vector<std::vector<std::vector<int>>> elements_of_type(mesh.cells().nb_types(), |
| 991 |
std::vector<std::vector<int>>(mpi_size)); |
| 992 |
std::vector<idx_t> nb_elements_of_type(mesh.cells().nb_types(), 0); |
| 993 |
|
| 994 |
for (idx_t jpart = 0; jpart < mpi_size; ++jpart) { |
| 995 |
for (const idx_t ielem : received_new_elems[jpart]) { |
| 996 |
elements_of_type[buf.elem_type[jpart][ielem]][jpart].emplace_back(ielem); |
| 997 |
++nb_elements_of_type[buf.elem_type[jpart][ielem]]; |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
for (idx_t t = 0; t < mesh.cells().nb_types(); ++t) { |
| 1002 |
const std::vector<std::vector<int>>& elems = elements_of_type[t]; |
| 1003 |
mesh::Elements& elements = mesh.cells().elements(t); |
| 1004 |
|
| 1005 |
// Add new elements |
| 1006 |
BlockConnectivity& node_connectivity = elements.node_connectivity(); |
| 1007 |
if (nb_elements_of_type[t] == 0) { |
| 1008 |
continue; |
| 1009 |
} |
| 1010 |
|
| 1011 |
idx_t old_size = elements.size(); |
| 1012 |
idx_t new_elems_pos = elements.add(nb_elements_of_type[t]); |
| 1013 |
|
| 1014 |
auto elem_type_glb_idx = elements.view<gidx_t, 1>(mesh.cells().global_index()); |
| 1015 |
auto elem_type_part = elements.view<int, 1>(mesh.cells().partition()); |
| 1016 |
auto elem_type_ridx = elements.indexview<idx_t, 1>(mesh.cells().remote_index()); |
| 1017 |
auto elem_type_halo = elements.view<int, 1>(mesh.cells().halo()); |
| 1018 |
auto elem_type_flags = elements.view<int, 1>(mesh.cells().flags()); |
| 1019 |
|
| 1020 |
// Copy information in new elements |
| 1021 |
idx_t new_elem(0); |
| 1022 |
for (idx_t jpart = 0; jpart < mpi_size; ++jpart) { |
| 1023 |
for (const idx_t jelem : elems[jpart]) { |
| 1024 |
int loc_idx = new_elems_pos + new_elem; |
| 1025 |
elem_type_glb_idx(loc_idx) = std::abs(buf.elem_glb_idx[jpart][jelem]); |
| 1026 |
elem_type_part(loc_idx) = buf.elem_part[jpart][jelem]; |
| 1027 |
elem_type_ridx(loc_idx) = buf.elem_ridx[jpart][jelem]; |
| 1028 |
elem_type_halo(loc_idx) = halosize + 1; |
| 1029 |
elem_type_flags(loc_idx) = buf.elem_flags[jpart][jelem]; |
| 1030 |
for (idx_t n = 0; n < node_connectivity.cols(); ++n) { |
| 1031 |
node_connectivity.set( |
| 1032 |
loc_idx, n, uid2node[buf.elem_nodes_id[jpart][buf.elem_nodes_displs[jpart][jelem] + n]]); |
| 1033 |
} |
| 1034 |
|
| 1035 |
if (Topology::check(elem_type_flags(loc_idx), Topology::PERIODIC)) { |
| 1036 |
status.new_periodic_ghost_cells[t].push_back(old_size + new_elem); |
| 1037 |
} |
| 1038 |
++new_elem; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
std::vector<std::vector<idx_t>> gather_nb_elements() { |
| 1045 |
idx_t nb_types = mesh.cells().nb_types(); |
| 1046 |
idx_t mpi_size = mpi::comm().size(); |
| 1047 |
std::vector<idx_t> elems_per_type(nb_types); |
| 1048 |
for (idx_t t = 0; t < nb_types; ++t) { |
| 1049 |
elems_per_type[t] = mesh.cells().elements(t).size(); |
| 1050 |
} |
| 1051 |
atlas::mpi::Buffer<idx_t, 1> recv(mpi_size); |
| 1052 |
ATLAS_TRACE_MPI(ALLGATHER) { mpi::comm().allGatherv(elems_per_type.begin(), elems_per_type.end(), recv); } |
| 1053 |
|
| 1054 |
std::vector<std::vector<idx_t>> result(nb_types, std::vector<idx_t>(mpi_size)); |
| 1055 |
for (idx_t p = 0; p < mpi_size; ++p) { |
| 1056 |
auto recv_p = recv[p]; |
| 1057 |
for (idx_t t = 0; t < nb_types; ++t) { |
| 1058 |
result[t][p] = recv_p[t]; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
return result; |
| 1062 |
}; |
| 1063 |
|
| 1064 |
|
| 1065 |
void add_buffers(Buffers& buf) { |
| 1066 |
add_nodes(buf); |
| 1067 |
|
| 1068 |
auto nb_elements_pre = gather_nb_elements(); |
| 1069 |
|
| 1070 |
add_elements(buf); |
| 1071 |
|
| 1072 |
auto nb_elements_post = gather_nb_elements(); |
| 1073 |
|
| 1074 |
// Renumber remote_index as the number of elements per type might have grown |
| 1075 |
{ |
| 1076 |
auto nb_partitions = mpi::comm().size(); |
| 1077 |
auto nb_types = mesh.cells().nb_types(); |
| 1078 |
|
| 1079 |
std::vector<std::vector<idx_t>> accumulated_diff(nb_types, std::vector<idx_t>(nb_partitions)); |
| 1080 |
for (idx_t t = 1; t < nb_types; ++t) { |
| 1081 |
for (idx_t p = 0; p < nb_partitions; ++p) { |
| 1082 |
accumulated_diff[t][p] = |
| 1083 |
accumulated_diff[t - 1][p] + nb_elements_post[t - 1][p] - nb_elements_pre[t - 1][p]; |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
for (idx_t t = 0; t < nb_types; ++t) { |
| 1088 |
auto& elements = mesh.cells().elements(t); |
| 1089 |
auto partition = elements.view<int, 1>(mesh.cells().partition()); |
| 1090 |
auto ridx = elements.indexview<idx_t, 1>(mesh.cells().remote_index()); |
| 1091 |
|
| 1092 |
auto& accumulated_diff_t = accumulated_diff[t]; |
| 1093 |
|
| 1094 |
for (idx_t elem = 0; elem < elements.size(); ++elem) { |
| 1095 |
ridx(elem) += accumulated_diff_t[partition(elem)]; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
update(); |
| 1101 |
} |
| 1102 |
}; |
| 1103 |
|
| 1104 |
namespace { |
| 1105 |
void gather_bdry_nodes(const BuildHaloHelper& helper, const std::vector<uid_t>& send, |
| 1106 |
atlas::mpi::Buffer<uid_t, 1>& recv, bool periodic = false) { |
| 1107 |
auto& comm = mpi::comm(); |
| 1108 |
#ifndef ATLAS_103 |
| 1109 |
/* deprecated */ |
| 1110 |
ATLAS_TRACE("gather_bdry_nodes old way"); |
| 1111 |
{ |
| 1112 |
ATLAS_TRACE_MPI(ALLGATHER) { comm.allGatherv(send.begin(), send.end(), recv); } |
| 1113 |
} |
| 1114 |
#else |
| 1115 |
ATLAS_TRACE(); |
| 1116 |
|
| 1117 |
Mesh::PartitionGraph::Neighbours neighbours = helper.mesh.nearestNeighbourPartitions(); |
| 1118 |
if (periodic) { |
| 1119 |
// add own rank to neighbours to allow periodicity with self (pole caps) |
| 1120 |
idx_t rank = comm.rank(); |
| 1121 |
neighbours.insert(std::upper_bound(neighbours.begin(), neighbours.end(), rank), rank); |
| 1122 |
} |
| 1123 |
|
| 1124 |
const idx_t mpi_size = comm.size(); |
| 1125 |
const int counts_tag = 0; |
| 1126 |
const int buffer_tag = 1; |
| 1127 |
|
| 1128 |
std::vector<eckit::mpi::Request> counts_requests; |
| 1129 |
counts_requests.reserve(neighbours.size()); |
| 1130 |
std::vector<eckit::mpi::Request> buffer_requests; |
| 1131 |
buffer_requests.reserve(neighbours.size()); |
| 1132 |
|
| 1133 |
int sendcnt = send.size(); |
| 1134 |
ATLAS_TRACE_MPI(ISEND) { |
| 1135 |
for (idx_t to : neighbours) { |
| 1136 |
counts_requests.push_back(comm.iSend(sendcnt, to, counts_tag)); |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
recv.counts.assign(0, mpi_size); |
| 1141 |
|
| 1142 |
ATLAS_TRACE_MPI(IRECEIVE) { |
| 1143 |
for (idx_t from : neighbours) { |
| 1144 |
counts_requests.push_back(comm.iReceive(recv.counts[from], from, counts_tag)); |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
ATLAS_TRACE_MPI(ISEND) { |
| 1149 |
for (idx_t to : neighbours) { |
| 1150 |
buffer_requests.push_back(comm.iSend(send.data(), send.size(), to, buffer_tag)); |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
ATLAS_TRACE_MPI(WAIT) { |
| 1155 |
for (auto request : counts_requests) { |
| 1156 |
comm.wait(request); |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
recv.displs[0] = 0; |
| 1161 |
recv.cnt = recv.counts[0]; |
| 1162 |
for (idx_t jpart = 1; jpart < mpi_size; ++jpart) { |
| 1163 |
recv.displs[jpart] = recv.displs[jpart - 1] + recv.counts[jpart - 1]; |
| 1164 |
recv.cnt += recv.counts[jpart]; |
| 1165 |
} |
| 1166 |
recv.buffer.resize(recv.cnt); |
| 1167 |
|
| 1168 |
ATLAS_TRACE_MPI(IRECEIVE) { |
| 1169 |
for (idx_t from : neighbours) { |
| 1170 |
buffer_requests.push_back( |
| 1171 |
comm.iReceive(recv.buffer.data() + recv.displs[from], recv.counts[from], from, buffer_tag)); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
ATLAS_TRACE_MPI(WAIT) { |
| 1176 |
for (auto request : buffer_requests) { |
| 1177 |
comm.wait(request); |
| 1178 |
} |
| 1179 |
} |
| 1180 |
#endif |
| 1181 |
} |
| 1182 |
} // namespace |
| 1183 |
|
| 1184 |
void increase_halo_interior(BuildHaloHelper& helper) { |
| 1185 |
helper.update(); |
| 1186 |
if (helper.node_to_elem.size() == 0) { |
| 1187 |
build_lookup_node2elem(helper.mesh, helper.node_to_elem); |
| 1188 |
} |
| 1189 |
|
| 1190 |
if (helper.uid2node.size() == 0) { |
| 1191 |
build_lookup_uid2node(helper.mesh, helper.uid2node); |
| 1192 |
} |
| 1193 |
|
| 1194 |
// All buffers needed to move elements and nodes |
| 1195 |
BuildHaloHelper::Buffers sendmesh(helper.mesh); |
| 1196 |
BuildHaloHelper::Buffers recvmesh(helper.mesh); |
| 1197 |
|
| 1198 |
// 1) Find boundary nodes of this partition: |
| 1199 |
|
| 1200 |
accumulate_partition_bdry_nodes(helper.mesh, helper.halosize, helper.bdry_nodes); |
| 1201 |
const std::vector<idx_t>& bdry_nodes = helper.bdry_nodes; |
| 1202 |
const idx_t nb_bdry_nodes = static_cast<idx_t>(bdry_nodes.size()); |
| 1203 |
|
| 1204 |
// 2) Communicate uid of these boundary nodes to other partitions |
| 1205 |
|
| 1206 |
std::vector<uid_t> send_bdry_nodes_uid(bdry_nodes.size()); |
| 1207 |
for (idx_t jnode = 0; jnode < nb_bdry_nodes; ++jnode) { |
| 1208 |
send_bdry_nodes_uid[jnode] = helper.compute_uid(bdry_nodes[jnode]); |
| 1209 |
} |
| 1210 |
|
| 1211 |
idx_t mpi_size = mpi::size(); |
| 1212 |
atlas::mpi::Buffer<uid_t, 1> recv_bdry_nodes_uid_from_parts(mpi_size); |
| 1213 |
|
| 1214 |
gather_bdry_nodes(helper, send_bdry_nodes_uid, recv_bdry_nodes_uid_from_parts); |
| 1215 |
|
| 1216 |
{ |
| 1217 |
runtime::trace::Barriers set_barriers(false); |
| 1218 |
runtime::trace::Logging set_logging(false); |
| 1219 |
#ifndef ATLAS_103 |
| 1220 |
/* deprecated */ |
| 1221 |
atlas_omp_parallel_for (idx_t jpart = 0; jpart < mpi_size; ++jpart) |
| 1222 |
#else |
| 1223 |
const Mesh::PartitionGraph::Neighbours neighbours = helper.mesh.nearestNeighbourPartitions(); |
| 1224 |
for (idx_t jpart : neighbours) |
| 1225 |
#endif |
| 1226 |
{ |
| 1227 |
// 3) Find elements and nodes completing these elements in |
| 1228 |
// other tasks that have my nodes through its UID |
| 1229 |
|
| 1230 |
mpi::BufferView<uid_t> recv_bdry_nodes_uid = recv_bdry_nodes_uid_from_parts[jpart]; |
| 1231 |
|
| 1232 |
std::vector<idx_t> found_bdry_elems; |
| 1233 |
std::set<uid_t> found_bdry_nodes_uid; |
| 1234 |
|
| 1235 |
accumulate_elements(helper.mesh, recv_bdry_nodes_uid, helper.uid2node, helper.node_to_elem, found_bdry_elems, |
| 1236 |
found_bdry_nodes_uid); |
| 1237 |
|
| 1238 |
// 4) Fill node and element buffers to send back |
| 1239 |
helper.fill_sendbuffer(sendmesh, found_bdry_nodes_uid, found_bdry_elems, jpart); |
| 1240 |
} |
| 1241 |
} |
| 1242 |
|
| 1243 |
// 5) Now communicate all buffers |
| 1244 |
helper.all_to_all(sendmesh, recvmesh); |
| 1245 |
|
| 1246 |
// 6) Adapt mesh |
| 1247 |
#ifdef DEBUG_OUTPUT |
| 1248 |
Log::debug() << "increase_halo_interior -- recv: \n" << recvmesh << std::endl; |
| 1249 |
#endif |
| 1250 |
helper.add_buffers(recvmesh); |
| 1251 |
} |
| 1252 |
|
| 1253 |
class PeriodicPoints { |
| 1254 |
public: |
| 1255 |
PeriodicPoints(Mesh& mesh, int flag, idx_t N): flags_(array::make_view<int, 1>(mesh.nodes().flags())) { |
| 1256 |
flag_ = flag; |
| 1257 |
N_ = N; |
| 1258 |
} |
| 1259 |
|
| 1260 |
bool operator()(int j) const { |
| 1261 |
if (j >= N_) { |
| 1262 |
return false; |
| 1263 |
} |
| 1264 |
if (Topology::check(flags_(j), flag_)) { |
| 1265 |
return true; |
| 1266 |
} |
| 1267 |
return false; |
| 1268 |
} |
| 1269 |
|
| 1270 |
private: |
| 1271 |
int N_; |
| 1272 |
int flag_; |
| 1273 |
array::ArrayView<int, 1> flags_; |
| 1274 |
|
| 1275 |
friend std::ostream& operator<<(std::ostream& os, const PeriodicPoints& periodic_points) { |
| 1276 |
os << "["; |
| 1277 |
for (idx_t j = 0; j < periodic_points.flags_.shape(0); ++j) { |
| 1278 |
if (periodic_points(j)) { |
| 1279 |
os << " " << j + 1; |
| 1280 |
} |
| 1281 |
} |
| 1282 |
os << " ]"; |
| 1283 |
return os; |
| 1284 |
} |
| 1285 |
}; |
| 1286 |
|
| 1287 |
void increase_halo_periodic(BuildHaloHelper& helper, const PeriodicPoints& periodic_points, |
| 1288 |
const util::PeriodicTransform& transform, int newflags) { |
| 1289 |
helper.update(); |
| 1290 |
// if (helper.node_to_elem.size() == 0 ) !!! NOT ALLOWED !!! (atlas_test_halo |
| 1291 |
// will fail) |
| 1292 |
build_lookup_node2elem(helper.mesh, helper.node_to_elem); |
| 1293 |
|
| 1294 |
// if( helper.uid2node.size() == 0 ) !!! NOT ALLOWED !!! (atlas_test_halo will |
| 1295 |
// fail) |
| 1296 |
build_lookup_uid2node(helper.mesh, helper.uid2node); |
| 1297 |
|
| 1298 |
// All buffers needed to move elements and nodes |
| 1299 |
BuildHaloHelper::Buffers sendmesh(helper.mesh); |
| 1300 |
BuildHaloHelper::Buffers recvmesh(helper.mesh); |
| 1301 |
|
| 1302 |
// 1) Find boundary nodes of this partition: |
| 1303 |
|
| 1304 |
if (!helper.bdry_nodes.size()) { |
| 1305 |
accumulate_partition_bdry_nodes(helper.mesh, helper.halosize, helper.bdry_nodes); |
| 1306 |
} |
| 1307 |
|
| 1308 |
std::vector<idx_t> bdry_nodes = filter_nodes(helper.bdry_nodes, periodic_points); |
| 1309 |
const idx_t nb_bdry_nodes = static_cast<idx_t>(bdry_nodes.size()); |
| 1310 |
|
| 1311 |
// 2) Compute transformed uid of these boundary nodes and send to other |
| 1312 |
// partitions |
| 1313 |
|
| 1314 |
std::vector<uid_t> send_bdry_nodes_uid(nb_bdry_nodes); |
| 1315 |
for (idx_t jnode = 0; jnode < nb_bdry_nodes; ++jnode) { |
| 1316 |
double crd[] = {helper.xy(bdry_nodes[jnode], XX), helper.xy(bdry_nodes[jnode], YY)}; |
| 1317 |
transform(crd, +1); |
| 1318 |
// Log::info() << " crd " << crd[0] << " " << crd[1] << " uid " << |
| 1319 |
// util::unique_lonlat(crd) << std::endl; |
| 1320 |
send_bdry_nodes_uid[jnode] = util::unique_lonlat(crd); |
| 1321 |
} |
| 1322 |
|
| 1323 |
idx_t mpi_size = mpi::size(); |
| 1324 |
atlas::mpi::Buffer<uid_t, 1> recv_bdry_nodes_uid_from_parts(mpi_size); |
| 1325 |
|
| 1326 |
gather_bdry_nodes(helper, send_bdry_nodes_uid, recv_bdry_nodes_uid_from_parts, |
| 1327 |
/* periodic = */ true); |
| 1328 |
|
| 1329 |
{ |
| 1330 |
runtime::trace::Barriers set_barriers(false); |
| 1331 |
runtime::trace::Logging set_logging(false); |
| 1332 |
#ifndef ATLAS_103 |
| 1333 |
/* deprecated */ |
| 1334 |
atlas_omp_parallel_for (idx_t jpart = 0; jpart < mpi_size; ++jpart) |
| 1335 |
#else |
| 1336 |
Mesh::PartitionGraph::Neighbours neighbours = helper.mesh.nearestNeighbourPartitions(); |
| 1337 |
// add own rank to neighbours to allow periodicity with self (pole caps) |
| 1338 |
idx_t rank = mpi::rank(); |
| 1339 |
neighbours.insert(std::upper_bound(neighbours.begin(), neighbours.end(), rank), rank); |
| 1340 |
for (idx_t jpart : neighbours) |
| 1341 |
#endif |
| 1342 |
{ |
| 1343 |
|
| 1344 |
// 3) Find elements and nodes completing these elements in |
| 1345 |
// other tasks that have my nodes through its UID |
| 1346 |
|
| 1347 |
atlas::mpi::BufferView<uid_t> recv_bdry_nodes_uid = recv_bdry_nodes_uid_from_parts[jpart]; |
| 1348 |
|
| 1349 |
std::vector<idx_t> found_bdry_elems; |
| 1350 |
std::set<uid_t> found_bdry_nodes_uid; |
| 1351 |
|
| 1352 |
accumulate_elements(helper.mesh, recv_bdry_nodes_uid, helper.uid2node, helper.node_to_elem, found_bdry_elems, |
| 1353 |
found_bdry_nodes_uid); |
| 1354 |
|
| 1355 |
// 4) Fill node and element buffers to send back |
| 1356 |
helper.fill_sendbuffer(sendmesh, found_bdry_nodes_uid, found_bdry_elems, transform, newflags, jpart); |
| 1357 |
} |
| 1358 |
} |
| 1359 |
|
| 1360 |
// 5) Now communicate all buffers |
| 1361 |
helper.all_to_all(sendmesh, recvmesh); |
| 1362 |
|
| 1363 |
// 6) Adapt mesh |
| 1364 |
#ifdef DEBUG_OUTPUT |
| 1365 |
Log::debug() << "increase_halo_periodic -- recv: \n" << recvmesh << std::endl; |
| 1366 |
#endif |
| 1367 |
helper.add_buffers(recvmesh); |
| 1368 |
} |
| 1369 |
|
| 1370 |
BuildHalo::BuildHalo(Mesh& mesh): mesh_(mesh), periodic_cells_local_index_(mesh.cells().nb_types()) {} |
| 1371 |
|
| 1372 |
void BuildHalo::operator()(int nb_elems) { |
| 1373 |
ATLAS_TRACE("BuildHalo"); |
| 1374 |
|
| 1375 |
mpi::Scope mpi_scope(mesh_.mpi_comm()); |
| 1376 |
|
| 1377 |
int halo = 0; |
| 1378 |
mesh_.metadata().get("halo", halo); |
| 1379 |
|
| 1380 |
// Locked halos must be set at mesh generation. |
| 1381 |
bool haloLocked = false; |
| 1382 |
if (mesh_.metadata().get("halo_locked", haloLocked)) { |
| 1383 |
if (haloLocked && halo < nb_elems) { |
| 1384 |
const auto errMsg = |
| 1385 |
"Error: Halo size locked. Please set MeshGenerator " |
| 1386 |
"parameter \"halo\" to at least " + |
| 1387 |
std::to_string(nb_elems) + |
| 1388 |
".\n" |
| 1389 |
"Current halo size is " + |
| 1390 |
std::to_string(halo) + ".\n"; |
| 1391 |
throw_Exception(errMsg, Here()); |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
if (halo == nb_elems) { |
| 1396 |
return; |
| 1397 |
} |
| 1398 |
|
| 1399 |
ATLAS_TRACE("Increasing mesh halo"); |
| 1400 |
|
| 1401 |
for (int jhalo = halo; jhalo < nb_elems; ++jhalo) { |
| 1402 |
Log::debug() << "Increase halo " << jhalo + 1 << std::endl; |
| 1403 |
idx_t nb_nodes_before_halo_increase = mesh_.nodes().size(); |
| 1404 |
|
| 1405 |
BuildHaloHelper helper(*this, mesh_); |
| 1406 |
|
| 1407 |
ATLAS_TRACE_SCOPE("increase_halo_interior") { increase_halo_interior(helper); } |
| 1408 |
|
| 1409 |
PeriodicPoints westpts(mesh_, Topology::PERIODIC | Topology::WEST, nb_nodes_before_halo_increase); |
| 1410 |
|
| 1411 |
#ifdef DEBUG_OUTPUT |
| 1412 |
Log::debug() << " periodic west : " << westpts << std::endl; |
| 1413 |
#endif |
| 1414 |
ATLAS_TRACE_SCOPE("increase_halo_periodic West") { |
| 1415 |
increase_halo_periodic(helper, westpts, WestEast(), Topology::PERIODIC | Topology::WEST | Topology::GHOST); |
| 1416 |
} |
| 1417 |
|
| 1418 |
PeriodicPoints eastpts(mesh_, Topology::PERIODIC | Topology::EAST, nb_nodes_before_halo_increase); |
| 1419 |
|
| 1420 |
#ifdef DEBUG_OUTPUT |
| 1421 |
Log::debug() << " periodic east : " << eastpts << std::endl; |
| 1422 |
#endif |
| 1423 |
ATLAS_TRACE_SCOPE("increase_halo_periodic East") { |
| 1424 |
increase_halo_periodic(helper, eastpts, EastWest(), Topology::PERIODIC | Topology::EAST | Topology::GHOST); |
| 1425 |
} |
| 1426 |
|
| 1427 |
for (idx_t p : helper.status.new_periodic_ghost_points) { |
| 1428 |
periodic_points_local_index_.push_back(p); |
| 1429 |
} |
| 1430 |
for (int t = 0; t < mesh_.cells().nb_types(); ++t) { |
| 1431 |
for (idx_t p : helper.status.new_periodic_ghost_cells[t]) { |
| 1432 |
periodic_cells_local_index_[t].push_back(p); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
{ |
| 1437 |
std::stringstream ss; |
| 1438 |
ss << "nb_nodes_including_halo[" << jhalo + 1 << "]"; |
| 1439 |
mesh_.metadata().set(ss.str(), mesh_.nodes().size()); |
| 1440 |
} |
| 1441 |
|
| 1442 |
for (int t = 0; t < mesh_.cells().nb_types(); ++t) { |
| 1443 |
std::stringstream ss; |
| 1444 |
ss << "nb_cells_including_halo[" << t << "][" << jhalo + 1 << "]"; |
| 1445 |
mesh_.metadata().set(ss.str(), mesh_.cells().elements(t).size()); |
| 1446 |
} |
| 1447 |
mesh_.metadata().set("halo", jhalo + 1); |
| 1448 |
mesh_.nodes().global_index().metadata().set("human_readable", false); |
| 1449 |
mesh_.cells().global_index().metadata().set("human_readable", false); |
| 1450 |
|
| 1451 |
#ifdef DEBUG_OUTPUT |
| 1452 |
output::Gmsh gmsh2d("build-halo-mesh2d.msh", util::Config("ghost", true)("coordinates", "xy")); |
| 1453 |
output::Gmsh gmsh3d("build-halo-mesh3d.msh", util::Config("ghost", true)("coordinates", "xyz")); |
| 1454 |
renumber_nodes_glb_idx(mesh_.nodes()); |
| 1455 |
BuildXYZField("xyz", true)(mesh_.nodes()); |
| 1456 |
mesh_.metadata().set("halo", jhalo + 1); |
| 1457 |
gmsh2d.write(mesh_); |
| 1458 |
gmsh3d.write(mesh_); |
| 1459 |
#endif |
| 1460 |
} |
| 1461 |
|
| 1462 |
make_nodes_global_index_human_readable(*this, mesh_.nodes(), |
| 1463 |
/*do_all*/ false); |
| 1464 |
|
| 1465 |
make_cells_global_index_human_readable(*this, mesh_.cells(), |
| 1466 |
/*do_all*/ false); |
| 1467 |
// renumber_nodes_glb_idx (mesh_.nodes()); |
| 1468 |
} |
| 1469 |
|
| 1470 |
// ------------------------------------------------------------------ |
| 1471 |
// C wrapper interfaces to C++ routines |
| 1472 |
|
| 1473 |
void atlas__build_halo(Mesh::Implementation* mesh, int nb_elems) { |
| 1474 |
ATLAS_ASSERT(mesh != nullptr, "Cannot access uninitialised atlas_Mesh"); |
| 1475 |
Mesh m(mesh); |
| 1476 |
build_halo(m, nb_elems); |
| 1477 |
} |
| 1478 |
|
| 1479 |
// ------------------------------------------------------------------ |
| 1480 |
|
| 1481 |
} // namespace actions |
| 1482 |
} // namespace mesh |
| 1483 |
} // namespace atlas |