OGS
MathLib::PETScMatrix Class Reference

Detailed Description

Wrapper class for PETSc matrix routines for matrix.

Definition at line 20 of file PETScMatrix.h.

#include <PETScMatrix.h>

Public Types

using IndexType = PetscInt

Public Member Functions

 PETScMatrix ()
 PETScMatrix (const PetscInt nrows, const PETScMatrixOption &mat_op=PETScMatrixOption())
 Constructor for a square matrix partitioning with more options.
 PETScMatrix (const PetscInt nrows, const PetscInt ncols, const PETScMatrixOption &mat_op=PETScMatrixOption())
 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.
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.
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 PetscInt d_nz, const PetscInt o_nz)
 Create the matrix, configure memory allocation and set the related member data.

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.

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 23 of file PETScMatrix.h.

Constructor & Destructor Documentation

◆ PETScMatrix() [1/4]

MathLib::PETScMatrix::PETScMatrix ( )
inline

Definition at line 26 of file PETScMatrix.h.

26{}

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

◆ PETScMatrix() [2/4]

MathLib::PETScMatrix::PETScMatrix ( const PetscInt nrows,
const PETScMatrixOption & mat_op = PETScMatrixOption() )

Constructor for a square matrix partitioning with more options.

Parameters
nrowsThe number of rows of the matrix or the local matrix.
mat_opThe configuration information for creating a matrix.

Definition at line 10 of file PETScMatrix.cpp.

11 : nrows_(nrows),
12 ncols_(nrows),
13 n_loc_rows_(PETSC_DECIDE),
14 n_loc_cols_(mat_opt.n_local_cols)
15{
16 if (!mat_opt.is_global_size)
17 {
18 n_loc_rows_ = nrows;
19 n_loc_cols_ = nrows;
20 nrows_ = PETSC_DECIDE;
21 ncols_ = PETSC_DECIDE;
22 }
23
24 create(mat_opt.d_nz, mat_opt.o_nz);
25}
PetscInt n_loc_cols_
Number of the local columns.
void create(const PetscInt d_nz, const PetscInt o_nz)
Create the matrix, configure memory allocation and set the related member data.
PetscInt n_loc_rows_
Number of the local rows.
PetscInt nrows_
Number of the global rows.
PetscInt ncols_
Number of the global columns.

References create(), MathLib::PETScMatrixOption::d_nz, MathLib::PETScMatrixOption::is_global_size, n_loc_cols_, n_loc_rows_, ncols_, nrows_, and MathLib::PETScMatrixOption::o_nz.

◆ PETScMatrix() [3/4]

MathLib::PETScMatrix::PETScMatrix ( const PetscInt nrows,
const PetscInt ncols,
const PETScMatrixOption & mat_op = PETScMatrixOption() )

Constructor for a rectangular matrix partitioning with more options.

Parameters
nrowsThe number of global or local rows.
ncolsThe number of global or local columns.
mat_opThe configuration information for creating a matrix.

Definition at line 27 of file PETScMatrix.cpp.

29 : nrows_(nrows),
30 ncols_(ncols),
31 n_loc_rows_(PETSC_DECIDE),
32 n_loc_cols_(mat_opt.n_local_cols)
33{
34 if (!mat_opt.is_global_size)
35 {
36 nrows_ = PETSC_DECIDE;
37 ncols_ = PETSC_DECIDE;
38 n_loc_rows_ = nrows;
39 n_loc_cols_ = ncols;
40 }
41
42 create(mat_opt.d_nz, mat_opt.o_nz);
43}

References create(), MathLib::PETScMatrixOption::d_nz, MathLib::PETScMatrixOption::is_global_size, n_loc_cols_, n_loc_rows_, ncols_, nrows_, and MathLib::PETScMatrixOption::o_nz.

◆ ~PETScMatrix()

MathLib::PETScMatrix::~PETScMatrix ( )
inline

Definition at line 46 of file PETScMatrix.h.

46{ destroy(); }

References destroy().

◆ PETScMatrix() [4/4]

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

Definition at line 45 of file PETScMatrix.cpp.

46 : nrows_(A.nrows_),
47 ncols_(A.ncols_),
48 n_loc_rows_(A.n_loc_rows_),
49 n_loc_cols_(A.n_loc_cols_),
50 start_rank_(A.start_rank_),
51 end_rank_(A.end_rank_)
52{
53 MatConvert(A.A_, MATSAME, MAT_INITIAL_MATRIX, &A_);
54}
PetscInt end_rank_
Ending index in a rank.
Mat A_
PETSc matrix.
PetscInt start_rank_
Starting index in a rank.

References PETScMatrix(), A_, end_rank_, n_loc_cols_, n_loc_rows_, ncols_, nrows_, 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 113 of file PETScMatrix.h.

114 {
115 MatSetValue(A_, i, j, value, ADD_VALUES);
116 }

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 135 of file PETScMatrix.h.

137 {
138 // Set global column indices to positive to allow all entries of columns
139 // to be added to the global matrix. For the ghost columns, only the
140 // off diagonal entries are added due to the negative indices of the
141 // corresponding rows.
142 std::vector<PetscInt> cols;
143 cols.reserve(indices.columns.size());
144 for (auto col : indices.columns)
145 {
146 // Ghost entries, and its original index is 0.
147 if (col == -ncols_)
148 {
149 cols.push_back(0);
150 }
151 else
152 {
153 cols.push_back(std::abs(col));
154 }
155 }
156
157 add(indices.rows, cols, sub_matrix);
158 }
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 251 of file PETScMatrix.h.

254{
255 const PetscInt nrows = static_cast<PetscInt>(row_pos.size());
256 const PetscInt ncols = static_cast<PetscInt>(col_pos.size());
257
258 MatSetValues(A_, nrows, &row_pos[0], ncols, &col_pos[0], sub_mat.data(),
259 ADD_VALUES);
260};

References A_.

◆ create()

void MathLib::PETScMatrix::create ( const PetscInt d_nz,
const PetscInt o_nz )
private

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

Parameters
d_nzNumber of nonzeros per row in the diagonal portion of local submatrix (same value is used for all local rows),
o_nzNumber of nonzeros per row in the off-diagonal portion of local submatrix (same value is used for all local rows)

Definition at line 127 of file PETScMatrix.cpp.

128{
129 MatCreate(PETSC_COMM_WORLD, &A_);
130 MatSetSizes(A_, n_loc_rows_, n_loc_cols_, nrows_, ncols_);
131
132 MatSetType(A_, MATAIJ);
133 MatSetFromOptions(A_);
134
135 MatSeqAIJSetPreallocation(A_, d_nz, PETSC_NULLPTR);
136 MatMPIAIJSetPreallocation(A_, d_nz, PETSC_NULLPTR, o_nz, PETSC_NULLPTR);
137 // If pre-allocation does not work one can use MatSetUp(A_), which is much
138 // slower.
139
140 MatGetOwnershipRange(A_, &start_rank_, &end_rank_);
141 MatGetSize(A_, &nrows_, &ncols_);
142 MatGetLocalSize(A_, &n_loc_rows_, &n_loc_cols_);
143}

References A_, end_rank_, n_loc_cols_, n_loc_rows_, ncols_, nrows_, and start_rank_.

Referenced by PETScMatrix(), and PETScMatrix().

◆ destroy()

void MathLib::PETScMatrix::destroy ( )
inlineprivate

Definition at line 206 of file PETScMatrix.h.

207 {
208 if (A_ != nullptr)
209 {
210 MatDestroy(&A_);
211 }
212 A_ = nullptr;
213 }

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 56 of file PETScMatrix.h.

57 {
58 MatAssemblyBegin(A_, asm_type);
59 MatAssemblyEnd(A_, asm_type);
60 }

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 65 of file PETScMatrix.h.

65{ 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 69 of file PETScMatrix.h.

69{ return n_loc_cols_; }

References n_loc_cols_.

◆ getNumberOfLocalRows()

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

Get the number of local rows.

Definition at line 67 of file PETScMatrix.h.

67{ return n_loc_rows_; }

References n_loc_rows_.

◆ getNumberOfRows()

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

Get the number of rows.

Definition at line 63 of file PETScMatrix.h.

63{ 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 71 of file PETScMatrix.h.

71{ 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 73 of file PETScMatrix.h.

73{ 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 82 of file PETScMatrix.h.

82{ return A_; }

References A_.

◆ operator=()

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

Definition at line 56 of file PETScMatrix.cpp.

57{
58 nrows_ = A.nrows_;
59 ncols_ = A.ncols_;
60 n_loc_rows_ = A.n_loc_rows_;
61 n_loc_cols_ = A.n_loc_cols_;
62 start_rank_ = A.start_rank_;
63 end_rank_ = A.end_rank_;
64
65 if (A_ != nullptr)
66 {
67 // TODO this is the slowest option for copying
68 MatCopy(A.A_, A_, DIFFERENT_NONZERO_PATTERN);
69 }
70 else
71 {
72 destroy();
73 MatConvert(A.A_, MATSAME, MAT_INITIAL_MATRIX, &A_);
74 }
75
76 return *this;
77}

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

◆ 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 102 of file PETScMatrix.h.

103 {
104 MatSetValue(A_, i, j, value, INSERT_VALUES);
105 }

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 79 of file PETScMatrix.cpp.

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

References A_.

Referenced by MathLib::applyKnownSolution().

◆ setZero()

void MathLib::PETScMatrix::setZero ( )
inline

Set all entries to zero.

Definition at line 84 of file PETScMatrix.h.

84{ 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 105 of file PETScMatrix.cpp.

107{
108 PetscViewer viewer;
109 PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &viewer);
110 PetscViewerPushFormat(viewer, vw_format);
111
113
114 PetscObjectSetName((PetscObject)A_, "Stiffness_matrix");
115 MatView(A_, viewer);
116
117// This preprocessor is only for debugging, e.g. dump the matrix and exit the
118// program.
119// #define EXIT_TEST
120#ifdef EXIT_TEST
121 MatDestroy(A_);
122 PetscFinalize();
123 exit(0);
124#endif
125}
void finalizeAssembly(const MatAssemblyType asm_type=MAT_FINAL_ASSEMBLY)
Perform MPI collection of assembled entries in buffer.
Definition PETScMatrix.h:56
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 145 of file PETScMatrix.cpp.

146{
147 mat.finalizeAssembly(asm_type);
148 return true;
149}

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 234 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 228 of file PETScMatrix.h.

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

◆ n_loc_rows_

PetscInt MathLib::PETScMatrix::n_loc_rows_
private

Number of the local rows.

Definition at line 225 of file PETScMatrix.h.

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

◆ ncols_

PetscInt MathLib::PETScMatrix::ncols_
private

Number of the global columns.

Definition at line 222 of file PETScMatrix.h.

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

◆ nrows_

PetscInt MathLib::PETScMatrix::nrows_
private

Number of the global rows.

Definition at line 219 of file PETScMatrix.h.

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

◆ start_rank_

PetscInt MathLib::PETScMatrix::start_rank_
private

Starting index in a rank.

Definition at line 231 of file PETScMatrix.h.

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


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