OGS
MathLib::PETScMatrix Class Reference

Detailed Description

Wrapper class for PETSc matrix routines for matrix.

Definition at line 22 of file PETScMatrix.h.

#include <PETScMatrix.h>

Public Types

using IndexType = PetscInt

Public Member Functions

 PETScMatrix ()
 PETScMatrix (const PetscInt nrows, const PETScSparsityPattern &sparsity_pattern)
 Constructor for a square matrix partitioning with more options.
 PETScMatrix (const PetscInt nrows, const PetscInt ncols, const PETScSparsityPattern &sparsity_pattern)
 Constructor for a rectangular matrix partitioning with more options.
 ~PETScMatrix ()
 PETScMatrix (PETScMatrix const &A)
PETScMatrixoperator= (PETScMatrix const &A)
void finalizeAssembly (const MatAssemblyType asm_type=MAT_FINAL_ASSEMBLY)
 Perform MPI collection of assembled entries in buffer.
PetscInt getNumberOfRows () const
 Get the number of rows.
PetscInt getNumberOfColumns () const
 Get the number of columns.
PetscInt getNumberOfLocalRows () const
 Get the number of local rows.
PetscInt getNumberOfLocalColumns () const
 Get the number of local columns.
PetscInt getRangeBegin () const
 Get the start global index of the rows of the same rank.
PetscInt getRangeEnd () const
 Get the end global index of the rows in the same rank.
std::vector< PetscInt > const & getSparsityColumnIndices () const
 Column indices cached at preallocation (empty on the fallback path).
Mat & getRawMatrix ()
 Get matrix reference.
Mat const & getRawMatrix () const
void setZero ()
 Set all entries to zero.
void setRowsColumnsZero (std::vector< PetscInt > const &row_pos)
 Set the specified rows to zero except diagonal entries, i.e. \(A(k, j) = \begin{cases} 0.0, &j\not=k, j=1,2,\dots,k-1, k+1, \dots, n \\ 1.0, &j = k \end{cases}\), where \(k \in \mbox{row\_pos}\) This function must be called by all ranks.
void set (const PetscInt i, const PetscInt j, const PetscScalar value)
 Set a single entry with a value.
void add (const PetscInt i, const PetscInt j, const PetscScalar value)
 Add value to a single entry.
void addToDiagonal (const PetscScalar value)
 Add a constant value to all diagonal entries of the matrix.
template<class T_DENSE_MATRIX>
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.
template<class T_DENSE_MATRIX>
void add (std::vector< PetscInt > const &row_pos, std::vector< PetscInt > const &col_pos, const T_DENSE_MATRIX &sub_mat)
 Add a dense sub-matrix to a PETSc matrix.
void viewer (const std::string &file_name, const PetscViewerFormat vw_format=PETSC_VIEWER_ASCII_MATLAB)

Private Member Functions

void destroy ()
void create (const PETScSparsityPattern &sparsity_pattern)
 Create the matrix, configure memory allocation and set the related member data.
void preallocateFromSparsityPattern (const PETScSparsityPattern &sparsity_pattern)
 Preallocate the matrix storage from an exact sparsity pattern.

Private Attributes

Mat A_ = nullptr
 PETSc matrix.
PetscInt nrows_
 Number of the global rows.
PetscInt ncols_
 Number of the global columns.
PetscInt n_loc_rows_
 Number of the local rows.
PetscInt n_loc_cols_
 Number of the local columns.
PetscInt start_rank_
 Starting index in a rank.
PetscInt end_rank_
 Ending index in a rank.
std::vector< PetscInt > sparsity_col_idx_
 Column indices from preallocation (empty when fallback path was used).

Friends

bool finalizeMatrixAssembly (PETScMatrix &mat, const MatAssemblyType asm_type=MAT_FINAL_ASSEMBLY)
 General interface for the matrix assembly.

Member Typedef Documentation

◆ IndexType

Definition at line 25 of file PETScMatrix.h.

Constructor & Destructor Documentation

◆ PETScMatrix() [1/4]

MathLib::PETScMatrix::PETScMatrix ( )
inline

Definition at line 28 of file PETScMatrix.h.

28{}

Referenced by PETScMatrix(), finalizeMatrixAssembly, and operator=().

◆ PETScMatrix() [2/4]

MathLib::PETScMatrix::PETScMatrix ( const PetscInt nrows,
const PETScSparsityPattern & sparsity_pattern )

Constructor for a square matrix partitioning with more options.

Parameters
nrowsThe number of local rows of the matrix; the global size is left to PETSc.
sparsity_patternCSR per-local-row column indices to reserve.

Definition at line 11 of file PETScMatrix.cpp.

13 : nrows_(PETSC_DECIDE),
14 ncols_(PETSC_DECIDE),
15 n_loc_rows_(nrows),
16 n_loc_cols_(nrows)
17{
18 create(sparsity_pattern);
19}
PetscInt n_loc_cols_
Number of the local columns.
PetscInt n_loc_rows_
Number of the local rows.
PetscInt nrows_
Number of the global rows.
PetscInt ncols_
Number of the global columns.
void create(const PETScSparsityPattern &sparsity_pattern)
Create the matrix, configure memory allocation and set the related member data.

References create(), n_loc_cols_, n_loc_rows_, ncols_, and nrows_.

◆ PETScMatrix() [3/4]

MathLib::PETScMatrix::PETScMatrix ( const PetscInt nrows,
const PetscInt ncols,
const PETScSparsityPattern & sparsity_pattern )

Constructor for a rectangular matrix partitioning with more options.

Parameters
nrowsThe number of local rows; the global size is left to PETSc.
ncolsThe number of local columns; the global size is left to PETSc.
sparsity_patternCSR per-local-row column indices to reserve.

Definition at line 21 of file PETScMatrix.cpp.

23 : nrows_(PETSC_DECIDE),
24 ncols_(PETSC_DECIDE),
25 n_loc_rows_(nrows),
26 n_loc_cols_(ncols)
27{
28 create(sparsity_pattern);
29}

References create(), n_loc_cols_, n_loc_rows_, ncols_, and nrows_.

◆ ~PETScMatrix()

MathLib::PETScMatrix::~PETScMatrix ( )
inline

Definition at line 50 of file PETScMatrix.h.

50{ destroy(); }

References destroy().

◆ PETScMatrix() [4/4]

MathLib::PETScMatrix::PETScMatrix ( PETScMatrix const & A)

Definition at line 31 of file PETScMatrix.cpp.

32 : nrows_(A.nrows_),
33 ncols_(A.ncols_),
34 n_loc_rows_(A.n_loc_rows_),
35 n_loc_cols_(A.n_loc_cols_),
36 start_rank_(A.start_rank_),
37 end_rank_(A.end_rank_),
38 sparsity_col_idx_(A.sparsity_col_idx_)
39{
40 // MatDuplicate reproduces the nonzero structure exactly, so the copy
41 // shares the source's structure.
42 PetscCallAbort(PETSC_COMM_WORLD, MatDuplicate(A.A_, MAT_COPY_VALUES, &A_));
43}
PetscInt end_rank_
Ending index in a rank.
Mat A_
PETSc matrix.
PetscInt start_rank_
Starting index in a rank.
std::vector< PetscInt > sparsity_col_idx_
Column indices from preallocation (empty when fallback path was used).

References PETScMatrix(), A_, end_rank_, n_loc_cols_, n_loc_rows_, ncols_, nrows_, sparsity_col_idx_, and start_rank_.

Member Function Documentation

◆ add() [1/3]

void MathLib::PETScMatrix::add ( const PetscInt i,
const PetscInt j,
const PetscScalar value )
inline

Add value to a single entry.

Parameters
iThe row index.
jThe column index.
valueThe entry value.

Definition at line 124 of file PETScMatrix.h.

125 {
126 PetscCallAbort(PETSC_COMM_WORLD,
127 MatSetValue(A_, i, j, value, ADD_VALUES));
128 }

References A_.

Referenced by add(), MathLib::addToMatrix(), and MathLib::setMatrix().

◆ add() [2/3]

template<class T_DENSE_MATRIX>
void MathLib::PETScMatrix::add ( RowColumnIndices< PetscInt > const & indices,
const T_DENSE_MATRIX & sub_matrix )
inline

Add sub-matrix at positions given by global indices, in which negative index indicates ghost entry.

In order to use MatZeroRows to apply Dirichlet boundary condition, entries in the rows with the negative global indices are skipped to added to the global matrix, meanwhile entries in the columns with the negative global indices are added the global matrix. By using MatZeroRows to apply Dirichlet boundary condition, the off diagonal entries in ghost rows of the global matrix are set to zero, while the off diagonal entries in ghost rows of the global matrix are assembled and kept for linear solver.

For the setting of Dirichlet boundary condition in PETSc, please refer to the PETSc:Documentation:FAQ.

Definition at line 154 of file PETScMatrix.h.

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 }
void add(const PetscInt i, const PetscInt j, const PetscScalar value)
Add value to a single entry.

References add(), MathLib::RowColumnIndices< IDX_TYPE >::columns, ncols_, and MathLib::RowColumnIndices< IDX_TYPE >::rows.

◆ add() [3/3]

template<class T_DENSE_MATRIX>
void MathLib::PETScMatrix::add ( std::vector< PetscInt > const & row_pos,
std::vector< PetscInt > const & col_pos,
const T_DENSE_MATRIX & sub_mat )

Add a dense sub-matrix to a PETSc matrix.

Parameters
row_posThe global indices of the rows of the dense sub-matrix.
col_posThe global indices of the columns of the dense sub-matrix.
sub_matA dense sub-matrix to be added. Its data of which must be row oriented stored.

Definition at line 283 of file PETScMatrix.h.

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};

References A_.

◆ addToDiagonal()

void MathLib::PETScMatrix::addToDiagonal ( const PetscScalar value)

Add a constant value to all diagonal entries of the matrix.

Parameters
valueThe value to add to each diagonal entry.
Note
This is more efficient than calling add(i, i, value) in a loop.

Definition at line 227 of file PETScMatrix.cpp.

228{
229 // MatShift computes A_ = A_ + value * I, i.e. adds value to every diagonal
230 // entry (creating missing ones). Correct in both serial and parallel.
231 // Temporarily allow new nonzero entries so a diagonal entry missing from
232 // the preallocated sparsity pattern doesn't abort the run.
233 PetscCallAbort(
234 PETSC_COMM_WORLD,
235 MatSetOption(A_, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
236 PetscCallAbort(PETSC_COMM_WORLD, MatShift(A_, value));
237 PetscCallAbort(
238 PETSC_COMM_WORLD,
239 MatSetOption(A_, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
240}

References A_.

◆ create()

void MathLib::PETScMatrix::create ( const PETScSparsityPattern & sparsity_pattern)
private

Create the matrix, configure memory allocation and set the related member data.

Parameters
sparsity_patternCSR per-local-row column indices to reserve.

Definition at line 126 of file PETScMatrix.cpp.

127{
128 PetscCallAbort(PETSC_COMM_WORLD, MatCreate(PETSC_COMM_WORLD, &A_));
129 PetscCallAbort(PETSC_COMM_WORLD,
130 MatSetSizes(A_, n_loc_rows_, n_loc_cols_, nrows_, ncols_));
131
132 PetscCallAbort(PETSC_COMM_WORLD, MatSetType(A_, MATAIJ));
133 PetscCallAbort(PETSC_COMM_WORLD, MatSetFromOptions(A_));
134
135 // Use MATPREALLOCATOR for type-agnostic exact sparsity preallocation.
136 // This works regardless of the matrix type chosen via -mat_type.
137 preallocateFromSparsityPattern(sparsity_pattern);
138
139 PetscCallAbort(PETSC_COMM_WORLD,
140 MatGetOwnershipRange(A_, &start_rank_, &end_rank_));
141 PetscCallAbort(PETSC_COMM_WORLD, MatGetSize(A_, &nrows_, &ncols_));
142 PetscCallAbort(PETSC_COMM_WORLD,
143 MatGetLocalSize(A_, &n_loc_rows_, &n_loc_cols_));
144}
void preallocateFromSparsityPattern(const PETScSparsityPattern &sparsity_pattern)
Preallocate the matrix storage from an exact sparsity pattern.

References A_, end_rank_, n_loc_cols_, n_loc_rows_, ncols_, nrows_, preallocateFromSparsityPattern(), and start_rank_.

Referenced by PETScMatrix(), and PETScMatrix().

◆ destroy()

void MathLib::PETScMatrix::destroy ( )
inlineprivate

Definition at line 225 of file PETScMatrix.h.

226 {
227 if (A_ != nullptr)
228 {
229 PetscCallAbort(PETSC_COMM_WORLD, MatDestroy(&A_));
230 }
231 A_ = nullptr;
232 }

References A_.

Referenced by ~PETScMatrix(), and operator=().

◆ finalizeAssembly()

void MathLib::PETScMatrix::finalizeAssembly ( const MatAssemblyType asm_type = MAT_FINAL_ASSEMBLY)
inline

Perform MPI collection of assembled entries in buffer.

Parameters
asm_typeAssembly type, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY

Definition at line 60 of file PETScMatrix.h.

61 {
62 MatAssemblyBegin(A_, asm_type);
63 MatAssemblyEnd(A_, asm_type);
64 }

References A_.

Referenced by MathLib::applyKnownSolution(), MathLib::LinAlg::finalizeAssembly(), finalizeMatrixAssembly, and viewer().

◆ getNumberOfColumns()

PetscInt MathLib::PETScMatrix::getNumberOfColumns ( ) const
inline

Get the number of columns.

Definition at line 69 of file PETScMatrix.h.

69{ return ncols_; }

References ncols_.

Referenced by MathLib::addToMatrix(), and MathLib::setMatrix().

◆ getNumberOfLocalColumns()

PetscInt MathLib::PETScMatrix::getNumberOfLocalColumns ( ) const
inline

Get the number of local columns.

Definition at line 73 of file PETScMatrix.h.

73{ return n_loc_cols_; }

References n_loc_cols_.

◆ getNumberOfLocalRows()

PetscInt MathLib::PETScMatrix::getNumberOfLocalRows ( ) const
inline

Get the number of local rows.

Definition at line 71 of file PETScMatrix.h.

71{ return n_loc_rows_; }

References n_loc_rows_.

◆ getNumberOfRows()

PetscInt MathLib::PETScMatrix::getNumberOfRows ( ) const
inline

Get the number of rows.

Definition at line 67 of file PETScMatrix.h.

67{ return nrows_; }

References nrows_.

Referenced by MathLib::addToMatrix(), and MathLib::setMatrix().

◆ getRangeBegin()

PetscInt MathLib::PETScMatrix::getRangeBegin ( ) const
inline

Get the start global index of the rows of the same rank.

Definition at line 75 of file PETScMatrix.h.

75{ return start_rank_; }

References start_rank_.

◆ getRangeEnd()

PetscInt MathLib::PETScMatrix::getRangeEnd ( ) const
inline

Get the end global index of the rows in the same rank.

Definition at line 77 of file PETScMatrix.h.

77{ return end_rank_; }

References end_rank_.

◆ getRawMatrix() [1/2]

Mat & MathLib::PETScMatrix::getRawMatrix ( )
inline

◆ getRawMatrix() [2/2]

Mat const & MathLib::PETScMatrix::getRawMatrix ( ) const
inline

Get a matrix reference.

Warning
This method is dangerous insofar as you can do arbitrary things also with a const PETSc matrix.

Definition at line 92 of file PETScMatrix.h.

92{ return A_; }

References A_.

◆ getSparsityColumnIndices()

std::vector< PetscInt > const & MathLib::PETScMatrix::getSparsityColumnIndices ( ) const
inline

Column indices cached at preallocation (empty on the fallback path).

Definition at line 79 of file PETScMatrix.h.

80 {
81 return sparsity_col_idx_;
82 }

References sparsity_col_idx_.

Referenced by MathLib::LinAlg::axpy(), and MathLib::LinAlg::aypx().

◆ operator=()

PETScMatrix & MathLib::PETScMatrix::operator= ( PETScMatrix const & A)

Definition at line 45 of file PETScMatrix.cpp.

46{
47 nrows_ = A.nrows_;
48 ncols_ = A.ncols_;
49 n_loc_rows_ = A.n_loc_rows_;
50 n_loc_cols_ = A.n_loc_cols_;
51 start_rank_ = A.start_rank_;
52 end_rank_ = A.end_rank_;
53
54 if (A_ != nullptr)
55 {
56 auto const pattern =
57 nonzeroPatternStructure(A.sparsity_col_idx_, sparsity_col_idx_);
58 sparsity_col_idx_ = A.sparsity_col_idx_;
59 PetscCallAbort(PETSC_COMM_WORLD, MatCopy(A.A_, A_, pattern));
60 }
61 else
62 {
63 sparsity_col_idx_ = A.sparsity_col_idx_;
64 destroy();
65 PetscCallAbort(PETSC_COMM_WORLD,
66 MatDuplicate(A.A_, MAT_COPY_VALUES, &A_));
67 }
68
69 return *this;
70}
MatStructure nonzeroPatternStructure(std::vector< PetscInt > const &col_a, std::vector< PetscInt > const &col_b)

References PETScMatrix(), A_, destroy(), end_rank_, n_loc_cols_, n_loc_rows_, ncols_, MathLib::nonzeroPatternStructure(), nrows_, sparsity_col_idx_, and start_rank_.

◆ preallocateFromSparsityPattern()

void MathLib::PETScMatrix::preallocateFromSparsityPattern ( const PETScSparsityPattern & sparsity_pattern)
private

Preallocate the matrix storage from an exact sparsity pattern.

Uses a MATPREALLOCATOR helper matrix to reserve, type-agnostically, exactly the nonzero positions described by sparsity_pattern, then transfers the preallocation to the real matrix. Also caches the column indices in sparsity_col_idx_ for later nonzero-pattern comparisons.

Parameters
sparsity_patternCSR-style per-local-row column indices to reserve.

Definition at line 146 of file PETScMatrix.cpp.

148{
149 Mat preallocator;
150 PetscCallAbort(PETSC_COMM_WORLD,
151 MatCreate(PETSC_COMM_WORLD, &preallocator));
152 PetscCallAbort(
153 PETSC_COMM_WORLD,
154 MatSetSizes(preallocator, n_loc_rows_, n_loc_cols_, nrows_, ncols_));
155 PetscCallAbort(PETSC_COMM_WORLD, MatSetType(preallocator, MATPREALLOCATOR));
156 PetscCallAbort(PETSC_COMM_WORLD, MatSetUp(preallocator));
157
158 PetscInt row_start;
159 PetscInt row_end;
160 PetscCallAbort(PETSC_COMM_WORLD,
161 MatGetOwnershipRange(preallocator, &row_start, &row_end));
162
163 PetscInt const n_local = row_end - row_start;
164
165 // The pattern is indexed by local row below, so PETSc's row distribution
166 // must agree with the one the pattern was built for. A mismatch would
167 // otherwise silently read the wrong rows, or read out of bounds.
168 //
169 // The condition is rank-local, so it is reduced before aborting: a rank
170 // aborting on its own would leave the other ranks hanging in the collective
171 // assembly below instead of failing with it.
172 if (!BaseLib::MPI::allOf(sparsity_pattern.numberOfRows() == n_local))
173 {
174 OGS_FATAL(
175 "PETScMatrix: the sparsity pattern's local row count and PETSc's "
176 "row distribution disagree on at least one rank. On this rank the "
177 "pattern describes {} local rows, but PETSc distributed {} rows.",
178 sparsity_pattern.numberOfRows(), n_local);
179 }
180
181 // Record the nonzero structure of this rank's rows in the MATPREALLOCATOR
182 // helper, one row at a time. MATPREALLOCATOR stores positions only, so the
183 // values array is ignored and nullptr is passed. All rows here are in
184 // [row_start, row_end), so the off-process stash path (which would read
185 // the values) is never taken.
186 for (PetscInt local_row = 0; local_row < n_local; ++local_row)
187 {
188 PetscInt const global_row = row_start + local_row;
189 PetscInt const ncols = sparsity_pattern.nnzInRow(local_row);
190 PetscInt const* const cols = sparsity_pattern.col_idx.data() +
191 sparsity_pattern.row_ptr[local_row];
192 PetscCallAbort(PETSC_COMM_WORLD,
193 MatSetValues(preallocator, 1, &global_row, ncols, cols,
194 nullptr, INSERT_VALUES));
195 }
196
197 PetscCallAbort(PETSC_COMM_WORLD,
198 MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY));
199 PetscCallAbort(PETSC_COMM_WORLD,
200 MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY));
201
202 // Insert explicit zeros at every preallocated position by
203 // MatPreallocatorPreallocate below (fill = PETSC_TRUE) so that
204 // MAT_FINAL_ASSEMBLY in subsequent partial assemblies (e.g. soil-only) does
205 // not discard entries that were allocated but not yet written to.
206 constexpr PetscBool fill_zeros = PETSC_TRUE;
207 PetscCallAbort(PETSC_COMM_WORLD,
208 MatPreallocatorPreallocate(preallocator, fill_zeros, A_));
209 // The preallocator is a transient helper, destroyed once the target matrix
210 // has its structure. Keeping it would let further matrices built from the
211 // same sparsity pattern skip the row-by-row MatSetValues loop above, but
212 // the cache would have to live where the pattern lives (the matrix
213 // specifications / matrix provider), not in a single matrix. It is not done
214 // here: matrices are constructed a handful of times per process, and the
215 // loop costs one pass over the pattern, negligible next to assembly.
216 PetscCallAbort(PETSC_COMM_WORLD, MatDestroy(&preallocator));
217
218 sparsity_col_idx_ = sparsity_pattern.col_idx;
219}
#define OGS_FATAL(...)
Definition Error.h:10
static bool allOf(bool const val, Mpi const &mpi=Mpi{OGS_COMM_WORLD})
Definition MPI.h:190

References A_, BaseLib::MPI::allOf(), MathLib::PETScSparsityPattern::col_idx, n_loc_cols_, n_loc_rows_, ncols_, MathLib::PETScSparsityPattern::nnzInRow(), nrows_, MathLib::PETScSparsityPattern::numberOfRows(), OGS_FATAL, MathLib::PETScSparsityPattern::row_ptr, and sparsity_col_idx_.

Referenced by create().

◆ set()

void MathLib::PETScMatrix::set ( const PetscInt i,
const PetscInt j,
const PetscScalar value )
inline

Set a single entry with a value.

Parameters
iThe row index.
jThe column index.
valueThe entry value.

Definition at line 112 of file PETScMatrix.h.

113 {
114 PetscCallAbort(PETSC_COMM_WORLD,
115 MatSetValue(A_, i, j, value, INSERT_VALUES));
116 }

References A_.

◆ setRowsColumnsZero()

void MathLib::PETScMatrix::setRowsColumnsZero ( std::vector< PetscInt > const & row_pos)

Set the specified rows to zero except diagonal entries, i.e. \(A(k, j) = \begin{cases} 0.0, &j\not=k, j=1,2,\dots,k-1, k+1, \dots, n \\ 1.0, &j = k \end{cases}\), where \(k \in \mbox{row\_pos}\) This function must be called by all ranks.

Parameters
row_posThe row indices of the specified rows.

Definition at line 72 of file PETScMatrix.cpp.

73{
74 // Each rank (compute core) processes only the rows that belong to the rank
75 // itself.
76 const PetscScalar one = 1.0;
77 const PetscInt nrows = static_cast<PetscInt>(row_pos.size());
78
79 // Each process will only zero its own rows.
80 // This avoids all reductions in the zero row routines
81 // and thus improves performance for very large process counts.
82 // See PETSc doc about MAT_NO_OFF_PROC_ZERO_ROWS.
83 PetscCallAbort(PETSC_COMM_WORLD,
84 MatSetOption(A_, MAT_NO_OFF_PROC_ZERO_ROWS, PETSC_TRUE));
85
86 // Keep the non-zero pattern for the assignment operator.
87 PetscCallAbort(PETSC_COMM_WORLD,
88 MatSetOption(A_, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE));
89
90 if (nrows > 0)
91 {
92 PetscCallAbort(PETSC_COMM_WORLD,
93 MatZeroRows(A_, nrows, &row_pos[0], one, PETSC_NULLPTR,
94 PETSC_NULLPTR));
95 }
96 else
97 {
98 PetscCallAbort(PETSC_COMM_WORLD,
99 MatZeroRows(A_, 0, PETSC_NULLPTR, one, PETSC_NULLPTR,
100 PETSC_NULLPTR));
101 }
102}

References A_.

Referenced by MathLib::applyKnownSolution().

◆ setZero()

void MathLib::PETScMatrix::setZero ( )
inline

Set all entries to zero.

Definition at line 94 of file PETScMatrix.h.

94{ MatZeroEntries(A_); }

References A_.

Referenced by MathLib::setMatrix(), and MathLib::setMatrix().

◆ viewer()

void MathLib::PETScMatrix::viewer ( const std::string & file_name,
const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB )

View the global vector for test purpose. Do not use it for output a big vector.

Parameters
file_nameFile name for output
vw_formatFile format listed as: PETSC_VIEWER_DEFAULT Default format PETSC_VIEWER_ASCII_MATLAB MATLAB format PETSC_VIEWER_ASCII_DENSE Print matrix as dense PETSC_VIEWER_ASCII_IMPL Implementation-specific format (which is in many cases the same as the default) PETSC_VIEWER_ASCII_INFO Basic information about object PETSC_VIEWER_ASCII_INFO_DETAIL More detailed info about object PETSC_VIEWER_ASCII_COMMON Identical output format for all objects of a particular type PETSC_VIEWER_ASCII_INDEX (for vectors) Prints the vector element number next to each vector entry PETSC_VIEWER_ASCII_SYMMODU Print parallel vectors without indicating the processor ranges PETSC_VIEWER_ASCII_VTK Outputs the object to a VTK file PETSC_VIEWER_NATIVE Store the object to the binary file in its native format (for example, dense matrices are stored as dense), DMDA vectors are dumped directly to the file instead of being first put in the natural ordering PETSC_VIEWER_DRAW_BASIC Views the vector with a simple 1d plot PETSC_VIEWER_DRAW_LG Views the vector with a line graph PETSC_VIEWER_DRAW_CONTOUR Views the vector with a contour plot

Definition at line 104 of file PETScMatrix.cpp.

106{
107 PetscViewer viewer;
108 PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &viewer);
109 PetscViewerPushFormat(viewer, vw_format);
110
112
113 PetscObjectSetName((PetscObject)A_, "Stiffness_matrix");
114 MatView(A_, viewer);
115
116// This preprocessor is only for debugging, e.g. dump the matrix and exit the
117// program.
118// #define EXIT_TEST
119#ifdef EXIT_TEST
120 MatDestroy(A_);
121 PetscFinalize();
122 exit(0);
123#endif
124}
void finalizeAssembly(const MatAssemblyType asm_type=MAT_FINAL_ASSEMBLY)
Perform MPI collection of assembled entries in buffer.
Definition PETScMatrix.h:60
void viewer(const std::string &file_name, const PetscViewerFormat vw_format=PETSC_VIEWER_ASCII_MATLAB)

References A_, finalizeAssembly(), and viewer().

Referenced by viewer().

◆ finalizeMatrixAssembly

bool finalizeMatrixAssembly ( PETScMatrix & mat,
const MatAssemblyType asm_type = MAT_FINAL_ASSEMBLY )
friend

General interface for the matrix assembly.

Parameters
matThe matrix to be finalized.
asm_typeAssembly type, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY.

Definition at line 221 of file PETScMatrix.cpp.

222{
223 mat.finalizeAssembly(asm_type);
224 return true;
225}

References PETScMatrix(), and finalizeAssembly().

Member Data Documentation

◆ A_

Mat MathLib::PETScMatrix::A_ = nullptr
private

◆ end_rank_

PetscInt MathLib::PETScMatrix::end_rank_
private

Ending index in a rank.

Definition at line 253 of file PETScMatrix.h.

Referenced by PETScMatrix(), create(), getRangeEnd(), and operator=().

◆ n_loc_cols_

PetscInt MathLib::PETScMatrix::n_loc_cols_
private

Number of the local columns.

Definition at line 247 of file PETScMatrix.h.

Referenced by PETScMatrix(), PETScMatrix(), PETScMatrix(), create(), getNumberOfLocalColumns(), operator=(), and preallocateFromSparsityPattern().

◆ n_loc_rows_

PetscInt MathLib::PETScMatrix::n_loc_rows_
private

Number of the local rows.

Definition at line 244 of file PETScMatrix.h.

Referenced by PETScMatrix(), PETScMatrix(), PETScMatrix(), create(), getNumberOfLocalRows(), operator=(), and preallocateFromSparsityPattern().

◆ ncols_

PetscInt MathLib::PETScMatrix::ncols_
private

Number of the global columns.

Definition at line 241 of file PETScMatrix.h.

Referenced by PETScMatrix(), PETScMatrix(), PETScMatrix(), add(), create(), getNumberOfColumns(), operator=(), and preallocateFromSparsityPattern().

◆ nrows_

PetscInt MathLib::PETScMatrix::nrows_
private

Number of the global rows.

Definition at line 238 of file PETScMatrix.h.

Referenced by PETScMatrix(), PETScMatrix(), PETScMatrix(), create(), getNumberOfRows(), operator=(), and preallocateFromSparsityPattern().

◆ sparsity_col_idx_

std::vector<PetscInt> MathLib::PETScMatrix::sparsity_col_idx_
private

Column indices from preallocation (empty when fallback path was used).

Definition at line 256 of file PETScMatrix.h.

Referenced by PETScMatrix(), getSparsityColumnIndices(), operator=(), and preallocateFromSparsityPattern().

◆ start_rank_

PetscInt MathLib::PETScMatrix::start_rank_
private

Starting index in a rank.

Definition at line 250 of file PETScMatrix.h.

Referenced by PETScMatrix(), create(), getRangeBegin(), and operator=().


The documentation for this class was generated from the following files: