OGS
PETScMatrix.cpp
Go to the documentation of this file.
1
17#include "PETScMatrix.h"
18
19#include "PETScVector.h"
20
21namespace MathLib
22{
23PETScMatrix::PETScMatrix(const PetscInt nrows, const PETScMatrixOption& mat_opt)
24 : nrows_(nrows),
25 ncols_(nrows),
26 n_loc_rows_(PETSC_DECIDE),
27 n_loc_cols_(mat_opt.n_local_cols)
28{
29 if (!mat_opt.is_global_size)
30 {
31 n_loc_rows_ = nrows;
32 n_loc_cols_ = nrows;
33 nrows_ = PETSC_DECIDE;
34 ncols_ = PETSC_DECIDE;
35 }
36
37 create(mat_opt.d_nz, mat_opt.o_nz);
38}
39
40PETScMatrix::PETScMatrix(const PetscInt nrows, const PetscInt ncols,
41 const PETScMatrixOption& mat_opt)
42 : nrows_(nrows),
43 ncols_(ncols),
44 n_loc_rows_(PETSC_DECIDE),
45 n_loc_cols_(mat_opt.n_local_cols)
46{
47 if (!mat_opt.is_global_size)
48 {
49 nrows_ = PETSC_DECIDE;
50 ncols_ = PETSC_DECIDE;
51 n_loc_rows_ = nrows;
52 n_loc_cols_ = ncols;
53 }
54
55 create(mat_opt.d_nz, mat_opt.o_nz);
56}
57
59 : nrows_(A.nrows_),
60 ncols_(A.ncols_),
61 n_loc_rows_(A.n_loc_rows_),
62 n_loc_cols_(A.n_loc_cols_),
63 start_rank_(A.start_rank_),
64 end_rank_(A.end_rank_)
65{
66 MatConvert(A.A_, MATSAME, MAT_INITIAL_MATRIX, &A_);
67}
68
70{
71 nrows_ = A.nrows_;
72 ncols_ = A.ncols_;
77
78 if (A_ != nullptr)
79 {
80 // TODO this is the slowest option for copying
81 MatCopy(A.A_, A_, DIFFERENT_NONZERO_PATTERN);
82 }
83 else
84 {
85 destroy();
86 MatConvert(A.A_, MATSAME, MAT_INITIAL_MATRIX, &A_);
87 }
88
89 return *this;
90}
91
92void PETScMatrix::setRowsColumnsZero(std::vector<PetscInt> const& row_pos)
93{
94 // Each rank (compute core) processes only the rows that belong to the rank
95 // itself.
96 const PetscScalar one = 1.0;
97 const PetscInt nrows = static_cast<PetscInt>(row_pos.size());
98
99 // Each process will only zero its own rows.
100 // This avoids all reductions in the zero row routines
101 // and thus improves performance for very large process counts.
102 // See PETSc doc about MAT_NO_OFF_PROC_ZERO_ROWS.
103 MatSetOption(A_, MAT_NO_OFF_PROC_ZERO_ROWS, PETSC_TRUE);
104
105 // Keep the non-zero pattern for the assignment operator.
106 MatSetOption(A_, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
107
108 if (nrows > 0)
109 {
110 MatZeroRows(A_, nrows, &row_pos[0], one, PETSC_NULLPTR, PETSC_NULLPTR);
111 }
112 else
113 {
114 MatZeroRows(A_, 0, PETSC_NULLPTR, one, PETSC_NULLPTR, PETSC_NULLPTR);
115 }
116}
117
118void PETScMatrix::viewer(const std::string& file_name,
119 const PetscViewerFormat vw_format)
120{
121 PetscViewer viewer;
122 PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &viewer);
123 PetscViewerPushFormat(viewer, vw_format);
124
126
127 PetscObjectSetName((PetscObject)A_, "Stiffness_matrix");
128 MatView(A_, viewer);
129
130// This preprocessor is only for debugging, e.g. dump the matrix and exit the
131// program.
132// #define EXIT_TEST
133#ifdef EXIT_TEST
134 MatDestroy(A_);
135 PetscFinalize();
136 exit(0);
137#endif
138}
139
140void PETScMatrix::create(const PetscInt d_nz, const PetscInt o_nz)
141{
142 MatCreate(PETSC_COMM_WORLD, &A_);
143 MatSetSizes(A_, n_loc_rows_, n_loc_cols_, nrows_, ncols_);
144
145 MatSetType(A_, MATAIJ);
146 MatSetFromOptions(A_);
147
148 MatSeqAIJSetPreallocation(A_, d_nz, PETSC_NULLPTR);
149 MatMPIAIJSetPreallocation(A_, d_nz, PETSC_NULLPTR, o_nz, PETSC_NULLPTR);
150 // If pre-allocation does not work one can use MatSetUp(A_), which is much
151 // slower.
152
153 MatGetOwnershipRange(A_, &start_rank_, &end_rank_);
154 MatGetSize(A_, &nrows_, &ncols_);
155 MatGetLocalSize(A_, &n_loc_rows_, &n_loc_cols_);
156}
157
158bool finalizeMatrixAssembly(PETScMatrix& mat, const MatAssemblyType asm_type)
159{
160 mat.finalizeAssembly(asm_type);
161 return true;
162}
163
164} // namespace MathLib
Declaration of class PETScMatrix, which provides an interface to PETSc matrix routines.
Declaration of class PETScVector, which provides an interface to PETSc vector 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.
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...
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.
PetscInt o_nz
Number of nonzeros per row in the off-diagonal portion of local submatrix (same value is used for all...
bool is_global_size
Flag for the type of size, which is one of arguments of the constructor of class PETScMatrix true: th...
PetscInt d_nz
Number of nonzeros per row in the diagonal portion of local submatrix (same value is used for all loc...