| 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 |
#pragma once |
| 12 |
|
| 13 |
#include <functional> |
| 14 |
#include <type_traits> |
| 15 |
|
| 16 |
#include "atlas/array/LocalView.h" |
| 17 |
#include "atlas/field/Field.h" |
| 18 |
#include "atlas/functionspace/FunctionSpace.h" |
| 19 |
#include "atlas/functionspace/detail/FunctionSpaceImpl.h" |
| 20 |
#include "atlas/library/config.h" |
| 21 |
#include "atlas/parallel/mpi/mpi.h" |
| 22 |
#include "atlas/parallel/omp/omp.h" |
| 23 |
#include "atlas/runtime/Exception.h" |
| 24 |
#include "atlas/util/Config.h" |
| 25 |
|
| 26 |
namespace atlas { |
| 27 |
class Field; |
| 28 |
class FieldSet; |
| 29 |
} // namespace atlas |
| 30 |
|
| 31 |
namespace atlas { |
| 32 |
namespace trans { |
| 33 |
class Trans; |
| 34 |
} |
| 35 |
} // namespace atlas |
| 36 |
|
| 37 |
namespace atlas { |
| 38 |
namespace functionspace { |
| 39 |
namespace detail { |
| 40 |
|
| 41 |
// ------------------------------------------------------------------- |
| 42 |
|
| 43 |
class Spectral : public functionspace::FunctionSpaceImpl { |
| 44 |
/* |
| 45 |
Spectral data is organised as: |
| 46 |
m = zonal wavenumber |
| 47 |
n = total wavenumber |
| 48 |
|
| 49 |
const auto zonal_wavenumbers = Spectral::zonal_wavenumbers(); |
| 50 |
const int truncation = Spectral::truncation(); |
| 51 |
idx_t jc=0; |
| 52 |
for( int jm=0; jm<zonal_wavenumbers.size(); ++jm ) { |
| 53 |
int m = zonal_wavenumbers(jm); |
| 54 |
for( int n=m; m<=truncation; ++n ) { |
| 55 |
data( jc++, jfld ) = func_real_part(m,n); |
| 56 |
data( jc++, jfld ) = func_imag_part(m,n); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
*/ |
| 61 |
|
| 62 |
public: |
| 63 |
Spectral(const eckit::Configuration&); |
| 64 |
|
| 65 |
Spectral(const int truncation, const eckit::Configuration& = util::NoConfig()); |
| 66 |
|
| 67 |
~Spectral() override; |
| 68 |
|
| 69 |
std::string type() const override { return "Spectral"; } |
| 70 |
|
| 71 |
std::string distribution() const override; |
| 72 |
|
| 73 |
idx_t part() const override; |
| 74 |
|
| 75 |
idx_t nb_parts() const override; |
| 76 |
|
| 77 |
using FunctionSpaceImpl::createField; |
| 78 |
Field createField(const eckit::Configuration&) const override; |
| 79 |
Field createField(const Field&, const eckit::Configuration&) const override; |
| 80 |
|
| 81 |
using FunctionSpaceImpl::gather; |
| 82 |
void gather(const FieldSet&, FieldSet&) const override; |
| 83 |
void gather(const Field&, Field&) const override; |
| 84 |
|
| 85 |
using FunctionSpaceImpl::scatter; |
| 86 |
void scatter(const FieldSet&, FieldSet&) const override; |
| 87 |
void scatter(const Field&, Field&) const override; |
| 88 |
|
| 89 |
std::string checksum(const FieldSet&) const; |
| 90 |
std::string checksum(const Field&) const; |
| 91 |
|
| 92 |
void norm(const Field&, double& norm, int rank = 0) const; |
| 93 |
void norm(const Field&, double norm_per_level[], int rank = 0) const; |
| 94 |
void norm(const Field&, std::vector<double>& norm_per_level, int rank = 0) const; |
| 95 |
|
| 96 |
array::LocalView<const int, 1> zonal_wavenumbers() const; // zero-based, OK |
| 97 |
|
| 98 |
idx_t levels() const { return nb_levels_; } |
| 99 |
|
| 100 |
class For { |
| 101 |
public: |
| 102 |
For(const Spectral& fs, const util::Config& config = util::NoConfig()): |
| 103 |
truncation{fs.truncation()}, |
| 104 |
zonal_wavenumbers{fs.zonal_wavenumbers()}, |
| 105 |
global{config.getBool("global", false)}, |
| 106 |
owner{config.getInt("owner", 0)} {} |
| 107 |
|
| 108 |
protected: |
| 109 |
using View = const array::LocalView<const int, 1>; |
| 110 |
int truncation; |
| 111 |
View zonal_wavenumbers; |
| 112 |
bool global; |
| 113 |
idx_t owner; |
| 114 |
|
| 115 |
public: |
| 116 |
#define FunctorArgs(...) \ |
| 117 |
typename std::enable_if<std::is_convertible<Functor, std::function<void(__VA_ARGS__)>>::value, Functor>::type* = \ |
| 118 |
nullptr |
| 119 |
|
| 120 |
// Functor: void f(real,imag,n,m) |
| 121 |
template <typename Functor, FunctorArgs(idx_t, idx_t, int, int)> |
| 122 |
void operator()(const Functor& f) const { |
| 123 |
idx_t index = 0; |
| 124 |
if (global) { |
| 125 |
if (owner == mpi::rank()) { |
| 126 |
for(int m = 0; m <= truncation; ++m) { |
| 127 |
for (int n = m; n <= truncation; ++n) { |
| 128 |
f(index, index + 1, n, m); |
| 129 |
index += 2; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
else { |
| 135 |
const int nb_zonal_wavenumbers{static_cast<int>(zonal_wavenumbers.size())}; |
| 136 |
for(int jm = 0; jm < nb_zonal_wavenumbers; ++jm) { |
| 137 |
const int m = zonal_wavenumbers(jm); |
| 138 |
for (int n = m; n <= truncation; ++n) { |
| 139 |
f(index, index + 1, n, m); |
| 140 |
index += 2; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// Functor: void f(real,imag,n) |
| 147 |
template <typename Functor, FunctorArgs(idx_t, idx_t, int)> |
| 148 |
void operator()(const Functor& f) const { |
| 149 |
idx_t index = 0; |
| 150 |
if (global) { |
| 151 |
if (owner == mpi::rank()) { |
| 152 |
for(int m = 0; m <= truncation; ++m) { |
| 153 |
for (int n = m; n <= truncation; ++n) { |
| 154 |
f(index, index + 1, n); |
| 155 |
index += 2; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
else { |
| 161 |
const int nb_zonal_wavenumbers{static_cast<int>(zonal_wavenumbers.size())}; |
| 162 |
for(int jm = 0; jm < nb_zonal_wavenumbers; ++jm) { |
| 163 |
const int m = zonal_wavenumbers(jm); |
| 164 |
for (int n = m; n <= truncation; ++n) { |
| 165 |
f(index, index + 1, n); |
| 166 |
index += 2; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
#undef FunctorArgs |
| 172 |
}; |
| 173 |
template <typename Functor> |
| 174 |
void parallel_for(const Functor& f) const { |
| 175 |
For(*this, util::NoConfig())(f); |
| 176 |
} |
| 177 |
|
| 178 |
template <typename Functor> |
| 179 |
void parallel_for(const util::Config& config, const Functor& f) const { |
| 180 |
For(*this, config)(f); |
| 181 |
} |
| 182 |
|
| 183 |
public: // methods |
| 184 |
idx_t nb_spectral_coefficients() const; |
| 185 |
idx_t nb_spectral_coefficients_global() const; |
| 186 |
int truncation() const { return truncation_; } |
| 187 |
|
| 188 |
idx_t size() const override { return nb_spectral_coefficients(); } |
| 189 |
|
| 190 |
private: // methods |
| 191 |
array::DataType config_datatype(const eckit::Configuration&) const; |
| 192 |
std::string config_name(const eckit::Configuration&) const; |
| 193 |
idx_t config_size(const eckit::Configuration&) const; |
| 194 |
idx_t config_levels(const eckit::Configuration&) const; |
| 195 |
void set_field_metadata(const eckit::Configuration&, Field&) const; |
| 196 |
size_t footprint() const override; |
| 197 |
|
| 198 |
|
| 199 |
private: // Fortran access |
| 200 |
friend struct SpectralFortranAccess; |
| 201 |
int nump() const; // equivalent to nmyms().size() |
| 202 |
array::LocalView<const int, 1> nvalue() const; // Return wave number n for a given index |
| 203 |
array::LocalView<const int, 1> nmyms() const; // Return list of local zonal wavenumbers "m" |
| 204 |
array::LocalView<const int, 1> nasm0() const; |
| 205 |
|
| 206 |
private: // data |
| 207 |
idx_t nb_levels_; |
| 208 |
int truncation_; |
| 209 |
|
| 210 |
class Parallelisation; |
| 211 |
std::unique_ptr<Parallelisation> parallelisation_; |
| 212 |
}; |
| 213 |
|
| 214 |
} // namespace detail |
| 215 |
|
| 216 |
// ------------------------------------------------------------------- |
| 217 |
|
| 218 |
class Spectral : public FunctionSpace { |
| 219 |
public: |
| 220 |
Spectral(); |
| 221 |
Spectral(const FunctionSpace&); |
| 222 |
Spectral(const eckit::Configuration&); |
| 223 |
Spectral(const int truncation, const eckit::Configuration& = util::NoConfig()); |
| 224 |
|
| 225 |
operator bool() const { return valid(); } |
| 226 |
bool valid() const { return functionspace_; } |
| 227 |
|
| 228 |
std::string checksum(const FieldSet&) const; |
| 229 |
std::string checksum(const Field&) const; |
| 230 |
|
| 231 |
void norm(const Field&, double& norm, int rank = 0) const; |
| 232 |
void norm(const Field&, double norm_per_level[], int rank = 0) const; |
| 233 |
void norm(const Field&, std::vector<double>& norm_per_level, int rank = 0) const; |
| 234 |
|
| 235 |
array::LocalView<const int, 1> zonal_wavenumbers() const; // zero-based, OK |
| 236 |
|
| 237 |
idx_t nb_spectral_coefficients() const; |
| 238 |
idx_t nb_spectral_coefficients_global() const; |
| 239 |
int truncation() const; |
| 240 |
idx_t levels() const { return functionspace_->levels(); } |
| 241 |
|
| 242 |
template <typename Functor> |
| 243 |
void parallel_for(const Functor& f) const { |
| 244 |
functionspace_->parallel_for(f); |
| 245 |
} |
| 246 |
template <typename Functor> |
| 247 |
void parallel_for(const util::Config& config, const Functor& f) const { |
| 248 |
functionspace_->parallel_for(config, f); |
| 249 |
} |
| 250 |
|
| 251 |
private: |
| 252 |
const detail::Spectral* functionspace_; |
| 253 |
}; |
| 254 |
|
| 255 |
} // namespace functionspace |
| 256 |
} // namespace atlas |