| 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 <cstdarg> |
| 12 |
//#include <functional> |
| 13 |
|
| 14 |
#include "eckit/utils/MD5.h" |
| 15 |
|
| 16 |
#include "atlas/array.h" |
| 17 |
#include "atlas/field/Field.h" |
| 18 |
#include "atlas/field/FieldSet.h" |
| 19 |
#include "atlas/functionspace/NodeColumns.h" |
| 20 |
#include "atlas/grid/Grid.h" |
| 21 |
#include "atlas/grid/UnstructuredGrid.h" |
| 22 |
#include "atlas/library/config.h" |
| 23 |
#include "atlas/mesh/IsGhostNode.h" |
| 24 |
#include "atlas/mesh/Mesh.h" |
| 25 |
#include "atlas/mesh/Nodes.h" |
| 26 |
#include "atlas/mesh/actions/BuildHalo.h" |
| 27 |
#include "atlas/mesh/actions/BuildParallelFields.h" |
| 28 |
#include "atlas/mesh/actions/BuildPeriodicBoundaries.h" |
| 29 |
#include "atlas/parallel/Checksum.h" |
| 30 |
#include "atlas/parallel/GatherScatter.h" |
| 31 |
#include "atlas/parallel/HaloExchange.h" |
| 32 |
#include "atlas/parallel/omp/omp.h" |
| 33 |
#include "atlas/runtime/Exception.h" |
| 34 |
#include "atlas/runtime/Trace.h" |
| 35 |
#include "atlas/util/detail/Cache.h" |
| 36 |
|
| 37 |
#if ATLAS_HAVE_FORTRAN |
| 38 |
#define REMOTE_IDX_BASE 1 |
| 39 |
#else |
| 40 |
#define REMOTE_IDX_BASE 0 |
| 41 |
#endif |
| 42 |
|
| 43 |
namespace atlas { |
| 44 |
namespace functionspace { |
| 45 |
namespace detail { |
| 46 |
|
| 47 |
namespace { |
| 48 |
|
| 49 |
template <typename T, typename Field> |
| 50 |
array::LocalView<T, 3> make_leveled_view(Field& field) { |
| 51 |
using namespace array; |
| 52 |
if (field.levels()) { |
| 53 |
if (field.variables()) { |
| 54 |
return make_view<T, 3>(field).slice(Range::all(), Range::all(), Range::all()); |
| 55 |
} |
| 56 |
else { |
| 57 |
return make_view<T, 2>(field).slice(Range::all(), Range::all(), Range::dummy()); |
| 58 |
} |
| 59 |
} |
| 60 |
else { |
| 61 |
if (field.variables()) { |
| 62 |
return make_view<T, 2>(field).slice(Range::all(), Range::dummy(), Range::all()); |
| 63 |
} |
| 64 |
else { |
| 65 |
return make_view<T, 1>(field).slice(Range::all(), Range::dummy(), Range::dummy()); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
} // namespace |
| 71 |
|
| 72 |
class NodeColumnsHaloExchangeCache : public util::Cache<std::string, parallel::HaloExchange>, |
| 73 |
public mesh::detail::MeshObserver { |
| 74 |
private: |
| 75 |
using Base = util::Cache<std::string, parallel::HaloExchange>; |
| 76 |
NodeColumnsHaloExchangeCache(): Base("NodeColumnsHaloExchangeCache") {} |
| 77 |
|
| 78 |
public: |
| 79 |
static NodeColumnsHaloExchangeCache& instance() { |
| 80 |
static NodeColumnsHaloExchangeCache inst; |
| 81 |
return inst; |
| 82 |
} |
| 83 |
util::ObjectHandle<value_type> get_or_create(const Mesh& mesh, long halo) { |
| 84 |
registerMesh(*mesh.get()); |
| 85 |
creator_type creator = std::bind(&NodeColumnsHaloExchangeCache::create, mesh, halo); |
| 86 |
return Base::get_or_create(key(*mesh.get(), halo), creator); |
| 87 |
} |
| 88 |
void onMeshDestruction(mesh::detail::MeshImpl& mesh) override { |
| 89 |
for (long jhalo = 0; jhalo <= mesh::Halo(mesh).size(); ++jhalo) { |
| 90 |
remove(key(mesh, jhalo)); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private: |
| 95 |
static Base::key_type key(const mesh::detail::MeshImpl& mesh, long halo) { |
| 96 |
std::ostringstream key; |
| 97 |
key << "mesh[address=" << &mesh << "],halo[size=" << halo << "]"; |
| 98 |
return key.str(); |
| 99 |
} |
| 100 |
|
| 101 |
static value_type* create(const Mesh& mesh, long halo) { |
| 102 |
value_type* value = new value_type(); |
| 103 |
|
| 104 |
std::ostringstream ss; |
| 105 |
ss << "nb_nodes_including_halo[" << halo << "]"; |
| 106 |
idx_t nb_nodes(mesh.nodes().size()); |
| 107 |
mesh.metadata().get(ss.str(), nb_nodes); |
| 108 |
|
| 109 |
value->setup(mesh.mpi_comm(), array::make_view<int, 1>(mesh.nodes().partition()).data(), |
| 110 |
array::make_view<idx_t, 1>(mesh.nodes().remote_index()).data(), REMOTE_IDX_BASE, nb_nodes); |
| 111 |
|
| 112 |
return value; |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
class NodeColumnsGatherScatterCache : public util::Cache<std::string, parallel::GatherScatter>, |
| 117 |
public mesh::detail::MeshObserver { |
| 118 |
private: |
| 119 |
using Base = util::Cache<std::string, parallel::GatherScatter>; |
| 120 |
NodeColumnsGatherScatterCache(): Base("NodeColumnsGatherScatterCache") {} |
| 121 |
|
| 122 |
public: |
| 123 |
static NodeColumnsGatherScatterCache& instance() { |
| 124 |
static NodeColumnsGatherScatterCache inst; |
| 125 |
return inst; |
| 126 |
} |
| 127 |
util::ObjectHandle<value_type> get_or_create(const Mesh& mesh) { |
| 128 |
registerMesh(*mesh.get()); |
| 129 |
creator_type creator = std::bind(&NodeColumnsGatherScatterCache::create, mesh); |
| 130 |
return Base::get_or_create(key(*mesh.get()), creator); |
| 131 |
} |
| 132 |
void onMeshDestruction(mesh::detail::MeshImpl& mesh) override { remove(key(mesh)); } |
| 133 |
|
| 134 |
private: |
| 135 |
static Base::key_type key(const mesh::detail::MeshImpl& mesh) { |
| 136 |
std::ostringstream key; |
| 137 |
key << "mesh[address=" << &mesh << "]"; |
| 138 |
return key.str(); |
| 139 |
} |
| 140 |
|
| 141 |
static value_type* create(const Mesh& mesh) { |
| 142 |
value_type* value = new value_type(); |
| 143 |
|
| 144 |
mesh::IsGhostNode is_ghost(mesh.nodes()); |
| 145 |
std::vector<int> mask(mesh.nodes().size()); |
| 146 |
const idx_t npts = mask.size(); |
| 147 |
atlas_omp_parallel_for(idx_t n = 0; n < npts; ++n) { |
| 148 |
mask[n] = is_ghost(n) ? 1 : 0; |
| 149 |
|
| 150 |
// --> This would add periodic west-bc to the gather, but means that |
| 151 |
// global-sums, means, etc are computed wrong |
| 152 |
// if( mask[j] == 1 && |
| 153 |
// internals::Topology::check(flags(j),internals::Topology::BC) ) { |
| 154 |
// mask[j] = 0; |
| 155 |
//} |
| 156 |
} |
| 157 |
|
| 158 |
value->setup(mesh.mpi_comm(), |
| 159 |
array::make_view<int, 1>(mesh.nodes().partition()).data(), |
| 160 |
array::make_view<idx_t, 1>(mesh.nodes().remote_index()).data(), REMOTE_IDX_BASE, |
| 161 |
array::make_view<gidx_t, 1>(mesh.nodes().global_index()).data(), mask.data(), mesh.nodes().size()); |
| 162 |
return value; |
| 163 |
} |
| 164 |
}; |
| 165 |
|
| 166 |
class NodeColumnsChecksumCache : public util::Cache<std::string, parallel::Checksum>, |
| 167 |
public mesh::detail::MeshObserver { |
| 168 |
private: |
| 169 |
using Base = util::Cache<std::string, parallel::Checksum>; |
| 170 |
NodeColumnsChecksumCache(): Base("NodeColumnsChecksumCache") {} |
| 171 |
|
| 172 |
public: |
| 173 |
static NodeColumnsChecksumCache& instance() { |
| 174 |
static NodeColumnsChecksumCache inst; |
| 175 |
return inst; |
| 176 |
} |
| 177 |
util::ObjectHandle<value_type> get_or_create(const Mesh& mesh) { |
| 178 |
registerMesh(*mesh.get()); |
| 179 |
creator_type creator = std::bind(&NodeColumnsChecksumCache::create, mesh); |
| 180 |
return Base::get_or_create(key(*mesh.get()), creator); |
| 181 |
} |
| 182 |
void onMeshDestruction(mesh::detail::MeshImpl& mesh) override { remove(key(mesh)); } |
| 183 |
|
| 184 |
private: |
| 185 |
static Base::key_type key(const mesh::detail::MeshImpl& mesh) { |
| 186 |
std::ostringstream key; |
| 187 |
key << "mesh[address=" << &mesh << "]"; |
| 188 |
return key.str(); |
| 189 |
} |
| 190 |
|
| 191 |
static value_type* create(const Mesh& mesh) { |
| 192 |
value_type* value = new value_type(); |
| 193 |
util::ObjectHandle<parallel::GatherScatter> gather( |
| 194 |
NodeColumnsGatherScatterCache::instance().get_or_create(mesh)); |
| 195 |
value->setup(gather); |
| 196 |
return value; |
| 197 |
} |
| 198 |
}; |
| 199 |
|
| 200 |
NodeColumns::NodeColumns(Mesh mesh): NodeColumns(mesh, util::NoConfig()) {} |
| 201 |
|
| 202 |
NodeColumns::NodeColumns(Mesh mesh, const eckit::Configuration& config): |
| 203 |
mesh_(mesh), nodes_(mesh_.nodes()), nb_levels_(config.getInt("levels", 0)), nb_nodes_(0) { |
| 204 |
ATLAS_TRACE(); |
| 205 |
if (config.has("halo")) { |
| 206 |
halo_ = mesh::Halo(config.getInt("halo")); |
| 207 |
} |
| 208 |
else { |
| 209 |
halo_ = mesh::Halo(mesh); |
| 210 |
} |
| 211 |
mesh::actions::build_nodes_parallel_fields(mesh_); |
| 212 |
mesh::actions::build_periodic_boundaries(mesh_); |
| 213 |
|
| 214 |
if (halo_.size() > 0) { |
| 215 |
mesh::actions::build_halo(mesh_, halo_.size()); |
| 216 |
std::stringstream ss; |
| 217 |
ss << "nb_nodes_including_halo[" << halo_.size() << "]"; |
| 218 |
mesh_.metadata().get(ss.str(), nb_nodes_); |
| 219 |
} |
| 220 |
if (!nb_nodes_) { |
| 221 |
std::stringstream ss; |
| 222 |
ss << "nb_nodes_including_halo[" << halo_.size() << "]"; |
| 223 |
if (!mesh_.metadata().get(ss.str(), nb_nodes_)) { |
| 224 |
nb_nodes_ = mesh_.nodes().size(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (mesh_.grid()) { |
| 229 |
grid_ = mesh_.grid(); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
NodeColumns::~NodeColumns() = default; |
| 234 |
|
| 235 |
std::string NodeColumns::distribution() const { |
| 236 |
return mesh().metadata().getString("distribution"); |
| 237 |
} |
| 238 |
|
| 239 |
idx_t NodeColumns::nb_nodes() const { |
| 240 |
return nb_nodes_; |
| 241 |
} |
| 242 |
|
| 243 |
idx_t NodeColumns::nb_nodes_global() const { |
| 244 |
if (nb_nodes_global_ >= 0) { |
| 245 |
return nb_nodes_global_; |
| 246 |
} |
| 247 |
if (Grid grid = mesh().grid()) { |
| 248 |
nb_nodes_global_ = grid.size(); |
| 249 |
} |
| 250 |
else { |
| 251 |
nb_nodes_global_ = gather().glb_dof(); |
| 252 |
} |
| 253 |
return nb_nodes_global_; |
| 254 |
} |
| 255 |
|
| 256 |
idx_t NodeColumns::config_nb_nodes(const eckit::Configuration& config) const { |
| 257 |
idx_t size = nb_nodes(); |
| 258 |
bool global(false); |
| 259 |
if (config.get("global", global)) { |
| 260 |
if (global) { |
| 261 |
idx_t owner(0); |
| 262 |
config.get("owner", owner); |
| 263 |
idx_t _nb_nodes_global = nb_nodes_global(); |
| 264 |
idx_t rank = mpi::comm(mpi_comm()).rank(); |
| 265 |
size = (rank == owner ? _nb_nodes_global : 0); |
| 266 |
} |
| 267 |
} |
| 268 |
return size; |
| 269 |
} |
| 270 |
|
| 271 |
void NodeColumns::set_field_metadata(const eckit::Configuration& config, Field& field) const { |
| 272 |
field.set_functionspace(this); |
| 273 |
|
| 274 |
bool global(false); |
| 275 |
if (config.get("global", global)) { |
| 276 |
if (global) { |
| 277 |
idx_t owner(0); |
| 278 |
config.get("owner", owner); |
| 279 |
field.metadata().set("owner", owner); |
| 280 |
} |
| 281 |
} |
| 282 |
field.metadata().set("global", global); |
| 283 |
|
| 284 |
idx_t levels(nb_levels_); |
| 285 |
config.get("levels", levels); |
| 286 |
field.set_levels(levels); |
| 287 |
|
| 288 |
idx_t variables(0); |
| 289 |
config.get("variables", variables); |
| 290 |
field.set_variables(variables); |
| 291 |
|
| 292 |
if (config.has("type")) { |
| 293 |
field.metadata().set("type", config.getString("type")); |
| 294 |
} |
| 295 |
|
| 296 |
if (config.has("vector_component")) { |
| 297 |
field.metadata().set("vector_component", config.getSubConfiguration("vector_component")); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
array::DataType NodeColumns::config_datatype(const eckit::Configuration& config) const { |
| 302 |
array::DataType::kind_t kind; |
| 303 |
if (!config.get("datatype", kind)) { |
| 304 |
throw_Exception("datatype missing", Here()); |
| 305 |
} |
| 306 |
return array::DataType(kind); |
| 307 |
} |
| 308 |
|
| 309 |
std::string NodeColumns::config_name(const eckit::Configuration& config) const { |
| 310 |
std::string name; |
| 311 |
config.get("name", name); |
| 312 |
return name; |
| 313 |
} |
| 314 |
|
| 315 |
idx_t NodeColumns::config_levels(const eckit::Configuration& config) const { |
| 316 |
idx_t levels(nb_levels_); |
| 317 |
config.get("levels", levels); |
| 318 |
return levels; |
| 319 |
} |
| 320 |
|
| 321 |
array::ArrayShape NodeColumns::config_shape(const eckit::Configuration& config) const { |
| 322 |
array::ArrayShape shape; |
| 323 |
|
| 324 |
shape.push_back(config_nb_nodes(config)); |
| 325 |
|
| 326 |
idx_t levels(nb_levels_); |
| 327 |
config.get("levels", levels); |
| 328 |
if (levels > 0) { |
| 329 |
shape.push_back(levels); |
| 330 |
} |
| 331 |
|
| 332 |
idx_t variables(0); |
| 333 |
config.get("variables", variables); |
| 334 |
if (variables > 0) { |
| 335 |
shape.push_back(variables); |
| 336 |
} |
| 337 |
|
| 338 |
return shape; |
| 339 |
} |
| 340 |
|
| 341 |
Field NodeColumns::createField(const eckit::Configuration& config) const { |
| 342 |
Field field = Field(config_name(config), config_datatype(config), config_shape(config)); |
| 343 |
|
| 344 |
set_field_metadata(config, field); |
| 345 |
|
| 346 |
return field; |
| 347 |
} |
| 348 |
|
| 349 |
Field NodeColumns::createField(const Field& other, const eckit::Configuration& config) const { |
| 350 |
return createField(option::name(other.name()) | option::datatype(other.datatype()) | option::levels(other.levels()) | |
| 351 |
option::variables(other.variables()) | config); |
| 352 |
} |
| 353 |
|
| 354 |
namespace { |
| 355 |
|
| 356 |
template <int RANK> |
| 357 |
void dispatch_haloExchange(Field& field, const parallel::HaloExchange& halo_exchange, bool on_device) { |
| 358 |
if (field.datatype() == array::DataType::kind<int>()) { |
| 359 |
halo_exchange.template execute<int, RANK>(field.array(), on_device); |
| 360 |
} |
| 361 |
else if (field.datatype() == array::DataType::kind<long>()) { |
| 362 |
halo_exchange.template execute<long, RANK>(field.array(), on_device); |
| 363 |
} |
| 364 |
else if (field.datatype() == array::DataType::kind<float>()) { |
| 365 |
halo_exchange.template execute<float, RANK>(field.array(), on_device); |
| 366 |
} |
| 367 |
else if (field.datatype() == array::DataType::kind<double>()) { |
| 368 |
halo_exchange.template execute<double, RANK>(field.array(), on_device); |
| 369 |
} |
| 370 |
else { |
| 371 |
throw_Exception("datatype not supported", Here()); |
| 372 |
} |
| 373 |
field.set_dirty(false); |
| 374 |
} |
| 375 |
|
| 376 |
template <int RANK> |
| 377 |
void dispatch_adjointHaloExchange(Field& field, const parallel::HaloExchange& halo_exchange, bool on_device) { |
| 378 |
if (field.datatype() == array::DataType::kind<int>()) { |
| 379 |
halo_exchange.template execute_adjoint<int, RANK>(field.array(), on_device); |
| 380 |
} |
| 381 |
else if (field.datatype() == array::DataType::kind<long>()) { |
| 382 |
halo_exchange.template execute_adjoint<long, RANK>(field.array(), on_device); |
| 383 |
} |
| 384 |
else if (field.datatype() == array::DataType::kind<float>()) { |
| 385 |
halo_exchange.template execute_adjoint<float, RANK>(field.array(), on_device); |
| 386 |
} |
| 387 |
else if (field.datatype() == array::DataType::kind<double>()) { |
| 388 |
halo_exchange.template execute_adjoint<double, RANK>(field.array(), on_device); |
| 389 |
} |
| 390 |
else { |
| 391 |
throw_Exception("datatype not supported", Here()); |
| 392 |
} |
| 393 |
field.set_dirty(false); |
| 394 |
} |
| 395 |
|
| 396 |
} // namespace |
| 397 |
|
| 398 |
void NodeColumns::haloExchange(const FieldSet& fieldset, bool on_device) const { |
| 399 |
for (idx_t f = 0; f < fieldset.size(); ++f) { |
| 400 |
Field& field = const_cast<FieldSet&>(fieldset)[f]; |
| 401 |
switch (field.rank()) { |
| 402 |
case 1: |
| 403 |
dispatch_haloExchange<1>(field, halo_exchange(), on_device); |
| 404 |
break; |
| 405 |
case 2: |
| 406 |
dispatch_haloExchange<2>(field, halo_exchange(), on_device); |
| 407 |
break; |
| 408 |
case 3: |
| 409 |
dispatch_haloExchange<3>(field, halo_exchange(), on_device); |
| 410 |
break; |
| 411 |
case 4: |
| 412 |
dispatch_haloExchange<4>(field, halo_exchange(), on_device); |
| 413 |
break; |
| 414 |
default: |
| 415 |
throw_Exception("Rank not supported", Here()); |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
void NodeColumns::adjointHaloExchange(const FieldSet& fieldset, bool on_device) const { |
| 421 |
for (idx_t f = 0; f < fieldset.size(); ++f) { |
| 422 |
Field& field = const_cast<FieldSet&>(fieldset)[f]; |
| 423 |
switch (field.rank()) { |
| 424 |
case 1: |
| 425 |
dispatch_adjointHaloExchange<1>(field, halo_exchange(), on_device); |
| 426 |
break; |
| 427 |
case 2: |
| 428 |
dispatch_adjointHaloExchange<2>(field, halo_exchange(), on_device); |
| 429 |
break; |
| 430 |
case 3: |
| 431 |
dispatch_adjointHaloExchange<3>(field, halo_exchange(), on_device); |
| 432 |
break; |
| 433 |
case 4: |
| 434 |
dispatch_adjointHaloExchange<4>(field, halo_exchange(), on_device); |
| 435 |
break; |
| 436 |
default: |
| 437 |
throw_Exception("Rank not supported", Here()); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
void NodeColumns::haloExchange(const Field& field, bool on_device) const { |
| 443 |
FieldSet fieldset; |
| 444 |
fieldset.add(field); |
| 445 |
haloExchange(fieldset, on_device); |
| 446 |
} |
| 447 |
|
| 448 |
void NodeColumns::adjointHaloExchange(const Field& field, bool) const { |
| 449 |
FieldSet fieldset; |
| 450 |
fieldset.add(field); |
| 451 |
adjointHaloExchange(fieldset); |
| 452 |
} |
| 453 |
|
| 454 |
const parallel::HaloExchange& NodeColumns::halo_exchange() const { |
| 455 |
if (halo_exchange_) { |
| 456 |
return *halo_exchange_; |
| 457 |
} |
| 458 |
halo_exchange_ = NodeColumnsHaloExchangeCache::instance().get_or_create(mesh_, halo_.size()); |
| 459 |
return *halo_exchange_; |
| 460 |
} |
| 461 |
|
| 462 |
void NodeColumns::gather(const FieldSet& local_fieldset, FieldSet& global_fieldset) const { |
| 463 |
ATLAS_ASSERT(local_fieldset.size() == global_fieldset.size()); |
| 464 |
|
| 465 |
mpi::Scope mpi_scope(mpi_comm()); |
| 466 |
|
| 467 |
for (idx_t f = 0; f < local_fieldset.size(); ++f) { |
| 468 |
const Field& loc = local_fieldset[f]; |
| 469 |
Field& glb = global_fieldset[f]; |
| 470 |
const idx_t nb_fields = 1; |
| 471 |
idx_t root(0); |
| 472 |
glb.metadata().get("owner", root); |
| 473 |
|
| 474 |
if (loc.datatype() == array::DataType::kind<int>()) { |
| 475 |
parallel::Field<int const> loc_field(make_leveled_view<const int>(loc)); |
| 476 |
parallel::Field<int> glb_field(make_leveled_view<int>(glb)); |
| 477 |
gather().gather(&loc_field, &glb_field, nb_fields, root); |
| 478 |
} |
| 479 |
else if (loc.datatype() == array::DataType::kind<long>()) { |
| 480 |
parallel::Field<long const> loc_field(make_leveled_view<const long>(loc)); |
| 481 |
parallel::Field<long> glb_field(make_leveled_view<long>(glb)); |
| 482 |
gather().gather(&loc_field, &glb_field, nb_fields, root); |
| 483 |
} |
| 484 |
else if (loc.datatype() == array::DataType::kind<float>()) { |
| 485 |
parallel::Field<float const> loc_field(make_leveled_view<const float>(loc)); |
| 486 |
parallel::Field<float> glb_field(make_leveled_view<float>(glb)); |
| 487 |
gather().gather(&loc_field, &glb_field, nb_fields, root); |
| 488 |
} |
| 489 |
else if (loc.datatype() == array::DataType::kind<double>()) { |
| 490 |
parallel::Field<double const> loc_field(make_leveled_view<const double>(loc)); |
| 491 |
parallel::Field<double> glb_field(make_leveled_view<double>(glb)); |
| 492 |
gather().gather(&loc_field, &glb_field, nb_fields, root); |
| 493 |
} |
| 494 |
else { |
| 495 |
throw_Exception("datatype not supported", Here()); |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
void NodeColumns::gather(const Field& local, Field& global) const { |
| 501 |
FieldSet local_fields; |
| 502 |
FieldSet global_fields; |
| 503 |
local_fields.add(local); |
| 504 |
global_fields.add(global); |
| 505 |
gather(local_fields, global_fields); |
| 506 |
} |
| 507 |
const parallel::GatherScatter& NodeColumns::gather() const { |
| 508 |
if (gather_scatter_) { |
| 509 |
return *gather_scatter_; |
| 510 |
} |
| 511 |
gather_scatter_ = NodeColumnsGatherScatterCache::instance().get_or_create(mesh_); |
| 512 |
return *gather_scatter_; |
| 513 |
} |
| 514 |
const parallel::GatherScatter& NodeColumns::scatter() const { |
| 515 |
if (gather_scatter_) { |
| 516 |
return *gather_scatter_; |
| 517 |
} |
| 518 |
gather_scatter_ = NodeColumnsGatherScatterCache::instance().get_or_create(mesh_); |
| 519 |
return *gather_scatter_; |
| 520 |
} |
| 521 |
|
| 522 |
void NodeColumns::scatter(const FieldSet& global_fieldset, FieldSet& local_fieldset) const { |
| 523 |
ATLAS_ASSERT(local_fieldset.size() == global_fieldset.size()); |
| 524 |
|
| 525 |
mpi::Scope mpi_scope(mpi_comm()); |
| 526 |
for (idx_t f = 0; f < local_fieldset.size(); ++f) { |
| 527 |
const Field& glb = global_fieldset[f]; |
| 528 |
Field& loc = local_fieldset[f]; |
| 529 |
const idx_t nb_fields = 1; |
| 530 |
idx_t root(0); |
| 531 |
glb.metadata().get("owner", root); |
| 532 |
|
| 533 |
if (loc.datatype() == array::DataType::kind<int>()) { |
| 534 |
parallel::Field<int const> glb_field(make_leveled_view<const int>(glb)); |
| 535 |
parallel::Field<int> loc_field(make_leveled_view<int>(loc)); |
| 536 |
scatter().scatter(&glb_field, &loc_field, nb_fields, root); |
| 537 |
} |
| 538 |
else if (loc.datatype() == array::DataType::kind<long>()) { |
| 539 |
parallel::Field<long const> glb_field(make_leveled_view<const long>(glb)); |
| 540 |
parallel::Field<long> loc_field(make_leveled_view<long>(loc)); |
| 541 |
scatter().scatter(&glb_field, &loc_field, nb_fields, root); |
| 542 |
} |
| 543 |
else if (loc.datatype() == array::DataType::kind<float>()) { |
| 544 |
parallel::Field<float const> glb_field(make_leveled_view<const float>(glb)); |
| 545 |
parallel::Field<float> loc_field(make_leveled_view<float>(loc)); |
| 546 |
scatter().scatter(&glb_field, &loc_field, nb_fields, root); |
| 547 |
} |
| 548 |
else if (loc.datatype() == array::DataType::kind<double>()) { |
| 549 |
parallel::Field<double const> glb_field(make_leveled_view<const double>(glb)); |
| 550 |
parallel::Field<double> loc_field(make_leveled_view<double>(loc)); |
| 551 |
scatter().scatter(&glb_field, &loc_field, nb_fields, root); |
| 552 |
} |
| 553 |
else { |
| 554 |
throw_Exception("datatype not supported", Here()); |
| 555 |
} |
| 556 |
|
| 557 |
auto name = loc.name(); |
| 558 |
glb.metadata().broadcast(loc.metadata(), root); |
| 559 |
loc.metadata().set("global", false); |
| 560 |
if( !name.empty() ) { |
| 561 |
loc.metadata().set("name", name); |
| 562 |
} |
| 563 |
loc.set_dirty(true); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
void NodeColumns::scatter(const Field& global, Field& local) const { |
| 568 |
FieldSet global_fields; |
| 569 |
FieldSet local_fields; |
| 570 |
global_fields.add(global); |
| 571 |
local_fields.add(local); |
| 572 |
scatter(global_fields, local_fields); |
| 573 |
} |
| 574 |
|
| 575 |
namespace { |
| 576 |
template <typename T> |
| 577 |
std::string checksum_3d_field(const parallel::Checksum& checksum, const Field& field) { |
| 578 |
auto values = make_leveled_view<const T>(field); |
| 579 |
array::ArrayT<T> surface_field(values.shape(0), values.shape(2)); |
| 580 |
auto surface = array::make_view<T, 2>(surface_field); |
| 581 |
const idx_t npts = values.shape(0); |
| 582 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 583 |
for (idx_t j = 0; j < surface.shape(1); ++j) { |
| 584 |
surface(n, j) = 0.; |
| 585 |
for (idx_t l = 0; l < values.shape(1); ++l) { |
| 586 |
surface(n, j) += values(n, l, j); |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
return checksum.execute(surface.data(), surface_field.stride(0)); |
| 591 |
} |
| 592 |
} // namespace |
| 593 |
|
| 594 |
std::string NodeColumns::checksum(const FieldSet& fieldset) const { |
| 595 |
eckit::MD5 md5; |
| 596 |
for (idx_t f = 0; f < fieldset.size(); ++f) { |
| 597 |
const Field& field = fieldset[f]; |
| 598 |
if (field.datatype() == array::DataType::kind<int>()) { |
| 599 |
md5 << checksum_3d_field<int>(checksum(), field); |
| 600 |
} |
| 601 |
else if (field.datatype() == array::DataType::kind<long>()) { |
| 602 |
md5 << checksum_3d_field<long>(checksum(), field); |
| 603 |
} |
| 604 |
else if (field.datatype() == array::DataType::kind<float>()) { |
| 605 |
md5 << checksum_3d_field<float>(checksum(), field); |
| 606 |
} |
| 607 |
else if (field.datatype() == array::DataType::kind<double>()) { |
| 608 |
md5 << checksum_3d_field<double>(checksum(), field); |
| 609 |
} |
| 610 |
else { |
| 611 |
throw_Exception("datatype not supported", Here()); |
| 612 |
} |
| 613 |
} |
| 614 |
return md5; |
| 615 |
} |
| 616 |
std::string NodeColumns::checksum(const Field& field) const { |
| 617 |
FieldSet fieldset; |
| 618 |
fieldset.add(field); |
| 619 |
return checksum(fieldset); |
| 620 |
} |
| 621 |
|
| 622 |
const parallel::Checksum& NodeColumns::checksum() const { |
| 623 |
if (checksum_) { |
| 624 |
return *checksum_; |
| 625 |
} |
| 626 |
checksum_ = NodeColumnsChecksumCache::instance().get_or_create(mesh_); |
| 627 |
return *checksum_; |
| 628 |
} |
| 629 |
|
| 630 |
// std::string NodesFunctionSpace::checksum( const FieldSet& fieldset ) const { |
| 631 |
// const parallel::Checksum& checksum = mesh_.checksum().get(checksum_name()); |
| 632 |
|
| 633 |
// eckit::MD5 md5; |
| 634 |
// for( idx_t f=0; f<fieldset.size(); ++f ) { |
| 635 |
// const Field& field=fieldset[f]; |
| 636 |
// if ( field.datatype() == array::DataType::kind<int>() ) |
| 637 |
// md5 << checksum.execute( field.data<int>(), field.stride(0) ); |
| 638 |
// else if( field.datatype() == array::DataType::kind<long>() ) |
| 639 |
// md5 << checksum.execute( field.data<long>(), field.stride(0) ); |
| 640 |
// else if( field.datatype() == array::DataType::kind<float>() ) |
| 641 |
// md5 << checksum.execute( field.data<float>(), field.stride(0) ); |
| 642 |
// else if( field.datatype() == array::DataType::kind<double>() ) |
| 643 |
// md5 << checksum.execute( field.data<double>(), field.stride(0) ); |
| 644 |
// else throw_Exception("datatype not supported",Here()); |
| 645 |
// } |
| 646 |
// return md5; |
| 647 |
//} |
| 648 |
// std::string NodesFunctionSpace::checksum( const Field& field ) const { |
| 649 |
// FieldSet fieldset; |
| 650 |
// fieldset.add(field); |
| 651 |
// return checksum(fieldset); |
| 652 |
//} |
| 653 |
|
| 654 |
const Grid& NodeColumns::grid() const { |
| 655 |
if (grid_) { |
| 656 |
return grid_; |
| 657 |
} |
| 658 |
|
| 659 |
const auto& comm = mpi::comm(mpi_comm()); |
| 660 |
std::vector<PointXY> points; |
| 661 |
if (comm.size() == 1) { |
| 662 |
const auto xy = atlas::array::make_view<double, 2>(mesh_.nodes().xy()); |
| 663 |
for (auto i = 0; i < xy.shape(0); i++) { |
| 664 |
points.push_back({xy(i, 0), xy(i, 1)}); |
| 665 |
} |
| 666 |
} else { |
| 667 |
std::vector<int> gidx; |
| 668 |
std::vector<double> x, y; |
| 669 |
const auto gidxView = array::make_view<gidx_t, 1>(global_index()); |
| 670 |
const auto ghostView = array::make_view<int, 1>(ghost()); |
| 671 |
const auto xy = atlas::array::make_view<double, 2>(mesh_.nodes().xy()); |
| 672 |
for (auto i = 0; i < xy.shape(0); i++) { |
| 673 |
if (ghostView(i) == 0) { |
| 674 |
gidx.push_back(gidxView(i)); |
| 675 |
x.push_back(xy(i, 0)); |
| 676 |
y.push_back(xy(i, 1)); |
| 677 |
} |
| 678 |
} |
| 679 |
eckit::mpi::Buffer<int> gidxBuffer(comm.size()); |
| 680 |
eckit::mpi::Buffer<double> xBuffer(comm.size()); |
| 681 |
eckit::mpi::Buffer<double> yBuffer(comm.size()); |
| 682 |
comm.allGatherv(gidx.begin(), gidx.end(), gidxBuffer); |
| 683 |
comm.allGatherv(x.begin(), x.end(), xBuffer); |
| 684 |
comm.allGatherv(y.begin(), y.end(), yBuffer); |
| 685 |
points.reserve(gidxBuffer.buffer.size()); |
| 686 |
for (auto i : gidxBuffer.buffer) { |
| 687 |
points[i - 1] = atlas::PointXY{xBuffer.buffer[i - 1], yBuffer.buffer[i - 1]}; |
| 688 |
} |
| 689 |
} |
| 690 |
grid_ = UnstructuredGrid(points); |
| 691 |
return grid_; |
| 692 |
} |
| 693 |
|
| 694 |
} // namespace detail |
| 695 |
|
| 696 |
NodeColumns::NodeColumns(): FunctionSpace(), functionspace_(nullptr) {} |
| 697 |
|
| 698 |
NodeColumns::NodeColumns(const FunctionSpace& functionspace): |
| 699 |
FunctionSpace(functionspace), functionspace_(dynamic_cast<const detail::NodeColumns*>(get())) {} |
| 700 |
|
| 701 |
namespace { |
| 702 |
detail::NodeColumns* make_functionspace(Mesh mesh, const eckit::Configuration& config) { |
| 703 |
return new detail::NodeColumns(mesh, config); |
| 704 |
} |
| 705 |
} // namespace |
| 706 |
|
| 707 |
NodeColumns::NodeColumns(Mesh mesh): |
| 708 |
FunctionSpace(make_functionspace(mesh, util::NoConfig())), |
| 709 |
functionspace_(dynamic_cast<const detail::NodeColumns*>(get())) {} |
| 710 |
|
| 711 |
NodeColumns::NodeColumns(Mesh mesh, const eckit::Configuration& config): |
| 712 |
FunctionSpace(make_functionspace(mesh, config)), functionspace_(dynamic_cast<const detail::NodeColumns*>(get())) {} |
| 713 |
|
| 714 |
idx_t NodeColumns::nb_nodes() const { |
| 715 |
return functionspace_->nb_nodes(); |
| 716 |
} |
| 717 |
|
| 718 |
idx_t NodeColumns::nb_nodes_global() const { // All MPI ranks will have same output |
| 719 |
return functionspace_->nb_nodes_global(); |
| 720 |
} |
| 721 |
|
| 722 |
const Mesh& NodeColumns::mesh() const { |
| 723 |
return functionspace_->mesh(); |
| 724 |
} |
| 725 |
|
| 726 |
mesh::Nodes& NodeColumns::nodes() const { |
| 727 |
return functionspace_->nodes(); |
| 728 |
} |
| 729 |
|
| 730 |
// -- Parallelisation aware methods |
| 731 |
|
| 732 |
const mesh::Halo& NodeColumns::halo() const { |
| 733 |
return functionspace_->halo(); |
| 734 |
} |
| 735 |
|
| 736 |
void NodeColumns::haloExchange(const FieldSet& fieldset, bool on_device) const { |
| 737 |
functionspace_->haloExchange(fieldset, on_device); |
| 738 |
} |
| 739 |
|
| 740 |
void NodeColumns::haloExchange(const Field& field, bool on_device) const { |
| 741 |
functionspace_->haloExchange(field, on_device); |
| 742 |
} |
| 743 |
|
| 744 |
const parallel::HaloExchange& NodeColumns::halo_exchange() const { |
| 745 |
return functionspace_->halo_exchange(); |
| 746 |
} |
| 747 |
|
| 748 |
std::string NodeColumns::checksum(const FieldSet& fieldset) const { |
| 749 |
return functionspace_->checksum(fieldset); |
| 750 |
} |
| 751 |
|
| 752 |
std::string NodeColumns::checksum(const Field& field) const { |
| 753 |
return functionspace_->checksum(field); |
| 754 |
} |
| 755 |
|
| 756 |
const parallel::Checksum& NodeColumns::checksum() const { |
| 757 |
return functionspace_->checksum(); |
| 758 |
} |
| 759 |
|
| 760 |
} // namespace functionspace |
| 761 |
} // namespace atlas |