Loading [MathJax]/extensions/TeX/newcommand.js
 Reference documentation for deal.II version 9.4.1
\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}} \newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=} \newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]} \newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mpi_remote_point_evaluation.cc
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (C) 2021 - 2022 by the deal.II authors
4//
5// This file is part of the deal.II library.
6//
7// The deal.II library is free software; you can use it, redistribute
8// it, and/or modify it under the terms of the GNU Lesser General
9// Public License as published by the Free Software Foundation; either
10// version 2.1 of the License, or (at your option) any later version.
11// The full text of the license can be found in the file LICENSE.md at
12// the top level directory of deal.II.
13//
14// ---------------------------------------------------------------------
15
16#include <deal.II/base/config.h>
17
21
23
24#include <deal.II/fe/mapping.h>
25
29#include <deal.II/grid/tria.h>
30
32
33
34namespace Utilities
35{
36 namespace MPI
37 {
38 template <int dim, int spacedim>
40 const double tolerance,
41 const bool enforce_unique_mapping,
42 const unsigned int rtree_level,
43 const std::function<std::vector<bool>()> &marked_vertices)
44 : tolerance(tolerance)
45 , enforce_unique_mapping(enforce_unique_mapping)
46 , rtree_level(rtree_level)
47 , marked_vertices(marked_vertices)
48 , ready_flag(false)
49 {}
50
51
52
53 template <int dim, int spacedim>
55 {
56 if (tria_signal.connected())
57 tria_signal.disconnect();
58 }
60
61
62 template <int dim, int spacedim>
63 void
65 const std::vector<Point<spacedim>> &points,
67 const Mapping<dim, spacedim> & mapping)
68 {
69#ifndef DEAL_II_WITH_MPI
70 Assert(false, ExcNeedsMPI());
71 (void)points;
72 (void)tria;
73 (void)mapping;
74#else
75 if (tria_signal.connected())
76 tria_signal.disconnect();
77
78 tria_signal =
79 tria.signals.any_change.connect([&]() { this->ready_flag = false; });
80
81 this->tria = &tria;
82 this->mapping = &mapping;
83
84 std::vector<BoundingBox<spacedim>> local_boxes;
85 for (const auto &cell :
87 local_boxes.push_back(mapping.get_bounding_box(cell));
88
89 // create r-tree of bounding boxes
90 const auto local_tree = pack_rtree(local_boxes);
91
92 // compress r-tree to a minimal set of bounding boxes
93 const auto local_reduced_box =
94 extract_rtree_level(local_tree, rtree_level);
95
96 // gather bounding boxes of other processes
97 const auto global_bboxes =
99
100 const GridTools::Cache<dim, spacedim> cache(tria, mapping);
101
102 const auto data =
104 cache,
105 points,
106 global_bboxes,
107 marked_vertices ? marked_vertices() : std::vector<bool>(),
108 tolerance,
109 true,
110 enforce_unique_mapping);
111
112 this->recv_ranks = data.recv_ranks;
113 this->recv_ptrs = data.recv_ptrs;
114
115 this->send_ranks = data.send_ranks;
116 this->send_ptrs = data.send_ptrs;
117
118 this->recv_permutation = {};
119 this->recv_permutation.resize(data.recv_components.size());
120 this->point_ptrs.assign(points.size() + 1, 0);
121 for (unsigned int i = 0; i < data.recv_components.size(); ++i)
122 {
123 AssertIndexRange(std::get<2>(data.recv_components[i]),
124 this->recv_permutation.size());
125 this->recv_permutation[std::get<2>(data.recv_components[i])] = i;
126
127 AssertIndexRange(std::get<1>(data.recv_components[i]) + 1,
128 this->point_ptrs.size());
129 this->point_ptrs[std::get<1>(data.recv_components[i]) + 1]++;
130 }
131
132 std::tuple<unsigned int, unsigned int> n_owning_processes_default{
134 std::tuple<unsigned int, unsigned int> n_owning_processes_local =
135 n_owning_processes_default;
136
137 for (unsigned int i = 0; i < points.size(); ++i)
138 {
139 std::get<0>(n_owning_processes_local) =
140 std::min(std::get<0>(n_owning_processes_local),
141 this->point_ptrs[i + 1]);
142 std::get<1>(n_owning_processes_local) =
143 std::max(std::get<1>(n_owning_processes_local),
144 this->point_ptrs[i + 1]);
145
146 this->point_ptrs[i + 1] += this->point_ptrs[i];
147 }
148
149 const auto n_owning_processes_global =
150 Utilities::MPI::all_reduce<std::tuple<unsigned int, unsigned int>>(
151 n_owning_processes_local,
153 [&](const auto &a,
154 const auto &b) -> std::tuple<unsigned int, unsigned int> {
155 if (a == n_owning_processes_default)
156 return b;
157
158 if (b == n_owning_processes_default)
159 return a;
161 return std::tuple<unsigned int, unsigned int>{
162 std::min(std::get<0>(a), std::get<0>(b)),
163 std::max(std::get<1>(a), std::get<1>(b))};
164 });
165
166 if (n_owning_processes_global == n_owning_processes_default)
167 {
168 unique_mapping = true;
169 all_points_found_flag = true;
170 }
171 else
173 unique_mapping = (std::get<0>(n_owning_processes_global) == 1) &&
174 (std::get<1>(n_owning_processes_global) == 1);
175 all_points_found_flag = std::get<0>(n_owning_processes_global) > 0;
176 }
177
178 Assert(enforce_unique_mapping == false || unique_mapping,
180
181 cell_data = {};
182 send_permutation = {};
183
184 std::pair<int, int> dummy{-1, -1};
185 for (const auto &i : data.send_components)
186 {
187 if (dummy != std::get<0>(i))
188 {
189 dummy = std::get<0>(i);
190 cell_data.cells.emplace_back(dummy);
191 cell_data.reference_point_ptrs.emplace_back(
192 cell_data.reference_point_values.size());
193 }
194
195 cell_data.reference_point_values.emplace_back(std::get<3>(i));
196 send_permutation.emplace_back(std::get<5>(i));
197 }
198
199 cell_data.reference_point_ptrs.emplace_back(
200 cell_data.reference_point_values.size());
201
202 this->ready_flag = true;
203#endif
204 }
205
206
207 template <int dim, int spacedim>
208 const std::vector<unsigned int> &
210 {
211 return point_ptrs;
212 }
213
214
215
216 template <int dim, int spacedim>
217 bool
219 {
220 return unique_mapping;
221 }
222
223
224
225 template <int dim, int spacedim>
226 bool
228 {
229 return all_points_found_flag;
230 }
231
232
233
234 template <int dim, int spacedim>
235 bool
237 const unsigned int i) const
238 {
239 AssertIndexRange(i, point_ptrs.size() - 1);
240
241 if (all_points_found_flag)
242 return true;
243 else
244 return (point_ptrs[i + 1] - point_ptrs[i]) > 0;
245 }
246
247
248
249 template <int dim, int spacedim>
252 {
253 return *tria;
254 }
255
256
257
258 template <int dim, int spacedim>
261 {
262 return *mapping;
263 }
264
265
266
267 template <int dim, int spacedim>
268 bool
270 {
271 return ready_flag;
272 }
273
274 } // end of namespace MPI
275} // end of namespace Utilities
276
277#include "mpi_remote_point_evaluation.inst"
278
Abstract base class for mapping classes.
Definition: mapping.h:311
virtual BoundingBox< spacedim > get_bounding_box(const typename Triangulation< dim, spacedim >::cell_iterator &cell) const
Definition: point.h:111
virtual MPI_Comm get_communicator() const
Signals signals
Definition: tria.h:2448
RemotePointEvaluation(const double tolerance=1e-6, const bool enforce_unique_mapping=false, const unsigned int rtree_level=0, const std::function< std::vector< bool >()> &marked_vertices={})
const Triangulation< dim, spacedim > & get_triangulation() const
const std::vector< unsigned int > & get_point_ptrs() const
const Mapping< dim, spacedim > & get_mapping() const
void reinit(const std::vector< Point< spacedim > > &points, const Triangulation< dim, spacedim > &tria, const Mapping< dim, spacedim > &mapping)
bool point_found(const unsigned int i) const
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
IteratorRange< active_cell_iterator > active_cell_iterators() const
static ::ExceptionBase & ExcNeedsMPI()
#define Assert(cond, exc)
Definition: exceptions.h:1473
#define AssertIndexRange(index, range)
Definition: exceptions.h:1732
static ::ExceptionBase & ExcInternalError()
DistributedComputePointLocationsInternal< dim, spacedim > distributed_compute_point_locations(const GridTools::Cache< dim, spacedim > &cache, const std::vector< Point< spacedim > > &points, const std::vector< std::vector< BoundingBox< spacedim > > > &global_bboxes, const std::vector< bool > &marked_vertices, const double tolerance, const bool perform_handshake, const bool enforce_unique_mapping=false)
Definition: grid_tools.cc:5930
std::vector< T > all_gather(const MPI_Comm &comm, const T &object_to_send)
static const unsigned int invalid_unsigned_int
Definition: types.h:201
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
std::vector< BoundingBox< boost::geometry::dimension< typename Rtree::indexable_type >::value > > extract_rtree_level(const Rtree &tree, const unsigned int level)
RTree< typename LeafTypeIterator::value_type, IndexType, IndexableGetter > pack_rtree(const LeafTypeIterator &begin, const LeafTypeIterator &end)
const ::Triangulation< dim, spacedim > & tria