KASKADE 7 development version
Loading...
Searching...
No Matches
bddcCoarseReduction.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2026 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef KASKADE_BDDC_COARSE_REDUCTION_HH
14#define KASKADE_BDDC_COARSE_REDUCTION_HH
15
16#include <algorithm>
17#include <cstddef>
18#include <limits>
19#include <stdexcept>
20#include <vector>
21
22#ifdef KASKADE_HAVE_MPI
23#include <mpi.h>
24
25namespace Kaskade::BDDC
26{
37 inline void allreduceCoarseSum(std::vector<double>& values,
38 int chunkLimit = std::numeric_limits<int>::max())
39 {
40 if (chunkLimit <= 0)
41 throw std::invalid_argument("coarse reduction chunk limit must be positive");
42 for (std::size_t offset = 0; offset < values.size();)
43 {
44 int const count = static_cast<int>(std::min(values.size()-offset,
45 static_cast<std::size_t>(chunkLimit)));
46 MPI_Allreduce(MPI_IN_PLACE,values.data()+offset,count,MPI_DOUBLE,MPI_SUM,
47 MPI_COMM_WORLD);
48 offset += count;
49 }
50 }
51
61 template <class Matrix>
62 void reduceCoarseMatrix(Matrix& matrix,
63 int chunkLimit = std::numeric_limits<int>::max())
64 {
65 std::vector<double> values;
66 for (auto row = matrix.begin(); row != matrix.end(); ++row)
67 for (auto entry = row->begin(); entry != row->end(); ++entry)
68 values.push_back((*entry)[0][0]);
69 allreduceCoarseSum(values,chunkLimit);
70 std::size_t position = 0;
71 for (auto row = matrix.begin(); row != matrix.end(); ++row)
72 for (auto entry = row->begin(); entry != row->end(); ++entry)
73 (*entry)[0][0] = values[position++];
74 }
75
82 template <class Vector>
83 void reduceCoarseRhs(Vector& rhs,
84 int chunkLimit = std::numeric_limits<int>::max())
85 {
86 std::vector<double> values(rhs.size());
87 for (std::size_t i = 0; i < values.size(); ++i)
88 values[i] = rhs[i][0];
89 allreduceCoarseSum(values,chunkLimit);
90 for (std::size_t i = 0; i < values.size(); ++i)
91 rhs[i][0] = values[i];
92 }
93}
94#endif
95#endif
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition fixdune.hh:667