Debian Codegraph @

src/atlas/mesh/actions/Reorder.cc

raw
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 <array>
12 #include <bitset>
13 #include <utility>
14
15 #include "atlas/mesh/actions/Reorder.h"
16
17 // For static linking
18 #include "atlas/mesh/actions/ReorderHilbert.h"
19 #include "atlas/mesh/actions/ReorderReverseCuthillMckee.h"
20
21
22 #include "atlas/array.h"
23 #include "atlas/mesh/Elements.h"
24 #include "atlas/mesh/HybridElements.h"
25 #include "atlas/mesh/Mesh.h"
26 #include "atlas/mesh/Nodes.h"
27 #include "atlas/runtime/Exception.h"
28 #include "atlas/runtime/Log.h"
29 #include "atlas/runtime/Trace.h"
30
31 namespace atlas {
32 namespace mesh {
33 namespace actions {
34
35 // ------------------------------------------------------------------
36
37 struct force_link {
38 template <typename T>
39 void load_builder() {
40 ReorderBuilder<T>("tmp");
41 }
42 force_link() {
43 load_builder<ReorderHilbert>();
44 load_builder<ReorderReverseCuthillMckee>();
45 }
46 };
47
48
49 // ------------------------------------------------------------------
50
51 const ReorderImpl* ReorderFactory::build(const eckit::Parametrisation& config) {
52 static force_link static_linking;
53 std::string builder{"none"};
54 config.get("type", builder);
55 auto factory = get(builder);
56 return factory->make(config);
57 }
58
59 // ------------------------------------------------------------------
60
61 template <typename Value, int Rank>
62 struct ReorderField {};
63
64 // ------------------------------------------------------------------
65
66 template <typename Value>
67 struct ReorderField<Value, 1> {
68 static constexpr int Rank = 1;
69 static std::string apply(Field& field, const std::vector<idx_t>& order, idx_t begin, idx_t end) {
70 auto array = array::make_view<Value, Rank>(field);
71 end = std::min(end, array.shape(0));
72 idx_t size = end - begin;
73 array::ArrayT<Value> tmp_array(size);
74 auto tmp = array::make_view<Value, Rank>(tmp_array);
75 for (idx_t n = 0; n < size; ++n) {
76 tmp(n) = array(begin + n);
77 }
78 for (idx_t n = 0; n < size; ++n) {
79 array(begin + n) = tmp(order[n]);
80 }
81 return field.name();
82 }
83 };
84
85 // ------------------------------------------------------------------
86
87 template <typename Value>
88 struct ReorderField<Value, 2> {
89 static constexpr int Rank = 2;
90 static std::string apply(Field& field, const std::vector<idx_t>& order, idx_t begin, idx_t end) {
91 auto array = array::make_view<Value, Rank>(field);
92 end = std::min(end, array.shape(0));
93 idx_t size = end - begin;
94 array::ArrayT<Value> tmp_array(size, field.shape(1));
95 auto tmp = array::make_view<Value, Rank>(tmp_array);
96 for (idx_t n = 0; n < size; ++n) {
97 for (idx_t v = 0; v < array.shape(1); ++v) {
98 tmp(n, v) = array(begin + n, v);
99 }
100 }
101 for (idx_t n = 0; n < size; ++n) {
102 for (idx_t v = 0; v < array.shape(1); ++v) {
103 array(begin + n, v) = tmp(order[n], v);
104 }
105 }
106 return field.name();
107 }
108 };
109
110 // ------------------------------------------------------------------
111
112 template <typename Value>
113 std::string reorder_field_T(Field& field, const std::vector<idx_t>& order, idx_t begin, idx_t end) {
114 if (field.rank() == 1) {
115 return ReorderField<Value, 1>::apply(field, order, begin, end);
116 }
117 else if (field.rank() == 2) {
118 return ReorderField<Value, 2>::apply(field, order, begin, end);
119 }
120 else {
121 throw_Exception("rank not supported", Here());
122 }
123 }
124
125 // ------------------------------------------------------------------
126
127 std::string reorder_field(Field& field, const std::vector<idx_t>& order, idx_t begin = 0,
128 idx_t end = std::numeric_limits<idx_t>::max()) {
129 if (field.datatype() == array::DataType::kind<int>()) {
130 return reorder_field_T<int>(field, order, begin, end);
131 }
132 else if (field.datatype() == array::DataType::kind<long>()) {
133 return reorder_field_T<long>(field, order, begin, end);
134 }
135 else if (field.datatype() == array::DataType::kind<float>()) {
136 return reorder_field_T<float>(field, order, begin, end);
137 }
138 else if (field.datatype() == array::DataType::kind<double>()) {
139 return reorder_field_T<double>(field, order, begin, end);
140 }
141 else {
142 throw_Exception("datatype not supported", Here());
143 }
144 }
145
146 // ------------------------------------------------------------------
147
148 void update_connectivity(mesh::HybridElements::Connectivity& connectivity, const std::vector<idx_t>& order) {
149 for (idx_t b = 0; b < connectivity.blocks(); ++b) {
150 auto& block = connectivity.block(b);
151 for (idx_t r = 0; r < block.rows(); ++r) {
152 for (idx_t c = 0; c < block.cols(); ++c) {
153 idx_t n = block(r, c);
154 block.set(r, c, order.at(n));
155 }
156 }
157 }
158 }
159
160 // ------------------------------------------------------------------
161
162 void reorder_connectivity(BlockConnectivity& connectivity, const std::vector<idx_t>& order) {
163 ATLAS_ASSERT(connectivity.rows() == static_cast<idx_t>(order.size()));
164 BlockConnectivity tmp;
165 tmp.add(connectivity.rows(), connectivity.cols(), connectivity.data(), true);
166 for (idx_t r = 0; r < connectivity.rows(); ++r) {
167 for (idx_t c = 0; c < connectivity.cols(); ++c) {
168 if (connectivity(r, c) != tmp(r, c)) {
169 ATLAS_DEBUG_VAR(r);
170 ATLAS_DEBUG_VAR(c);
171 ATLAS_DEBUG_VAR(connectivity(r, c));
172 ATLAS_DEBUG_VAR(tmp(r, c));
173 }
174 ATLAS_ASSERT(connectivity(r, c) == tmp(r, c));
175 connectivity.set(r, c, tmp(order.at(r), c));
176 }
177 }
178 }
179
180 // ------------------------------------------------------------------
181
182 void ReorderImpl::reorderNodes(Mesh& mesh, const std::vector<idx_t>& order) {
183 std::vector<idx_t> order_inverse(order.size());
184 for (idx_t i = 0; i < static_cast<idx_t>(order.size()); ++i) {
185 order_inverse[order[i]] = i;
186 }
187
188 for (idx_t ifield = 0; ifield < mesh.nodes().nb_fields(); ++ifield) {
189 reorder_field(mesh.nodes().field(ifield), order);
190 }
191
192 if (mesh.cells().size()) {
193 update_connectivity(mesh.cells().node_connectivity(), order_inverse);
194 }
195 if (mesh.edges().size()) {
196 update_connectivity(mesh.edges().node_connectivity(), order_inverse);
197 }
198 }
199
200 // ------------------------------------------------------------------
201
202 void reorder_elements_using_nodes(Mesh& mesh, Mesh::HybridElements& elements) {
203 for (idx_t t = 0; t < elements.nb_types(); ++t) {
204 auto& elems = elements.elements(t);
205 auto& connectivity = elems.node_connectivity();
206 idx_t nb_nodes = elems.nb_nodes();
207 idx_t nb_elems = elems.size();
208 std::vector<std::pair<idx_t, idx_t>> node_lowest_index;
209 node_lowest_index.reserve(elems.size());
210 for (idx_t e = 0; e < nb_elems; ++e) {
211 idx_t lowest = std::numeric_limits<idx_t>::max();
212 for (idx_t n = 0; n < nb_nodes; ++n) {
213 lowest = std::min(lowest, connectivity(e, n));
214 }
215 node_lowest_index.emplace_back(lowest, e);
216 }
217 std::sort(node_lowest_index.begin(), node_lowest_index.end());
218 std::vector<idx_t> order;
219 order.reserve(nb_elems);
220 for (const auto& pair : node_lowest_index) {
221 order.emplace_back(pair.second);
222 }
223 for (idx_t ifield = 0; ifield < mesh.edges().nb_fields(); ++ifield) {
224 reorder_field(elements.field(ifield), order, elems.begin(), elems.end());
225 }
226
227 reorder_connectivity(elems.node_connectivity(), order);
228 }
229 }
230
231 // ------------------------------------------------------------------
232
233 void ReorderImpl::reorderCellsUsingNodes(Mesh& mesh) {
234 reorder_elements_using_nodes(mesh, mesh.cells());
235 }
236
237 // ------------------------------------------------------------------
238
239 void ReorderImpl::reorderEdgesUsingNodes(Mesh& mesh) {
240 reorder_elements_using_nodes(mesh, mesh.edges());
241 }
242
243 // ------------------------------------------------------------------
244
245 void ReorderImpl::operator()(Mesh& mesh) {
246 ATLAS_TRACE("ReorderImpl(mesh)");
247
248 reorderNodes(mesh, computeNodesOrder(mesh));
249 reorderCellsUsingNodes(mesh);
250 reorderEdgesUsingNodes(mesh);
251 }
252
253 // ------------------------------------------------------------------
254
255 namespace {
256 static ReorderBuilder<NoReorder> __ReorderReverseCuthillMckee("none");
257 } // namespace
258
259 // ------------------------------------------------------------------
260
261 } // namespace actions
262 } // namespace mesh
263 } // namespace atlas