OGS
PETScMatrix.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <petscmat.h>
7
8#include <string>
9#include <vector>
10
13
14typedef Mat PETSc_Mat;
15
16namespace MathLib
17{
18class PETScVector;
23{
24public:
25 using IndexType = PetscInt;
26
27public:
36 PETScMatrix(const PetscInt nrows,
37 const PETScSparsityPattern& sparsity_pattern);
38
47 PETScMatrix(const PetscInt nrows, const PetscInt ncols,
48 const PETScSparsityPattern& sparsity_pattern);
49
51 PETScMatrix(PETScMatrix const& A);
52
54
60 void finalizeAssembly(const MatAssemblyType asm_type = MAT_FINAL_ASSEMBLY)
61 {
62 MatAssemblyBegin(A_, asm_type);
63 MatAssemblyEnd(A_, asm_type);
64 }
65
67 PetscInt getNumberOfRows() const { return nrows_; }
69 PetscInt getNumberOfColumns() const { return ncols_; }
71 PetscInt getNumberOfLocalRows() const { return n_loc_rows_; }
73 PetscInt getNumberOfLocalColumns() const { return n_loc_cols_; }
75 PetscInt getRangeBegin() const { return start_rank_; }
77 PetscInt getRangeEnd() const { return end_rank_; }
79 std::vector<PetscInt> const& getSparsityColumnIndices() const
80 {
81 return sparsity_col_idx_;
82 }
83
85 Mat& getRawMatrix() { return A_; }
92 Mat const& getRawMatrix() const { return A_; }
94 void setZero() { MatZeroEntries(A_); }
104 void setRowsColumnsZero(std::vector<PetscInt> const& row_pos);
105
112 void set(const PetscInt i, const PetscInt j, const PetscScalar value)
113 {
114 PetscCallAbort(PETSC_COMM_WORLD,
115 MatSetValue(A_, i, j, value, INSERT_VALUES));
116 }
117
124 void add(const PetscInt i, const PetscInt j, const PetscScalar value)
125 {
126 PetscCallAbort(PETSC_COMM_WORLD,
127 MatSetValue(A_, i, j, value, ADD_VALUES));
128 }
129
135 void addToDiagonal(const PetscScalar value);
136
153 template <class T_DENSE_MATRIX>
154 void add(RowColumnIndices<PetscInt> const& indices,
155 const T_DENSE_MATRIX& sub_matrix)
156 {
157 // Set global column indices to positive to allow all entries of columns
158 // to be added to the global matrix. For the ghost columns, only the
159 // off diagonal entries are added due to the negative indices of the
160 // corresponding rows.
161 std::vector<PetscInt> cols;
162 cols.reserve(indices.columns.size());
163 for (auto col : indices.columns)
164 {
165 // Ghost entries, and its original index is 0.
166 if (col == -ncols_)
167 {
168 cols.push_back(0);
169 }
170 else
171 {
172 cols.push_back(std::abs(col));
173 }
174 }
175
176 add(indices.rows, cols, sub_matrix);
177 }
178
187 template <class T_DENSE_MATRIX>
188 void add(std::vector<PetscInt> const& row_pos,
189 std::vector<PetscInt> const& col_pos,
190 const T_DENSE_MATRIX& sub_mat);
191
221 void viewer(const std::string& file_name,
222 const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB);
223
224private:
225 void destroy()
226 {
227 if (A_ != nullptr)
228 {
229 PetscCallAbort(PETSC_COMM_WORLD, MatDestroy(&A_));
230 }
231 A_ = nullptr;
232 }
233
235 Mat A_ = nullptr;
236
238 PetscInt nrows_;
239
241 PetscInt ncols_;
242
244 PetscInt n_loc_rows_;
245
247 PetscInt n_loc_cols_;
248
250 PetscInt start_rank_;
251
253 PetscInt end_rank_;
254
256 std::vector<PetscInt> sparsity_col_idx_;
257
263 void create(const PETScSparsityPattern& sparsity_pattern);
264
276 const PETScSparsityPattern& sparsity_pattern);
277
278 friend bool finalizeMatrixAssembly(PETScMatrix& mat,
279 const MatAssemblyType asm_type);
280};
281
282template <class T_DENSE_MATRIX>
283void PETScMatrix::add(std::vector<PetscInt> const& row_pos,
284 std::vector<PetscInt> const& col_pos,
285 const T_DENSE_MATRIX& sub_mat)
286{
287 const PetscInt nrows = static_cast<PetscInt>(row_pos.size());
288 const PetscInt ncols = static_cast<PetscInt>(col_pos.size());
289
290 PetscCallAbort(PETSC_COMM_WORLD,
291 MatSetValues(A_, nrows, &row_pos[0], ncols, &col_pos[0],
292 sub_mat.data(), ADD_VALUES));
293};
294
302 PETScMatrix& mat, const MatAssemblyType asm_type = MAT_FINAL_ASSEMBLY);
303
309void setPreallocationNonzeroOption(PETScMatrix& matrix, bool has_col_idx);
310
314MatStructure nonzeroPatternStructure(std::vector<PetscInt> const& col_a,
315 std::vector<PetscInt> const& col_b);
316
317} // namespace MathLib
318
320
321namespace MathLib
322{
325template <typename SPARSITY_PATTERN>
326struct SetMatrixSparsity<PETScMatrix, SPARSITY_PATTERN>
327{
329 SPARSITY_PATTERN const& sparsity_pattern) const
330 {
332 !sparsity_pattern.col_idx.empty());
333 }
334};
335} // namespace MathLib
Mat PETSc_Mat
Definition PETScMatrix.h:14
Wrapper class for PETSc matrix routines for matrix.
Definition PETScMatrix.h:23
PetscInt n_loc_cols_
Number of the local columns.
Mat const & getRawMatrix() const
Definition PETScMatrix.h:92
PetscInt getNumberOfLocalRows() const
Get the number of local rows.
Definition PETScMatrix.h:71
friend bool finalizeMatrixAssembly(PETScMatrix &mat, const MatAssemblyType asm_type)
General interface for the matrix assembly.
PETScMatrix & operator=(PETScMatrix const &A)
Mat & getRawMatrix()
Get matrix reference.
Definition PETScMatrix.h:85
PetscInt getNumberOfColumns() const
Get the number of columns.
Definition PETScMatrix.h:69
void add(RowColumnIndices< PetscInt > const &indices, const T_DENSE_MATRIX &sub_matrix)
Add sub-matrix at positions given by global indices, in which negative index indicates ghost entry.
void setZero()
Set all entries to zero.
Definition PETScMatrix.h:94
PetscInt getRangeEnd() const
Get the end global index of the rows in the same rank.
Definition PETScMatrix.h:77
void set(const PetscInt i, const PetscInt j, const PetscScalar value)
Set a single entry with a value.
PetscInt end_rank_
Ending index in a rank.
PetscInt getRangeBegin() const
Get the start global index of the rows of the same rank.
Definition PETScMatrix.h:75
void finalizeAssembly(const MatAssemblyType asm_type=MAT_FINAL_ASSEMBLY)
Perform MPI collection of assembled entries in buffer.
Definition PETScMatrix.h:60
PetscInt n_loc_rows_
Number of the local rows.
Mat A_
PETSc matrix.
void preallocateFromSparsityPattern(const PETScSparsityPattern &sparsity_pattern)
Preallocate the matrix storage from an exact sparsity pattern.
PetscInt nrows_
Number of the global rows.
void setRowsColumnsZero(std::vector< PetscInt > const &row_pos)
Set the specified rows to zero except diagonal entries, i.e. , where This function must be called...
PetscInt ncols_
Number of the global columns.
void viewer(const std::string &file_name, const PetscViewerFormat vw_format=PETSC_VIEWER_ASCII_MATLAB)
void add(const PetscInt i, const PetscInt j, const PetscScalar value)
Add value to a single entry.
void create(const PETScSparsityPattern &sparsity_pattern)
Create the matrix, configure memory allocation and set the related member data.
void addToDiagonal(const PetscScalar value)
Add a constant value to all diagonal entries of the matrix.
PetscInt getNumberOfRows() const
Get the number of rows.
Definition PETScMatrix.h:67
PetscInt start_rank_
Starting index in a rank.
std::vector< PetscInt > sparsity_col_idx_
Column indices from preallocation (empty when fallback path was used).
std::vector< PetscInt > const & getSparsityColumnIndices() const
Column indices cached at preallocation (empty on the fallback path).
Definition PETScMatrix.h:79
PetscInt getNumberOfLocalColumns() const
Get the number of local columns.
Definition PETScMatrix.h:73
void setPreallocationNonzeroOption(PETScMatrix &matrix, bool const has_col_idx)
MatStructure nonzeroPatternStructure(std::vector< PetscInt > const &col_a, std::vector< PetscInt > const &col_b)
bool finalizeMatrixAssembly(MAT_T &)
void operator()(PETScMatrix &matrix, SPARSITY_PATTERN const &sparsity_pattern) const