| 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 "eckit/os/BackTrace.h" |
| 12 |
#include "eckit/utils/MD5.h" |
| 13 |
|
| 14 |
#include "atlas/array/Array.h" |
| 15 |
#include "atlas/array/MakeView.h" |
| 16 |
#include "atlas/field/FieldSet.h" |
| 17 |
#include "atlas/functionspace/Spectral.h" |
| 18 |
#include "atlas/mesh/Mesh.h" |
| 19 |
#include "atlas/option.h" |
| 20 |
#include "atlas/parallel/mpi/mpi.h" |
| 21 |
#include "atlas/runtime/Exception.h" |
| 22 |
#include "atlas/runtime/Log.h" |
| 23 |
#include "atlas/trans/Trans.h" |
| 24 |
|
| 25 |
|
| 26 |
#if ATLAS_HAVE_TRANS |
| 27 |
#include "atlas/trans/ifs/TransIFS.h" |
| 28 |
#if ATLAS_HAVE_ECTRANS |
| 29 |
#include "ectrans/transi.h" |
| 30 |
#else |
| 31 |
#include "transi/trans.h" |
| 32 |
#endif |
| 33 |
|
| 34 |
namespace { |
| 35 |
void trans_check(const int code, const char* msg, const eckit::CodeLocation& location) { |
| 36 |
if (code != TRANS_SUCCESS) { |
| 37 |
std::stringstream errmsg; |
| 38 |
errmsg << "atlas::trans ERROR: " << msg << " failed: \n"; |
| 39 |
errmsg << ::trans_error_msg(code); |
| 40 |
atlas::throw_Exception(errmsg.str(), location); |
| 41 |
} |
| 42 |
} |
| 43 |
#define TRANS_CHECK(CALL) trans_check(CALL, #CALL, Here()) |
| 44 |
} // namespace |
| 45 |
#endif |
| 46 |
|
| 47 |
namespace atlas { |
| 48 |
namespace functionspace { |
| 49 |
namespace detail { |
| 50 |
|
| 51 |
#if ATLAS_HAVE_TRANS |
| 52 |
class Spectral::Parallelisation { |
| 53 |
public: |
| 54 |
Parallelisation(const std::shared_ptr<::Trans_t> other): trans_(other) {} |
| 55 |
|
| 56 |
Parallelisation(int truncation) { |
| 57 |
trans_ = std::shared_ptr<::Trans_t>(new ::Trans_t, [](::Trans_t* p) { |
| 58 |
TRANS_CHECK(::trans_delete(p)); |
| 59 |
delete p; |
| 60 |
}); |
| 61 |
#if ATLAS_ECTRANS_VERSION_AT_LEAST(1, 8, 0) |
| 62 |
TRANS_CHECK(::trans_set_mpi_comm(mpi::comm().communicator())); |
| 63 |
#endif |
| 64 |
TRANS_CHECK(::trans_new(trans_.get())); |
| 65 |
TRANS_CHECK(::trans_set_trunc(trans_.get(), truncation)); |
| 66 |
TRANS_CHECK(::trans_use_mpi(mpi::size() > 1)); |
| 67 |
TRANS_CHECK(::trans_setup(trans_.get())); |
| 68 |
} |
| 69 |
|
| 70 |
int nb_spectral_coefficients_global() const { return trans_->nspec2g; } |
| 71 |
int nb_spectral_coefficients() const { return trans_->nspec2; } |
| 72 |
|
| 73 |
int nump() const { return trans_->nump; } |
| 74 |
|
| 75 |
array::LocalView<const int, 1> nvalue() const { |
| 76 |
if (trans_->nvalue == nullptr) { |
| 77 |
::trans_inquire(trans_.get(), "nvalue"); |
| 78 |
} |
| 79 |
return array::make_view<const int, 1>(trans_->nvalue, array::make_shape(trans_->nspec2)); |
| 80 |
} |
| 81 |
|
| 82 |
array::LocalView<const int, 1> nmyms() const { |
| 83 |
if (trans_->nmyms == nullptr) { |
| 84 |
::trans_inquire(trans_.get(), "nmyms"); |
| 85 |
} |
| 86 |
return array::make_view<const int, 1>(trans_->nmyms, array::make_shape(nump())); |
| 87 |
} |
| 88 |
|
| 89 |
array::LocalView<const int, 1> nasm0() const { |
| 90 |
if (trans_->nasm0 == nullptr) { |
| 91 |
::trans_inquire(trans_.get(), "nasm0"); |
| 92 |
} |
| 93 |
return array::make_view<const int, 1>(trans_->nasm0, array::make_shape(trans_->nsmax + 1)); |
| 94 |
} |
| 95 |
|
| 96 |
std::string distribution() const { return "ectrans"; } |
| 97 |
operator ::Trans_t*() const { return trans_.get(); } |
| 98 |
std::shared_ptr<::Trans_t> trans_; |
| 99 |
}; |
| 100 |
#else |
| 101 |
class Spectral::Parallelisation { |
| 102 |
public: |
| 103 |
Parallelisation(int truncation): truncation_(truncation) { |
| 104 |
// Assume serial!!! |
| 105 |
nmyms_.resize(truncation_ + 1); |
| 106 |
nasm0_.resize(truncation_ + 1); |
| 107 |
nvalue_.resize(nb_spectral_coefficients()); |
| 108 |
idx_t jc{0}; |
| 109 |
for (idx_t m = 0; m <= truncation_; ++m) { |
| 110 |
nmyms_[m] = m; |
| 111 |
nasm0_[m] = jc + 1; // Fortran index |
| 112 |
for (idx_t n = m; n <= truncation_; ++n) { |
| 113 |
nvalue_[jc++] = n; |
| 114 |
nvalue_[jc++] = n; |
| 115 |
} |
| 116 |
} |
| 117 |
ATLAS_ASSERT(jc == nb_spectral_coefficients()); |
| 118 |
} |
| 119 |
int nb_spectral_coefficients_global() const { return (truncation_ + 1) * (truncation_ + 2); } |
| 120 |
int nb_spectral_coefficients() const { return nb_spectral_coefficients_global(); } |
| 121 |
int truncation_; |
| 122 |
std::string distribution() const { return "serial"; } |
| 123 |
|
| 124 |
int nump() const { return truncation_ + 1; } |
| 125 |
|
| 126 |
array::LocalView<const int, 1> nmyms() const { |
| 127 |
return array::make_view<int, 1>(nmyms_.data(), array::make_shape(nump())); |
| 128 |
} |
| 129 |
|
| 130 |
array::LocalView<const int, 1> nvalue() const { |
| 131 |
return array::make_view<int, 1>(nvalue_.data(), array::make_shape(nb_spectral_coefficients())); |
| 132 |
} |
| 133 |
|
| 134 |
array::LocalView<const int, 1> nasm0() const { |
| 135 |
return array::make_view<int, 1>(nasm0_.data(), array::make_shape(truncation_ + 1)); |
| 136 |
} |
| 137 |
std::vector<int> nmyms_; |
| 138 |
std::vector<int> nasm0_; |
| 139 |
std::vector<int> nvalue_; |
| 140 |
}; |
| 141 |
#endif |
| 142 |
|
| 143 |
void Spectral::set_field_metadata(const eckit::Configuration& config, Field& field) const { |
| 144 |
field.set_functionspace(this); |
| 145 |
|
| 146 |
bool global(false); |
| 147 |
if (config.get("global", global)) { |
| 148 |
if (global) { |
| 149 |
idx_t owner(0); |
| 150 |
config.get("owner", owner); |
| 151 |
field.metadata().set("owner", owner); |
| 152 |
} |
| 153 |
} |
| 154 |
field.metadata().set("global", global); |
| 155 |
|
| 156 |
field.set_levels(config_levels(config)); |
| 157 |
field.set_variables(0); |
| 158 |
} |
| 159 |
|
| 160 |
idx_t Spectral::config_size(const eckit::Configuration& config) const { |
| 161 |
idx_t size = nb_spectral_coefficients(); |
| 162 |
bool global(false); |
| 163 |
if (config.get("global", global)) { |
| 164 |
if (global) { |
| 165 |
idx_t owner(0); |
| 166 |
config.get("owner", owner); |
| 167 |
size = (idx_t(mpi::rank()) == owner ? nb_spectral_coefficients_global() : 0); |
| 168 |
} |
| 169 |
} |
| 170 |
return size; |
| 171 |
} |
| 172 |
|
| 173 |
// ---------------------------------------------------------------------- |
| 174 |
|
| 175 |
Spectral::Spectral(const eckit::Configuration& config): Spectral::Spectral(config.getUnsigned("truncation"), config) {} |
| 176 |
|
| 177 |
// ---------------------------------------------------------------------- |
| 178 |
|
| 179 |
Spectral::Spectral(const int truncation, const eckit::Configuration& config): |
| 180 |
nb_levels_(0), truncation_(truncation), parallelisation_(new Parallelisation(truncation_)) { |
| 181 |
config.get("levels", nb_levels_); |
| 182 |
} |
| 183 |
|
| 184 |
Spectral::~Spectral() = default; |
| 185 |
|
| 186 |
std::string Spectral::distribution() const { |
| 187 |
return parallelisation_->distribution(); |
| 188 |
} |
| 189 |
|
| 190 |
idx_t Spectral::part() const { |
| 191 |
return mpi::rank(); |
| 192 |
} |
| 193 |
|
| 194 |
idx_t Spectral::nb_parts() const { |
| 195 |
return mpi::size(); |
| 196 |
} |
| 197 |
|
| 198 |
size_t Spectral::footprint() const { |
| 199 |
size_t size = sizeof(*this); |
| 200 |
// TODO |
| 201 |
return size; |
| 202 |
} |
| 203 |
|
| 204 |
idx_t Spectral::nb_spectral_coefficients() const { |
| 205 |
return parallelisation_->nb_spectral_coefficients(); |
| 206 |
} |
| 207 |
|
| 208 |
idx_t Spectral::nb_spectral_coefficients_global() const { |
| 209 |
return parallelisation_->nb_spectral_coefficients_global(); |
| 210 |
} |
| 211 |
|
| 212 |
array::DataType Spectral::config_datatype(const eckit::Configuration& config) const { |
| 213 |
array::DataType::kind_t kind; |
| 214 |
if (!config.get("datatype", kind)) { |
| 215 |
throw_Exception("datatype missing", Here()); |
| 216 |
} |
| 217 |
return array::DataType(kind); |
| 218 |
} |
| 219 |
|
| 220 |
std::string Spectral::config_name(const eckit::Configuration& config) const { |
| 221 |
std::string name; |
| 222 |
config.get("name", name); |
| 223 |
return name; |
| 224 |
} |
| 225 |
|
| 226 |
idx_t Spectral::config_levels(const eckit::Configuration& config) const { |
| 227 |
idx_t levels(nb_levels_); |
| 228 |
config.get("levels", levels); |
| 229 |
return levels; |
| 230 |
} |
| 231 |
|
| 232 |
Field Spectral::createField(const eckit::Configuration& options) const { |
| 233 |
array::ArrayShape array_shape; |
| 234 |
|
| 235 |
idx_t nb_spec_coeffs = config_size(options); |
| 236 |
array_shape.push_back(nb_spec_coeffs); |
| 237 |
|
| 238 |
idx_t levels = config_levels(options); |
| 239 |
if (levels) { |
| 240 |
array_shape.push_back(levels); |
| 241 |
} |
| 242 |
|
| 243 |
idx_t variables = 0; |
| 244 |
if (options.get("variables",variables)) { |
| 245 |
if (variables) { |
| 246 |
array_shape.push_back(variables); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
Field field = Field(config_name(options), config_datatype(options), array_shape); |
| 251 |
|
| 252 |
set_field_metadata(options, field); |
| 253 |
return field; |
| 254 |
} |
| 255 |
|
| 256 |
Field Spectral::createField(const Field& other, const eckit::Configuration& config) const { |
| 257 |
return createField(option::datatype(other.datatype()) | option::levels(other.levels()) | config); |
| 258 |
} |
| 259 |
|
| 260 |
void Spectral::gather(const FieldSet& local_fieldset, FieldSet& global_fieldset) const { |
| 261 |
ATLAS_ASSERT(local_fieldset.size() == global_fieldset.size()); |
| 262 |
|
| 263 |
for (idx_t f = 0; f < local_fieldset.size(); ++f) { |
| 264 |
const Field& loc = local_fieldset[f]; |
| 265 |
if (loc.datatype() != array::DataType::str<double>()) { |
| 266 |
std::stringstream err; |
| 267 |
err << "Cannot gather spectral field " << loc.name() << " of datatype " << loc.datatype().str() << "."; |
| 268 |
err << "Only " << array::DataType::str<double>() << " supported."; |
| 269 |
throw_Exception(err.str(), Here()); |
| 270 |
} |
| 271 |
|
| 272 |
#if ATLAS_HAVE_TRANS |
| 273 |
Field& glb = global_fieldset[f]; |
| 274 |
idx_t root = 0; |
| 275 |
idx_t rank = mpi::rank(); |
| 276 |
glb.metadata().get("owner", root); |
| 277 |
ATLAS_ASSERT(loc.shape(0) == nb_spectral_coefficients()); |
| 278 |
if (rank == root) { |
| 279 |
ATLAS_ASSERT(glb.shape(0) == nb_spectral_coefficients_global()); |
| 280 |
} |
| 281 |
std::vector<int> nto(1, root + 1); |
| 282 |
if (loc.rank() > 1) { |
| 283 |
nto.resize(loc.stride(0)); |
| 284 |
for (size_t i = 0; i < nto.size(); ++i) { |
| 285 |
nto[i] = root + 1; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
if (not loc.contiguous()) { |
| 290 |
throw_Exception("Cannot gather field " + loc.name() + |
| 291 |
" using IFS trans library as its data is not contiguous"); |
| 292 |
} |
| 293 |
|
| 294 |
struct ::GathSpec_t args = new_gathspec(*parallelisation_); |
| 295 |
args.nfld = nto.size(); |
| 296 |
args.rspecg = glb.array().data<double>(); |
| 297 |
args.nto = nto.data(); |
| 298 |
args.rspec = loc.array().data<double>(); |
| 299 |
TRANS_CHECK(::trans_gathspec(&args)); |
| 300 |
#else |
| 301 |
|
| 302 |
throw_Exception( |
| 303 |
"Cannot gather spectral fields because Atlas has " |
| 304 |
"not been compiled with TRANS support."); |
| 305 |
#endif |
| 306 |
} |
| 307 |
} |
| 308 |
void Spectral::gather(const Field& local, Field& global) const { |
| 309 |
FieldSet local_fields; |
| 310 |
FieldSet global_fields; |
| 311 |
local_fields.add(local); |
| 312 |
global_fields.add(global); |
| 313 |
gather(local_fields, global_fields); |
| 314 |
} |
| 315 |
|
| 316 |
void Spectral::scatter(const FieldSet& global_fieldset, FieldSet& local_fieldset) const { |
| 317 |
ATLAS_ASSERT(local_fieldset.size() == global_fieldset.size()); |
| 318 |
|
| 319 |
for (idx_t f = 0; f < local_fieldset.size(); ++f) { |
| 320 |
const Field& glb = global_fieldset[f]; |
| 321 |
Field& loc = local_fieldset[f]; |
| 322 |
if (loc.datatype() != array::DataType::str<double>()) { |
| 323 |
std::stringstream err; |
| 324 |
err << "Cannot scatter spectral field " << glb.name() << " of datatype " << glb.datatype().str() << "."; |
| 325 |
err << "Only " << array::DataType::str<double>() << " supported."; |
| 326 |
throw_Exception(err.str(), Here()); |
| 327 |
} |
| 328 |
|
| 329 |
#if ATLAS_HAVE_TRANS |
| 330 |
idx_t root = 0; |
| 331 |
idx_t rank = mpi::rank(); |
| 332 |
|
| 333 |
glb.metadata().get("owner", root); |
| 334 |
ATLAS_ASSERT(loc.shape(0) == nb_spectral_coefficients()); |
| 335 |
if (rank == root) { |
| 336 |
ATLAS_ASSERT(glb.shape(0) == nb_spectral_coefficients_global()); |
| 337 |
} |
| 338 |
std::vector<int> nfrom(1, root + 1); |
| 339 |
if (loc.rank() > 1) { |
| 340 |
nfrom.resize(loc.stride(0)); |
| 341 |
for (size_t i = 0; i < nfrom.size(); ++i) { |
| 342 |
nfrom[i] = root + 1; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
if (not loc.contiguous()) { |
| 347 |
throw_Exception("Cannot scatter field " + glb.name() + |
| 348 |
" using IFS trans library as its data is not contiguous"); |
| 349 |
} |
| 350 |
|
| 351 |
struct ::DistSpec_t args = new_distspec(*parallelisation_); |
| 352 |
args.nfld = int(nfrom.size()); |
| 353 |
args.rspecg = glb.array().data<double>(); |
| 354 |
args.nfrom = nfrom.data(); |
| 355 |
args.rspec = loc.array().data<double>(); |
| 356 |
TRANS_CHECK(::trans_distspec(&args)); |
| 357 |
|
| 358 |
glb.metadata().broadcast(loc.metadata(), root); |
| 359 |
loc.metadata().set("global", false); |
| 360 |
#else |
| 361 |
throw_Exception( |
| 362 |
"Cannot scatter spectral fields because Atlas has " |
| 363 |
"not been compiled with TRANS support."); |
| 364 |
#endif |
| 365 |
} |
| 366 |
} |
| 367 |
void Spectral::scatter(const Field& global, Field& local) const { |
| 368 |
FieldSet global_fields; |
| 369 |
FieldSet local_fields; |
| 370 |
global_fields.add(global); |
| 371 |
local_fields.add(local); |
| 372 |
scatter(global_fields, local_fields); |
| 373 |
} |
| 374 |
|
| 375 |
std::string Spectral::checksum(const FieldSet&) const { |
| 376 |
eckit::MD5 md5; |
| 377 |
ATLAS_NOTIMPLEMENTED; |
| 378 |
} |
| 379 |
std::string Spectral::checksum(const Field& field) const { |
| 380 |
FieldSet fieldset; |
| 381 |
fieldset.add(field); |
| 382 |
return checksum(fieldset); |
| 383 |
} |
| 384 |
|
| 385 |
void Spectral::norm(const Field& field, double& norm, int rank) const { |
| 386 |
#if ATLAS_HAVE_TRANS |
| 387 |
ATLAS_ASSERT(std::max<int>(1, field.levels()) == 1, |
| 388 |
"Only a single-level field can be used for computing single norm."); |
| 389 |
struct ::SpecNorm_t args = new_specnorm(*parallelisation_); |
| 390 |
args.nfld = 1; |
| 391 |
args.rspec = field.array().data<double>(); |
| 392 |
args.rnorm = &norm; |
| 393 |
args.nmaster = rank + 1; |
| 394 |
TRANS_CHECK(::trans_specnorm(&args)); |
| 395 |
#else |
| 396 |
throw_Exception( |
| 397 |
"Cannot compute spectral norms because Atlas has not " |
| 398 |
"been compiled with TRANS support."); |
| 399 |
#endif |
| 400 |
} |
| 401 |
void Spectral::norm(const Field& field, double norm_per_level[], int rank) const { |
| 402 |
#if ATLAS_HAVE_TRANS |
| 403 |
if (not field.contiguous()) { |
| 404 |
throw_Exception("Cannot compute spectral norm of field " + field.name() + |
| 405 |
" using IFS trans library as its data is not contiguous"); |
| 406 |
} |
| 407 |
struct ::SpecNorm_t args = new_specnorm(*parallelisation_); |
| 408 |
args.nfld = std::max<int>(1, field.levels()); |
| 409 |
args.rspec = field.array().data<double>(); |
| 410 |
args.rnorm = norm_per_level; |
| 411 |
args.nmaster = rank + 1; |
| 412 |
TRANS_CHECK(::trans_specnorm(&args)); |
| 413 |
#else |
| 414 |
throw_Exception( |
| 415 |
"Cannot compute spectral norms because Atlas has not " |
| 416 |
"been compiled with TRANS support."); |
| 417 |
#endif |
| 418 |
} |
| 419 |
void Spectral::norm(const Field& field, std::vector<double>& norm_per_level, int rank) const { |
| 420 |
norm(field, norm_per_level.data(), rank); |
| 421 |
} |
| 422 |
|
| 423 |
array::LocalView<const int, 1> Spectral::zonal_wavenumbers() const { |
| 424 |
return parallelisation_->nmyms(); |
| 425 |
} |
| 426 |
|
| 427 |
int Spectral::nump() const { |
| 428 |
return parallelisation_->nump(); |
| 429 |
} |
| 430 |
|
| 431 |
array::LocalView<const int, 1> Spectral::nvalue() const { |
| 432 |
return parallelisation_->nvalue(); |
| 433 |
} |
| 434 |
|
| 435 |
array::LocalView<const int, 1> Spectral::nmyms() const { |
| 436 |
return parallelisation_->nmyms(); |
| 437 |
} |
| 438 |
|
| 439 |
array::LocalView<const int, 1> Spectral::nasm0() const { |
| 440 |
return parallelisation_->nasm0(); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
} // namespace detail |
| 445 |
|
| 446 |
// ---------------------------------------------------------------------- |
| 447 |
|
| 448 |
Spectral::Spectral(): FunctionSpace(), functionspace_{nullptr} {} |
| 449 |
|
| 450 |
Spectral::Spectral(const FunctionSpace& functionspace): |
| 451 |
FunctionSpace(functionspace), functionspace_(dynamic_cast<const detail::Spectral*>(get())) {} |
| 452 |
|
| 453 |
Spectral::Spectral(const eckit::Configuration& config): |
| 454 |
FunctionSpace(new detail::Spectral(config)), functionspace_(dynamic_cast<const detail::Spectral*>(get())) {} |
| 455 |
|
| 456 |
Spectral::Spectral(const int truncation, const eckit::Configuration& config): |
| 457 |
FunctionSpace(new detail::Spectral(truncation, config)), |
| 458 |
functionspace_(dynamic_cast<const detail::Spectral*>(get())) {} |
| 459 |
|
| 460 |
idx_t Spectral::nb_spectral_coefficients() const { |
| 461 |
return functionspace_->nb_spectral_coefficients(); |
| 462 |
} |
| 463 |
|
| 464 |
idx_t Spectral::nb_spectral_coefficients_global() const { |
| 465 |
return functionspace_->nb_spectral_coefficients_global(); |
| 466 |
} |
| 467 |
|
| 468 |
int Spectral::truncation() const { |
| 469 |
return functionspace_->truncation(); |
| 470 |
} |
| 471 |
|
| 472 |
std::string Spectral::checksum(const FieldSet& fieldset) const { |
| 473 |
return functionspace_->checksum(fieldset); |
| 474 |
} |
| 475 |
|
| 476 |
std::string Spectral::checksum(const Field& field) const { |
| 477 |
return functionspace_->checksum(field); |
| 478 |
} |
| 479 |
|
| 480 |
void Spectral::norm(const Field& field, double& norm, int rank) const { |
| 481 |
functionspace_->norm(field, norm, rank); |
| 482 |
} |
| 483 |
|
| 484 |
void Spectral::norm(const Field& field, double norm_per_level[], int rank) const { |
| 485 |
functionspace_->norm(field, norm_per_level, rank); |
| 486 |
} |
| 487 |
|
| 488 |
void Spectral::norm(const Field& field, std::vector<double>& norm_per_level, int rank) const { |
| 489 |
functionspace_->norm(field, norm_per_level, rank); |
| 490 |
} |
| 491 |
|
| 492 |
array::LocalView<const int, 1> Spectral::zonal_wavenumbers() const { |
| 493 |
return functionspace_->zonal_wavenumbers(); |
| 494 |
} |
| 495 |
|
| 496 |
// ---------------------------------------------------------------------- |
| 497 |
|
| 498 |
} // namespace functionspace |
| 499 |
} // namespace atlas |