OGS
PETScMatrix.cpp
Go to the documentation of this file.
1
17#include "PETScMatrix.h"
18
19namespace MathLib
20{
21PETScMatrix::PETScMatrix(const PetscInt nrows, const PETScMatrixOption& mat_opt)
22 : nrows_(nrows),
23 ncols_(nrows),
24 n_loc_rows_(PETSC_DECIDE),
25 n_loc_cols_(mat_opt.n_local_cols)
26{
27 if (!mat_opt.is_global_size)
28 {
29 n_loc_rows_ = nrows;
30 n_loc_cols_ = nrows;
33 }
34
35 create(mat_opt.d_nz, mat_opt.o_nz);
36}
37
38PETScMatrix::PETScMatrix(const PetscInt nrows, const PetscInt ncols,
39 const PETScMatrixOption& mat_opt)
40 : nrows_(nrows),
41 ncols_(ncols),
42 n_loc_rows_(PETSC_DECIDE),
43 n_loc_cols_(mat_opt.n_local_cols)
44{
45 if (!mat_opt.is_global_size)
46 {
49 n_loc_rows_ = nrows;
50 n_loc_cols_ = ncols;
51 }
52
53 create(mat_opt.d_nz, mat_opt.o_nz);
54}
55
57 : nrows_(A.nrows_),
58 ncols_(A.ncols_),
59 n_loc_rows_(A.n_loc_rows_),
60 n_loc_cols_(A.n_loc_cols_),
61 start_rank_(A.start_rank_),
62 end_rank_(A.end_rank_)
63{
65}
66
68{
69 nrows_ = A.nrows_;
70 ncols_ = A.ncols_;
75
76 if (A_ != nullptr)
77 {
78 // TODO this is the slowest option for copying
80 }
81 else
82 {
83 destroy();
85 }
86
87 return *this;
88}
89
90void PETScMatrix::setRowsColumnsZero(std::vector<PetscInt> const& row_pos)
91{
92 // Each rank (compute core) processes only the rows that belong to the rank
93 // itself.
94 const PetscScalar one = 1.0;
95 const PetscInt nrows = static_cast<PetscInt>(row_pos.size());
96
97 // Each process will only zero its own rows.
98 // This avoids all reductions in the zero row routines
99 // and thus improves performance for very large process counts.
100 // See PETSc doc about MAT_NO_OFF_PROC_ZERO_ROWS.
102
103 // Keep the non-zero pattern for the assignment operator.
105
106 if (nrows > 0)
107 {
109 }
110 else
111 {
113 }
114}
115
116void PETScMatrix::viewer(const std::string& file_name,
117 const PetscViewerFormat vw_format)
118{
122
124
125 PetscObjectSetName((PetscObject)A_, "Stiffness_matrix");
126 MatView(A_, viewer);
127
128// This preprocessor is only for debugging, e.g. dump the matrix and exit the
129// program.
130// #define EXIT_TEST
131#ifdef EXIT_TEST
132 MatDestroy(A_);
134 exit(0);
135#endif
136}
137
138void PETScMatrix::create(const PetscInt d_nz, const PetscInt o_nz)
139{
142
145
148 // If pre-allocation does not work one can use MatSetUp(A_), which is much
149 // slower.
150
154}
155
156bool finalizeMatrixAssembly(PETScMatrix& mat, const MatAssemblyType asm_type)
157{
158 mat.finalizeAssembly(asm_type);
159 return true;
160}
161
162} // namespace MathLib
Declaration of class PETScMatrix, which provides an interface to PETSc matrix routines.
Wrapper class for PETSc matrix routines for matrix.
Definition PETScMatrix.h:32
PetscInt n_loc_cols_
Number of the local columns.
PETScMatrix & operator=(PETScMatrix const &A)
void create(const PetscInt d_nz, const PetscInt o_nz)
Create the matrix, configure memory allocation and set the related member data.
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.
void finalizeAssembly(const MatAssemblyType asm_type=MAT_FINAL_ASSEMBLY)
Perform MPI collection of assembled entries in buffer.
Definition PETScMatrix.h:67
PetscInt n_loc_rows_
Number of the local rows.
Mat A_
PETSc matrix.
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 b...
PetscInt ncols_
Number of the global columns.
void viewer(const std::string &file_name, const PetscViewerFormat vw_format=PETSC_VIEWER_ASCII_MATLAB)
PetscInt start_rank_
Starting index in a rank.
bool finalizeMatrixAssembly(MAT_T &)
This a struct data containing the configuration information to create a PETSc type matrix.