Debian Codegraph @

include/deal.II/base/mpi.templates.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2016 - 2025 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_mpi_templates_h
16 #define dealii_mpi_templates_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/exceptions.h>
21 #include <deal.II/base/mpi.h>
22 #include <deal.II/base/symmetric_tensor.h>
23 #include <deal.II/base/tensor.h>
24
25 #include <deal.II/lac/sparse_matrix.h>
26 #include <deal.II/lac/vector.h>
27
28 #include <set>
29 #include <vector>
30
31 DEAL_II_NAMESPACE_OPEN
32
33 namespace Utilities
34 {
35 namespace MPI
36 {
37 namespace internal
38 {
39 template <typename T>
40 void
41 all_reduce(const MPI_Op &mpi_op,
42 const ArrayView<const T> &values,
43 const MPI_Comm mpi_communicator,
44 const ArrayView<T> &output)
45 {
46 AssertDimension(values.size(), output.size());
47 #ifdef DEAL_II_WITH_MPI
48 if (job_supports_mpi())
49 {
50 if constexpr (running_in_debug_mode())
51 {
52 {
53 const unsigned int rank = this_mpi_process(mpi_communicator);
54 unsigned int size = values.size();
55 unsigned int size_min = 0;
56 unsigned int size_max = 0;
57 int ierr2 = 0;
58 ierr2 = MPI_Reduce(&size,
59 &size_min,
60 1,
61 MPI_UNSIGNED,
62 MPI_MIN,
63 0,
64 mpi_communicator);
65 AssertThrowMPI(ierr2);
66 ierr2 = MPI_Reduce(&size,
67 &size_max,
68 1,
69 MPI_UNSIGNED,
70 MPI_MAX,
71 0,
72 mpi_communicator);
73 AssertThrowMPI(ierr2);
74 if (rank == 0)
75 Assert(
76 size_min == size_max,
77 ExcMessage(
78 "values has different size across MPI processes."));
79 }
80 }
81 const int ierr =
82 MPI_Allreduce(values != output ? values.data() : MPI_IN_PLACE,
83 static_cast<void *>(output.data()),
84 static_cast<int>(values.size()),
85 mpi_type_id_for_type<decltype(*values.data())>,
86 mpi_op,
87 mpi_communicator);
88 AssertThrowMPI(ierr);
89 }
90 else
91 #endif
92 {
93 (void)mpi_op;
94 (void)mpi_communicator;
95 if (values != output)
96 std::copy(values.begin(), values.end(), output.begin());
97 }
98 }
99
100
101
102 template <typename T>
103 void
104 all_reduce(const MPI_Op &mpi_op,
105 const ArrayView<const std::complex<T>> &values,
106 const MPI_Comm mpi_communicator,
107 const ArrayView<std::complex<T>> &output)
108 {
109 AssertDimension(values.size(), output.size());
110 #ifdef DEAL_II_WITH_MPI
111 if (job_supports_mpi())
112 {
113 const int ierr =
114 MPI_Allreduce(values != output ?
115 // TODO This const_cast is only needed for older
116 // (e.g., openMPI 1.6, released in 2012)
117 // implementations of MPI-2. It is not needed as
118 // of MPI-3 and we should remove it at some
119 // point in the future.
120 const_cast<void *>(
121 static_cast<const void *>(values.data())) :
122 MPI_IN_PLACE,
123 static_cast<void *>(output.data()),
124 static_cast<int>(values.size() * 2),
125 mpi_type_id_for_type<T>,
126 mpi_op,
127 mpi_communicator);
128 AssertThrowMPI(ierr);
129 }
130 else
131 #endif
132 {
133 (void)mpi_op;
134 (void)mpi_communicator;
135 if (values != output)
136 std::copy(values.begin(), values.end(), output.begin());
137 }
138 }
139 } // namespace internal
140
141
142
143 template <typename T>
144 T
145 sum(const T &t, const MPI_Comm mpi_communicator)
146 {
147 if (mpi_communicator == MPI_COMM_SELF)
148 return t;
149 else
150 {
151 T return_value{};
152 internal::all_reduce(MPI_SUM,
153 ArrayView<const T>(&t, 1),
154 mpi_communicator,
155 ArrayView<T>(&return_value, 1));
156 return return_value;
157 }
158 }
159
160
161
162 template <typename T, typename U>
163 void
164 sum(const T &values, const MPI_Comm mpi_communicator, U &sums)
165 {
166 static_assert(std::is_same_v<std::decay_t<T>, std::decay_t<U>>,
167 "Input and output arguments must have the same type!");
168 const auto array_view_values = make_array_view(values);
169 using const_type =
170 ArrayView<const typename decltype(array_view_values)::value_type>;
171 sum(static_cast<const_type>(array_view_values),
172 mpi_communicator,
173 make_array_view(sums));
174 }
175
176
177
178 template <typename T>
179 void
180 sum(const ArrayView<const T> &values,
181 const MPI_Comm mpi_communicator,
182 const ArrayView<T> &sums)
183 {
184 if (mpi_communicator == MPI_COMM_SELF)
185 for (unsigned int i = 0; i < values.size(); ++i)
186 sums[i] = values[i];
187 else
188 internal::all_reduce(MPI_SUM, values, mpi_communicator, sums);
189 }
190
191
192
193 template <int rank, int dim, typename Number>
194 Tensor<rank, dim, Number>
195 sum(const Tensor<rank, dim, Number> &t, const MPI_Comm mpi_communicator)
196 {
197 // Copy the tensor into a C-style array with which we can then
198 // call the other sum() function.
199 Number array[Tensor<rank, dim, Number>::n_independent_components];
200 for (unsigned int i = 0;
201 i < Tensor<rank, dim, Number>::n_independent_components;
202 ++i)
203 array[i] =
204 t[Tensor<rank, dim, Number>::unrolled_to_component_indices(i)];
205
206 sum(array, mpi_communicator, array);
207
208 return Tensor<rank, dim, Number>(make_array_view(array));
209 }
210
211
212
213 template <int rank, int dim, typename Number>
214 SymmetricTensor<rank, dim, Number>
215 sum(const SymmetricTensor<rank, dim, Number> &local,
216 const MPI_Comm mpi_communicator)
217 {
218 // Copy the tensor into a C-style array with which we can then
219 // call the other sum() function.
220 const unsigned int n_entries =
221 SymmetricTensor<rank, dim, Number>::n_independent_components;
222 Number
223 entries[SymmetricTensor<rank, dim, Number>::n_independent_components];
224
225 for (unsigned int i = 0; i < n_entries; ++i)
226 entries[i] = local[local.unrolled_to_component_indices(i)];
227
228 Number global_entries
229 [SymmetricTensor<rank, dim, Number>::n_independent_components];
230 sum(entries, mpi_communicator, global_entries);
231
232 SymmetricTensor<rank, dim, Number> global;
233 for (unsigned int i = 0; i < n_entries; ++i)
234 global[global.unrolled_to_component_indices(i)] = global_entries[i];
235
236 return global;
237 }
238
239
240
241 template <typename Number>
242 void
243 sum(const SparseMatrix<Number> &local,
244 const MPI_Comm mpi_communicator,
245 SparseMatrix<Number> &global)
246 {
247 Assert(
248 local.get_sparsity_pattern() == global.get_sparsity_pattern(),
249 ExcMessage(
250 "The sparsity pattern of the local and the global matrices should match."));
251 #ifdef DEAL_II_WITH_MPI
252 // makes use of the fact that the matrix stores its data in a
253 // contiguous array.
254 sum(ArrayView<const Number>(local.val.get(), local.n_nonzero_elements()),
255 mpi_communicator,
256 ArrayView<Number>(global.val.get(), global.n_nonzero_elements()));
257 #else
258 (void)mpi_communicator;
259 if (!PointerComparison::equal(&local, &global))
260 global = local;
261 #endif
262 }
263
264
265
266 template <typename T>
267 T
268 max(const T &t, const MPI_Comm mpi_communicator)
269 {
270 T return_value{};
271 internal::all_reduce(MPI_MAX,
272 ArrayView<const T>(&t, 1),
273 mpi_communicator,
274 ArrayView<T>(&return_value, 1));
275 return return_value;
276 }
277
278
279
280 template <typename T, typename U>
281 void
282 max(const T &values, const MPI_Comm mpi_communicator, U &maxima)
283 {
284 static_assert(std::is_same_v<std::decay_t<T>, std::decay_t<U>>,
285 "Input and output arguments must have the same type!");
286 const auto array_view_values = make_array_view(values);
287 using const_type =
288 ArrayView<const typename decltype(array_view_values)::value_type>;
289 max(static_cast<const_type>(array_view_values),
290 mpi_communicator,
291 make_array_view(maxima));
292 }
293
294
295
296 template <typename T>
297 void
298 max(const ArrayView<const T> &values,
299 const MPI_Comm mpi_communicator,
300 const ArrayView<T> &maxima)
301 {
302 internal::all_reduce(MPI_MAX, values, mpi_communicator, maxima);
303 }
304
305
306
307 template <typename T>
308 T
309 min(const T &t, const MPI_Comm mpi_communicator)
310 {
311 T return_value{};
312 internal::all_reduce(MPI_MIN,
313 ArrayView<const T>(&t, 1),
314 mpi_communicator,
315 ArrayView<T>(&return_value, 1));
316 return return_value;
317 }
318
319
320
321 template <typename T, typename U>
322 void
323 min(const T &values, const MPI_Comm mpi_communicator, U &minima)
324 {
325 static_assert(std::is_same_v<std::decay_t<T>, std::decay_t<U>>,
326 "Input and output arguments must have the same type!");
327 const auto array_view_values = make_array_view(values);
328 using const_type =
329 ArrayView<const typename decltype(array_view_values)::value_type>;
330 min(static_cast<const_type>(array_view_values),
331 mpi_communicator,
332 make_array_view(minima));
333 }
334
335
336
337 template <typename T>
338 void
339 min(const ArrayView<const T> &values,
340 const MPI_Comm mpi_communicator,
341 const ArrayView<T> &minima)
342 {
343 internal::all_reduce(MPI_MIN, values, mpi_communicator, minima);
344 }
345
346
347
348 template <typename T>
349 T
350 logical_or(const T &t, const MPI_Comm mpi_communicator)
351 {
352 static_assert(std::is_integral_v<T>,
353 "The MPI_LOR operation only allows integral data types.");
354
355 T return_value{};
356 internal::all_reduce(MPI_LOR,
357 ArrayView<const T>(&t, 1),
358 mpi_communicator,
359 ArrayView<T>(&return_value, 1));
360 return return_value;
361 }
362
363
364
365 template <typename T, typename U>
366 void
367 logical_or(const T &values, const MPI_Comm mpi_communicator, U &results)
368 {
369 static_assert(std::is_same_v<std::decay_t<T>, std::decay_t<U>>,
370 "Input and output arguments must have the same type!");
371
372 static_assert(std::is_integral_v<typename T::value_type>,
373 "The MPI_LOR operation only allows integral data types.");
374
375 // Specializations of std containers for the data type bool do not
376 // necessarily store its elements as a contiguous array. Thus we will use
377 // the make_array_view() function with iterators here, which verifies
378 // this.
379 const auto array_view_values =
380 make_array_view(values.cbegin(), values.cend());
381 const auto array_view_results =
382 make_array_view(results.begin(), results.end());
383
384 using const_type =
385 ArrayView<const typename decltype(array_view_values)::value_type>;
386 logical_or(static_cast<const_type>(array_view_values),
387 mpi_communicator,
388 array_view_results);
389 }
390
391
392
393 template <typename T>
394 void
395 logical_or(const ArrayView<const T> &values,
396 const MPI_Comm mpi_communicator,
397 const ArrayView<T> &results)
398 {
399 static_assert(std::is_integral_v<T>,
400 "The MPI_LOR operation only allows integral data types.");
401
402 internal::all_reduce(MPI_LOR, values, mpi_communicator, results);
403 }
404
405
406
407 template <typename T>
408 T
409 reduce(const T &vec,
410 const MPI_Comm comm,
411 const std::function<T(const T &, const T &)> &combiner,
412 const unsigned int root_process)
413 {
414 #ifdef DEAL_II_WITH_MPI
415 if (n_mpi_processes(comm) > 1)
416 {
417 // 1) perform custom reduction
418 T result = vec;
419
420 const unsigned int rank = this_mpi_process(comm);
421 const unsigned int nproc = n_mpi_processes(comm);
422
423 for (unsigned int stride = 1; stride < nproc; stride *= 2)
424 {
425 unsigned int rank_recv =
426 (2 * stride) *
427 ((rank + nproc - root_process) % nproc / (2 * stride)) +
428 root_process;
429 unsigned int rank_send = rank_recv + stride;
430
431 if (rank_send >= nproc + root_process) // nothing to do
432 continue;
433
434 rank_recv = rank_recv % nproc;
435 rank_send = rank_send % nproc;
436
437 if (rank_recv == rank) // process receives data
438 {
439 MPI_Status status;
440 const auto ierr_1 = MPI_Probe(rank_send,
441 internal::Tags::compute_union,
442 comm,
443 &status);
444 AssertThrowMPI(ierr_1);
445
446 int amount;
447 const auto ierr_2 = MPI_Get_count(&status, MPI_CHAR, &amount);
448 AssertThrowMPI(ierr_2);
449
450 std::vector<char> temp(amount);
451
452 const auto ierr_3 = MPI_Recv(temp.data(),
453 amount,
454 MPI_CHAR,
455 status.MPI_SOURCE,
456 status.MPI_TAG,
457 comm,
458 MPI_STATUS_IGNORE);
459 AssertThrowMPI(ierr_3);
460
461 result = combiner(result, Utilities::unpack<T>(temp, false));
462 }
463 else if (rank_send == rank) // process sends data
464 {
465 const std::vector<char> temp = Utilities::pack(result, false);
466
467 const auto ierr = MPI_Send(temp.data(),
468 temp.size(),
469 MPI_CHAR,
470 rank_recv,
471 internal::Tags::compute_union,
472 comm);
473 AssertThrowMPI(ierr);
474 }
475 }
476
477 if (rank == root_process)
478 return result;
479 else
480 return {};
481 }
482 #endif
483 (void)comm;
484 (void)combiner;
485 (void)root_process;
486 return vec;
487 }
488
489
490
491 template <typename T>
492 T
493 all_reduce(const T &vec,
494 const MPI_Comm comm,
495 const std::function<T(const T &, const T &)> &combiner)
496 {
497 if (n_mpi_processes(comm) > 1)
498 {
499 // 1) perform reduction
500 const auto result = Utilities::MPI::reduce<T>(vec, comm, combiner);
501
502 // 2) broadcast result
503 return Utilities::MPI::broadcast(comm, result);
504 }
505 else
506 return vec;
507 }
508
509
510 template <typename T>
511 std::vector<T>
512 compute_set_union(const std::vector<T> &vec, const MPI_Comm comm)
513 {
514 return Utilities::MPI::all_reduce<std::vector<T>>(
515 vec, comm, [](const auto &set_1, const auto &set_2) {
516 // merge vectors, sort, and eliminate duplicates -> mimic std::set<T>
517 auto result = set_1;
518 result.insert(result.end(), set_2.begin(), set_2.end());
519 std::sort(result.begin(), result.end());
520 result.erase(std::unique(result.begin(), result.end()), result.end());
521 return result;
522 });
523 }
524
525
526
527 template <typename T>
528 std::set<T>
529 compute_set_union(const std::set<T> &set_in, const MPI_Comm comm)
530 {
531 // convert vector to set
532 std::vector<T> vector_in(set_in.begin(), set_in.end());
533
534 // perform operation to vector
535 const std::vector<T> vector_out = compute_set_union(vector_in, comm);
536
537 // convert vector to set
538 return std::set<T>(vector_out.begin(), vector_out.end());
539 }
540
541 } // end of namespace MPI
542 } // end of namespace Utilities
543
544
545 DEAL_II_NAMESPACE_CLOSE
546
547 #endif