Debian Codegraph @

src/atlas/interpolation/method/binning/Binning.cc

raw
1 /*
2 * (C) Crown Copyright 2024 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 #include "atlas/interpolation/method/binning/Binning.h"
9
10 #include <climits>
11 #include <cstddef>
12 #include <cstring>
13 #include <type_traits>
14 #include <utility>
15 #include <vector>
16
17 #include "atlas/functionspace/FunctionSpace.h"
18 #include "atlas/functionspace/NodeColumns.h"
19 #include "atlas/grid.h"
20 #include "atlas/grid/CubedSphereGrid.h"
21 #include "atlas/interpolation/Cache.h"
22 #include "atlas/interpolation/Interpolation.h"
23 #include "atlas/interpolation/method/MethodFactory.h"
24 #include "atlas/library/config.h"
25 #include "atlas/linalg/sparse/SparseMatrixTriplet.h"
26 #include "atlas/mesh/actions/GetCubedSphereNodalArea.h"
27 #include "atlas/parallel/mpi/mpi.h"
28 #include "atlas/runtime/Exception.h"
29 #include "atlas/runtime/Trace.h"
30 #include "eckit/config/LocalConfiguration.h"
31
32
33 namespace atlas {
34 namespace interpolation {
35 namespace method {
36
37 namespace {
38
39 MethodBuilder<Binning> __builder("binning");
40
41 using TripletType = linalg::Triplet<Binning::ValueType, Binning::IndexType>;
42 static_assert(std::is_trivially_copyable_v<TripletType>);
43
44 class MpiBuffer {
45 public:
46 MpiBuffer(): buffer_{mpi::comm().size()} {}
47
48 std::size_t size(std::size_t rank) const {
49 const auto n = buffer_.at(rank).size();
50 ATLAS_ASSERT(n % sizeof(TripletType) == 0, "Buffer size not a multiple of Triplet size");
51 return n / sizeof(TripletType);
52 }
53
54 void pushBack(std::size_t rank, const TripletType& triplet) {
55 auto& subBuffer = buffer_.at(rank);
56 alignas(TripletType) auto appendBuffer = std::array<char, sizeof(TripletType)>{};
57 std::memcpy(appendBuffer.data(), &triplet, sizeof(TripletType));
58 subBuffer.insert(subBuffer.end(), appendBuffer.begin(), appendBuffer.end());
59 }
60
61 auto get(std::size_t rank) const {
62 const auto& subBuffer = buffer_.at(rank);
63 return [&subBuffer](size_t index) -> TripletType {
64 TripletType triplet{};
65 std::memcpy(&triplet, subBuffer.data() + index * sizeof(TripletType), sizeof(TripletType));
66 return triplet;
67 };
68 }
69
70 MpiBuffer allToAll() const {
71 auto recvBuffer = MpiBuffer{};
72 mpi::comm().allToAll(buffer_, recvBuffer.buffer_);
73 return recvBuffer;
74 }
75
76 private:
77 std::vector<std::vector<char>> buffer_{};
78 };
79
80 } // namespace
81
82 Binning::Binning(const Config& config): Method(config) {
83 const auto* conf = dynamic_cast<const eckit::LocalConfiguration*>(&config);
84 ATLAS_ASSERT(conf, "config must be derived from eckit::LocalConfiguration");
85 interpAncillaryScheme_ = conf->getSubConfiguration("scheme");
86 }
87
88
89 void Binning::do_setup(const Grid& source, const Grid& target, const Cache&) {
90 ATLAS_NOTIMPLEMENTED;
91 }
92
93 void Binning::do_setup(const FunctionSpace& source, const FunctionSpace& target, const Cache&) {
94 ATLAS_NOTIMPLEMENTED;
95 }
96
97
98 void Binning::do_setup(const FunctionSpace& source, const FunctionSpace& target) {
99 ATLAS_TRACE("atlas::interpolation::method::Binning::do_setup()");
100
101 source_ = source;
102 target_ = target;
103
104 if (target_.size() == 0) {
105 return;
106 }
107
108 const auto interp = Interpolation(interpAncillaryScheme_, target_, source_);
109 const auto interpMatrixStorage = interpolation::MatrixCache(interp).matrix();
110 const auto interpMatrixView = linalg::make_host_view<ValueType, IndexType>(interpMatrixStorage);
111 auto transposeMatrixStorage = transposeAndHaloExchange(interpMatrixView);
112 auto transposeMatrixView = linalg::make_host_view<ValueType, IndexType>(transposeMatrixStorage);
113
114 const IndexType transposeRows = transposeMatrixView.rows();
115 const IndexType transposeCols = transposeMatrixView.cols();
116
117
118 const auto areaWeightsVector = getAreaWeights();
119
120 auto triplets = std::vector<TripletType>{};
121 triplets.reserve(transposeMatrixView.nnz());
122
123 const auto weightedValue = [&](IndexType col, ValueType value) {
124 return value * areaWeightsVector.at(col);
125 };
126
127 // Area-weight and normalise rows.
128 for (std::size_t rowIdx = 0; rowIdx < transposeRows; ++rowIdx) {
129
130 ValueType rowSum = 0.;
131 linalg::sparse_matrix_for_each_row(rowIdx, transposeMatrixView,
132 [&](IndexType row, IndexType col, ValueType value) { rowSum += weightedValue(col, value); });
133
134 linalg::sparse_matrix_for_each_row(rowIdx, transposeMatrixView, [&](IndexType row, IndexType col, ValueType value) {
135 triplets.emplace_back(row, col, weightedValue(col, value) / rowSum);
136 });
137 }
138
139 setMatrix(linalg::make_sparse_matrix_storage_from_triplets(transposeRows, transposeCols, triplets));
140 }
141
142
143 void Binning::print(std::ostream&) const {
144 ATLAS_NOTIMPLEMENTED;
145 }
146
147 Binning::SparseMatrixStorage Binning::transposeAndHaloExchange(const SparseMatrixView& interpMatrix) const {
148 // Transpose interpolation matrix. If transposed matrix contains rows which map into ghost elements,
149 // move the rows to the PE which owns the element.
150
151 struct Views {
152 Views(const FunctionSpace& functionSpace):
153 partition{array::make_view<int, 1>(functionSpace.partition())},
154 remote_index{array::make_indexview<idx_t, 1>(functionSpace.remote_index())},
155 ghost{array::make_view<int, 1>(functionSpace.ghost())} {}
156 array::ArrayView<int, 1> partition;
157 array::IndexView<idx_t, 1> remote_index;
158 array::ArrayView<int, 1> ghost;
159 };
160 const Views sourceViews{source_};
161 const Views targetViews{target_};
162
163 // MPI send buffers.
164 auto triplets = std::vector<TripletType>{};
165 auto sendBuffer = MpiBuffer{};
166
167 linalg::sparse_matrix_for_each(interpMatrix, [&](IndexType row, IndexType col, ValueType weight) {
168 // Swap row and column indices for transpose.
169 std::swap(row, col);
170
171
172 // Skip rows of interp matrix (i.e., cols of this transposed matrix) that are made redundant by halo exchanges.
173 // These shouldn't exist, but some interpolation methods generate them anyway.
174 if (sourceViews.ghost(col)) {
175 return;
176 }
177
178 if (!targetViews.ghost(row)) {
179 // Rows is owned by target function space.
180 triplets.emplace_back(row, col, weight);
181 }
182 else {
183 // Create MPI send data buffers for non-owned rows.
184 const int remoteRank = targetViews.partition(row);
185 const IndexType remoteRow = static_cast<IndexType>(targetViews.remote_index(row));
186 sendBuffer.pushBack(remoteRank, {remoteRow, col, weight});
187 }
188 });
189
190 const auto recvBuffer = sendBuffer.allToAll();
191
192 // Create a map from source partition and remote index to local index for ghost points only.
193 auto sourceRemoteToLocalMap = std::map<std::pair<int, idx_t>, idx_t>{};
194 for (idx_t localCol = 0; localCol < source_.size(); ++localCol) {
195 if (sourceViews.ghost(localCol)) {
196 const int remoteRank = sourceViews.partition(localCol);
197 const idx_t remoteCol = sourceViews.remote_index(localCol);
198 sourceRemoteToLocalMap.insert({{remoteRank, remoteCol}, localCol});
199 }
200 }
201
202 // Iterate over received data, creating owned rows for ghost columns
203 for (std::size_t remoteRank = 0; remoteRank < mpi::comm().size(); ++remoteRank) {
204 const auto subBuffer = recvBuffer.get(remoteRank);
205 const auto subBufferSize = recvBuffer.size(remoteRank);
206 for (std::size_t i = 0; i < subBufferSize; ++i) {
207 const auto triplet = subBuffer(i);
208
209 const IndexType row = triplet.row();
210 const IndexType remoteCol = triplet.col();
211 const ValueType weight = triplet.value();
212
213 ATLAS_ASSERT_MSG(!targetViews.ghost(row), "Row should be owned by the target functionspace");
214
215 const auto col = [&] {
216 try {
217 return sourceRemoteToLocalMap.at({remoteRank, remoteCol});
218 }
219 catch (const std::out_of_range&) {
220 ATLAS_THROW_EXCEPTION(
221 "Column local index not found. Try increasing source functionspace halo size.");
222 }
223 }();
224 ATLAS_ASSERT_MSG(sourceViews.ghost(col), "Column should not be owned by the source functionspace.");
225 triplets.emplace_back(row, col, weight);
226 }
227 }
228 return linalg::make_sparse_matrix_storage_from_triplets(
229 static_cast<IndexType>(target_.size()), static_cast<IndexType>(source_.size()), triplets);
230 }
231
232 std::vector<double> Binning::getAreaWeights() const {
233 const auto csGrid = CubedSphereGrid(source_.grid());
234 auto ncFunctionSpace = functionspace::NodeColumns(source_);
235
236 if (!(csGrid && ncFunctionSpace)) {
237 // If not a cubed sphere grid and a node columns function space, return equal weights.
238 return std::vector<double>(source_.size(), 1.);
239 }
240
241 auto mesh = ncFunctionSpace.mesh();
242 const auto nodeWeights = mesh::actions::GetCubedSphereNodalArea()(mesh);
243 const auto nodeWeightsView = array::make_view<double, 1>(nodeWeights);
244
245 auto weightVector = std::vector<double>(source_.size());
246 for (idx_t i = 0; i < source_.size(); ++i) {
247 weightVector[i] = nodeWeightsView(i);
248 }
249 return weightVector;
250 }
251
252 } // namespace method
253 } // namespace interpolation
254 } // namespace atlas