| 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 |
#pragma once |
| 11 |
|
| 12 |
#include <functional> |
| 13 |
#include <type_traits> |
| 14 |
|
| 15 |
#include "atlas/functionspace/FunctionSpace.h" |
| 16 |
#include "atlas/functionspace/detail/StructuredColumns.h" |
| 17 |
|
| 18 |
namespace atlas { |
| 19 |
namespace functionspace { |
| 20 |
|
| 21 |
// ------------------------------------------------------------------- |
| 22 |
|
| 23 |
class StructuredColumns : public FunctionSpace { |
| 24 |
public: |
| 25 |
StructuredColumns(); |
| 26 |
StructuredColumns(const FunctionSpace&); |
| 27 |
StructuredColumns(const Grid&, const eckit::Configuration& = util::NoConfig()); |
| 28 |
StructuredColumns(const Grid&, const grid::Partitioner&, const eckit::Configuration& = util::NoConfig()); |
| 29 |
StructuredColumns(const Grid&, const grid::Distribution&, const eckit::Configuration& = util::NoConfig()); |
| 30 |
StructuredColumns(const Grid&, const Vertical&, const eckit::Configuration& = util::NoConfig()); |
| 31 |
StructuredColumns(const Grid&, const Vertical&, const grid::Partitioner&, |
| 32 |
const eckit::Configuration& = util::NoConfig()); |
| 33 |
StructuredColumns(const Grid&, const grid::Distribution&, const Vertical&, |
| 34 |
const eckit::Configuration& = util::NoConfig()); |
| 35 |
|
| 36 |
static std::string type() { return detail::StructuredColumns::static_type(); } |
| 37 |
|
| 38 |
operator bool() const { return valid(); } |
| 39 |
bool valid() const { return functionspace_; } |
| 40 |
|
| 41 |
idx_t size() const { return functionspace_->size(); } |
| 42 |
idx_t sizeOwned() const { return functionspace_->sizeOwned(); } |
| 43 |
idx_t sizeHalo() const { return functionspace_->sizeHalo(); } |
| 44 |
|
| 45 |
idx_t levels() const { return functionspace_->levels(); } |
| 46 |
|
| 47 |
idx_t halo() const { return functionspace_->halo(); } |
| 48 |
|
| 49 |
const Vertical& vertical() const { return functionspace_->vertical(); } |
| 50 |
|
| 51 |
const StructuredGrid& grid() const { return functionspace_->grid(); } |
| 52 |
|
| 53 |
std::string checksum(const FieldSet&) const; |
| 54 |
std::string checksum(const Field&) const; |
| 55 |
|
| 56 |
idx_t index(idx_t i, idx_t j) const { return functionspace_->index(i, j); } |
| 57 |
|
| 58 |
idx_t i_begin(idx_t j) const { return functionspace_->i_begin(j); } |
| 59 |
idx_t i_end(idx_t j) const { return functionspace_->i_end(j); } |
| 60 |
|
| 61 |
idx_t i_begin_halo(idx_t j) const { return functionspace_->i_begin_halo(j); } |
| 62 |
idx_t i_end_halo(idx_t j) const { return functionspace_->i_end_halo(j); } |
| 63 |
|
| 64 |
idx_t j_begin() const { return functionspace_->j_begin(); } |
| 65 |
idx_t j_end() const { return functionspace_->j_end(); } |
| 66 |
|
| 67 |
idx_t j_begin_halo() const { return functionspace_->j_begin_halo(); } |
| 68 |
idx_t j_end_halo() const { return functionspace_->j_end_halo(); } |
| 69 |
|
| 70 |
idx_t k_begin() const { return functionspace_->k_begin(); } |
| 71 |
idx_t k_end() const { return functionspace_->k_end(); } |
| 72 |
|
| 73 |
Field xy() const { return functionspace_->xy(); } |
| 74 |
Field partition() const { return functionspace_->partition(); } |
| 75 |
Field global_index() const { return functionspace_->global_index(); } |
| 76 |
Field remote_index() const { return functionspace_->remote_index(); } |
| 77 |
Field index_i() const { return functionspace_->index_i(); } |
| 78 |
Field index_j() const { return functionspace_->index_j(); } |
| 79 |
Field ghost() const { return functionspace_->ghost(); } |
| 80 |
|
| 81 |
void compute_xy(idx_t i, idx_t j, PointXY& xy) const { return functionspace_->compute_xy(i, j, xy); } |
| 82 |
PointXY compute_xy(idx_t i, idx_t j) const { return functionspace_->compute_xy(i, j); } |
| 83 |
|
| 84 |
size_t footprint() const { return functionspace_->footprint(); } |
| 85 |
|
| 86 |
const util::PartitionPolygon& polygon(idx_t halo = 0) const { return functionspace_->polygon(halo); } |
| 87 |
|
| 88 |
class For { |
| 89 |
public: |
| 90 |
For(const StructuredColumns& fs, const util::Config& config = util::NoConfig()): |
| 91 |
fs_{fs}, |
| 92 |
global{config.getBool("global", false)}, |
| 93 |
owner{config.getInt("owner", 0)}, |
| 94 |
levels{config.getInt("levels", fs_.levels())} {} |
| 95 |
|
| 96 |
protected: |
| 97 |
const StructuredColumns& fs_; |
| 98 |
bool global; |
| 99 |
idx_t owner; |
| 100 |
idx_t levels; |
| 101 |
|
| 102 |
public: |
| 103 |
#define FunctorArgs(...) \ |
| 104 |
typename std::enable_if<std::is_convertible<Functor, std::function<void(__VA_ARGS__)>>::value, Functor>::type* = \ |
| 105 |
nullptr |
| 106 |
|
| 107 |
|
| 108 |
// Functor: void f(index,i,j,k) |
| 109 |
template <typename Functor, FunctorArgs(idx_t, idx_t, idx_t, idx_t)> |
| 110 |
void operator()(const Functor& f) const { |
| 111 |
ATLAS_ASSERT(levels); |
| 112 |
if (global) { |
| 113 |
auto mpi_rank = mpi::comm(fs_.mpi_comm()).rank(); |
| 114 |
if (owner == mpi_rank) { |
| 115 |
const idx_t ny = fs_.grid().ny(); |
| 116 |
std::vector<idx_t> offset(ny); |
| 117 |
offset[0] = 0; |
| 118 |
for (idx_t j = 1; j < ny; ++j) { |
| 119 |
offset[j] = offset[j - 1] + fs_.grid().nx(j - 1); |
| 120 |
} |
| 121 |
atlas_omp_parallel_for(idx_t j = 0; j < ny; ++j) { |
| 122 |
idx_t index = offset[j]; |
| 123 |
for (auto i = 0; i < fs_.grid().nx(j); ++i, ++index) { |
| 124 |
for (auto k = 0; k < levels; ++k) { |
| 125 |
f(index, i, j, k); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
else { |
| 132 |
for (auto j = fs_.j_begin(); j < fs_.j_end(); ++j) { |
| 133 |
for (auto i = fs_.i_begin(j); i < fs_.i_end(j); ++i) { |
| 134 |
for (auto k = 0; k < levels; ++k) { |
| 135 |
f(fs_.index(i, j), i, j, k); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// Functor: void f(index,i,j) |
| 143 |
template <typename Functor, FunctorArgs(idx_t, idx_t, idx_t)> |
| 144 |
void operator()(const Functor& f) const { |
| 145 |
ATLAS_ASSERT(levels == 0); |
| 146 |
if (global) { |
| 147 |
auto mpi_rank = mpi::comm(fs_.mpi_comm()).rank(); |
| 148 |
if (owner == mpi_rank) { |
| 149 |
const idx_t ny = fs_.grid().ny(); |
| 150 |
std::vector<idx_t> offset(ny); |
| 151 |
offset[0] = 0; |
| 152 |
for (idx_t j = 1; j < ny; ++j) { |
| 153 |
offset[j] = offset[j - 1] + fs_.grid().nx(j - 1); |
| 154 |
} |
| 155 |
atlas_omp_parallel_for(idx_t j = 0; j < ny; ++j) { |
| 156 |
idx_t index = offset[j]; |
| 157 |
for (idx_t i = 0; i < fs_.grid().nx(j); ++i, ++index) { |
| 158 |
f(index, i, j); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
else { |
| 164 |
for (auto j = fs_.j_begin(); j < fs_.j_end(); ++j) { |
| 165 |
for (auto i = fs_.i_begin(j); i < fs_.i_end(j); ++i) { |
| 166 |
f(fs_.index(i, j), i, j); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// Functor: void f(index,k) |
| 173 |
template <typename Functor, FunctorArgs(idx_t, idx_t)> |
| 174 |
void operator()(const Functor& f) const { |
| 175 |
ATLAS_ASSERT(levels); |
| 176 |
if (global) { |
| 177 |
auto mpi_rank = mpi::comm(fs_.mpi_comm()).rank(); |
| 178 |
if (owner == mpi_rank) { |
| 179 |
const idx_t size = fs_.grid().size(); |
| 180 |
atlas_omp_parallel_for(idx_t n = 0; n < size; ++n) { |
| 181 |
for (idx_t k = 0; k < levels; ++k) { |
| 182 |
f(n, k); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
else { |
| 188 |
const idx_t size = fs_.sizeOwned(); |
| 189 |
atlas_omp_parallel_for(idx_t n = 0; n < size; ++n) { |
| 190 |
for (idx_t k = 0; k < levels; ++k) { |
| 191 |
f(n, k); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
// Functor: void f(index) |
| 199 |
template <typename Functor, FunctorArgs(idx_t)> |
| 200 |
void operator()(const Functor& f) const { |
| 201 |
ATLAS_ASSERT(levels == 0); |
| 202 |
if (global) { |
| 203 |
auto mpi_rank = mpi::comm(fs_.mpi_comm()).rank(); |
| 204 |
if (owner == mpi_rank) { |
| 205 |
const idx_t size = fs_.grid().size(); |
| 206 |
atlas_omp_parallel_for(idx_t n = 0; n < size; ++n) { f(n); } |
| 207 |
} |
| 208 |
} |
| 209 |
else { |
| 210 |
const idx_t size = fs_.sizeOwned(); |
| 211 |
atlas_omp_parallel_for(idx_t n = 0; n < size; ++n) { f(n); } |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
#undef FunctorArgs |
| 216 |
}; |
| 217 |
|
| 218 |
template <typename Functor> |
| 219 |
void parallel_for(const Functor& f) const { |
| 220 |
For(*this, util::NoConfig())(f); |
| 221 |
} |
| 222 |
template <typename Functor> |
| 223 |
void parallel_for(const util::Config& config, const Functor& f) const { |
| 224 |
For(*this, config)(f); |
| 225 |
} |
| 226 |
|
| 227 |
private: |
| 228 |
const detail::StructuredColumns* functionspace_; |
| 229 |
void setup(const Grid& grid, const Vertical& vertical, const grid::Distribution& distribution, |
| 230 |
const eckit::Configuration& config); |
| 231 |
}; |
| 232 |
|
| 233 |
// ------------------------------------------------------------------- |
| 234 |
|
| 235 |
|
| 236 |
} // namespace functionspace |
| 237 |
} // namespace atlas |