| 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 <memory> |
| 12 |
|
| 13 |
#include "atlas/interpolation/method/Method.h" |
| 14 |
|
| 15 |
|
| 16 |
#include "atlas/array.h" |
| 17 |
#include "atlas/field/Field.h" |
| 18 |
#include "atlas/field/FieldSet.h" |
| 19 |
#include "atlas/field/MissingValue.h" |
| 20 |
#include "atlas/functionspace/NodeColumns.h" |
| 21 |
#include "atlas/linalg/sparse.h" |
| 22 |
#include "atlas/mesh/Nodes.h" |
| 23 |
#include "atlas/runtime/Exception.h" |
| 24 |
#include "atlas/runtime/Log.h" |
| 25 |
#include "atlas/runtime/Trace.h" |
| 26 |
|
| 27 |
using namespace atlas::linalg; |
| 28 |
namespace atlas { |
| 29 |
namespace interpolation { |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
template <typename Value> |
| 34 |
void set_missing_values_rank1(Field& tgt, const std::vector<idx_t>& missing, const Value& missing_value) { |
| 35 |
auto tgt_v = array::make_view<Value, 1>(tgt); |
| 36 |
for (auto i : missing) { |
| 37 |
tgt_v(i) = missing_value; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
template <typename Value> |
| 42 |
void set_missing_values_rank2(Field& tgt, const std::vector<idx_t>& missing, const Value& missing_value) { |
| 43 |
auto tgt_v = array::make_view<Value, 2>(tgt); |
| 44 |
const idx_t Nj = tgt_v.shape(1); |
| 45 |
for (auto i : missing) { |
| 46 |
for (idx_t j = 0; j < Nj; ++j) { |
| 47 |
tgt_v(i, j) = missing_value; |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
template <typename Value> |
| 53 |
void set_missing_values_rank3(Field& tgt, const std::vector<idx_t>& missing, const Value& missing_value) { |
| 54 |
auto tgt_v = array::make_view<Value, 3>(tgt); |
| 55 |
const idx_t Nj = tgt_v.shape(1); |
| 56 |
const idx_t Nk = tgt_v.shape(2); |
| 57 |
for (auto i : missing) { |
| 58 |
for (idx_t j = 0; j < Nj; ++j) { |
| 59 |
for (idx_t k = 0; k < Nk; ++k) { |
| 60 |
tgt_v(i, j, k) = missing_value; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
template <typename Value> |
| 67 |
void set_missing_values_T(Field& tgt, const std::vector<idx_t>& missing) { |
| 68 |
Value missing_value = tgt.metadata().get<Value>("missing_value"); |
| 69 |
if (tgt.rank() == 1) { |
| 70 |
set_missing_values_rank1(tgt, missing, missing_value); |
| 71 |
} |
| 72 |
else if (tgt.rank() == 2) { |
| 73 |
set_missing_values_rank2(tgt, missing, missing_value); |
| 74 |
} |
| 75 |
else if (tgt.rank() == 3) { |
| 76 |
set_missing_values_rank3(tgt, missing, missing_value); |
| 77 |
} |
| 78 |
else { |
| 79 |
ATLAS_NOTIMPLEMENTED; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
void set_missing_values(Field& tgt, const std::vector<idx_t>& missing) { |
| 84 |
if (missing.empty()) { |
| 85 |
return; |
| 86 |
} |
| 87 |
if (tgt.datatype().kind() == array::DataType::KIND_REAL64) { |
| 88 |
set_missing_values_T<double>(tgt, missing); |
| 89 |
} |
| 90 |
else if (tgt.datatype().kind() == array::DataType::KIND_REAL32) { |
| 91 |
set_missing_values_T<float>(tgt, missing); |
| 92 |
} |
| 93 |
else { |
| 94 |
ATLAS_NOTIMPLEMENTED; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
bool executesOnDevice(const sparse::Backend& backend) { |
| 99 |
if (backend.type() == "eckit_linalg") { |
| 100 |
return false; |
| 101 |
} else if (backend.type() == "openmp") { |
| 102 |
return false; |
| 103 |
} else if (backend.type() == "hicsparse") { |
| 104 |
return true; |
| 105 |
} else { |
| 106 |
ATLAS_NOTIMPLEMENTED; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
template<typename Value, int Rank> |
| 111 |
atlas::array::ArrayView<Value, Rank> make_device_view_rw(atlas::Field& f) { |
| 112 |
f.syncDevice(); |
| 113 |
return atlas::array::make_device_view<Value, Rank>(f); |
| 114 |
} |
| 115 |
|
| 116 |
template<typename Value, int Rank> |
| 117 |
atlas::array::ArrayView<Value, Rank> make_device_view_w(atlas::Field& f) { |
| 118 |
if (not f.deviceAllocated()) { |
| 119 |
f.allocateDevice(); |
| 120 |
} |
| 121 |
f.setDeviceNeedsUpdate(false); // as it will be written to here |
| 122 |
return atlas::array::make_device_view<Value, Rank>(f); |
| 123 |
} |
| 124 |
|
| 125 |
template<typename Value, int Rank> |
| 126 |
atlas::array::ArrayView<const Value, Rank> make_device_view_r(const atlas::Field& f) { |
| 127 |
f.syncDevice(); |
| 128 |
return atlas::array::make_device_view<const Value, Rank>(f); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
template<typename Value, int Rank> |
| 133 |
atlas::array::ArrayView<Value, Rank> make_host_view_rw(atlas::Field& f) { |
| 134 |
f.syncHost(); |
| 135 |
return atlas::array::make_host_view<Value, Rank>(f); |
| 136 |
} |
| 137 |
|
| 138 |
template<typename Value, int Rank> |
| 139 |
atlas::array::ArrayView<const Value, Rank> make_host_view_r(const atlas::Field& f) { |
| 140 |
f.syncHost(); |
| 141 |
return atlas::array::make_host_view<const Value, Rank>(f); |
| 142 |
} |
| 143 |
|
| 144 |
template<typename Value, int Rank> |
| 145 |
atlas::array::ArrayView<Value, Rank> make_host_view_w(atlas::Field& f) { |
| 146 |
f.setHostNeedsUpdate(false); // as it will be written to |
| 147 |
return atlas::array::make_host_view<Value, Rank>(f); |
| 148 |
} |
| 149 |
|
| 150 |
template<typename Value, typename Index> |
| 151 |
atlas::linalg::SparseMatrixView<Value,Index> make_device_view_r(const atlas::linalg::SparseMatrixStorage& m) { |
| 152 |
if (m.deviceNeedsUpdate()) { |
| 153 |
ATLAS_TRACE("Copy interpolation matrix to device"); |
| 154 |
m.updateDevice(); |
| 155 |
} |
| 156 |
return make_device_view<Value, Index>(m); |
| 157 |
} |
| 158 |
|
| 159 |
template<typename Value, typename Index> |
| 160 |
atlas::linalg::SparseMatrixView<Value,Index> make_host_view_r(const atlas::linalg::SparseMatrixStorage& m) { |
| 161 |
m.syncHost(); |
| 162 |
return make_host_view<Value, Index>(m); |
| 163 |
} |
| 164 |
|
| 165 |
} // anonymous namespace |
| 166 |
|
| 167 |
|
| 168 |
template <typename Value> |
| 169 |
void Method::interpolate_field_rank1(const Field& src, Field& tgt, const Matrix& W) const { |
| 170 |
auto backend = sparse::Backend{linalg_backend_}; |
| 171 |
|
| 172 |
if (backend.type() == "hicsparse" && !std::is_same<eckit::linalg::Scalar, Value>::value) { |
| 173 |
ATLAS_NOTIMPLEMENTED; // hicsparse does not support mixed double-float |
| 174 |
} |
| 175 |
|
| 176 |
if (backend.type() == "eckit_linalg" && std::is_same<Value, float>::value) { |
| 177 |
// Switch to OpenMP as eckit_linalg does not support float |
| 178 |
backend = sparse::backend::openmp(); |
| 179 |
} |
| 180 |
|
| 181 |
const auto on_device = executesOnDevice(backend); |
| 182 |
|
| 183 |
auto src_v = on_device ? make_device_view_r<Value, 1>(src) : make_host_view_r<Value, 1>(src); |
| 184 |
auto tgt_v = on_device ? make_device_view_w<Value, 1>(tgt) : make_host_view_w<Value, 1>(tgt); |
| 185 |
|
| 186 |
if (nonLinear_(src)) { |
| 187 |
eckit::linalg::SparseMatrix W_copy = atlas::linalg::make_eckit_sparse_matrix(W); |
| 188 |
nonLinear_->execute(W_copy, src); |
| 189 |
auto W_nl = make_sparse_matrix_storage(std::move(W_copy)); |
| 190 |
auto W_nl_v = on_device ? make_device_view_r<eckit::linalg::Scalar, eckit::linalg::Index>(W_nl) |
| 191 |
: make_host_view_r<eckit::linalg::Scalar, eckit::linalg::Index>(W_nl); |
| 192 |
sparse_matrix_multiply(W_nl_v, src_v, tgt_v, backend); |
| 193 |
} |
| 194 |
else { |
| 195 |
auto W_v = on_device ? make_device_view_r<eckit::linalg::Scalar, eckit::linalg::Index>(W) |
| 196 |
: make_host_view_r<eckit::linalg::Scalar, eckit::linalg::Index>(W); |
| 197 |
sparse_matrix_multiply(W_v, src_v, tgt_v, backend); |
| 198 |
} |
| 199 |
|
| 200 |
on_device ? tgt.setHostNeedsUpdate(true) : tgt.setDeviceNeedsUpdate(true); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
template <typename Value> |
| 205 |
void Method::interpolate_field_rank2(const Field& src, Field& tgt, const Matrix& W) const { |
| 206 |
auto backend = sparse::Backend{linalg_backend_}; |
| 207 |
|
| 208 |
if (backend.type() == "hicsparse" && !std::is_same<eckit::linalg::Scalar, Value>::value) { |
| 209 |
ATLAS_NOTIMPLEMENTED; // hicsparse does not support mixed double-float |
| 210 |
} |
| 211 |
|
| 212 |
if (backend.type() == "eckit_linalg") { |
| 213 |
// Switch to OpenMP as eckit_linalg does not support this layout |
| 214 |
backend = sparse::backend::openmp(); |
| 215 |
} |
| 216 |
|
| 217 |
if (nonLinear_(src)) { |
| 218 |
// We cannot apply the same matrix to full columns as e.g. missing values could be present in only certain parts. |
| 219 |
|
| 220 |
// Allocate temporary rank-1 fields corresponding to one horizontal level |
| 221 |
auto src_slice = Field("s", array::make_datatype<Value>(), {src.shape(0)}); |
| 222 |
auto tgt_slice = Field("t", array::make_datatype<Value>(), {tgt.shape(0)}); |
| 223 |
|
| 224 |
// Copy metadata to the source rank-1 field |
| 225 |
src_slice.metadata() = src.metadata(); |
| 226 |
|
| 227 |
auto src_v = make_host_view_r<Value,2>(src); |
| 228 |
auto tgt_v = make_host_view_w<Value,2>(tgt); |
| 229 |
|
| 230 |
auto src_slice_v = array::make_host_view<Value, 1>(src_slice); |
| 231 |
auto tgt_slice_v = array::make_host_view<Value, 1>(tgt_slice); |
| 232 |
|
| 233 |
for (idx_t lev = 0; lev < src_v.shape(1); ++lev) { |
| 234 |
// Copy this level to temporary rank-1 field |
| 235 |
for (idx_t i = 0; i < src.shape(0); ++i) { |
| 236 |
src_slice_v(i) = src_v(i, lev); |
| 237 |
} |
| 238 |
|
| 239 |
// Interpolate between rank-1 fields |
| 240 |
interpolate_field_rank1<Value>(src_slice, tgt_slice, W); |
| 241 |
|
| 242 |
// Copy rank-1 field to this level in the rank-2 field |
| 243 |
tgt_slice.syncHost(); |
| 244 |
for (idx_t i = 0; i < tgt.shape(0); ++i) { |
| 245 |
tgt_v(i, lev) = tgt_slice_v(i); |
| 246 |
} |
| 247 |
} |
| 248 |
tgt.setDeviceNeedsUpdate(true); |
| 249 |
tgt.setHostNeedsUpdate(false); |
| 250 |
} |
| 251 |
else { |
| 252 |
const auto on_device = executesOnDevice(backend); |
| 253 |
|
| 254 |
auto W_v = on_device ? make_device_view_r<eckit::linalg::Scalar, eckit::linalg::Index>(W) |
| 255 |
: make_host_view_r<eckit::linalg::Scalar, eckit::linalg::Index>(W); |
| 256 |
auto src_dv = on_device ? make_device_view_r<Value, 2>(src) : make_host_view_r<Value, 2>(src); |
| 257 |
auto tgt_dv = on_device ? make_device_view_w<Value, 2>(tgt) : make_host_view_w<Value, 2>(tgt); |
| 258 |
|
| 259 |
sparse_matrix_multiply(W_v, src_dv, tgt_dv, backend); |
| 260 |
|
| 261 |
on_device ? tgt.setHostNeedsUpdate(true) : tgt.setDeviceNeedsUpdate(true); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
template <typename Value> |
| 267 |
void Method::interpolate_field_rank3(const Field& src, Field& tgt, const Matrix& W) const { |
| 268 |
sparse::Backend backend{linalg_backend_}; |
| 269 |
|
| 270 |
if (backend.type() == "hicsparse") { |
| 271 |
ATLAS_NOTIMPLEMENTED; // hicsparse does not support rank-3 fields |
| 272 |
} |
| 273 |
|
| 274 |
if (backend.type() == "eckit_linalg") { |
| 275 |
// Switch to OpenMP as eckit_linalg does not support rank-3 fields |
| 276 |
backend = sparse::backend::openmp(); |
| 277 |
} |
| 278 |
|
| 279 |
auto W_v = make_host_view<eckit::linalg::Scalar, eckit::linalg::Index>(W); |
| 280 |
auto src_v = make_host_view_r<Value, 3>(src); |
| 281 |
auto tgt_v = make_host_view_w<Value, 3>(tgt); |
| 282 |
if (not W.empty() && nonLinear_(src)) { |
| 283 |
ATLAS_ASSERT(false, "nonLinear interpolation not supported for rank-3 fields."); |
| 284 |
} |
| 285 |
sparse_matrix_multiply(W_v, src_v, tgt_v, backend); |
| 286 |
|
| 287 |
tgt.setHostNeedsUpdate(false); |
| 288 |
tgt.setDeviceNeedsUpdate(true); |
| 289 |
} |
| 290 |
|
| 291 |
template <typename Value> |
| 292 |
void Method::adjoint_interpolate_field_rank1(Field& src, const Field& tgt, const Matrix& W) const { |
| 293 |
auto backend = sparse::Backend{linalg_backend_}; |
| 294 |
|
| 295 |
if (backend.type() == "hicsparse" && !std::is_same<eckit::linalg::Scalar, Value>::value) { |
| 296 |
ATLAS_NOTIMPLEMENTED; // hicsparse does not support mixed double-float |
| 297 |
} |
| 298 |
|
| 299 |
if (backend.type() == "eckit_linalg" && std::is_same<Value, float>::value) { |
| 300 |
// Switch to OpenMP as eckit_linalg does not support float |
| 301 |
backend = sparse::backend::openmp(); |
| 302 |
} |
| 303 |
|
| 304 |
const auto on_device = executesOnDevice(backend); |
| 305 |
|
| 306 |
auto src_v = on_device ? make_device_view_rw<Value, 1>(src) : make_host_view_rw<Value, 1>(src); |
| 307 |
auto tgt_v = on_device ? make_device_view_r<Value, 1>(tgt) : make_host_view_r<Value, 1>(tgt); |
| 308 |
auto W_v = on_device ? make_device_view_r<eckit::linalg::Scalar, eckit::linalg::Index>(W) |
| 309 |
: make_host_view_r<eckit::linalg::Scalar, eckit::linalg::Index>(W); |
| 310 |
|
| 311 |
sparse_matrix_multiply_add(W_v, tgt_v, src_v, backend); |
| 312 |
|
| 313 |
on_device ? src.setHostNeedsUpdate(true) : src.setDeviceNeedsUpdate(true); |
| 314 |
} |
| 315 |
|
| 316 |
template <typename Value> |
| 317 |
void Method::adjoint_interpolate_field_rank2(Field& src, const Field& tgt, const Matrix& W) const { |
| 318 |
auto backend = sparse::Backend{linalg_backend_}; |
| 319 |
|
| 320 |
if (backend.type() == "hicsparse" && !std::is_same<eckit::linalg::Scalar, Value>::value) { |
| 321 |
ATLAS_NOTIMPLEMENTED; // hicsparse does not support mixed double-float |
| 322 |
} |
| 323 |
|
| 324 |
if (backend.type() == "eckit_linalg") { |
| 325 |
// Switch to OpenMP as eckit_linalg does not support this layout |
| 326 |
backend = sparse::backend::openmp(); |
| 327 |
} |
| 328 |
|
| 329 |
const auto on_device = executesOnDevice(backend); |
| 330 |
|
| 331 |
auto src_v = on_device ? make_device_view_rw<Value, 2>(src) : make_host_view_rw<Value, 2>(src); |
| 332 |
auto tgt_v = on_device ? make_device_view_r<Value, 2>(tgt) : make_host_view_r<Value, 2>(tgt); |
| 333 |
auto W_v = on_device ? make_device_view_r<eckit::linalg::Scalar, eckit::linalg::Index>(W) |
| 334 |
: make_host_view_r<eckit::linalg::Scalar, eckit::linalg::Index>(W); |
| 335 |
|
| 336 |
sparse_matrix_multiply_add(W_v, tgt_v, src_v, backend); |
| 337 |
|
| 338 |
on_device ? src.setHostNeedsUpdate(true) : src.setDeviceNeedsUpdate(true); |
| 339 |
} |
| 340 |
|
| 341 |
template <typename Value> |
| 342 |
void Method::adjoint_interpolate_field_rank3(Field& src, const Field& tgt, const Matrix& W) const { |
| 343 |
sparse::Backend backend{linalg_backend_}; |
| 344 |
|
| 345 |
if (backend.type() == "hicsparse") { |
| 346 |
ATLAS_NOTIMPLEMENTED; // hicsparse does not support rank-3 fields |
| 347 |
} |
| 348 |
|
| 349 |
if (backend.type() == "eckit_linalg") { |
| 350 |
// Switch to OpenMP as eckit_linalg does not support rank-3 fields |
| 351 |
backend = sparse::backend::openmp(); |
| 352 |
} |
| 353 |
|
| 354 |
auto src_v = make_host_view_rw<Value, 3>(src); |
| 355 |
auto tgt_v = make_host_view_r<Value, 3>(tgt); |
| 356 |
auto W_v = make_host_view_r<eckit::linalg::Scalar,eckit::linalg::Index>(W); |
| 357 |
|
| 358 |
sparse_matrix_multiply_add(W_v, tgt_v, src_v, backend); |
| 359 |
|
| 360 |
src.setDeviceNeedsUpdate(true); |
| 361 |
} |
| 362 |
|
| 363 |
void Method::check_compatibility(const Field& src, const Field& tgt, const Matrix& W) const { |
| 364 |
ATLAS_ASSERT(src.datatype() == tgt.datatype()); |
| 365 |
ATLAS_ASSERT(src.rank() == tgt.rank()); |
| 366 |
ATLAS_ASSERT(src.levels() == tgt.levels()); |
| 367 |
ATLAS_ASSERT(src.variables() == tgt.variables()); |
| 368 |
|
| 369 |
ATLAS_ASSERT(!W.empty()); |
| 370 |
ATLAS_ASSERT(tgt.shape(0) >= static_cast<idx_t>(W.rows())); |
| 371 |
ATLAS_ASSERT(src.shape(0) >= static_cast<idx_t>(W.cols())); |
| 372 |
} |
| 373 |
|
| 374 |
template <typename Value> |
| 375 |
void Method::interpolate_field(const Field& src, Field& tgt, const Matrix& W) const { |
| 376 |
// do nothing if there are no observations to interpolate (W will be NULL |
| 377 |
// and would fail the compatibility check) |
| 378 |
if (tgt.shape(0) == 0) { |
| 379 |
return; |
| 380 |
} |
| 381 |
check_compatibility(src, tgt, W); |
| 382 |
|
| 383 |
if (src.rank() == 1) { |
| 384 |
interpolate_field_rank1<Value>(src, tgt, W); |
| 385 |
} |
| 386 |
else if (src.rank() == 2) { |
| 387 |
interpolate_field_rank2<Value>(src, tgt, W); |
| 388 |
} |
| 389 |
else if (src.rank() == 3) { |
| 390 |
interpolate_field_rank3<Value>(src, tgt, W); |
| 391 |
} |
| 392 |
else { |
| 393 |
ATLAS_NOTIMPLEMENTED; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
template <typename Value> |
| 398 |
void Method::adjoint_interpolate_field(Field& src, const Field& tgt, const Matrix& W) const { |
| 399 |
// do nothing if there are no observations to interpolate (W will be NULL |
| 400 |
// and would fail the compatibility check) |
| 401 |
if (tgt.shape(0) == 0) { |
| 402 |
return; |
| 403 |
} |
| 404 |
check_compatibility(tgt, src, W); |
| 405 |
|
| 406 |
if (src.rank() == 1) { |
| 407 |
adjoint_interpolate_field_rank1<Value>(src, tgt, W); |
| 408 |
} |
| 409 |
else if (src.rank() == 2) { |
| 410 |
adjoint_interpolate_field_rank2<Value>(src, tgt, W); |
| 411 |
} |
| 412 |
else if (src.rank() == 3) { |
| 413 |
adjoint_interpolate_field_rank3<Value>(src, tgt, W); |
| 414 |
} |
| 415 |
else { |
| 416 |
ATLAS_NOTIMPLEMENTED; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
Method::Method(const Method::Config& config) { |
| 421 |
config.get("sparse_matrix_multiply", linalg_backend_); // empty is allowed -> sparse::current_backend() |
| 422 |
|
| 423 |
std::string non_linear; |
| 424 |
if (config.get("non_linear", non_linear)) { |
| 425 |
nonLinear_ = NonLinear(non_linear, config); |
| 426 |
} |
| 427 |
|
| 428 |
config.get("adjoint", adjoint_ = false); |
| 429 |
} |
| 430 |
|
| 431 |
void Method::setup(const FunctionSpace& source, const FunctionSpace& target) { |
| 432 |
ATLAS_TRACE("atlas::interpolation::method::Method::setup(FunctionSpace, FunctionSpace)"); |
| 433 |
this->do_setup(source, target); |
| 434 |
if (adjoint_) { |
| 435 |
adjoint_matrix(); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
const Method::Matrix& Method::adjoint_matrix() const { |
| 440 |
if (not matrix_transpose_) { |
| 441 |
if (target().size() == 0) { |
| 442 |
matrix_transpose_ = std::make_unique<Matrix>(); // Empty matrix |
| 443 |
} |
| 444 |
else { |
| 445 |
ATLAS_ASSERT(matrix_); |
| 446 |
eckit::linalg::SparseMatrix matrix_copy = make_eckit_sparse_matrix(*matrix_); // Makes a copy! |
| 447 |
matrix_copy.transpose(); // transpose the copy in place |
| 448 |
matrix_transpose_ = std::make_unique<Matrix>(linalg::make_sparse_matrix_storage(std::move(matrix_copy))); // Move the copy into storage |
| 449 |
} |
| 450 |
} |
| 451 |
return *matrix_transpose_; |
| 452 |
} |
| 453 |
|
| 454 |
void Method::setup(const Grid& source, const Grid& target) { |
| 455 |
ATLAS_TRACE("atlas::interpolation::method::Method::setup(Grid, Grid)"); |
| 456 |
this->do_setup(source, target, Cache()); |
| 457 |
} |
| 458 |
|
| 459 |
void Method::setup(const FunctionSpace& source, const Field& target) { |
| 460 |
ATLAS_TRACE("atlas::interpolation::method::Method::setup(FunctionSpace, Field)"); |
| 461 |
this->do_setup(source, target); |
| 462 |
} |
| 463 |
|
| 464 |
void Method::setup(const FunctionSpace& source, const FieldSet& target) { |
| 465 |
ATLAS_TRACE("atlas::interpolation::method::Method::setup(FunctionSpace, FieldSet)"); |
| 466 |
this->do_setup(source, target); |
| 467 |
} |
| 468 |
|
| 469 |
void Method::setup(const Grid& source, const Grid& target, const Cache& cache) { |
| 470 |
ATLAS_TRACE("atlas::interpolation::method::Method::setup(Grid, Grid, Cache)"); |
| 471 |
this->do_setup(source, target, cache); |
| 472 |
} |
| 473 |
|
| 474 |
void Method::setup(const FunctionSpace& source, const FunctionSpace& target, const Cache& cache) { |
| 475 |
ATLAS_TRACE("atlas::interpolation::method::Method::setup(FunctionSpace, FunctionSpace, Cache)"); |
| 476 |
this->do_setup(source, target, cache); |
| 477 |
} |
| 478 |
|
| 479 |
Method::Metadata Method::execute(const FieldSet& source, FieldSet& target) const { |
| 480 |
ATLAS_TRACE("atlas::interpolation::method::Method::execute(FieldSet, FieldSet)"); |
| 481 |
Metadata metadata; |
| 482 |
this->do_execute(source, target, metadata); |
| 483 |
return metadata; |
| 484 |
} |
| 485 |
|
| 486 |
Method::Metadata Method::execute(const Field& source, Field& target) const { |
| 487 |
ATLAS_TRACE("atlas::interpolation::method::Method::execute(Field, Field)"); |
| 488 |
Metadata metadata; |
| 489 |
this->do_execute(source, target, metadata); |
| 490 |
return metadata; |
| 491 |
} |
| 492 |
|
| 493 |
Method::Metadata Method::execute_adjoint(FieldSet& source, const FieldSet& target) const { |
| 494 |
ATLAS_TRACE("atlas::interpolation::method::Method::execute_adjoint(FieldSet, FieldSet)"); |
| 495 |
Metadata metadata; |
| 496 |
this->do_execute_adjoint(source, target, metadata); |
| 497 |
return metadata; |
| 498 |
} |
| 499 |
|
| 500 |
Method::Metadata Method::execute_adjoint(Field& source, const Field& target) const { |
| 501 |
ATLAS_TRACE("atlas::interpolation::method::Method::execute_adjoint(Field, Field)"); |
| 502 |
Metadata metadata; |
| 503 |
this->do_execute_adjoint(source, target, metadata); |
| 504 |
return metadata; |
| 505 |
} |
| 506 |
|
| 507 |
void Method::do_setup(const FunctionSpace& /*source*/, const Field& /*target*/) { |
| 508 |
ATLAS_NOTIMPLEMENTED; |
| 509 |
} |
| 510 |
|
| 511 |
void Method::do_setup(const FunctionSpace& /*source*/, const FieldSet& /*target*/) { |
| 512 |
ATLAS_NOTIMPLEMENTED; |
| 513 |
} |
| 514 |
|
| 515 |
void Method::do_execute(const FieldSet& fieldsSource, FieldSet& fieldsTarget, Metadata& metadata) const { |
| 516 |
ATLAS_TRACE("atlas::interpolation::method::Method::do_execute()"); |
| 517 |
|
| 518 |
const idx_t N = fieldsSource.size(); |
| 519 |
ATLAS_ASSERT(N == fieldsTarget.size()); |
| 520 |
|
| 521 |
for (idx_t i = 0; i < fieldsSource.size(); ++i) { |
| 522 |
Method::do_execute(fieldsSource[i], fieldsTarget[i], metadata); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
void Method::do_execute(const Field& src, Field& tgt, Metadata&) const { |
| 527 |
ATLAS_TRACE("atlas::interpolation::method::Method::do_execute()"); |
| 528 |
|
| 529 |
if (src.hostNeedsUpdate() && src.deviceNeedsUpdate()) { |
| 530 |
throw_AssertionFailed("Inconsistent memory state flags - we will not be able to " |
| 531 |
"determine which memory space to perform the halo exchange on", |
| 532 |
Here()); |
| 533 |
} |
| 534 |
|
| 535 |
sparse::Backend backend{linalg_backend_}; |
| 536 |
|
| 537 |
const bool on_device = executesOnDevice(backend); |
| 538 |
|
| 539 |
haloExchange(src, on_device); |
| 540 |
|
| 541 |
if( matrix_ ) { // (matrix == nullptr) when a partition is empty |
| 542 |
if (src.datatype().kind() == array::DataType::KIND_REAL64) { |
| 543 |
interpolate_field<double>(src, tgt, *matrix_); |
| 544 |
} |
| 545 |
else if (src.datatype().kind() == array::DataType::KIND_REAL32) { |
| 546 |
interpolate_field<float>(src, tgt, *matrix_); |
| 547 |
} |
| 548 |
else { |
| 549 |
ATLAS_NOTIMPLEMENTED; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
// carry over missing value metadata |
| 554 |
if (not tgt.metadata().has("missing_value")) { |
| 555 |
field::MissingValue mv_src(src); |
| 556 |
if (mv_src) { |
| 557 |
mv_src.metadata(tgt); |
| 558 |
ATLAS_ASSERT(field::MissingValue(tgt)); |
| 559 |
} |
| 560 |
else if (not missing_.empty()) { |
| 561 |
if (not tgt.metadata().has("missing_value")) { |
| 562 |
tgt.metadata().set("missing_value", 9999.); |
| 563 |
} |
| 564 |
tgt.metadata().set("missing_value_type", "equals"); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
// set missing values on host |
| 569 |
if (not missing_.empty()) { |
| 570 |
if (on_device) { |
| 571 |
tgt.updateHost(); |
| 572 |
} |
| 573 |
set_missing_values(tgt, missing_); |
| 574 |
tgt.set_dirty(); |
| 575 |
tgt.setDeviceNeedsUpdate(true); |
| 576 |
} |
| 577 |
else { |
| 578 |
tgt.set_dirty(); |
| 579 |
on_device ? tgt.setHostNeedsUpdate(true) : tgt.setDeviceNeedsUpdate(true); |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
void Method::do_execute_adjoint(FieldSet& fieldsSource, const FieldSet& fieldsTarget, Metadata& metadata) const { |
| 584 |
ATLAS_TRACE("atlas::interpolation::method::Method::do_execute_adjoint()"); |
| 585 |
|
| 586 |
const idx_t N = fieldsSource.size(); |
| 587 |
ATLAS_ASSERT(N == fieldsTarget.size()); |
| 588 |
|
| 589 |
for (idx_t i = 0; i < fieldsSource.size(); ++i) { |
| 590 |
Method::do_execute_adjoint(fieldsSource[i], fieldsTarget[i], metadata); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
void Method::do_execute_adjoint(Field& src, const Field& tgt, Metadata&) const { |
| 595 |
ATLAS_TRACE("atlas::interpolation::method::Method::do_execute_adjoint()"); |
| 596 |
|
| 597 |
if (src.hostNeedsUpdate() && src.deviceNeedsUpdate()) { |
| 598 |
throw_AssertionFailed("Inconsistent memory state flags - we will not be able to " |
| 599 |
"determine which memory space to perform the adjoint halo exchange on", |
| 600 |
Here()); |
| 601 |
} |
| 602 |
|
| 603 |
sparse::Backend backend{linalg_backend_}; |
| 604 |
|
| 605 |
if (nonLinear_(src)) { |
| 606 |
throw_NotImplemented("Adjoint interpolation only works for interpolation schemes that are linear", Here()); |
| 607 |
} |
| 608 |
|
| 609 |
if (not missing_.empty()) { |
| 610 |
throw_NotImplemented("Adjoint Interpolation does not work for fields that have missing data. ", Here()); |
| 611 |
} |
| 612 |
|
| 613 |
if (src.datatype().kind() == array::DataType::KIND_REAL64) { |
| 614 |
adjoint_interpolate_field<double>(src, tgt, adjoint_matrix()); |
| 615 |
} |
| 616 |
else if (src.datatype().kind() == array::DataType::KIND_REAL32) { |
| 617 |
adjoint_interpolate_field<float>(src, tgt, adjoint_matrix()); |
| 618 |
} |
| 619 |
else { |
| 620 |
ATLAS_NOTIMPLEMENTED; |
| 621 |
} |
| 622 |
|
| 623 |
src.set_dirty(); |
| 624 |
|
| 625 |
const bool on_device = executesOnDevice(backend); |
| 626 |
|
| 627 |
adjointHaloExchange(src, on_device); |
| 628 |
|
| 629 |
on_device ? src.setHostNeedsUpdate(true) : src.setDeviceNeedsUpdate(true); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
void Method::normalise(Triplets& triplets) { |
| 634 |
// sum all calculated weights for normalisation |
| 635 |
double sum = 0.0; |
| 636 |
|
| 637 |
for (size_t j = 0; j < triplets.size(); ++j) { |
| 638 |
sum += triplets[j].value(); |
| 639 |
} |
| 640 |
|
| 641 |
// now normalise all weights according to the total |
| 642 |
const double invSum = 1.0 / sum; |
| 643 |
for (size_t j = 0; j < triplets.size(); ++j) { |
| 644 |
triplets[j].value() *= invSum; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
void Method::haloExchange(const FieldSet& fields, bool on_device) const { |
| 649 |
for (auto& field : fields) { |
| 650 |
haloExchange(field, on_device); |
| 651 |
} |
| 652 |
} |
| 653 |
void Method::haloExchange(const Field& field, bool on_device) const { |
| 654 |
if (field.dirty() && allow_halo_exchange_) { |
| 655 |
ATLAS_TRACE("haloExchange"); |
| 656 |
if (on_device) { |
| 657 |
if (field.deviceNeedsUpdate()) { |
| 658 |
// Prefer halo exchange on host and copy to device |
| 659 |
source().haloExchange(field); // on host |
| 660 |
field.syncDevice(); |
| 661 |
} |
| 662 |
else { |
| 663 |
source().haloExchange(field, on_device); // on device |
| 664 |
} |
| 665 |
} |
| 666 |
else { |
| 667 |
source().haloExchange(field); // on host |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
void Method::adjointHaloExchange(const FieldSet& fields, bool on_device) const { |
| 673 |
for (auto& field : fields) { |
| 674 |
adjointHaloExchange(field, on_device); |
| 675 |
} |
| 676 |
} |
| 677 |
void Method::adjointHaloExchange(const Field& field, bool on_device) const { |
| 678 |
if (field.dirty() && allow_halo_exchange_) { |
| 679 |
ATLAS_TRACE("adjointHaloExchange"); |
| 680 |
if (on_device) { |
| 681 |
if (field.deviceNeedsUpdate()) { |
| 682 |
// Prefer halo exchange on host and copy to device |
| 683 |
source().adjointHaloExchange(field); // on host |
| 684 |
field.syncDevice(); |
| 685 |
} |
| 686 |
else { |
| 687 |
source().adjointHaloExchange(field, on_device); // on device |
| 688 |
} |
| 689 |
} |
| 690 |
else { |
| 691 |
source().adjointHaloExchange(field); // on host |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
interpolation::Cache Method::createCache() const { |
| 697 |
return matrix_cache_; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
} // namespace interpolation |
| 702 |
} // namespace atlas |