| 1 |
/* |
| 2 |
* (C) Crown Copyright 2021 Met Office |
| 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 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include <functional> |
| 11 |
#include <type_traits> |
| 12 |
|
| 13 |
#include "atlas/array/ArrayView.h" |
| 14 |
#include "atlas/functionspace/CellColumns.h" |
| 15 |
#include "atlas/functionspace/NodeColumns.h" |
| 16 |
#include "atlas/functionspace/detail/CubedSphereStructure.h" |
| 17 |
#include "atlas/meshgenerator/detail/cubedsphere/CubedSphereUtility.h" |
| 18 |
#include "atlas/parallel/omp/omp.h" |
| 19 |
#include "atlas/util/ObjectHandle.h" |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
class Mesh; |
| 23 |
} |
| 24 |
|
| 25 |
namespace atlas { |
| 26 |
namespace functionspace { |
| 27 |
|
| 28 |
/// Extend NodeColumns and CellColumns so that they can exploit CubedSphere structure. |
| 29 |
template <typename BaseFunctionSpace> |
| 30 |
class CubedSphereColumns : public BaseFunctionSpace { |
| 31 |
public: |
| 32 |
/// Constructors. |
| 33 |
CubedSphereColumns(); |
| 34 |
CubedSphereColumns(const FunctionSpace& functionSpace); |
| 35 |
CubedSphereColumns(const Mesh& mesh, const eckit::Configuration& configuration); |
| 36 |
CubedSphereColumns(const Mesh& mesh); |
| 37 |
|
| 38 |
/// Invalid index. |
| 39 |
idx_t invalid_index() const; |
| 40 |
|
| 41 |
/// Get number of owned elements. |
| 42 |
idx_t sizeOwned() const; |
| 43 |
|
| 44 |
/// i lower bound for tile t (including halo) |
| 45 |
idx_t i_begin(idx_t t) const; |
| 46 |
/// i lower bound for tile t (including halo) |
| 47 |
idx_t i_end(idx_t t) const; |
| 48 |
|
| 49 |
/// j lower bound for tile t (including halo) |
| 50 |
idx_t j_begin(idx_t t) const; |
| 51 |
/// j lower bound for tile t (including halo) |
| 52 |
idx_t j_end(idx_t t) const; |
| 53 |
|
| 54 |
/// Return array_view index for (t, i, j). |
| 55 |
idx_t index(idx_t t, idx_t i, idx_t j) const; |
| 56 |
|
| 57 |
/// Return true if (t, i, j) is a valid index. |
| 58 |
bool is_valid_index(idx_t t, idx_t i, idx_t j) const; |
| 59 |
|
| 60 |
/// Return tij field. |
| 61 |
Field tij() const; |
| 62 |
|
| 63 |
private: |
| 64 |
class For { |
| 65 |
public: |
| 66 |
For(const CubedSphereColumns<BaseFunctionSpace>& functionSpace, const util::Config& config = util::NoConfig()): |
| 67 |
functionSpace_{functionSpace}, |
| 68 |
indexMax_{config.getBool("include_halo", false) ? functionSpace.size() : functionSpace.sizeOwned()}, |
| 69 |
levels_{config.getInt("levels", functionSpace_.levels())}, |
| 70 |
tijView_(array::make_view<idx_t, 2>(functionSpace_.tij())) {} |
| 71 |
|
| 72 |
// Define template to disable invalid functors. |
| 73 |
template <typename FuncType, typename... ArgTypes> |
| 74 |
using EnableFunctor = |
| 75 |
typename std::enable_if<std::is_convertible<FuncType, std::function<void(ArgTypes...)>>::value>::type*; |
| 76 |
|
| 77 |
// Functor: void f(index, t, i, j, k) |
| 78 |
template <typename Functor, EnableFunctor<Functor, idx_t, idx_t, idx_t, idx_t, idx_t> = nullptr> |
| 79 |
void operator()(const Functor& f) const { |
| 80 |
using namespace meshgenerator::detail::cubedsphere; |
| 81 |
|
| 82 |
// Loop over elements. |
| 83 |
atlas_omp_parallel_for(idx_t index = 0; index < indexMax_; ++index) { |
| 84 |
const idx_t t = tijView_(index, Coordinates::T); |
| 85 |
const idx_t i = tijView_(index, Coordinates::I); |
| 86 |
const idx_t j = tijView_(index, Coordinates::J); |
| 87 |
for (idx_t k = 0; k < levels_; ++k) { |
| 88 |
f(index, t, i, j, k); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Functor: void f(index, t, i, j) |
| 94 |
template <typename Functor, EnableFunctor<Functor, idx_t, idx_t, idx_t, idx_t> = nullptr> |
| 95 |
void operator()(const Functor& f) const { |
| 96 |
using namespace meshgenerator::detail::cubedsphere; |
| 97 |
|
| 98 |
// Loop over elements. |
| 99 |
atlas_omp_parallel_for(idx_t index = 0; index < indexMax_; ++index) { |
| 100 |
const idx_t t = tijView_(index, Coordinates::T); |
| 101 |
const idx_t i = tijView_(index, Coordinates::I); |
| 102 |
const idx_t j = tijView_(index, Coordinates::J); |
| 103 |
f(index, t, i, j); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// Functor: void f(index, k) |
| 108 |
template <typename Functor, EnableFunctor<Functor, idx_t, idx_t> = nullptr> |
| 109 |
void operator()(const Functor& f) const { |
| 110 |
using namespace meshgenerator::detail::cubedsphere; |
| 111 |
|
| 112 |
// Loop over elements. |
| 113 |
atlas_omp_parallel_for(idx_t index = 0; index < indexMax_; ++index) { |
| 114 |
for (idx_t k = 0; k < levels_; ++k) { |
| 115 |
f(index, k); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// Functor: void f(index ) |
| 121 |
template <typename Functor, EnableFunctor<Functor, idx_t> = nullptr> |
| 122 |
void operator()(const Functor& f) const { |
| 123 |
using namespace meshgenerator::detail::cubedsphere; |
| 124 |
|
| 125 |
// Loop over elements. |
| 126 |
atlas_omp_parallel_for(idx_t index = 0; index < indexMax_; ++index) { f(index); } |
| 127 |
} |
| 128 |
|
| 129 |
private: |
| 130 |
const CubedSphereColumns<BaseFunctionSpace>& functionSpace_; |
| 131 |
idx_t indexMax_; |
| 132 |
idx_t levels_; |
| 133 |
array::ArrayView<idx_t, 2> tijView_; |
| 134 |
}; |
| 135 |
|
| 136 |
public: |
| 137 |
/// Visit each element index and apply functor with signature: |
| 138 |
/// f(index, t, i, j, k), f(index t, i, j), f(index, k) or f(index). |
| 139 |
template <typename Functor> |
| 140 |
void parallel_for(const Functor& f) const { |
| 141 |
For(*this, util::NoConfig())(f); |
| 142 |
} |
| 143 |
|
| 144 |
/// Visit each element index and apply functor with signature: |
| 145 |
/// f(index, t, i, j, k), f(index t, i, j), f(index, k) or f(index). |
| 146 |
/// Can also specify "idx_t levels" and "bool include_halo". |
| 147 |
template <typename Functor> |
| 148 |
void parallel_for(const util::Config& config, const Functor& f) const { |
| 149 |
For(*this, config)(f); |
| 150 |
} |
| 151 |
|
| 152 |
private: |
| 153 |
// Object hanldle for CubedSphereStructure |
| 154 |
util::ObjectHandle<detail::CubedSphereStructure> cubedSphereColumnsHandle_; |
| 155 |
}; |
| 156 |
|
| 157 |
using CubedSphereNodeColumns = CubedSphereColumns<NodeColumns>; |
| 158 |
using CubedSphereCellColumns = CubedSphereColumns<CellColumns>; |
| 159 |
|
| 160 |
} // namespace functionspace |
| 161 |
} // namespace atlas |