Skip to content

Commit 645583a

Browse files
committed
MIR- eckit::geo ProxyWeightedMethod
1 parent a9d59f5 commit 645583a

File tree

2 files changed

+56
-46
lines changed

2 files changed

+56
-46
lines changed

src/mir/method/ProxyWeightedMethod.cc

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,44 @@
1313
#include "mir/method/ProxyWeightedMethod.h"
1414

1515
#include <ostream>
16+
#include <vector>
1617

1718
#include "eckit/log/JSON.h"
1819
#include "eckit/utils/MD5.h"
1920

2021
#include "mir/param/MIRParametrisation.h"
2122
#include "mir/repres/Representation.h"
2223
#include "mir/util/Exceptions.h"
24+
#include "mir/util/Log.h"
25+
#include "mir/util/allocator/InPlaceAllocator.h"
2326

2427

2528
namespace mir::method {
2629

2730

31+
namespace {
32+
33+
2834
struct StructuredBicubic final : public ProxyWeightedMethod {
2935
explicit StructuredBicubic(const param::MIRParametrisation& param) :
30-
ProxyWeightedMethod(param, "structured-bicubic") {}
36+
ProxyWeightedMethod(param, "structured-bicubic", "serial-halo to serial") {}
3137
};
3238

3339

40+
} // namespace
41+
42+
3443
static const MethodFactory* METHODS[]{
3544
new MethodBuilder<StructuredBicubic>("structured-bicubic"),
3645
};
3746

3847

39-
ProxyWeightedMethod::ProxyWeightedMethod(const param::MIRParametrisation& param, const std::string& type) :
40-
MethodWeighted(param), type_(type) {
41-
options_ = {"type", type};
42-
options_.set("matrix_free", false);
48+
ProxyWeightedMethod::ProxyWeightedMethod(const param::MIRParametrisation& param, const std::string& interpolation_type,
49+
const std::string& renumber_type) :
50+
MethodWeighted(param), type_(interpolation_type) {
51+
interpol_.set("matrix_free", false);
52+
interpol_.set("type", interpolation_type);
53+
renumber_.set("type", renumber_type);
4354
}
4455

4556

@@ -55,70 +66,72 @@ int ProxyWeightedMethod::version() const {
5566

5667
void ProxyWeightedMethod::hash(eckit::MD5& h) const {
5768
MethodWeighted::hash(h);
58-
h.add(options_);
69+
h.add(interpol_);
5970
}
6071

6172

6273
bool ProxyWeightedMethod::sameAs(const Method& other) const {
63-
auto digest = [](const auto& options) {
74+
auto digest = [](const auto& config) {
6475
eckit::MD5 h;
65-
h.add(options);
76+
config.hash(h);
6677
return h.digest();
6778
};
6879

6980
const auto* o = dynamic_cast<const ProxyWeightedMethod*>(&other);
70-
return (o != nullptr) && name() == std::string{o->name()} && digest(options_) == digest(o->options_) &&
71-
MethodWeighted::sameAs(*o);
81+
return (o != nullptr) && name() == std::string{o->name()} && digest(interpol_) == digest(o->interpol_) &&
82+
digest(renumber_) == digest(o->renumber_) && MethodWeighted::sameAs(*o);
7283
}
7384

7485

7586
void ProxyWeightedMethod::print(std::ostream& out) const {
76-
out << "ProxyWeightedMethod[options=" << options_ << ",";
87+
out << "ProxyWeightedMethod[interpolation=" << interpol_ << ",renumber=" << renumber_ << ",";
7788
MethodWeighted::print(out);
7889
out << "]";
7990
}
8091

8192

8293
void ProxyWeightedMethod::json(eckit::JSON& j) const {
8394
j.startObject();
84-
j << "options" << options_.json(eckit::JSON::Formatting::compact());
95+
j << "options" << interpol_.json(eckit::JSON::Formatting::compact());
8596
MethodWeighted::json(j);
8697
j.endObject();
8798
}
8899

89100

90101
void ProxyWeightedMethod::assemble(util::MIRStatistics&, WeightMatrix& W, const repres::Representation& in,
91102
const repres::Representation& out) const {
92-
atlas::Interpolation interpol(options_, in.atlasGrid(), out.atlasGrid());
93-
94-
auto cache = interpol.createCache();
95-
ASSERT(cache);
96-
97-
const auto* entry = dynamic_cast<const atlas::interpolation::MatrixCacheEntry*>(cache.get("Matrix"));
98-
ASSERT(entry != nullptr);
99-
100-
W.swap(const_cast<eckit::linalg::SparseMatrix&>(entry->matrix()));
101-
102-
auto& fs = interpol.source();
103-
const auto gi{atlas::array::make_view<atlas::gidx_t, 1>(fs.global_index())};
104-
105-
atlas::gidx_t min = std::numeric_limits<atlas::gidx_t>::max();
106-
atlas::gidx_t max = std::numeric_limits<atlas::gidx_t>::min();
107-
for (size_t i = 0; i < gi.size(); ++i) {
108-
min = std::min(min, gi(i));
109-
max = std::max(max, gi(i));
103+
// interpolation matrix build and assign (with a halo on the source grid)
104+
atlas::Interpolation interpol{interpol_, in.atlasGrid(), out.atlasGrid()};
105+
{
106+
atlas::interpolation::MatrixCache cache{interpol};
107+
const auto& M = cache.matrix();
108+
W.swap(const_cast<eckit::linalg::SparseMatrix&>(M));
110109
}
111110

112-
std::cout << "min: " << min << std::endl;
113-
std::cout << "max: " << max << std::endl;
114-
111+
// fold serial+halo into serial
112+
const auto Nr = out.numberOfPoints();
113+
const auto Nc = in.numberOfPoints();
114+
ASSERT(Nr == W.rows());
115+
ASSERT(Nc < W.cols());
116+
117+
{
118+
const auto& fs = interpol.source();
119+
const auto gidx{atlas::array::make_view<atlas::gidx_t, 1>(fs.global_index())};
120+
121+
auto* a = const_cast<eckit::linalg::Scalar*>(W.data());
122+
for (auto c = Nc; c < W.cols(); ++c) {
123+
ASSERT(1 <= gidx[c] && gidx[c] < Nc + 1); // (global indexing is 1-based)
124+
a[gidx[c] - 1] += a[c];
125+
a[c] = 0.;
126+
}
127+
W.prune();
128+
}
115129

116-
auto Nr = W.rows();
117-
auto Nc = W.cols();
118-
auto Nin = in.numberOfPoints();
119-
auto Nout = out.numberOfPoints();
120-
ASSERT(Nr == Nout);
121-
ASSERT(Nc == Nin);
130+
// reshape matrix
131+
WeightMatrix M(new util::allocator::InPlaceAllocator{
132+
Nr, Nc, W.nonZeros(), const_cast<eckit::linalg::Index*>(W.outer()),
133+
const_cast<eckit::linalg::Index*>(W.inner()), const_cast<eckit::linalg::Scalar*>(W.data())});
134+
W.swap(M);
122135
}
123136

124137

src/mir/method/ProxyWeightedMethod.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,19 @@ class ProxyWeightedMethod : public MethodWeighted {
2424
protected:
2525
// -- Constructor
2626

27-
ProxyWeightedMethod(const param::MIRParametrisation&, const std::string& type);
27+
ProxyWeightedMethod(const param::MIRParametrisation&, const std::string& interpolation_type,
28+
const std::string& renumber_type = "");
2829

2930
// -- Destructor
3031

3132
~ProxyWeightedMethod() override = default;
3233

33-
// -- Methods
34-
35-
const auto& options() const { return options_; }
36-
auto& options() { return options_; }
37-
3834
private:
3935
// -- Members
4036

4137
const std::string type_;
42-
atlas::util::Config options_;
38+
atlas::util::Config interpol_;
39+
atlas::util::Config renumber_;
4340

4441
// -- Overridden methods
4542

0 commit comments

Comments
 (0)