Debian Codegraph @

include/deal.II/distributed/cell_data_transfer.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2019 - 2024 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_distributed_cell_data_transfer_templates_h
16 #define dealii_distributed_cell_data_transfer_templates_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/distributed/cell_data_transfer.h>
22
23 #ifdef DEAL_II_WITH_P4EST
24
25 # include <deal.II/lac/block_vector.h>
26 # include <deal.II/lac/la_parallel_block_vector.h>
27 # include <deal.II/lac/la_parallel_vector.h>
28 # include <deal.II/lac/petsc_block_vector.h>
29 # include <deal.II/lac/petsc_vector.h>
30 # include <deal.II/lac/trilinos_epetra_vector.h>
31 # include <deal.II/lac/trilinos_parallel_block_vector.h>
32 # include <deal.II/lac/trilinos_vector.h>
33 # include <deal.II/lac/vector.h>
34
35 DEAL_II_NAMESPACE_OPEN
36
37
38 namespace internal
39 {
40 namespace parallel
41 {
42 namespace distributed
43 {
44 namespace CellDataTransferImplementation
45 {
46 template <typename VectorType>
47 void
48 post_unpack_action(std::vector<VectorType *> &all_out)
49 {
50 for (auto &out : all_out)
51 out->compress(VectorOperation::insert);
52 }
53
54 template <typename value_type>
55 void
56 post_unpack_action(std::vector<std::vector<value_type> *> &)
57 {
58 // Do nothing for std::vector as VectorType.
59 }
60 } // namespace CellDataTransferImplementation
61 } // namespace distributed
62 } // namespace parallel
63 } // namespace internal
64
65
66
67 namespace parallel
68 {
69 namespace distributed
70 {
71 template <int dim, int spacedim, typename VectorType>
72 CellDataTransfer<dim, spacedim, VectorType>::CellDataTransfer(
73 const parallel::distributed::Triangulation<dim, spacedim> &triangulation,
74 const bool transfer_variable_size_data,
75 const std::function<std::vector<value_type>(
76 const typename dealii::Triangulation<dim, spacedim>::cell_iterator
77 &parent,
78 const value_type parent_value)> refinement_strategy,
79 const std::function<value_type(
80 const typename dealii::Triangulation<dim, spacedim>::cell_iterator
81 &parent,
82 const std::vector<value_type> &children_values)> coarsening_strategy)
83 : triangulation(&triangulation, typeid(*this).name())
84 , transfer_variable_size_data(transfer_variable_size_data)
85 , refinement_strategy(refinement_strategy)
86 , coarsening_strategy(coarsening_strategy)
87 , handle(numbers::invalid_unsigned_int)
88 {}
89
90
91
92 // Interface for packing
93 // ---------------------
94
95 template <int dim, int spacedim, typename VectorType>
96 void
97 CellDataTransfer<dim, spacedim, VectorType>::register_data_attach()
98 {
99 // TODO: casting away constness is bad
100 parallel::distributed::Triangulation<dim, spacedim> *tria =
101 const_cast<parallel::distributed::Triangulation<dim, spacedim> *>(
102 &(*triangulation));
103 Assert(tria != nullptr, ExcInternalError());
104
105 Assert(handle == numbers::invalid_unsigned_int,
106 ExcMessage("You can only add one data container per "
107 "CellDataTransfer object."));
108
109 handle = tria->register_data_attach(
110 [this](const typename parallel::distributed::
111 Triangulation<dim, spacedim>::cell_iterator &cell,
112 const CellStatus status) {
113 return this->pack_callback(cell, status);
114 },
115 /*returns_variable_size_data=*/transfer_variable_size_data);
116 }
117
118
119
120 template <int dim, int spacedim, typename VectorType>
121 void
122 CellDataTransfer<dim, spacedim, VectorType>::
123 prepare_for_coarsening_and_refinement(
124 const std::vector<const VectorType *> &all_in)
125 {
126 input_vectors = all_in;
127 register_data_attach();
128 }
129
130
131
132 template <int dim, int spacedim, typename VectorType>
133 void
134 CellDataTransfer<dim, spacedim, VectorType>::
135 prepare_for_coarsening_and_refinement(const VectorType &in)
136 {
137 std::vector<const VectorType *> all_in(1, &in);
138 prepare_for_coarsening_and_refinement(all_in);
139 }
140
141
142
143 template <int dim, int spacedim, typename VectorType>
144 void
145 CellDataTransfer<dim, spacedim, VectorType>::prepare_for_serialization(
146 const std::vector<const VectorType *> &all_in)
147 {
148 prepare_for_coarsening_and_refinement(all_in);
149 }
150
151
152
153 template <int dim, int spacedim, typename VectorType>
154 void
155 CellDataTransfer<dim, spacedim, VectorType>::prepare_for_serialization(
156 const VectorType &in)
157 {
158 std::vector<const VectorType *> all_in(1, &in);
159 prepare_for_serialization(all_in);
160 }
161
162
163
164 // Interface for unpacking
165 // -----------------------
166
167 template <int dim, int spacedim, typename VectorType>
168 void
169 CellDataTransfer<dim, spacedim, VectorType>::unpack(
170 std::vector<VectorType *> &all_out)
171 {
172 Assert(input_vectors.size() == all_out.size(),
173 ExcDimensionMismatch(input_vectors.size(), all_out.size()));
174
175 // TODO: casting away constness is bad
176 parallel::distributed::Triangulation<dim, spacedim> *tria =
177 const_cast<parallel::distributed::Triangulation<dim, spacedim> *>(
178 &(*triangulation));
179 Assert(tria != nullptr, ExcInternalError());
180
181 tria->notify_ready_to_unpack(
182 handle,
183 [this, &all_out](
184 const typename parallel::distributed::Triangulation<dim, spacedim>::
185 cell_iterator &cell,
186 const CellStatus status,
187 const boost::iterator_range<std::vector<char>::const_iterator>
188 &data_range) {
189 this->unpack_callback(cell, status, data_range, all_out);
190 });
191
192 dealii::internal::parallel::distributed::CellDataTransferImplementation::
193 post_unpack_action(all_out);
194
195 input_vectors.clear();
196 handle = numbers::invalid_unsigned_int;
197 }
198
199
200
201 template <int dim, int spacedim, typename VectorType>
202 void
203 CellDataTransfer<dim, spacedim, VectorType>::unpack(VectorType &out)
204 {
205 std::vector<VectorType *> all_out(1, &out);
206 unpack(all_out);
207 }
208
209
210
211 template <int dim, int spacedim, typename VectorType>
212 void
213 CellDataTransfer<dim, spacedim, VectorType>::deserialize(
214 std::vector<VectorType *> &all_out)
215 {
216 // For deserialization, we need to register this object
217 // to the triangulation first to get a valid handle for
218 // data access.
219 register_data_attach();
220
221 // This makes unpack() happy.
222 input_vectors.resize(all_out.size());
223
224 unpack(all_out);
225 }
226
227
228
229 template <int dim, int spacedim, typename VectorType>
230 void
231 CellDataTransfer<dim, spacedim, VectorType>::deserialize(VectorType &out)
232 {
233 std::vector<VectorType *> all_out(1, &out);
234 deserialize(all_out);
235 }
236
237
238
239 // Callback functions
240 // ------------------
241
242 template <int dim, int spacedim, typename VectorType>
243 std::vector<char>
244 CellDataTransfer<dim, spacedim, VectorType>::pack_callback(
245 const typename parallel::distributed::Triangulation<dim, spacedim>::
246 cell_iterator &cell,
247 const CellStatus status)
248 {
249 std::vector<value_type> cell_data(input_vectors.size());
250
251 // Extract data from input_vectors for this particular cell.
252 auto it_input = input_vectors.cbegin();
253 auto it_output = cell_data.begin();
254 for (; it_input != input_vectors.cend(); ++it_input, ++it_output)
255 {
256 switch (status)
257 {
258 case CellStatus::cell_will_persist:
259 case CellStatus::cell_will_be_refined:
260 // Cell either persists, or will be refined, and its children do
261 // not exist yet in the latter case.
262 *it_output = (**it_input)[cell->active_cell_index()];
263 break;
264
265 case CellStatus::children_will_be_coarsened:
266 {
267 // Cell is parent whose children will get coarsened to.
268 // Decide data to store on parent by provided strategy.
269 std::vector<value_type> children_values(cell->n_children());
270 for (unsigned int child_index = 0;
271 child_index < cell->n_children();
272 ++child_index)
273 {
274 const auto &child = cell->child(child_index);
275 Assert(child->is_active() && child->coarsen_flag_set(),
276 typename dealii::Triangulation<
277 dim>::ExcInconsistentCoarseningFlags());
278
279 children_values[child_index] =
280 (**it_input)[child->active_cell_index()];
281 }
282
283 *it_output = coarsening_strategy(cell, children_values);
284 break;
285 }
286
287 default:
288 DEAL_II_ASSERT_UNREACHABLE();
289 break;
290 }
291 }
292
293 // We don't have to pack the whole container if there is just one entry.
294 if (input_vectors.size() == 1)
295 return Utilities::pack(
296 cell_data[0], /*allow_compression=*/transfer_variable_size_data);
297 else
298 return Utilities::pack(
299 cell_data, /*allow_compression=*/transfer_variable_size_data);
300 }
301
302
303
304 template <int dim, int spacedim, typename VectorType>
305 void
306 CellDataTransfer<dim, spacedim, VectorType>::unpack_callback(
307 const typename parallel::distributed::Triangulation<dim, spacedim>::
308 cell_iterator &cell,
309 const CellStatus status,
310 const boost::iterator_range<std::vector<char>::const_iterator>
311 &data_range,
312 std::vector<VectorType *> &all_out)
313 {
314 std::vector<value_type> cell_data;
315
316 // We have to unpack the corresponding datatype that has been packed
317 // beforehand.
318 if (all_out.size() == 1)
319 cell_data.push_back(Utilities::unpack<value_type>(
320 data_range.begin(),
321 data_range.end(),
322 /*allow_compression=*/transfer_variable_size_data));
323 else
324 cell_data = Utilities::unpack<std::vector<value_type>>(
325 data_range.begin(),
326 data_range.end(),
327 /*allow_compression=*/transfer_variable_size_data);
328
329 // Check if sizes match.
330 Assert(cell_data.size() == all_out.size(), ExcInternalError());
331
332 auto it_input = cell_data.cbegin();
333 auto it_output = all_out.begin();
334 for (; it_input != cell_data.cend(); ++it_input, ++it_output)
335 switch (status)
336 {
337 case CellStatus::cell_will_persist:
338 case CellStatus::children_will_be_coarsened:
339 // Cell either persists, or has been coarsened.
340 // Thus, cell has no (longer) children.
341 (**it_output)[cell->active_cell_index()] = *it_input;
342 break;
343
344 case CellStatus::cell_will_be_refined:
345 {
346 // Cell has been refined, and is now parent of its children.
347 // Thus, distribute parent's data on its children.
348 const std::vector<value_type> children_values =
349 refinement_strategy(cell, *it_input);
350 for (unsigned int child_index = 0;
351 child_index < cell->n_children();
352 ++child_index)
353 (**it_output)[cell->child(child_index)->active_cell_index()] =
354 children_values[child_index];
355 break;
356 }
357
358 default:
359 DEAL_II_ASSERT_UNREACHABLE();
360 break;
361 }
362 }
363 } // namespace distributed
364 } // namespace parallel
365
366
367 DEAL_II_NAMESPACE_CLOSE
368
369 #else
370
371 // Make sure the scripts that create the C++20 module input files have
372 // something to latch on if the preprocessor #ifdef above would
373 // otherwise lead to an empty content of the file.
374 DEAL_II_NAMESPACE_OPEN
375 DEAL_II_NAMESPACE_CLOSE
376
377 #endif /* DEAL_II_WITH_P4EST */
378
379 #endif /* dealii_distributed_cell_data_transfer_templates_h */