| 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 <algorithm> |
| 12 |
#include <cmath> |
| 13 |
#include <cstdarg> |
| 14 |
#include <functional> |
| 15 |
#include <limits> |
| 16 |
|
| 17 |
#include "atlas/array.h" |
| 18 |
#include "atlas/field/Field.h" |
| 19 |
#include "atlas/functionspace/NodeColumns.h" |
| 20 |
#include "atlas/library/config.h" |
| 21 |
#include "atlas/mesh/IsGhostNode.h" |
| 22 |
#include "atlas/mesh/Nodes.h" |
| 23 |
#include "atlas/parallel/GatherScatter.h" |
| 24 |
#include "atlas/parallel/omp/omp.h" |
| 25 |
#include "atlas/runtime/Exception.h" |
| 26 |
#include "atlas/runtime/Trace.h" |
| 27 |
|
| 28 |
|
| 29 |
#undef atlas_omp_critical_ordered |
| 30 |
#define atlas_omp_critical_ordered atlas_omp_critical |
| 31 |
|
| 32 |
namespace atlas { |
| 33 |
namespace functionspace { |
| 34 |
namespace detail { |
| 35 |
|
| 36 |
namespace { |
| 37 |
|
| 38 |
template <typename T, typename Field> |
| 39 |
array::LocalView<T, 3> make_leveled_view(Field& field) { |
| 40 |
using namespace array; |
| 41 |
if (field.levels()) { |
| 42 |
if (field.variables()) { |
| 43 |
return make_view<T, 3>(field).slice(Range::all(), Range::all(), Range::all()); |
| 44 |
} |
| 45 |
else { |
| 46 |
return make_view<T, 2>(field).slice(Range::all(), Range::all(), Range::dummy()); |
| 47 |
} |
| 48 |
} |
| 49 |
else { |
| 50 |
if (field.variables()) { |
| 51 |
return make_view<T, 2>(field).slice(Range::all(), Range::dummy(), Range::all()); |
| 52 |
} |
| 53 |
else { |
| 54 |
return make_view<T, 1>(field).slice(Range::all(), Range::dummy(), Range::dummy()); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
template <typename T, typename Field> |
| 60 |
array::LocalView<T, 2> make_leveled_scalar_view(Field& field) { |
| 61 |
using namespace array; |
| 62 |
if (field.levels()) { |
| 63 |
return make_view<T, 2>(field).slice(Range::all(), Range::all()); |
| 64 |
} |
| 65 |
else { |
| 66 |
return make_view<T, 1>(field).slice(Range::all(), Range::dummy()); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
template <typename T, typename Field> |
| 71 |
array::LocalView<T, 2> make_surface_view(Field& field) { |
| 72 |
using namespace array; |
| 73 |
if (field.variables()) { |
| 74 |
return make_view<T, 2>(field).slice(Range::all(), Range::all()); |
| 75 |
} |
| 76 |
else { |
| 77 |
return make_view<T, 1>(field).slice(Range::all(), Range::dummy()); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
template <typename T, typename Field> |
| 82 |
array::LocalView<T, 2> make_per_level_view(Field& field) { |
| 83 |
using namespace array; |
| 84 |
if (field.rank() == 2) { |
| 85 |
return make_view<T, 2>(field).slice(Range::all(), Range::all()); |
| 86 |
} |
| 87 |
else { |
| 88 |
return make_view<T, 1>(field).slice(Range::all(), Range::dummy()); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
} // namespace |
| 93 |
|
| 94 |
namespace { |
| 95 |
inline double sqr(const double& val) { |
| 96 |
return val * val; |
| 97 |
} |
| 98 |
} // namespace |
| 99 |
|
| 100 |
namespace detail { // Collectives implementation |
| 101 |
|
| 102 |
template <typename T> |
| 103 |
void dispatch_sum(const NodeColumns& fs, const Field& field, T& result, idx_t& N) { |
| 104 |
const mesh::IsGhostNode is_ghost(fs.nodes()); |
| 105 |
const array::LocalView<const T, 2> arr = make_leveled_scalar_view<const T>(field); |
| 106 |
T local_sum = 0; |
| 107 |
const idx_t npts = std::min<idx_t>(arr.shape(0), fs.nb_nodes()); |
| 108 |
const idx_t nlev = arr.shape(1); |
| 109 |
atlas_omp_pragma( omp parallel for default(shared) reduction(+:local_sum) ) |
| 110 |
for( idx_t n=0; n<npts; ++n ) { |
| 111 |
if (!is_ghost(n)) { |
| 112 |
for (idx_t l = 0; l < nlev; ++l) { |
| 113 |
local_sum += arr(n, l); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm(fs.mpi_comm()).allReduce(local_sum, result, eckit::mpi::sum()); } |
| 118 |
|
| 119 |
N = fs.nb_nodes_global() * arr.shape(1); |
| 120 |
} |
| 121 |
|
| 122 |
template <typename T> |
| 123 |
void sum(const NodeColumns& fs, const Field& field, T& result, idx_t& N) { |
| 124 |
if (field.datatype() == array::DataType::kind<T>()) { |
| 125 |
dispatch_sum(fs, field, result, N); |
| 126 |
} |
| 127 |
else { |
| 128 |
switch (field.datatype().kind()) { |
| 129 |
case array::DataType::KIND_INT32: { |
| 130 |
int tmp; |
| 131 |
dispatch_sum(fs, field, tmp, N); |
| 132 |
result = tmp; |
| 133 |
return; |
| 134 |
} |
| 135 |
case array::DataType::KIND_INT64: { |
| 136 |
long tmp; |
| 137 |
dispatch_sum(fs, field, tmp, N); |
| 138 |
result = tmp; |
| 139 |
return; |
| 140 |
} |
| 141 |
case array::DataType::KIND_REAL32: { |
| 142 |
float tmp; |
| 143 |
dispatch_sum(fs, field, tmp, N); |
| 144 |
result = tmp; |
| 145 |
return; |
| 146 |
} |
| 147 |
case array::DataType::KIND_REAL64: { |
| 148 |
double tmp; |
| 149 |
dispatch_sum(fs, field, tmp, N); |
| 150 |
result = tmp; |
| 151 |
return; |
| 152 |
} |
| 153 |
default: |
| 154 |
throw_Exception("datatype not supported", Here()); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
template <typename T> |
| 160 |
void dispatch_sum(const NodeColumns& fs, const Field& field, std::vector<T>& result, idx_t& N) { |
| 161 |
auto arr = make_leveled_view<const T>(field); |
| 162 |
const mesh::IsGhostNode is_ghost(fs.nodes()); |
| 163 |
const idx_t npts = std::min(arr.shape(0), fs.nb_nodes()); |
| 164 |
const idx_t nlev = arr.shape(1); |
| 165 |
const idx_t nvar = arr.shape(2); |
| 166 |
std::vector<T> local_sum(nvar, 0); |
| 167 |
result.resize(nvar); |
| 168 |
|
| 169 |
atlas_omp_parallel { |
| 170 |
std::vector<T> local_sum_private(nvar, 0); |
| 171 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 172 |
if (!is_ghost(n)) { |
| 173 |
for (idx_t l = 0; l < nlev; ++l) { |
| 174 |
for (idx_t j = 0; j < nvar; ++j) { |
| 175 |
local_sum_private[j] += arr(n, l, j); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
atlas_omp_critical { |
| 181 |
for (idx_t j = 0; j < nvar; ++j) { |
| 182 |
local_sum[j] += local_sum_private[j]; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm(fs.mpi_comm()).allReduce(local_sum, result, eckit::mpi::sum()); } |
| 188 |
|
| 189 |
N = fs.nb_nodes_global() * nlev; |
| 190 |
} |
| 191 |
|
| 192 |
template <typename T> |
| 193 |
void sum(const NodeColumns& fs, const Field& field, std::vector<T>& result, idx_t& N) { |
| 194 |
if (field.datatype() == array::DataType::kind<T>()) { |
| 195 |
dispatch_sum(fs, field, result, N); |
| 196 |
} |
| 197 |
else { |
| 198 |
switch (field.datatype().kind()) { |
| 199 |
case array::DataType::KIND_INT32: { |
| 200 |
std::vector<int> tmp; |
| 201 |
dispatch_sum(fs, field, tmp, N); |
| 202 |
result.assign(tmp.begin(), tmp.end()); |
| 203 |
return; |
| 204 |
} |
| 205 |
case array::DataType::KIND_INT64: { |
| 206 |
std::vector<long> tmp; |
| 207 |
dispatch_sum(fs, field, tmp, N); |
| 208 |
result.assign(tmp.begin(), tmp.end()); |
| 209 |
return; |
| 210 |
} |
| 211 |
case array::DataType::KIND_REAL32: { |
| 212 |
std::vector<float> tmp; |
| 213 |
dispatch_sum(fs, field, tmp, N); |
| 214 |
result.assign(tmp.begin(), tmp.end()); |
| 215 |
return; |
| 216 |
} |
| 217 |
case array::DataType::KIND_REAL64: { |
| 218 |
std::vector<double> tmp; |
| 219 |
dispatch_sum(fs, field, tmp, N); |
| 220 |
result.assign(tmp.begin(), tmp.end()); |
| 221 |
return; |
| 222 |
} |
| 223 |
default: |
| 224 |
throw_Exception("datatype not supported", Here()); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
template <typename T> |
| 230 |
void dispatch_sum_per_level(const NodeColumns& fs, const Field& field, Field& sum, idx_t& N) { |
| 231 |
mesh::IsGhostNode is_ghost(fs.nodes()); |
| 232 |
|
| 233 |
array::ArrayShape shape; |
| 234 |
shape.reserve(field.rank() - 1); |
| 235 |
for (idx_t j = 1; j < field.rank(); ++j) { |
| 236 |
shape.push_back(field.shape(j)); |
| 237 |
} |
| 238 |
sum.resize(shape); |
| 239 |
|
| 240 |
auto arr = make_leveled_view<const T>(field); |
| 241 |
|
| 242 |
const idx_t npts = std::min(arr.shape(0), fs.nb_nodes()); |
| 243 |
const idx_t nlev = arr.shape(1); |
| 244 |
const idx_t nvar = arr.shape(2); |
| 245 |
|
| 246 |
auto sum_per_level = make_per_level_view<T>(sum); |
| 247 |
|
| 248 |
for (idx_t l = 0; l < sum_per_level.shape(0); ++l) { |
| 249 |
for (idx_t j = 0; j < sum_per_level.shape(1); ++j) { |
| 250 |
sum_per_level(l, j) = 0; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
atlas_omp_parallel { |
| 255 |
array::ArrayT<T> sum_per_level_private(sum_per_level.shape(0), sum_per_level.shape(1)); |
| 256 |
array::ArrayView<T, 2> sum_per_level_private_view = array::make_view<T, 2>(sum_per_level_private); |
| 257 |
|
| 258 |
for (idx_t l = 0; l < sum_per_level_private_view.shape(0); ++l) { |
| 259 |
for (idx_t j = 0; j < sum_per_level_private_view.shape(1); ++j) { |
| 260 |
sum_per_level_private_view(l, j) = 0; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 265 |
if (!is_ghost(n)) { |
| 266 |
for (idx_t l = 0; l < nlev; ++l) { |
| 267 |
for (idx_t j = 0; j < nvar; ++j) { |
| 268 |
sum_per_level_private_view(l, j) += arr(n, l, j); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
atlas_omp_critical { |
| 274 |
for (idx_t l = 0; l < sum_per_level_private.shape(0); ++l) { |
| 275 |
for (idx_t j = 0; j < sum_per_level_private.shape(1); ++j) { |
| 276 |
sum_per_level(l, j) += sum_per_level_private_view(l, j); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm(fs.mpi_comm()).allReduceInPlace(sum_per_level.data(), sum.size(), eckit::mpi::sum()); } |
| 282 |
N = fs.nb_nodes_global(); |
| 283 |
} |
| 284 |
|
| 285 |
void sum_per_level(const NodeColumns& fs, const Field& field, Field& sum, idx_t& N) { |
| 286 |
if (field.datatype() != sum.datatype()) { |
| 287 |
throw_Exception("Field and sum are not of same datatype.", Here()); |
| 288 |
} |
| 289 |
switch (field.datatype().kind()) { |
| 290 |
case array::DataType::KIND_INT32: |
| 291 |
return dispatch_sum_per_level<int>(fs, field, sum, N); |
| 292 |
case array::DataType::KIND_INT64: |
| 293 |
return dispatch_sum_per_level<long>(fs, field, sum, N); |
| 294 |
case array::DataType::KIND_REAL32: |
| 295 |
return dispatch_sum_per_level<float>(fs, field, sum, N); |
| 296 |
case array::DataType::KIND_REAL64: |
| 297 |
return dispatch_sum_per_level<double>(fs, field, sum, N); |
| 298 |
default: |
| 299 |
throw_Exception("datatype not supported", Here()); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
template <typename DATATYPE> |
| 304 |
void dispatch_order_independent_sum_2d(const NodeColumns& fs, const Field& field, DATATYPE& result, idx_t& N) { |
| 305 |
idx_t root = 0; |
| 306 |
Field global = fs.createField(field, option::global()); |
| 307 |
fs.gather(field, global); |
| 308 |
result = 0; |
| 309 |
auto glb = array::make_view<DATATYPE, 1>(global); |
| 310 |
for (idx_t jnode = 0; jnode < glb.size(); ++jnode) { |
| 311 |
result += glb(jnode); |
| 312 |
} |
| 313 |
ATLAS_TRACE_MPI(BROADCAST) { mpi::comm(fs.mpi_comm()).broadcast(&result, 1, root); } |
| 314 |
N = fs.nb_nodes_global(); |
| 315 |
} |
| 316 |
|
| 317 |
template <typename T> |
| 318 |
void dispatch_order_independent_sum(const NodeColumns& fs, const Field& field, T& result, idx_t& N) { |
| 319 |
if (field.levels()) { |
| 320 |
auto arr = make_leveled_scalar_view<const T>(field); |
| 321 |
|
| 322 |
Field surface_field = fs.createField<T>(option::name("surface") | option::levels(false)); |
| 323 |
auto surface = array::make_view<T, 1>(surface_field); |
| 324 |
|
| 325 |
const idx_t N0 = std::min(surface.shape(0), arr.shape(0)); |
| 326 |
const idx_t N1 = arr.shape(1); |
| 327 |
for (idx_t n = 0; n < N0; ++n) { |
| 328 |
surface(n) = 0; |
| 329 |
for (idx_t l = 0; l < N1; ++l) { |
| 330 |
surface(n) += arr(n, l); |
| 331 |
} |
| 332 |
} |
| 333 |
dispatch_order_independent_sum_2d(fs, surface_field, result, N); |
| 334 |
N *= arr.shape(1); |
| 335 |
} |
| 336 |
else { |
| 337 |
dispatch_order_independent_sum_2d(fs, field, result, N); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
template <typename T> |
| 342 |
void order_independent_sum(const NodeColumns& fs, const Field& field, T& result, idx_t& N) { |
| 343 |
if (field.datatype() == array::DataType::kind<T>()) { |
| 344 |
dispatch_order_independent_sum(fs, field, result, N); |
| 345 |
} |
| 346 |
else { |
| 347 |
switch (field.datatype().kind()) { |
| 348 |
case array::DataType::KIND_INT32: { |
| 349 |
int tmp; |
| 350 |
dispatch_order_independent_sum(fs, field, tmp, N); |
| 351 |
result = tmp; |
| 352 |
return; |
| 353 |
} |
| 354 |
case array::DataType::KIND_INT64: { |
| 355 |
long tmp; |
| 356 |
dispatch_order_independent_sum(fs, field, tmp, N); |
| 357 |
result = tmp; |
| 358 |
return; |
| 359 |
} |
| 360 |
case array::DataType::KIND_REAL32: { |
| 361 |
float tmp; |
| 362 |
dispatch_order_independent_sum(fs, field, tmp, N); |
| 363 |
result = tmp; |
| 364 |
return; |
| 365 |
} |
| 366 |
case array::DataType::KIND_REAL64: { |
| 367 |
double tmp; |
| 368 |
dispatch_order_independent_sum(fs, field, tmp, N); |
| 369 |
result = tmp; |
| 370 |
return; |
| 371 |
} |
| 372 |
default: |
| 373 |
throw_Exception("datatype not supported", Here()); |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
template <typename DATATYPE> |
| 379 |
void dispatch_order_independent_sum_2d(const NodeColumns& fs, const Field& field, std::vector<DATATYPE>& result, |
| 380 |
idx_t& N) { |
| 381 |
idx_t nvar = field.variables(); |
| 382 |
result.resize(nvar); |
| 383 |
for (idx_t j = 0; j < nvar; ++j) { |
| 384 |
result[j] = 0.; |
| 385 |
} |
| 386 |
Field global = fs.createField(field, option::name("global") | option::global()); |
| 387 |
fs.gather(field, global); |
| 388 |
if (mpi::rank() == 0) { |
| 389 |
const auto glb = make_surface_view<DATATYPE>(global); |
| 390 |
for (idx_t n = 0; n < fs.nb_nodes_global(); ++n) { |
| 391 |
for (idx_t j = 0; j < nvar; ++j) { |
| 392 |
result[j] += glb(n, j); |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
idx_t root = global.metadata().get<idx_t>("owner"); |
| 397 |
ATLAS_TRACE_MPI(BROADCAST) { mpi::comm(fs.mpi_comm()).broadcast(result, root); } |
| 398 |
N = fs.nb_nodes_global(); |
| 399 |
} |
| 400 |
|
| 401 |
template <typename T> |
| 402 |
void dispatch_order_independent_sum(const NodeColumns& fs, const Field& field, std::vector<T>& result, idx_t& N) { |
| 403 |
if (field.levels()) { |
| 404 |
const auto arr = make_leveled_view<const T>(field); |
| 405 |
const idx_t npts = std::min(arr.shape(0), fs.nb_nodes()); |
| 406 |
const idx_t nlev = arr.shape(1); |
| 407 |
const idx_t nvar = arr.shape(2); |
| 408 |
|
| 409 |
Field surface_field = |
| 410 |
fs.createField<T>(option::name("surface") | option::variables(nvar) | option::levels(false)); |
| 411 |
auto surface = make_surface_view<T>(surface_field); |
| 412 |
|
| 413 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 414 |
for (idx_t j = 0; j < nvar; ++j) { |
| 415 |
surface(n, j) = 0; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
for (idx_t n = 0; n < npts; ++n) { |
| 420 |
for (idx_t l = 0; l < nlev; ++l) { |
| 421 |
for (idx_t j = 0; j < nvar; ++j) { |
| 422 |
surface(n, j) += arr(n, l, j); |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
dispatch_order_independent_sum_2d(fs, surface_field, result, N); |
| 428 |
N *= arr.shape(1); |
| 429 |
} |
| 430 |
else { |
| 431 |
dispatch_order_independent_sum_2d(fs, field, result, N); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
template <typename T> |
| 436 |
void order_independent_sum(const NodeColumns& fs, const Field& field, std::vector<T>& result, idx_t& N) { |
| 437 |
if (field.datatype() == array::DataType::kind<T>()) { |
| 438 |
dispatch_order_independent_sum(fs, field, result, N); |
| 439 |
} |
| 440 |
else { |
| 441 |
switch (field.datatype().kind()) { |
| 442 |
case array::DataType::KIND_INT32: { |
| 443 |
std::vector<int> tmp; |
| 444 |
dispatch_order_independent_sum(fs, field, tmp, N); |
| 445 |
result.assign(tmp.begin(), tmp.end()); |
| 446 |
return; |
| 447 |
} |
| 448 |
case array::DataType::KIND_INT64: { |
| 449 |
std::vector<long> tmp; |
| 450 |
dispatch_order_independent_sum(fs, field, tmp, N); |
| 451 |
result.assign(tmp.begin(), tmp.end()); |
| 452 |
return; |
| 453 |
} |
| 454 |
case array::DataType::KIND_REAL32: { |
| 455 |
std::vector<float> tmp; |
| 456 |
dispatch_order_independent_sum(fs, field, tmp, N); |
| 457 |
result.assign(tmp.begin(), tmp.end()); |
| 458 |
return; |
| 459 |
} |
| 460 |
case array::DataType::KIND_REAL64: { |
| 461 |
std::vector<double> tmp; |
| 462 |
dispatch_order_independent_sum(fs, field, tmp, N); |
| 463 |
result.assign(tmp.begin(), tmp.end()); |
| 464 |
return; |
| 465 |
} |
| 466 |
default: |
| 467 |
throw_Exception("datatype not supported", Here()); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
template <typename T> |
| 473 |
void dispatch_order_independent_sum_per_level(const NodeColumns& fs, const Field& field, Field& sumfield, idx_t& N) { |
| 474 |
array::ArrayShape shape; |
| 475 |
shape.reserve(field.rank() - 1); |
| 476 |
for (idx_t j = 1; j < field.rank(); ++j) { |
| 477 |
shape.push_back(field.shape(j)); |
| 478 |
} |
| 479 |
sumfield.resize(shape); |
| 480 |
|
| 481 |
auto sum = make_per_level_view<T>(sumfield); |
| 482 |
for (idx_t l = 0; l < sum.shape(0); ++l) { |
| 483 |
for (idx_t j = 0; j < sum.shape(1); ++j) { |
| 484 |
sum(l, j) = 0.; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
idx_t root = 0; |
| 489 |
Field global = fs.createField(field, option::name("global") | option::global()); |
| 490 |
|
| 491 |
fs.gather(field, global); |
| 492 |
if (mpi::rank() == 0) { |
| 493 |
const array::LocalView<T, 3> glb = make_leveled_view<T>(global); |
| 494 |
|
| 495 |
for (idx_t n = 0; n < glb.shape(0); ++n) { |
| 496 |
for (idx_t l = 0; l < glb.shape(1); ++l) { |
| 497 |
for (idx_t j = 0; j < glb.shape(2); ++j) { |
| 498 |
sum(l, j) += glb(n, l, j); |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
ATLAS_TRACE_MPI(BROADCAST) { |
| 504 |
std::vector<T> sum_array(sumfield.size()); |
| 505 |
if (mpi::rank() == root) { |
| 506 |
idx_t c(0); |
| 507 |
for (idx_t l = 0; l < sum.shape(0); ++l) { |
| 508 |
for (idx_t j = 0; j < sum.shape(1); ++j) { |
| 509 |
sum_array[c++] = sum(l, j); |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
mpi::comm(fs.mpi_comm()).broadcast(sum_array, root); |
| 514 |
if (mpi::rank() != root) { |
| 515 |
idx_t c(0); |
| 516 |
for (idx_t l = 0; l < sum.shape(0); ++l) { |
| 517 |
for (idx_t j = 0; j < sum.shape(1); ++j) { |
| 518 |
sum(l, j) = sum_array[c++]; |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
N = fs.nb_nodes_global(); |
| 524 |
} |
| 525 |
|
| 526 |
void order_independent_sum_per_level(const NodeColumns& fs, const Field& field, Field& sum, idx_t& N) { |
| 527 |
if (field.datatype() != sum.datatype()) { |
| 528 |
throw_Exception("Field and sum are not of same datatype.", Here()); |
| 529 |
} |
| 530 |
switch (field.datatype().kind()) { |
| 531 |
case array::DataType::KIND_INT32: |
| 532 |
return dispatch_order_independent_sum_per_level<int>(fs, field, sum, N); |
| 533 |
case array::DataType::KIND_INT64: |
| 534 |
return dispatch_order_independent_sum_per_level<long>(fs, field, sum, N); |
| 535 |
case array::DataType::KIND_REAL32: |
| 536 |
return dispatch_order_independent_sum_per_level<float>(fs, field, sum, N); |
| 537 |
case array::DataType::KIND_REAL64: |
| 538 |
return dispatch_order_independent_sum_per_level<double>(fs, field, sum, N); |
| 539 |
default: |
| 540 |
throw_Exception("datatype not supported", Here()); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
template <typename T> |
| 545 |
void dispatch_minimum(const NodeColumns& fs, const Field& field, std::vector<T>& min) { |
| 546 |
auto arr = make_leveled_view<const T>(field); |
| 547 |
const idx_t nvar = arr.shape(2); |
| 548 |
min.resize(nvar); |
| 549 |
std::vector<T> local_minimum(nvar, std::numeric_limits<T>::max()); |
| 550 |
atlas_omp_parallel { |
| 551 |
std::vector<T> local_minimum_private(nvar, std::numeric_limits<T>::max()); |
| 552 |
const idx_t npts = std::min(arr.shape(0), fs.size()); |
| 553 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 554 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 555 |
for (idx_t j = 0; j < arr.shape(2); ++j) { |
| 556 |
local_minimum_private[j] = std::min(arr(n, l, j), local_minimum_private[j]); |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
atlas_omp_critical { |
| 561 |
for (idx_t j = 0; j < arr.shape(2); ++j) { |
| 562 |
local_minimum[j] = std::min(local_minimum_private[j], local_minimum[j]); |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm(fs.mpi_comm()).allReduce(local_minimum, min, eckit::mpi::min()); } |
| 568 |
} |
| 569 |
|
| 570 |
template <typename T> |
| 571 |
void minimum(const NodeColumns& fs, const Field& field, std::vector<T>& min) { |
| 572 |
if (field.datatype() == array::DataType::kind<T>()) { |
| 573 |
dispatch_minimum(fs, field, min); |
| 574 |
} |
| 575 |
else { |
| 576 |
switch (field.datatype().kind()) { |
| 577 |
case array::DataType::KIND_INT32: { |
| 578 |
std::vector<int> tmp; |
| 579 |
dispatch_minimum(fs, field, tmp); |
| 580 |
min.assign(tmp.begin(), tmp.end()); |
| 581 |
return; |
| 582 |
} |
| 583 |
case array::DataType::KIND_INT64: { |
| 584 |
std::vector<long> tmp; |
| 585 |
dispatch_minimum(fs, field, tmp); |
| 586 |
min.assign(tmp.begin(), tmp.end()); |
| 587 |
return; |
| 588 |
} |
| 589 |
case array::DataType::KIND_REAL32: { |
| 590 |
std::vector<float> tmp; |
| 591 |
dispatch_minimum(fs, field, tmp); |
| 592 |
min.assign(tmp.begin(), tmp.end()); |
| 593 |
return; |
| 594 |
} |
| 595 |
case array::DataType::KIND_REAL64: { |
| 596 |
std::vector<double> tmp; |
| 597 |
dispatch_minimum(fs, field, tmp); |
| 598 |
min.assign(tmp.begin(), tmp.end()); |
| 599 |
return; |
| 600 |
} |
| 601 |
default: |
| 602 |
throw_Exception("datatype not supported", Here()); |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
template <typename T> |
| 608 |
void dispatch_maximum(const NodeColumns& fs, const Field& field, std::vector<T>& max) { |
| 609 |
auto arr = make_leveled_view<const T>(field); |
| 610 |
const idx_t nvar = arr.shape(2); |
| 611 |
max.resize(nvar); |
| 612 |
std::vector<T> local_maximum(nvar, -std::numeric_limits<T>::max()); |
| 613 |
atlas_omp_parallel { |
| 614 |
std::vector<T> local_maximum_private(nvar, -std::numeric_limits<T>::max()); |
| 615 |
const idx_t npts = std::min(arr.shape(0), fs.size()); |
| 616 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 617 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 618 |
for (idx_t j = 0; j < nvar; ++j) { |
| 619 |
local_maximum_private[j] = std::max(arr(n, l, j), local_maximum_private[j]); |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
atlas_omp_critical { |
| 624 |
for (idx_t j = 0; j < nvar; ++j) { |
| 625 |
local_maximum[j] = std::max(local_maximum_private[j], local_maximum[j]); |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm(fs.mpi_comm()).allReduce(local_maximum, max, eckit::mpi::max()); } |
| 630 |
} |
| 631 |
|
| 632 |
template <typename T> |
| 633 |
void maximum(const NodeColumns& fs, const Field& field, std::vector<T>& max) { |
| 634 |
if (field.datatype() == array::DataType::kind<T>()) { |
| 635 |
dispatch_maximum(fs, field, max); |
| 636 |
} |
| 637 |
else { |
| 638 |
switch (field.datatype().kind()) { |
| 639 |
case array::DataType::KIND_INT32: { |
| 640 |
std::vector<int> tmp; |
| 641 |
dispatch_maximum(fs, field, tmp); |
| 642 |
max.assign(tmp.begin(), tmp.end()); |
| 643 |
return; |
| 644 |
} |
| 645 |
case array::DataType::KIND_INT64: { |
| 646 |
std::vector<long> tmp; |
| 647 |
dispatch_maximum(fs, field, tmp); |
| 648 |
max.assign(tmp.begin(), tmp.end()); |
| 649 |
return; |
| 650 |
} |
| 651 |
case array::DataType::KIND_REAL32: { |
| 652 |
std::vector<float> tmp; |
| 653 |
dispatch_maximum(fs, field, tmp); |
| 654 |
max.assign(tmp.begin(), tmp.end()); |
| 655 |
return; |
| 656 |
} |
| 657 |
case array::DataType::KIND_REAL64: { |
| 658 |
std::vector<double> tmp; |
| 659 |
dispatch_maximum(fs, field, tmp); |
| 660 |
max.assign(tmp.begin(), tmp.end()); |
| 661 |
return; |
| 662 |
} |
| 663 |
default: |
| 664 |
throw_Exception("datatype not supported", Here()); |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
template <typename T> |
| 670 |
void minimum(const NodeColumns& fs, const Field& field, T& min) { |
| 671 |
std::vector<T> v; |
| 672 |
minimum(fs, field, v); |
| 673 |
min = v[0]; |
| 674 |
} |
| 675 |
|
| 676 |
template <typename T> |
| 677 |
void maximum(const NodeColumns& fs, const Field& field, T& max) { |
| 678 |
std::vector<T> v; |
| 679 |
maximum(fs, field, v); |
| 680 |
max = v[0]; |
| 681 |
} |
| 682 |
|
| 683 |
template <typename T> |
| 684 |
void dispatch_minimum_per_level(const NodeColumns& fs, const Field& field, Field& min_field) { |
| 685 |
array::ArrayShape shape; |
| 686 |
shape.reserve(field.rank() - 1); |
| 687 |
for (idx_t j = 1; j < field.rank(); ++j) { |
| 688 |
shape.push_back(field.shape(j)); |
| 689 |
} |
| 690 |
min_field.resize(shape); |
| 691 |
auto min = make_per_level_view<T>(min_field); |
| 692 |
|
| 693 |
for (idx_t l = 0; l < min.shape(0); ++l) { |
| 694 |
for (idx_t j = 0; j < min.shape(1); ++j) { |
| 695 |
min(l, j) = std::numeric_limits<T>::max(); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
auto arr = make_leveled_view<const T>(field); |
| 700 |
atlas_omp_parallel { |
| 701 |
array::ArrayT<T> min_private(min.shape(0), min.shape(1)); |
| 702 |
array::ArrayView<T, 2> min_private_view = array::make_view<T, 2>(min_private); |
| 703 |
for (idx_t l = 0; l < min.shape(0); ++l) { |
| 704 |
for (idx_t j = 0; j < min.shape(1); ++j) { |
| 705 |
min_private_view(l, j) = std::numeric_limits<T>::max(); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
const idx_t npts = arr.shape(0); |
| 710 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 711 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 712 |
for (idx_t j = 0; j < arr.shape(2); ++j) { |
| 713 |
min_private_view(l, j) = std::min(arr(n, l, j), min_private_view(l, j)); |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
atlas_omp_critical { |
| 718 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 719 |
for (idx_t j = 0; j < arr.shape(2); ++j) { |
| 720 |
min(l, j) = std::min(min_private_view(l, j), min(l, j)); |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
} |
| 725 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm(fs.mpi_comm()).allReduceInPlace(min.data(), min_field.size(), eckit::mpi::min()); } |
| 726 |
} |
| 727 |
|
| 728 |
void minimum_per_level(const NodeColumns& fs, const Field& field, Field& min) { |
| 729 |
if (field.datatype() != min.datatype()) { |
| 730 |
throw_Exception("Field and min are not of same datatype.", Here()); |
| 731 |
} |
| 732 |
switch (field.datatype().kind()) { |
| 733 |
case array::DataType::KIND_INT32: |
| 734 |
return dispatch_minimum_per_level<int>(fs, field, min); |
| 735 |
case array::DataType::KIND_INT64: |
| 736 |
return dispatch_minimum_per_level<long>(fs, field, min); |
| 737 |
case array::DataType::KIND_REAL32: |
| 738 |
return dispatch_minimum_per_level<float>(fs, field, min); |
| 739 |
case array::DataType::KIND_REAL64: |
| 740 |
return dispatch_minimum_per_level<double>(fs, field, min); |
| 741 |
default: |
| 742 |
throw_Exception("datatype not supported", Here()); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
template <typename T> |
| 747 |
void dispatch_maximum_per_level(const NodeColumns& fs, const Field& field, Field& max_field) { |
| 748 |
array::ArrayShape shape; |
| 749 |
shape.reserve(field.rank() - 1); |
| 750 |
for (idx_t j = 1; j < field.rank(); ++j) { |
| 751 |
shape.push_back(field.shape(j)); |
| 752 |
} |
| 753 |
max_field.resize(shape); |
| 754 |
auto max = make_per_level_view<T>(max_field); |
| 755 |
|
| 756 |
for (idx_t l = 0; l < max.shape(0); ++l) { |
| 757 |
for (idx_t j = 0; j < max.shape(1); ++j) { |
| 758 |
max(l, j) = -std::numeric_limits<T>::max(); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
auto arr = make_leveled_view<const T>(field); |
| 763 |
atlas_omp_parallel { |
| 764 |
array::ArrayT<T> max_private(max.shape(0), max.shape(1)); |
| 765 |
array::ArrayView<T, 2> max_private_view = array::make_view<T, 2>(max_private); |
| 766 |
|
| 767 |
for (idx_t l = 0; l < max_private_view.shape(0); ++l) { |
| 768 |
for (idx_t j = 0; j < max_private_view.shape(1); ++j) { |
| 769 |
max_private_view(l, j) = -std::numeric_limits<T>::max(); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
const idx_t npts = arr.shape(0); |
| 774 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 775 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 776 |
for (idx_t j = 0; j < arr.shape(2); ++j) { |
| 777 |
max_private_view(l, j) = std::max(arr(n, l, j), max_private_view(l, j)); |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
atlas_omp_critical { |
| 782 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 783 |
for (idx_t j = 0; j < arr.shape(2); ++j) { |
| 784 |
max(l, j) = std::max(max_private_view(l, j), max(l, j)); |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
} |
| 789 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm(fs.mpi_comm()).allReduceInPlace(max.data(), max_field.size(), eckit::mpi::max()); } |
| 790 |
} |
| 791 |
|
| 792 |
void maximum_per_level(const NodeColumns& fs, const Field& field, Field& max) { |
| 793 |
if (field.datatype() != max.datatype()) { |
| 794 |
throw_Exception("Field and max are not of same datatype.", Here()); |
| 795 |
} |
| 796 |
switch (field.datatype().kind()) { |
| 797 |
case array::DataType::KIND_INT32: |
| 798 |
return dispatch_maximum_per_level<int>(fs, field, max); |
| 799 |
case array::DataType::KIND_INT64: |
| 800 |
return dispatch_maximum_per_level<long>(fs, field, max); |
| 801 |
case array::DataType::KIND_REAL32: |
| 802 |
return dispatch_maximum_per_level<float>(fs, field, max); |
| 803 |
case array::DataType::KIND_REAL64: |
| 804 |
return dispatch_maximum_per_level<double>(fs, field, max); |
| 805 |
default: |
| 806 |
throw_Exception("datatype not supported", Here()); |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
template <typename T> |
| 811 |
void dispatch_minimum_and_location(const NodeColumns& fs, const Field& field, std::vector<T>& min, |
| 812 |
std::vector<gidx_t>& glb_idx, std::vector<idx_t>& level) { |
| 813 |
auto arr = make_leveled_view<const T>(field); |
| 814 |
idx_t nvar = arr.shape(2); |
| 815 |
min.resize(nvar); |
| 816 |
glb_idx.resize(nvar); |
| 817 |
level.resize(nvar); |
| 818 |
std::vector<T> local_minimum(nvar, std::numeric_limits<T>::max()); |
| 819 |
std::vector<idx_t> loc_node(nvar); |
| 820 |
std::vector<idx_t> loc_level(nvar); |
| 821 |
atlas_omp_parallel { |
| 822 |
std::vector<T> local_minimum_private(nvar, std::numeric_limits<T>::max()); |
| 823 |
std::vector<idx_t> loc_node_private(nvar); |
| 824 |
std::vector<idx_t> loc_level_private(nvar); |
| 825 |
const idx_t npts = arr.shape(0); |
| 826 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 827 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 828 |
for (idx_t j = 0; j < nvar; ++j) { |
| 829 |
if (arr(n, l, j) < local_minimum_private[j]) { |
| 830 |
local_minimum_private[j] = arr(n, l, j); |
| 831 |
loc_node_private[j] = n; |
| 832 |
loc_level_private[j] = l; |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
atlas_omp_critical_ordered { |
| 838 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 839 |
for (idx_t j = 0; j < nvar; ++j) { |
| 840 |
if (local_minimum_private[j] < local_minimum[j]) { |
| 841 |
local_minimum[j] = local_minimum_private[j]; |
| 842 |
loc_node[j] = loc_node_private[j]; |
| 843 |
loc_level[j] = loc_level_private[j]; |
| 844 |
} |
| 845 |
} |
| 846 |
} |
| 847 |
} |
| 848 |
} |
| 849 |
std::vector<std::pair<T, int>> min_and_gidx_loc(nvar); |
| 850 |
std::vector<std::pair<T, int>> min_and_level_loc(nvar); |
| 851 |
std::vector<std::pair<T, int>> min_and_gidx_glb(nvar); |
| 852 |
std::vector<std::pair<T, int>> min_and_level_glb(nvar); |
| 853 |
const array::ArrayView<gidx_t, 1> global_index = array::make_view<gidx_t, 1>(fs.nodes().global_index()); |
| 854 |
for (idx_t j = 0; j < nvar; ++j) { |
| 855 |
gidx_t glb_idx = global_index(loc_node[j]); |
| 856 |
ATLAS_ASSERT(glb_idx < std::numeric_limits<int>::max()); // pairs with 64bit |
| 857 |
// integer for second not |
| 858 |
// implemented |
| 859 |
min_and_gidx_loc[j] = std::make_pair(local_minimum[j], glb_idx); |
| 860 |
min_and_level_loc[j] = std::make_pair(local_minimum[j], loc_level[j]); |
| 861 |
} |
| 862 |
|
| 863 |
ATLAS_TRACE_MPI(ALLREDUCE) { |
| 864 |
mpi::comm(fs.mpi_comm()).allReduce(min_and_gidx_loc, min_and_gidx_glb, eckit::mpi::minloc()); |
| 865 |
mpi::comm(fs.mpi_comm()).allReduce(min_and_level_loc, min_and_level_glb, eckit::mpi::minloc()); |
| 866 |
} |
| 867 |
|
| 868 |
for (idx_t j = 0; j < nvar; ++j) { |
| 869 |
min[j] = min_and_gidx_glb[j].first; |
| 870 |
glb_idx[j] = min_and_gidx_glb[j].second; |
| 871 |
level[j] = min_and_level_glb[j].second; |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
template <typename T> |
| 876 |
void minimum_and_location(const NodeColumns& fs, const Field& field, std::vector<T>& min, std::vector<gidx_t>& glb_idx, |
| 877 |
std::vector<idx_t>& level) { |
| 878 |
if (field.datatype() == array::DataType::kind<T>()) { |
| 879 |
return dispatch_minimum_and_location(fs, field, min, glb_idx, level); |
| 880 |
} |
| 881 |
else { |
| 882 |
switch (field.datatype().kind()) { |
| 883 |
case array::DataType::KIND_INT32: { |
| 884 |
std::vector<int> tmp; |
| 885 |
dispatch_minimum_and_location(fs, field, tmp, glb_idx, level); |
| 886 |
min.assign(tmp.begin(), tmp.end()); |
| 887 |
return; |
| 888 |
} |
| 889 |
case array::DataType::KIND_INT64: { |
| 890 |
std::vector<long> tmp; |
| 891 |
dispatch_minimum_and_location(fs, field, tmp, glb_idx, level); |
| 892 |
min.assign(tmp.begin(), tmp.end()); |
| 893 |
return; |
| 894 |
} |
| 895 |
case array::DataType::KIND_REAL32: { |
| 896 |
std::vector<float> tmp; |
| 897 |
dispatch_minimum_and_location(fs, field, tmp, glb_idx, level); |
| 898 |
min.assign(tmp.begin(), tmp.end()); |
| 899 |
return; |
| 900 |
} |
| 901 |
case array::DataType::KIND_REAL64: { |
| 902 |
std::vector<double> tmp; |
| 903 |
dispatch_minimum_and_location(fs, field, tmp, glb_idx, level); |
| 904 |
min.assign(tmp.begin(), tmp.end()); |
| 905 |
return; |
| 906 |
} |
| 907 |
default: |
| 908 |
throw_Exception("datatype not supported", Here()); |
| 909 |
} |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
template <typename T> |
| 914 |
void dispatch_maximum_and_location(const NodeColumns& fs, const Field& field, std::vector<T>& max, |
| 915 |
std::vector<gidx_t>& glb_idx, std::vector<idx_t>& level) { |
| 916 |
auto arr = make_leveled_view<const T>(field); |
| 917 |
idx_t nvar = arr.shape(2); |
| 918 |
max.resize(nvar); |
| 919 |
glb_idx.resize(nvar); |
| 920 |
level.resize(nvar); |
| 921 |
std::vector<T> local_maximum(nvar, -std::numeric_limits<T>::max()); |
| 922 |
std::vector<idx_t> loc_node(nvar); |
| 923 |
std::vector<idx_t> loc_level(nvar); |
| 924 |
atlas_omp_parallel { |
| 925 |
std::vector<T> local_maximum_private(nvar, -std::numeric_limits<T>::max()); |
| 926 |
std::vector<idx_t> loc_node_private(nvar); |
| 927 |
std::vector<idx_t> loc_level_private(nvar); |
| 928 |
const idx_t npts = arr.shape(0); |
| 929 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 930 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 931 |
for (idx_t j = 0; j < nvar; ++j) { |
| 932 |
if (arr(n, l, j) > local_maximum_private[j]) { |
| 933 |
local_maximum_private[j] = arr(n, l, j); |
| 934 |
loc_node_private[j] = n; |
| 935 |
loc_level_private[j] = l; |
| 936 |
} |
| 937 |
} |
| 938 |
} |
| 939 |
} |
| 940 |
atlas_omp_critical_ordered { |
| 941 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 942 |
for (idx_t j = 0; j < nvar; ++j) { |
| 943 |
if (local_maximum_private[j] > local_maximum[j]) { |
| 944 |
local_maximum[j] = local_maximum_private[j]; |
| 945 |
loc_node[j] = loc_node_private[j]; |
| 946 |
loc_level[j] = loc_level_private[j]; |
| 947 |
} |
| 948 |
} |
| 949 |
} |
| 950 |
} |
| 951 |
} |
| 952 |
std::vector<std::pair<T, int>> max_and_gidx_loc(nvar); |
| 953 |
std::vector<std::pair<T, int>> max_and_level_loc(nvar); |
| 954 |
std::vector<std::pair<T, int>> max_and_gidx_glb(nvar); |
| 955 |
std::vector<std::pair<T, int>> max_and_level_glb(nvar); |
| 956 |
const array::ArrayView<gidx_t, 1> global_index = array::make_view<gidx_t, 1>(fs.nodes().global_index()); |
| 957 |
for (idx_t j = 0; j < nvar; ++j) { |
| 958 |
gidx_t glb_idx = global_index(loc_node[j]); |
| 959 |
ATLAS_ASSERT(glb_idx < std::numeric_limits<int>::max()); // pairs with 64bit |
| 960 |
// integer for second not |
| 961 |
// implemented |
| 962 |
max_and_gidx_loc[j] = std::make_pair(local_maximum[j], glb_idx); |
| 963 |
max_and_level_loc[j] = std::make_pair(local_maximum[j], loc_level[j]); |
| 964 |
} |
| 965 |
|
| 966 |
ATLAS_TRACE_MPI(ALLREDUCE) { |
| 967 |
mpi::comm(fs.mpi_comm()).allReduce(max_and_gidx_loc, max_and_gidx_glb, eckit::mpi::maxloc()); |
| 968 |
mpi::comm(fs.mpi_comm()).allReduce(max_and_level_loc, max_and_level_glb, eckit::mpi::maxloc()); |
| 969 |
} |
| 970 |
|
| 971 |
for (idx_t j = 0; j < nvar; ++j) { |
| 972 |
max[j] = max_and_gidx_glb[j].first; |
| 973 |
glb_idx[j] = max_and_gidx_glb[j].second; |
| 974 |
level[j] = max_and_level_glb[j].second; |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
template <typename T> |
| 979 |
void maximum_and_location(const NodeColumns& fs, const Field& field, std::vector<T>& max, std::vector<gidx_t>& glb_idx, |
| 980 |
std::vector<idx_t>& level) { |
| 981 |
if (field.datatype() == array::DataType::kind<T>()) { |
| 982 |
return dispatch_maximum_and_location(fs, field, max, glb_idx, level); |
| 983 |
} |
| 984 |
else { |
| 985 |
switch (field.datatype().kind()) { |
| 986 |
case array::DataType::KIND_INT32: { |
| 987 |
std::vector<int> tmp; |
| 988 |
dispatch_maximum_and_location(fs, field, tmp, glb_idx, level); |
| 989 |
max.assign(tmp.begin(), tmp.end()); |
| 990 |
return; |
| 991 |
} |
| 992 |
case array::DataType::KIND_INT64: { |
| 993 |
std::vector<long> tmp; |
| 994 |
dispatch_maximum_and_location(fs, field, tmp, glb_idx, level); |
| 995 |
max.assign(tmp.begin(), tmp.end()); |
| 996 |
return; |
| 997 |
} |
| 998 |
case array::DataType::KIND_REAL32: { |
| 999 |
std::vector<float> tmp; |
| 1000 |
dispatch_maximum_and_location(fs, field, tmp, glb_idx, level); |
| 1001 |
max.assign(tmp.begin(), tmp.end()); |
| 1002 |
return; |
| 1003 |
} |
| 1004 |
case array::DataType::KIND_REAL64: { |
| 1005 |
std::vector<double> tmp; |
| 1006 |
dispatch_maximum_and_location(fs, field, tmp, glb_idx, level); |
| 1007 |
max.assign(tmp.begin(), tmp.end()); |
| 1008 |
return; |
| 1009 |
} |
| 1010 |
default: |
| 1011 |
throw_Exception("datatype not supported", Here()); |
| 1012 |
} |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
template <typename T> |
| 1017 |
void minimum_and_location(const NodeColumns& fs, const Field& field, std::vector<T>& min, |
| 1018 |
std::vector<gidx_t>& glb_idx) { |
| 1019 |
std::vector<idx_t> level; |
| 1020 |
minimum_and_location(fs, field, min, glb_idx, level); |
| 1021 |
} |
| 1022 |
|
| 1023 |
template <typename T> |
| 1024 |
void maximum_and_location(const NodeColumns& fs, const Field& field, std::vector<T>& max, |
| 1025 |
std::vector<gidx_t>& glb_idx) { |
| 1026 |
std::vector<idx_t> level; |
| 1027 |
maximum_and_location(fs, field, max, glb_idx, level); |
| 1028 |
} |
| 1029 |
|
| 1030 |
template <typename T> |
| 1031 |
void minimum_and_location(const NodeColumns& fs, const Field& field, T& min, gidx_t& glb_idx, idx_t& level) { |
| 1032 |
std::vector<T> minv; |
| 1033 |
std::vector<gidx_t> gidxv; |
| 1034 |
std::vector<idx_t> levelv; |
| 1035 |
minimum_and_location(fs, field, minv, gidxv, levelv); |
| 1036 |
min = minv[0]; |
| 1037 |
glb_idx = gidxv[0]; |
| 1038 |
level = levelv[0]; |
| 1039 |
} |
| 1040 |
|
| 1041 |
template <typename T> |
| 1042 |
void maximum_and_location(const NodeColumns& fs, const Field& field, T& max, gidx_t& glb_idx, idx_t& level) { |
| 1043 |
std::vector<T> maxv; |
| 1044 |
std::vector<gidx_t> gidxv; |
| 1045 |
std::vector<idx_t> levelv; |
| 1046 |
maximum_and_location(fs, field, maxv, gidxv, levelv); |
| 1047 |
max = maxv[0]; |
| 1048 |
glb_idx = gidxv[0]; |
| 1049 |
level = levelv[0]; |
| 1050 |
} |
| 1051 |
|
| 1052 |
template <typename T> |
| 1053 |
void minimum_and_location(const NodeColumns& fs, const Field& field, T& min, gidx_t& glb_idx) { |
| 1054 |
idx_t level; |
| 1055 |
minimum_and_location(fs, field, min, glb_idx, level); |
| 1056 |
} |
| 1057 |
|
| 1058 |
template <typename T> |
| 1059 |
void maximum_and_location(const NodeColumns& fs, const Field& field, T& max, gidx_t& glb_idx) { |
| 1060 |
idx_t level; |
| 1061 |
maximum_and_location(fs, field, max, glb_idx, level); |
| 1062 |
} |
| 1063 |
|
| 1064 |
template <typename T> |
| 1065 |
void dispatch_minimum_and_location_per_level(const NodeColumns& fs, const Field& field, Field& min_field, |
| 1066 |
Field& glb_idx_field) { |
| 1067 |
auto arr = make_leveled_view<const T>(field); |
| 1068 |
array::ArrayShape shape; |
| 1069 |
shape.reserve(field.rank() - 1); |
| 1070 |
for (idx_t j = 1; j < field.rank(); ++j) { |
| 1071 |
shape.push_back(field.shape(j)); |
| 1072 |
} |
| 1073 |
min_field.resize(shape); |
| 1074 |
glb_idx_field.resize(shape); |
| 1075 |
const idx_t nvar = arr.shape(2); |
| 1076 |
auto min = make_per_level_view<T>(min_field); |
| 1077 |
auto glb_idx = make_per_level_view<gidx_t>(glb_idx_field); |
| 1078 |
|
| 1079 |
for (idx_t l = 0; l < min.shape(0); ++l) { |
| 1080 |
for (idx_t j = 0; j < min.shape(1); ++j) { |
| 1081 |
min(l, j) = std::numeric_limits<T>::max(); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
atlas_omp_parallel { |
| 1086 |
array::ArrayT<T> min_private(min.shape(0), min.shape(1)); |
| 1087 |
array::ArrayView<T, 2> min_private_view = array::make_view<T, 2>(min_private); |
| 1088 |
|
| 1089 |
for (idx_t l = 0; l < min_private_view.shape(0); ++l) { |
| 1090 |
for (idx_t j = 0; j < min_private_view.shape(1); ++j) { |
| 1091 |
min_private_view(l, j) = std::numeric_limits<T>::max(); |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
array::ArrayT<gidx_t> glb_idx_private(glb_idx.shape(0), glb_idx.shape(1)); |
| 1096 |
array::ArrayView<gidx_t, 2> glb_idx_private_view = array::make_view<gidx_t, 2>(glb_idx_private); |
| 1097 |
const idx_t npts = arr.shape(0); |
| 1098 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 1099 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 1100 |
for (idx_t j = 0; j < nvar; ++j) { |
| 1101 |
if (arr(n, l, j) < min(l, j)) { |
| 1102 |
min_private_view(l, j) = arr(n, l, j); |
| 1103 |
glb_idx_private_view(l, j) = n; |
| 1104 |
} |
| 1105 |
} |
| 1106 |
} |
| 1107 |
} |
| 1108 |
atlas_omp_critical_ordered { |
| 1109 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 1110 |
for (idx_t j = 0; j < nvar; ++j) { |
| 1111 |
if (min_private_view(l, j) < min(l, j)) { |
| 1112 |
min(l, j) = min_private_view(l, j); |
| 1113 |
glb_idx(l, j) = glb_idx_private_view(l, j); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
} |
| 1117 |
} |
| 1118 |
} |
| 1119 |
const idx_t nlev = arr.shape(1); |
| 1120 |
std::vector<std::pair<T, int>> min_and_gidx_loc(nlev * nvar); |
| 1121 |
std::vector<std::pair<T, int>> min_and_gidx_glb(nlev * nvar); |
| 1122 |
const array::ArrayView<gidx_t, 1> global_index = array::make_view<gidx_t, 1>(fs.nodes().global_index()); |
| 1123 |
atlas_omp_parallel_for(idx_t l = 0; l < nlev; ++l) { |
| 1124 |
for (idx_t j = 0; j < nvar; ++j) { |
| 1125 |
gidx_t gidx = global_index(glb_idx(l, j)); |
| 1126 |
ATLAS_ASSERT(gidx < std::numeric_limits<int>::max()); // pairs with 64bit |
| 1127 |
// integer for second not |
| 1128 |
// implemented |
| 1129 |
min_and_gidx_loc[j + nvar * l] = std::make_pair(min(l, j), gidx); |
| 1130 |
} |
| 1131 |
} |
| 1132 |
|
| 1133 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm(fs.mpi_comm()).allReduce(min_and_gidx_loc, min_and_gidx_glb, eckit::mpi::minloc()); } |
| 1134 |
|
| 1135 |
atlas_omp_parallel_for(idx_t l = 0; l < nlev; ++l) { |
| 1136 |
for (idx_t j = 0; j < nvar; ++j) { |
| 1137 |
min(l, j) = min_and_gidx_glb[j + l * nvar].first; |
| 1138 |
glb_idx(l, j) = min_and_gidx_glb[j + l * nvar].second; |
| 1139 |
} |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
void minimum_and_location_per_level(const NodeColumns& fs, const Field& field, Field& min, Field& glb_idx) { |
| 1144 |
if (field.datatype() != min.datatype()) { |
| 1145 |
throw_Exception("Field and min are not of same datatype.", Here()); |
| 1146 |
} |
| 1147 |
if (glb_idx.datatype() != array::DataType::kind<gidx_t>()) { |
| 1148 |
throw_Exception("glb_idx Field is not of correct datatype", Here()); |
| 1149 |
} |
| 1150 |
switch (field.datatype().kind()) { |
| 1151 |
case array::DataType::KIND_INT32: |
| 1152 |
return dispatch_minimum_and_location_per_level<int>(fs, field, min, glb_idx); |
| 1153 |
case array::DataType::KIND_INT64: |
| 1154 |
return dispatch_minimum_and_location_per_level<long>(fs, field, min, glb_idx); |
| 1155 |
case array::DataType::KIND_REAL32: |
| 1156 |
return dispatch_minimum_and_location_per_level<float>(fs, field, min, glb_idx); |
| 1157 |
case array::DataType::KIND_REAL64: |
| 1158 |
return dispatch_minimum_and_location_per_level<double>(fs, field, min, glb_idx); |
| 1159 |
default: |
| 1160 |
throw_Exception("datatype not supported", Here()); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
|
| 1164 |
template <typename T> |
| 1165 |
void dispatch_maximum_and_location_per_level(const NodeColumns& fs, const Field& field, Field& max_field, |
| 1166 |
Field& glb_idx_field) { |
| 1167 |
auto arr = make_leveled_view<const T>(field); |
| 1168 |
array::ArrayShape shape; |
| 1169 |
shape.reserve(field.rank() - 1); |
| 1170 |
for (idx_t j = 1; j < field.rank(); ++j) { |
| 1171 |
shape.push_back(field.shape(j)); |
| 1172 |
} |
| 1173 |
max_field.resize(shape); |
| 1174 |
glb_idx_field.resize(shape); |
| 1175 |
const idx_t nvar = arr.shape(2); |
| 1176 |
auto max = make_per_level_view<T>(max_field); |
| 1177 |
auto glb_idx = make_per_level_view<gidx_t>(glb_idx_field); |
| 1178 |
|
| 1179 |
for (idx_t l = 0; l < max.shape(0); ++l) { |
| 1180 |
for (idx_t j = 0; j < max.shape(1); ++j) { |
| 1181 |
max(l, j) = -std::numeric_limits<T>::max(); |
| 1182 |
} |
| 1183 |
} |
| 1184 |
|
| 1185 |
atlas_omp_parallel { |
| 1186 |
array::ArrayT<T> max_private(max.shape(0), max.shape(1)); |
| 1187 |
array::ArrayView<T, 2> max_private_view = array::make_view<T, 2>(max_private); |
| 1188 |
|
| 1189 |
for (idx_t l = 0; l < max_private_view.shape(0); ++l) { |
| 1190 |
for (idx_t j = 0; j < max_private_view.shape(1); ++j) { |
| 1191 |
max_private_view(l, j) = -std::numeric_limits<T>::max(); |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
array::ArrayT<gidx_t> glb_idx_private(glb_idx.shape(0), glb_idx.shape(1)); |
| 1196 |
array::ArrayView<gidx_t, 2> glb_idx_private_view = array::make_view<gidx_t, 2>(glb_idx_private); |
| 1197 |
const idx_t npts = arr.shape(0); |
| 1198 |
atlas_omp_for(idx_t n = 0; n < npts; ++n) { |
| 1199 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 1200 |
for (idx_t j = 0; j < nvar; ++j) { |
| 1201 |
if (arr(n, l, j) > max(l, j)) { |
| 1202 |
max_private_view(l, j) = arr(n, l, j); |
| 1203 |
glb_idx_private_view(l, j) = n; |
| 1204 |
} |
| 1205 |
} |
| 1206 |
} |
| 1207 |
} |
| 1208 |
atlas_omp_critical_ordered { |
| 1209 |
for (idx_t l = 0; l < arr.shape(1); ++l) { |
| 1210 |
for (idx_t j = 0; j < nvar; ++j) { |
| 1211 |
if (max_private_view(l, j) > max(l, j)) { |
| 1212 |
max(l, j) = max_private_view(l, j); |
| 1213 |
glb_idx(l, j) = glb_idx_private_view(l, j); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
} |
| 1217 |
} |
| 1218 |
} |
| 1219 |
|
| 1220 |
const idx_t nlev = arr.shape(1); |
| 1221 |
std::vector<std::pair<T, int>> max_and_gidx_loc(nlev * nvar); |
| 1222 |
std::vector<std::pair<T, int>> max_and_gidx_glb(nlev * nvar); |
| 1223 |
const array::ArrayView<gidx_t, 1> global_index = array::make_view<gidx_t, 1>(fs.nodes().global_index()); |
| 1224 |
atlas_omp_parallel_for(idx_t l = 0; l < nlev; ++l) { |
| 1225 |
for (idx_t j = 0; j < nvar; ++j) { |
| 1226 |
gidx_t gidx = global_index(glb_idx(l, j)); |
| 1227 |
ATLAS_ASSERT(gidx < std::numeric_limits<int>::max()); // pairs with 64bit |
| 1228 |
// integer for second not |
| 1229 |
// implemented |
| 1230 |
max_and_gidx_loc[j + nvar * l] = std::make_pair(max(l, j), gidx); |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm(fs.mpi_comm()).allReduce(max_and_gidx_loc, max_and_gidx_glb, eckit::mpi::maxloc()); } |
| 1235 |
|
| 1236 |
atlas_omp_parallel_for(idx_t l = 0; l < nlev; ++l) { |
| 1237 |
for (idx_t j = 0; j < nvar; ++j) { |
| 1238 |
max(l, j) = max_and_gidx_glb[j + l * nvar].first; |
| 1239 |
glb_idx(l, j) = max_and_gidx_glb[j + l * nvar].second; |
| 1240 |
} |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
void maximum_and_location_per_level(const NodeColumns& fs, const Field& field, Field& max, Field& glb_idx) { |
| 1245 |
if (field.datatype() != max.datatype()) { |
| 1246 |
throw_Exception("Field and max are not of same datatype.", Here()); |
| 1247 |
} |
| 1248 |
if (glb_idx.datatype() != array::DataType::kind<gidx_t>()) { |
| 1249 |
throw_Exception("glb_idx Field is not of correct datatype", Here()); |
| 1250 |
} |
| 1251 |
switch (field.datatype().kind()) { |
| 1252 |
case array::DataType::KIND_INT32: |
| 1253 |
return dispatch_maximum_and_location_per_level<int>(fs, field, max, glb_idx); |
| 1254 |
case array::DataType::KIND_INT64: |
| 1255 |
return dispatch_maximum_and_location_per_level<long>(fs, field, max, glb_idx); |
| 1256 |
case array::DataType::KIND_REAL32: |
| 1257 |
return dispatch_maximum_and_location_per_level<float>(fs, field, max, glb_idx); |
| 1258 |
case array::DataType::KIND_REAL64: |
| 1259 |
return dispatch_maximum_and_location_per_level<double>(fs, field, max, glb_idx); |
| 1260 |
default: |
| 1261 |
throw_Exception("datatype not supported", Here()); |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
template <typename T> |
| 1266 |
void mean(const NodeColumns& fs, const Field& field, T& result, idx_t& N) { |
| 1267 |
sum(fs, field, result, N); |
| 1268 |
result /= static_cast<double>(N); |
| 1269 |
} |
| 1270 |
|
| 1271 |
template <typename T> |
| 1272 |
void mean(const NodeColumns& fs, const Field& field, std::vector<T>& result, idx_t& N) { |
| 1273 |
sum(fs, field, result, N); |
| 1274 |
for (size_t j = 0; j < result.size(); ++j) { |
| 1275 |
result[j] /= static_cast<double>(N); |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
template <typename T> |
| 1280 |
void dispatch_mean_per_level(const NodeColumns& fs, const Field& field, Field& mean, idx_t& N) { |
| 1281 |
dispatch_sum_per_level<T>(fs, field, mean, N); |
| 1282 |
auto view = make_per_level_view<T>(mean); |
| 1283 |
for (idx_t l = 0; l < view.shape(0); ++l) { |
| 1284 |
for (idx_t j = 0; j < view.shape(1); ++j) { |
| 1285 |
view(l, j) /= static_cast<double>(N); |
| 1286 |
} |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
void mean_per_level(const NodeColumns& fs, const Field& field, Field& mean, idx_t& N) { |
| 1291 |
if (field.datatype() != mean.datatype()) { |
| 1292 |
throw_Exception("Field and sum are not of same datatype.", Here()); |
| 1293 |
} |
| 1294 |
switch (field.datatype().kind()) { |
| 1295 |
case array::DataType::KIND_INT32: |
| 1296 |
return dispatch_mean_per_level<int>(fs, field, mean, N); |
| 1297 |
case array::DataType::KIND_INT64: |
| 1298 |
return dispatch_mean_per_level<long>(fs, field, mean, N); |
| 1299 |
case array::DataType::KIND_REAL32: |
| 1300 |
return dispatch_mean_per_level<float>(fs, field, mean, N); |
| 1301 |
case array::DataType::KIND_REAL64: |
| 1302 |
return dispatch_mean_per_level<double>(fs, field, mean, N); |
| 1303 |
default: |
| 1304 |
throw_Exception("datatype not supported", Here()); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
template <typename T> |
| 1309 |
void mean_and_standard_deviation(const NodeColumns& fs, const Field& field, T& mu, T& sigma, idx_t& N) { |
| 1310 |
mean(fs, field, mu, N); |
| 1311 |
Field squared_diff_field = |
| 1312 |
fs.createField(option::name("sqr_diff") | option::datatype(field.datatype()) | option::levels(field.levels())); |
| 1313 |
|
| 1314 |
auto squared_diff = make_leveled_scalar_view<T>(squared_diff_field); |
| 1315 |
auto values = make_leveled_scalar_view<const T>(field); |
| 1316 |
|
| 1317 |
const idx_t npts = std::min<idx_t>(values.shape(0), fs.nb_nodes()); |
| 1318 |
atlas_omp_parallel_for(idx_t n = 0; n < npts; ++n) { |
| 1319 |
for (idx_t l = 0; l < values.shape(1); ++l) { |
| 1320 |
squared_diff(n, l) = sqr(values(n, l) - mu); |
| 1321 |
} |
| 1322 |
} |
| 1323 |
mean(fs, squared_diff_field, sigma, N); |
| 1324 |
sigma = std::sqrt(sigma); |
| 1325 |
} |
| 1326 |
|
| 1327 |
template <typename T> |
| 1328 |
void mean_and_standard_deviation(const NodeColumns& fs, const Field& field, std::vector<T>& mu, std::vector<T>& sigma, |
| 1329 |
idx_t& N) { |
| 1330 |
mean(fs, field, mu, N); |
| 1331 |
Field squared_diff_field = fs.createField<T>(option::name("sqr_diff") | option::levels(field.levels()) | |
| 1332 |
option::variables(field.variables())); |
| 1333 |
auto squared_diff = make_leveled_view<T>(squared_diff_field); |
| 1334 |
auto values = make_leveled_view<const T>(field); |
| 1335 |
|
| 1336 |
const idx_t npts = std::min<idx_t>(values.shape(0), fs.nb_nodes()); |
| 1337 |
atlas_omp_parallel_for(idx_t n = 0; n < npts; ++n) { |
| 1338 |
for (idx_t l = 0; l < values.shape(1); ++l) { |
| 1339 |
for (idx_t j = 0; j < values.shape(2); ++j) { |
| 1340 |
squared_diff(n, l, j) = sqr(values(n, l, j) - mu[j]); |
| 1341 |
} |
| 1342 |
} |
| 1343 |
} |
| 1344 |
mean(fs, squared_diff_field, sigma, N); |
| 1345 |
for (size_t j = 0; j < sigma.size(); ++j) { |
| 1346 |
sigma[j] = std::sqrt(sigma[j]); |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
template <typename T> |
| 1351 |
void dispatch_mean_and_standard_deviation_per_level(const NodeColumns& fs, const Field& field, Field& mean, |
| 1352 |
Field& stddev, idx_t& N) { |
| 1353 |
dispatch_mean_per_level<T>(fs, field, mean, N); |
| 1354 |
Field squared_diff_field = fs.createField<T>(option::name("sqr_diff") | option::levels(field.levels()) | |
| 1355 |
option::variables(field.variables())); |
| 1356 |
auto squared_diff = make_leveled_view<T>(squared_diff_field); |
| 1357 |
auto values = make_leveled_view<const T>(field); |
| 1358 |
auto mu = make_per_level_view<T>(mean); |
| 1359 |
|
| 1360 |
const idx_t npts = std::min<idx_t>(values.shape(0), fs.nb_nodes()); |
| 1361 |
atlas_omp_parallel_for(idx_t n = 0; n < npts; ++n) { |
| 1362 |
for (idx_t l = 0; l < values.shape(1); ++l) { |
| 1363 |
for (idx_t j = 0; j < values.shape(2); ++j) { |
| 1364 |
squared_diff(n, l, j) = sqr(values(n, l, j) - mu(l, j)); |
| 1365 |
} |
| 1366 |
} |
| 1367 |
} |
| 1368 |
dispatch_mean_per_level<T>(fs, squared_diff_field, stddev, N); |
| 1369 |
auto sigma = make_per_level_view<T>(stddev); |
| 1370 |
atlas_omp_for(idx_t l = 0; l < sigma.shape(0); ++l) { |
| 1371 |
for (idx_t j = 0; j < sigma.shape(1); ++j) { |
| 1372 |
sigma(l, j) = std::sqrt(sigma(l, j)); |
| 1373 |
} |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
void mean_and_standard_deviation_per_level(const NodeColumns& fs, const Field& field, Field& mean, Field& stddev, |
| 1378 |
idx_t& N) { |
| 1379 |
if (field.datatype() != mean.datatype()) { |
| 1380 |
throw_Exception("Field and mean are not of same datatype.", Here()); |
| 1381 |
} |
| 1382 |
if (field.datatype() != stddev.datatype()) { |
| 1383 |
throw_Exception("Field and stddev are not of same datatype.", Here()); |
| 1384 |
} |
| 1385 |
switch (field.datatype().kind()) { |
| 1386 |
case array::DataType::KIND_INT32: |
| 1387 |
return dispatch_mean_and_standard_deviation_per_level<int>(fs, field, mean, stddev, N); |
| 1388 |
case array::DataType::KIND_INT64: |
| 1389 |
return dispatch_mean_and_standard_deviation_per_level<long>(fs, field, mean, stddev, N); |
| 1390 |
case array::DataType::KIND_REAL32: |
| 1391 |
return dispatch_mean_and_standard_deviation_per_level<float>(fs, field, mean, stddev, N); |
| 1392 |
case array::DataType::KIND_REAL64: |
| 1393 |
return dispatch_mean_and_standard_deviation_per_level<double>(fs, field, mean, stddev, N); |
| 1394 |
default: |
| 1395 |
throw_Exception("datatype not supported", Here()); |
| 1396 |
} |
| 1397 |
} |
| 1398 |
|
| 1399 |
} // namespace detail |
| 1400 |
|
| 1401 |
template <typename Value> |
| 1402 |
NodeColumns::FieldStatisticsT<Value>::FieldStatisticsT(const NodeColumns* f): functionspace(*f) {} |
| 1403 |
|
| 1404 |
template <typename Vector> |
| 1405 |
NodeColumns::FieldStatisticsVectorT<Vector>::FieldStatisticsVectorT(const NodeColumns* f): functionspace(*f) {} |
| 1406 |
|
| 1407 |
NodeColumns::FieldStatistics::FieldStatistics(const NodeColumns* f): functionspace(*f) {} |
| 1408 |
|
| 1409 |
template <typename Value> |
| 1410 |
void NodeColumns::FieldStatisticsT<Value>::sum(const Field& field, Value& result, idx_t& N) const { |
| 1411 |
detail::sum(functionspace, field, result, N); |
| 1412 |
} |
| 1413 |
|
| 1414 |
template <typename Vector> |
| 1415 |
void NodeColumns::FieldStatisticsVectorT<Vector>::sum(const Field& field, Vector& result, idx_t& N) const { |
| 1416 |
detail::sum(functionspace, field, result, N); |
| 1417 |
} |
| 1418 |
|
| 1419 |
void NodeColumns::FieldStatistics::sumPerLevel(const Field& field, Field& result, idx_t& N) const { |
| 1420 |
detail::sum_per_level(functionspace, field, result, N); |
| 1421 |
} |
| 1422 |
|
| 1423 |
template <typename Value> |
| 1424 |
void NodeColumns::FieldStatisticsT<Value>::orderIndependentSum(const Field& field, Value& result, idx_t& N) const { |
| 1425 |
detail::order_independent_sum(functionspace, field, result, N); |
| 1426 |
} |
| 1427 |
|
| 1428 |
template <typename Vector> |
| 1429 |
void NodeColumns::FieldStatisticsVectorT<Vector>::orderIndependentSum(const Field& field, Vector& result, |
| 1430 |
idx_t& N) const { |
| 1431 |
detail::order_independent_sum(functionspace, field, result, N); |
| 1432 |
} |
| 1433 |
|
| 1434 |
void NodeColumns::FieldStatistics::orderIndependentSumPerLevel(const Field& field, Field& result, idx_t& N) const { |
| 1435 |
detail::order_independent_sum_per_level(functionspace, field, result, N); |
| 1436 |
} |
| 1437 |
|
| 1438 |
template <typename Value> |
| 1439 |
void NodeColumns::FieldStatisticsT<Value>::minimum(const Field& field, Value& minimum) const { |
| 1440 |
detail::minimum(functionspace, field, minimum); |
| 1441 |
} |
| 1442 |
|
| 1443 |
template <typename Value> |
| 1444 |
void NodeColumns::FieldStatisticsT<Value>::maximum(const Field& field, Value& maximum) const { |
| 1445 |
detail::maximum(functionspace, field, maximum); |
| 1446 |
} |
| 1447 |
|
| 1448 |
template <typename Vector> |
| 1449 |
void NodeColumns::FieldStatisticsVectorT<Vector>::minimum(const Field& field, Vector& minimum) const { |
| 1450 |
detail::minimum(functionspace, field, minimum); |
| 1451 |
} |
| 1452 |
|
| 1453 |
template <typename Vector> |
| 1454 |
void NodeColumns::FieldStatisticsVectorT<Vector>::maximum(const Field& field, Vector& maximum) const { |
| 1455 |
detail::maximum(functionspace, field, maximum); |
| 1456 |
} |
| 1457 |
|
| 1458 |
void NodeColumns::FieldStatistics::minimumPerLevel(const Field& field, Field& minimum) const { |
| 1459 |
detail::minimum_per_level(functionspace, field, minimum); |
| 1460 |
} |
| 1461 |
|
| 1462 |
void NodeColumns::FieldStatistics::maximumPerLevel(const Field& field, Field& maximum) const { |
| 1463 |
detail::maximum_per_level(functionspace, field, maximum); |
| 1464 |
} |
| 1465 |
|
| 1466 |
template <typename Value> |
| 1467 |
void NodeColumns::FieldStatisticsT<Value>::minimumAndLocation(const Field& field, Value& minimum, |
| 1468 |
gidx_t& glb_idx) const { |
| 1469 |
detail::minimum_and_location(functionspace, field, minimum, glb_idx); |
| 1470 |
} |
| 1471 |
|
| 1472 |
template <typename Value> |
| 1473 |
void NodeColumns::FieldStatisticsT<Value>::maximumAndLocation(const Field& field, Value& maximum, |
| 1474 |
gidx_t& glb_idx) const { |
| 1475 |
detail::maximum_and_location(functionspace, field, maximum, glb_idx); |
| 1476 |
} |
| 1477 |
|
| 1478 |
template <typename Value> |
| 1479 |
void NodeColumns::FieldStatisticsT<Value>::minimumAndLocation(const Field& field, Value& minimum, gidx_t& glb_idx, |
| 1480 |
idx_t& level) const { |
| 1481 |
detail::minimum_and_location(functionspace, field, minimum, glb_idx, level); |
| 1482 |
} |
| 1483 |
|
| 1484 |
template <typename Value> |
| 1485 |
void NodeColumns::FieldStatisticsT<Value>::maximumAndLocation(const Field& field, Value& maximum, gidx_t& glb_idx, |
| 1486 |
idx_t& level) const { |
| 1487 |
detail::maximum_and_location(functionspace, field, maximum, glb_idx, level); |
| 1488 |
} |
| 1489 |
|
| 1490 |
template <typename Vector> |
| 1491 |
void NodeColumns::FieldStatisticsVectorT<Vector>::minimumAndLocation(const Field& field, Vector& minimum, |
| 1492 |
std::vector<gidx_t>& glb_idx) const { |
| 1493 |
detail::minimum_and_location(functionspace, field, minimum, glb_idx); |
| 1494 |
} |
| 1495 |
|
| 1496 |
template <typename Vector> |
| 1497 |
void NodeColumns::FieldStatisticsVectorT<Vector>::maximumAndLocation(const Field& field, Vector& maximum, |
| 1498 |
std::vector<gidx_t>& glb_idx) const { |
| 1499 |
detail::maximum_and_location(functionspace, field, maximum, glb_idx); |
| 1500 |
} |
| 1501 |
|
| 1502 |
template <typename Vector> |
| 1503 |
void NodeColumns::FieldStatisticsVectorT<Vector>::minimumAndLocation(const Field& field, Vector& minimum, |
| 1504 |
std::vector<gidx_t>& glb_idx, |
| 1505 |
std::vector<idx_t>& level) const { |
| 1506 |
detail::minimum_and_location(functionspace, field, minimum, glb_idx, level); |
| 1507 |
} |
| 1508 |
|
| 1509 |
template <typename Vector> |
| 1510 |
void NodeColumns::FieldStatisticsVectorT<Vector>::maximumAndLocation(const Field& field, Vector& maximum, |
| 1511 |
std::vector<gidx_t>& glb_idx, |
| 1512 |
std::vector<idx_t>& level) const { |
| 1513 |
detail::maximum_and_location(functionspace, field, maximum, glb_idx, level); |
| 1514 |
} |
| 1515 |
|
| 1516 |
void NodeColumns::FieldStatistics::minimumAndLocationPerLevel(const Field& field, Field& column, Field& glb_idx) const { |
| 1517 |
detail::minimum_and_location_per_level(functionspace, field, column, glb_idx); |
| 1518 |
} |
| 1519 |
|
| 1520 |
void NodeColumns::FieldStatistics::maximumAndLocationPerLevel(const Field& field, Field& column, Field& glb_idx) const { |
| 1521 |
detail::maximum_and_location_per_level(functionspace, field, column, glb_idx); |
| 1522 |
} |
| 1523 |
|
| 1524 |
template <typename Value> |
| 1525 |
void NodeColumns::FieldStatisticsT<Value>::mean(const Field& field, Value& mean, idx_t& N) const { |
| 1526 |
detail::mean(functionspace, field, mean, N); |
| 1527 |
} |
| 1528 |
|
| 1529 |
template <typename Vector> |
| 1530 |
void NodeColumns::FieldStatisticsVectorT<Vector>::mean(const Field& field, Vector& mean, idx_t& N) const { |
| 1531 |
detail::mean(functionspace, field, mean, N); |
| 1532 |
} |
| 1533 |
|
| 1534 |
void NodeColumns::FieldStatistics::meanPerLevel(const Field& field, Field& mean, idx_t& N) const { |
| 1535 |
detail::mean_per_level(functionspace, field, mean, N); |
| 1536 |
} |
| 1537 |
|
| 1538 |
template <typename Value> |
| 1539 |
void NodeColumns::FieldStatisticsT<Value>::meanAndStandardDeviation(const Field& field, Value& mean, Value& stddev, |
| 1540 |
idx_t& N) const { |
| 1541 |
detail::mean_and_standard_deviation(functionspace, field, mean, stddev, N); |
| 1542 |
} |
| 1543 |
|
| 1544 |
template <typename Vector> |
| 1545 |
void NodeColumns::FieldStatisticsVectorT<Vector>::meanAndStandardDeviation(const Field& field, Vector& mean, |
| 1546 |
Vector& stddev, idx_t& N) const { |
| 1547 |
detail::mean_and_standard_deviation(functionspace, field, mean, stddev, N); |
| 1548 |
} |
| 1549 |
|
| 1550 |
void NodeColumns::FieldStatistics::meanAndStandardDeviationPerLevel(const Field& field, Field& mean, Field& stddev, |
| 1551 |
idx_t& N) const { |
| 1552 |
detail::mean_and_standard_deviation_per_level(functionspace, field, mean, stddev, N); |
| 1553 |
} |
| 1554 |
|
| 1555 |
template struct NodeColumns::FieldStatisticsT<int>; |
| 1556 |
template struct NodeColumns::FieldStatisticsT<long>; |
| 1557 |
template struct NodeColumns::FieldStatisticsT<float>; |
| 1558 |
template struct NodeColumns::FieldStatisticsT<double>; |
| 1559 |
// template struct NodeColumns::FieldStatisticsT<unsigned long>; |
| 1560 |
template struct NodeColumns::FieldStatisticsVectorT<std::vector<int>>; |
| 1561 |
template struct NodeColumns::FieldStatisticsVectorT<std::vector<long>>; |
| 1562 |
template struct NodeColumns::FieldStatisticsVectorT<std::vector<float>>; |
| 1563 |
template struct NodeColumns::FieldStatisticsVectorT<std::vector<double>>; |
| 1564 |
// template struct NodeColumns::FieldStatisticsVectorT< std::vector<unsigned |
| 1565 |
// long> >; |
| 1566 |
|
| 1567 |
} // namespace detail |
| 1568 |
|
| 1569 |
} // namespace functionspace |
| 1570 |
} // namespace atlas |