| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <iosfwd> |
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
#include "atlas/interpolation/Cache.h" |
| 18 |
#include "atlas/interpolation/NonLinear.h" |
| 19 |
#include "atlas/linalg/sparse/SparseMatrixStorage.h" |
| 20 |
#include "atlas/util/Metadata.h" |
| 21 |
#include "atlas/util/Object.h" |
| 22 |
#include "eckit/config/Configuration.h" |
| 23 |
#include "eckit/linalg/SparseMatrix.h" |
| 24 |
#include "atlas/linalg/sparse/MakeSparseMatrixStorageEckit.h" |
| 25 |
|
| 26 |
namespace atlas { |
| 27 |
class Field; |
| 28 |
class FieldSet; |
| 29 |
class FunctionSpace; |
| 30 |
class Grid; |
| 31 |
} // namespace atlas |
| 32 |
|
| 33 |
namespace atlas { |
| 34 |
namespace test { |
| 35 |
class Access; |
| 36 |
} |
| 37 |
} // namespace atlas |
| 38 |
|
| 39 |
namespace atlas { |
| 40 |
namespace interpolation { |
| 41 |
|
| 42 |
class Method : public util::Object { |
| 43 |
public: |
| 44 |
using Config = eckit::Parametrisation; |
| 45 |
using Metadata = util::Metadata; |
| 46 |
|
| 47 |
Method(const Config&); |
| 48 |
virtual ~Method() {} |
| 49 |
|
| 50 |
/** |
| 51 |
* @brief Setup the interpolator relating two functionspaces |
| 52 |
* @param source functionspace containing source elements |
| 53 |
* @param target functionspace containing target points |
| 54 |
*/ |
| 55 |
void setup(const FunctionSpace& source, const FunctionSpace& target); |
| 56 |
void setup(const Grid& source, const Grid& target); |
| 57 |
void setup(const FunctionSpace& source, const Field& target); |
| 58 |
void setup(const FunctionSpace& source, const FieldSet& target); |
| 59 |
void setup(const Grid& source, const Grid& target, const Cache&); |
| 60 |
void setup(const FunctionSpace& source, const FunctionSpace& target, const Cache&); |
| 61 |
|
| 62 |
Metadata execute(const FieldSet& source, FieldSet& target) const; |
| 63 |
Metadata execute(const Field& source, Field& target) const; |
| 64 |
|
| 65 |
/** |
| 66 |
* @brief execute_adjoint |
| 67 |
* @param source - it is either a FieldSet or a Field |
| 68 |
* @param target - it is either a FieldSet or a Field |
| 69 |
* Note that formally in an adjoint operation of this |
| 70 |
* type we should be setting the values in the target |
| 71 |
* to zero. This is not done for efficiency reasons and |
| 72 |
* because in most cases it is not necessary. |
| 73 |
*/ |
| 74 |
Metadata execute_adjoint(FieldSet& source, const FieldSet& target) const; |
| 75 |
Metadata execute_adjoint(Field& source, const Field& target) const; |
| 76 |
|
| 77 |
virtual void print(std::ostream&) const = 0; |
| 78 |
|
| 79 |
virtual const FunctionSpace& source() const = 0; |
| 80 |
virtual const FunctionSpace& target() const = 0; |
| 81 |
|
| 82 |
virtual interpolation::Cache createCache() const; |
| 83 |
|
| 84 |
protected: |
| 85 |
virtual void do_execute(const FieldSet& source, FieldSet& target, Metadata&) const; |
| 86 |
virtual void do_execute(const Field& source, Field& target, Metadata&) const; |
| 87 |
|
| 88 |
virtual void do_execute_adjoint(FieldSet& source, const FieldSet& target, Metadata&) const; |
| 89 |
virtual void do_execute_adjoint(Field& source, const Field& target, Metadata&) const; |
| 90 |
|
| 91 |
using Triplet = eckit::linalg::Triplet; |
| 92 |
using Triplets = std::vector<Triplet>; |
| 93 |
using Matrix = atlas::linalg::SparseMatrixStorage; |
| 94 |
|
| 95 |
static void normalise(Triplets& triplets); |
| 96 |
|
| 97 |
void haloExchange(const FieldSet&, bool on_device = false) const; |
| 98 |
void haloExchange(const Field&, bool on_device = false) const; |
| 99 |
|
| 100 |
void adjointHaloExchange(const FieldSet&, bool on_device = false) const; |
| 101 |
void adjointHaloExchange(const Field&, bool on_device = false) const; |
| 102 |
|
| 103 |
// NOTE : Matrix-free or non-linear interpolation operators do not have matrices, so do not expose here |
| 104 |
friend class atlas::test::Access; |
| 105 |
friend class interpolation::MatrixCache; |
| 106 |
|
| 107 |
protected: |
| 108 |
|
| 109 |
void setMatrix(Matrix&& m, const std::string& uid = "") { |
| 110 |
if (not matrix_shared_) { |
| 111 |
matrix_shared_ = std::make_shared<Matrix>(); |
| 112 |
} |
| 113 |
*matrix_shared_ = std::move(m); |
| 114 |
matrix_cache_ = interpolation::MatrixCache(matrix_shared_, uid); |
| 115 |
matrix_ = &matrix_cache_.matrix(); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
void setMatrix(interpolation::MatrixCache matrix_cache) { |
| 120 |
ATLAS_ASSERT(matrix_cache); |
| 121 |
matrix_cache_ = matrix_cache; |
| 122 |
matrix_ = &matrix_cache_.matrix(); |
| 123 |
matrix_shared_.reset(); |
| 124 |
} |
| 125 |
|
| 126 |
void setMatrix(eckit::linalg::SparseMatrix&& m, const std::string& uid = "") { |
| 127 |
setMatrix( linalg::make_sparse_matrix_storage(std::move(m)), uid ); |
| 128 |
} |
| 129 |
|
| 130 |
void setMatrix(std::size_t rows, std::size_t cols, const Triplets& triplets, const std::string& uid = "") { |
| 131 |
setMatrix( eckit::linalg::SparseMatrix{rows, cols, triplets}, uid); |
| 132 |
} |
| 133 |
|
| 134 |
bool matrixAllocated() const { return matrix_shared_.use_count(); } |
| 135 |
|
| 136 |
const Matrix& matrix() const { |
| 137 |
ATLAS_ASSERT(matrix_ != nullptr); |
| 138 |
return *matrix_; |
| 139 |
} |
| 140 |
|
| 141 |
const Matrix& adjoint_matrix() const; |
| 142 |
|
| 143 |
virtual void do_setup(const FunctionSpace& source, const FunctionSpace& target) = 0; |
| 144 |
virtual void do_setup(const Grid& source, const Grid& target, const Cache&) = 0; |
| 145 |
virtual void do_setup(const FunctionSpace& source, const FunctionSpace& target, const Cache&) = 0; |
| 146 |
virtual void do_setup(const FunctionSpace& source, const Field& target); |
| 147 |
virtual void do_setup(const FunctionSpace& source, const FieldSet& target); |
| 148 |
|
| 149 |
void check_compatibility(const Field& src, const Field& tgt, const Matrix& W) const; |
| 150 |
|
| 151 |
private: |
| 152 |
template <typename Value> |
| 153 |
void interpolate_field(const Field& src, Field& tgt, const Matrix&) const; |
| 154 |
|
| 155 |
template <typename Value> |
| 156 |
void interpolate_field_rank1(const Field& src, Field& tgt, const Matrix&) const; |
| 157 |
|
| 158 |
template <typename Value> |
| 159 |
void interpolate_field_rank2(const Field& src, Field& tgt, const Matrix&) const; |
| 160 |
|
| 161 |
template <typename Value> |
| 162 |
void interpolate_field_rank3(const Field& src, Field& tgt, const Matrix&) const; |
| 163 |
|
| 164 |
template <typename Value> |
| 165 |
void adjoint_interpolate_field(Field& src, const Field& tgt, const Matrix&) const; |
| 166 |
|
| 167 |
template <typename Value> |
| 168 |
void adjoint_interpolate_field_rank1(Field& src, const Field& tgt, const Matrix&) const; |
| 169 |
|
| 170 |
template <typename Value> |
| 171 |
void adjoint_interpolate_field_rank2(Field& src, const Field& tgt, const Matrix&) const; |
| 172 |
|
| 173 |
template <typename Value> |
| 174 |
void adjoint_interpolate_field_rank3(Field& src, const Field& tgt, const Matrix&) const; |
| 175 |
|
| 176 |
private: |
| 177 |
const Matrix* matrix_ = nullptr; |
| 178 |
std::shared_ptr<Matrix> matrix_shared_; |
| 179 |
interpolation::MatrixCache matrix_cache_; |
| 180 |
NonLinear nonLinear_; |
| 181 |
std::string linalg_backend_; |
| 182 |
bool adjoint_{false}; |
| 183 |
mutable std::unique_ptr<Matrix> matrix_transpose_; |
| 184 |
|
| 185 |
protected: |
| 186 |
bool allow_halo_exchange_{true}; |
| 187 |
std::vector<idx_t> missing_; |
| 188 |
}; |
| 189 |
|
| 190 |
} // namespace interpolation |
| 191 |
} // namespace atlas |