Debian Codegraph @

src/atlas/mesh/detail/MeshImpl.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 <algorithm>
12
13 #include "eckit/types/FloatCompare.h"
14
15 #include "atlas/grid/Grid.h"
16 #include "atlas/mesh/Elements.h"
17 #include "atlas/mesh/HybridElements.h"
18 #include "atlas/mesh/Mesh.h"
19 #include "atlas/mesh/Nodes.h"
20 #include "atlas/mesh/detail/MeshImpl.h"
21 #include "atlas/parallel/mpi/mpi.h"
22 #include "atlas/runtime/Exception.h"
23
24 using atlas::Grid;
25 using atlas::Projection;
26
27 namespace atlas {
28 namespace mesh {
29 namespace detail {
30
31 //----------------------------------------------------------------------------------------------------------------------
32
33 MeshImpl::MeshImpl(eckit::Stream&) {
34 ATLAS_NOTIMPLEMENTED;
35 }
36
37 void MeshImpl::encode(eckit::Stream&) const {
38 ATLAS_NOTIMPLEMENTED;
39 }
40
41 MeshImpl::MeshImpl(): nodes_(new mesh::Nodes()), dimensionality_(2) {
42 metadata_.set("mpi_comm",mpi::comm().name());
43 createElements();
44 }
45
46 MeshImpl::~MeshImpl() {
47 while (mesh_observers_.size()) {
48 MeshObserver* o = mesh_observers_.back();
49 o->onMeshDestruction(*this);
50 o->unregisterMesh(*this); // will also delete observer from mesh
51 }
52 }
53
54 void MeshImpl::print(std::ostream&) const {}
55
56 size_t MeshImpl::footprint() const {
57 size_t size = sizeof(*this);
58
59 size += metadata_.footprint();
60 if (nodes_) {
61 size += nodes_->footprint();
62 }
63 if (cells_) {
64 size += cells_->footprint();
65 }
66 if (facets_) {
67 size += facets_->footprint();
68 }
69 if (ridges_) {
70 size += ridges_->footprint();
71 }
72 if (peaks_) {
73 size += peaks_->footprint();
74 }
75 if (partition_graph_) {
76 size += partition_graph_->footprint();
77 }
78 for (const auto& polygon : polygons_) {
79 if (polygon) {
80 size += polygon->footprint();
81 }
82 }
83
84 return size;
85 }
86
87 void MeshImpl::createElements() {
88 cells_.reset(new HybridElements());
89 facets_.reset(new HybridElements());
90 ridges_.reset(new HybridElements());
91 peaks_.reset(new HybridElements());
92 if (dimensionality_ == 2) {
93 edges_ = facets_;
94 }
95 else if (dimensionality_ == 3) {
96 edges_ = ridges_;
97 }
98 else {
99 throw_Exception("Invalid Mesh dimensionality", Here());
100 }
101
102 ATLAS_ASSERT(edges_.owners() == 2);
103 }
104
105 bool MeshImpl::generated() const {
106 return !(cells_->size() == 0 && facets_->size() == 0 && ridges_->size() == 0 && peaks_->size() == 0);
107 }
108
109 void MeshImpl::setProjection(const Projection& projection) {
110 projection_ = projection;
111 }
112
113 void MeshImpl::setGrid(const Grid& grid) {
114 grid_ = grid;
115 if (not projection_) {
116 projection_ = grid_.projection();
117 }
118 }
119
120 idx_t MeshImpl::nb_parts() const {
121 idx_t n;
122 if (not metadata().get("nb_parts", n)) {
123 n = mpi::comm(mpi_comm()).size();
124 }
125 return n;
126 }
127
128 idx_t MeshImpl::part() const {
129 idx_t p;
130 if (not metadata().get("part", p)) {
131 p = mpi::comm(mpi_comm()).rank();
132 }
133 return p;
134 }
135
136 std::string MeshImpl::mpi_comm() const {
137 return metadata().getString("mpi_comm");
138 }
139
140
141 void MeshImpl::updateDevice() const {
142 if (nodes_) {
143 nodes_->updateDevice();
144 }
145 if (cells_) {
146 cells_->updateDevice();
147 }
148 if (facets_) {
149 facets_->updateDevice();
150 }
151 if (ridges_) {
152 ridges_->updateDevice();
153 }
154 if (peaks_) {
155 peaks_->updateDevice();
156 }
157 }
158
159 void MeshImpl::updateHost() const {
160 if (nodes_) {
161 nodes_->updateHost();
162 }
163 if (cells_) {
164 cells_->updateHost();
165 }
166 if (facets_) {
167 facets_->updateHost();
168 }
169 if (ridges_) {
170 ridges_->updateHost();
171 }
172 if (peaks_) {
173 peaks_->updateHost();
174 }
175 }
176
177 void MeshImpl::syncHostDevice() const {
178 if (nodes_) {
179 nodes_->syncHostDevice();
180 }
181 if (cells_) {
182 cells_->syncHostDevice();
183 }
184 if (facets_) {
185 facets_->syncHostDevice();
186 }
187 if (ridges_) {
188 ridges_->syncHostDevice();
189 }
190 if (peaks_) {
191 peaks_->syncHostDevice();
192 }
193 }
194
195 const PartitionGraph& MeshImpl::partitionGraph() const {
196 if (not partition_graph_) {
197 partition_graph_.reset(build_partition_graph(*this));
198 }
199 return *partition_graph_;
200 }
201
202 PartitionGraph::Neighbours MeshImpl::nearestNeighbourPartitions() const {
203 return partitionGraph().nearestNeighbours(part());
204 }
205
206 const PartitionPolygon& MeshImpl::polygon(idx_t halo) const {
207 if (halo >= static_cast<idx_t>(polygons_.size())) {
208 polygons_.resize(halo + 1);
209 }
210 if (not polygons_[halo]) {
211 int mesh_halo = 0;
212 metadata().get("halo", mesh_halo);
213 if (halo > mesh_halo) {
214 throw_Exception("Mesh does not contain a halo of size " + std::to_string(halo) + ".", Here());
215 }
216
217 polygons_[halo].reset(new PartitionPolygon(*this, halo));
218 }
219 return *polygons_[halo];
220 }
221
222 const util::PartitionPolygons& MeshImpl::polygons() const {
223 if (all_polygons_.size() == 0) {
224 polygon().allGather(all_polygons_);
225 }
226 return all_polygons_;
227 }
228
229 void MeshImpl::attachObserver(MeshObserver& observer) const {
230 if (std::find(mesh_observers_.begin(), mesh_observers_.end(), &observer) == mesh_observers_.end()) {
231 mesh_observers_.push_back(&observer);
232 }
233 }
234
235 void MeshImpl::detachObserver(MeshObserver& observer) const {
236 mesh_observers_.erase(std::remove(mesh_observers_.begin(), mesh_observers_.end(), &observer),
237 mesh_observers_.end());
238 }
239
240 //----------------------------------------------------------------------------------------------------------------------
241
242 } // namespace detail
243 } // namespace mesh
244 } // namespace atlas