dune-assembler 2.12
Loading...
Searching...
No Matches
petscmatrixbackend.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3
4#ifndef DUNE_ASSEMBLER_BACKENDS_PETSCMATRIXBACKEND_HH
5#define DUNE_ASSEMBLER_BACKENDS_PETSCMATRIXBACKEND_HH
6
7#include <algorithm>
8#include <cassert>
9#include <numeric>
10#include <vector>
11
16
17#include <petscmat.h>
18
19namespace Dune::Assembler {
20
21
23{
24 using size_type = PetscInt;
25
26public:
28 : rows_(rows,0)
29 , cols_(cols,0)
30 {}
31
32 template <class S, std::size_t r, std::size_t c>
34 : PETScLocalPattern(extents.extent(0), extents.extent(1))
35 {}
36
37 void resize (size_type rows, size_type cols)
38 {
39 rows_.resize(rows);
40 cols_.resize(cols);
41 }
42
44 {
45 rows_[row] = true;
46 cols_[col] = true;
47 }
48
49 void addAll ()
50 {
51 containsAll_ = true;
52 }
53
54 [[nodiscard]] bool row (size_type i) const
55 {
56 return rows_[i];
57 }
58
59 [[nodiscard]] bool col (size_type j) const
60 {
61 return cols_[j];
62 }
63
64 [[nodiscard]] bool containsAll () const
65 {
66 return containsAll_;
67 }
68
69 void clear ()
70 {
71 for (std::size_t i = 0; i < rows_.size(); ++i)
72 rows_[i] = 0;
73 for (std::size_t i = 0; i < cols_.size(); ++i)
74 cols_[i] = 0;
75 containsAll_ = false;
76 }
77
78private:
81 bool containsAll_ = false;
82};
83
84
86{
87public:
88 using Matrix = ::Mat;
93
94public:
96 : matrix_(&matrix)
97 {}
98
99 void setZero ()
100 {
101 MatZeroEntries(*matrix_);
102 }
103
105 {
106 assert(matrix_);
107 MatAssemblyBegin(*matrix_, type);
108 MatAssemblyEnd(*matrix_, type);
109 }
110
111 template <class LocalView>
112 void scatter (const LocalView& localView, const LocalMatrix& localMatrix, const LocalPattern& localPattern)
113 {
114 assert(matrix_);
115
116 rowIndices_.resize(localView.size());
117 for (std::size_t i = 0; i < localView.size(); ++i)
118 rowIndices_[i] = localView.index(i);
119
120 // Use negative indices to indicate ignored rows/columns
121 if (!localPattern.containsAll())
122 {
123 for (std::size_t i = 0; i < localView.size(); ++i)
124 if (!localPattern.row(i))
125 rowIndices_[i] = -1;
126 }
127
128 MatSetValues(*matrix_, rowIndices_.size(), rowIndices_.data(), rowIndices_.size(), rowIndices_.data(),
129 localMatrix.container_data(), ADD_VALUES);
130 }
131
132
133 template <class RowLocalView, class ColLocalView>
134 void scatter (const RowLocalView& rowLocalView, const ColLocalView& colLocalView,
136 {
137 assert(matrix_);
138 // specialization for the case that both localviews are identical
139 if ((void*)(&rowLocalView) == (void*)(&colLocalView))
141
142 rowIndices_.resize(rowLocalView.size());
143 colIndices_.resize(colLocalView.size());
144
145 for (std::size_t i = 0; i < rowLocalView.size(); ++i)
146 rowIndices_[i] = rowLocalView.index(i);
147 for (std::size_t j = 0; j < colLocalView.size(); ++j)
148 colIndices_[j] = colLocalView.index(j);
149
150 // Use negative indices to indicate ignored rows/columns
151 if (!localPattern.containsAll())
152 {
153 for (std::size_t i = 0; i < rowLocalView.size(); ++i)
154 if (!localPattern.row(i))
155 rowIndices_[i] = -1;
156 for (std::size_t j = 0; j < colLocalView.size(); ++j)
157 if (!localPattern.col(j))
158 colIndices_[j] = -1;
159 }
160
161 MatSetValues(*matrix_, rowIndices_.size(), rowIndices_.data(), colIndices_.size(), colIndices_.data(),
162 localMatrix.container_data(), ADD_VALUES);
163 }
164
165
167 {
168 public:
169 using Matrix = ::Mat;
171
172 public:
173 explicit PatternBuilder (Matrix& matrix)
174 : matrix_(&matrix)
175 {}
176
177 void setupMatrix (bool setColumnIndices = false)
178 {
179 assert(matrix_);
180
181 if (*matrix_ == nullptr)
182 MatCreate(PETSC_COMM_SELF, matrix_);
183
184 MatSetSizes(*matrix_, rowSize_, colSize_, rowSize_, colSize_);
185 MatSetType(*matrix_, MATSEQAIJ);
186
187 // number of nonzeros per row
189 std::transform(nz_.begin(), nz_.end(), nnzPerRow.begin(), [](auto const& s) {
190 return s.size();
191 });
192
193 // create a MATSEQAIJ matrix with provided number of nonzeros per row
194 MatSeqAIJSetPreallocation(*matrix_, {}, nnzPerRow.data());
195
197 {
198 // total number of nonzeros
200
201 // insert the column indices from the sparsity pattern
202 std::vector<size_type> columnIndices(nnz);
203 auto it = columnIndices.begin();
204 for (const auto& row : nz_)
205 it = std::copy(row.begin(), row.end(), it);
206 MatSeqAIJSetColumnIndices(*matrix_, columnIndices.data());
207
208 // set all entries to zero
209 MatZeroEntries(*matrix_);
210 }
211 }
212
213 template <class RowBasis, class ColBasis>
214 void resize (const RowBasis& rowBasis, const ColBasis& colBasis)
215 {
216 rowSize_ = rowBasis.dimension();
217 colSize_ = colBasis.dimension();
218 nz_.resize(rowSize_);
219 }
220
221 // use a given pattern
222 template <class RowLocalView, class ColLocalView>
223 void scatter (const RowLocalView& rowLocalView, const ColLocalView& colLocalView,
225 {
226 if (localPattern.containsAll())
227 {
228 for (std::size_t i = 0; i < rowLocalView.size(); ++i)
229 for (std::size_t j = 0; j < colLocalView.size(); ++j)
230 nz_[rowLocalView.index(i)].insert(colLocalView.index(j));
231 }
232 else
233 {
234 for (std::size_t i = 0; i < rowLocalView.size(); ++i)
235 if (localPattern.row(i))
236 for (std::size_t j = 0; j < colLocalView.size(); ++j)
237 if (localPattern.col(j))
238 nz_[rowLocalView.index(i)].insert(colLocalView.index(j));
239 }
240 }
241
242 // use the full pattern
243 template <class RowLocalView, class ColLocalView>
244 void scatter (const RowLocalView& rowLocalView, const ColLocalView& colLocalView)
245 {
246 for (std::size_t i = 0; i < rowLocalView.size(); ++i)
247 for (std::size_t j = 0; j < colLocalView.size(); ++j)
248 nz_[rowLocalView.index(i)].insert(colLocalView.index(j));
249 }
250
251 private:
252 Matrix* matrix_ = nullptr;
253 size_type rowSize_ = 0;
254 size_type colSize_ = 0;
255
257 };
258
260 {
261 return PatternBuilder{*matrix_};
262 }
263
265 {
266 MatInfo info;
267 MatGetInfo(*matrix_, MAT_LOCAL, &info);
268 return std::size_t(info.nz_used);
269 }
270
271private:
272 Matrix* matrix_ = nullptr;
273
274 std::vector<size_type> rowIndices_{};
275 std::vector<size_type> colIndices_{};
276};
277
278} // end namespace Dune::Assembler
279
280#endif // DUNE_ASSEMBLER_BACKENDS_PETSCMATRIXBACKEND_HH
Definition localboundaryintersectionmatrixassembler.hh:9
Col col
Definition petscmatrixbackend.hh:23
void addAll()
Definition petscmatrixbackend.hh:49
PETScLocalPattern(size_type rows=0, size_type cols=0)
Definition petscmatrixbackend.hh:27
bool row(size_type i) const
Definition petscmatrixbackend.hh:54
bool containsAll() const
Definition petscmatrixbackend.hh:64
bool col(size_type j) const
Definition petscmatrixbackend.hh:59
void add(size_type row, size_type col)
Definition petscmatrixbackend.hh:43
void clear()
Definition petscmatrixbackend.hh:69
PETScLocalPattern(Std::extents< S, r, c > const &extents)
Definition petscmatrixbackend.hh:33
void resize(size_type rows, size_type cols)
Definition petscmatrixbackend.hh:37
Definition petscmatrixbackend.hh:86
PetscInt size_type
Definition petscmatrixbackend.hh:90
PatternBuilder patternBuilder()
Definition petscmatrixbackend.hh:259
PetscScalar value_type
Definition petscmatrixbackend.hh:89
PETScMatrixBackend(Matrix &matrix) noexcept
Definition petscmatrixbackend.hh:95
std::size_t nnz() const
Definition petscmatrixbackend.hh:264
void scatter(const RowLocalView &rowLocalView, const ColLocalView &colLocalView, const LocalMatrix &localMatrix, const LocalPattern &localPattern)
Definition petscmatrixbackend.hh:134
FlatMatrix< value_type > LocalMatrix
Definition petscmatrixbackend.hh:91
::Mat Matrix
Definition petscmatrixbackend.hh:88
void setZero()
Definition petscmatrixbackend.hh:99
void scatter(const LocalView &localView, const LocalMatrix &localMatrix, const LocalPattern &localPattern)
Definition petscmatrixbackend.hh:112
void finish(MatAssemblyType type=MAT_FINAL_ASSEMBLY)
Definition petscmatrixbackend.hh:104
Definition petscmatrixbackend.hh:167
PatternBuilder(Matrix &matrix)
Definition petscmatrixbackend.hh:173
void resize(const RowBasis &rowBasis, const ColBasis &colBasis)
Definition petscmatrixbackend.hh:214
void scatter(const RowLocalView &rowLocalView, const ColLocalView &colLocalView, const LocalPattern &localPattern)
Definition petscmatrixbackend.hh:223
::Mat Matrix
Definition petscmatrixbackend.hh:169
void setupMatrix(bool setColumnIndices=false)
Definition petscmatrixbackend.hh:177
void scatter(const RowLocalView &rowLocalView, const ColLocalView &colLocalView)
Definition petscmatrixbackend.hh:244
T accumulate(T... args)
T begin(T... args)
T copy(T... args)
T data(T... args)
T resize(T... args)
T size(T... args)
T transform(T... args)