OGS
PETScMatrix.cpp
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#include "PETScMatrix.h"
5
6#include "PETScVector.h"
7
8namespace MathLib
9{
10PETScMatrix::PETScMatrix(const PetscInt nrows, const PETScMatrixOption& mat_opt)
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}
26
27PETScMatrix::PETScMatrix(const PetscInt nrows, const PetscInt ncols,
28 const PETScMatrixOption& mat_opt)
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}
44
46 : nrows_(A.nrows_),
47 ncols_(A.ncols_),
52{
53 PetscCallAbort(PETSC_COMM_WORLD,
54 MatConvert(A.A_, MATSAME, MAT_INITIAL_MATRIX, &A_));
55}
56
58{
59 nrows_ = A.nrows_;
60 ncols_ = A.ncols_;
65
66 if (A_ != nullptr)
67 {
68 // TODO this is the slowest option for copying
69 PetscCallAbort(PETSC_COMM_WORLD,
70 MatCopy(A.A_, A_, DIFFERENT_NONZERO_PATTERN));
71 }
72 else
73 {
74 destroy();
75 PetscCallAbort(PETSC_COMM_WORLD,
76 MatConvert(A.A_, MATSAME, MAT_INITIAL_MATRIX, &A_));
77 }
78
79 return *this;
80}
81
82void PETScMatrix::setRowsColumnsZero(std::vector<PetscInt> const& row_pos)
83{
84 // Each rank (compute core) processes only the rows that belong to the rank
85 // itself.
86 const PetscScalar one = 1.0;
87 const PetscInt nrows = static_cast<PetscInt>(row_pos.size());
88
89 // Each process will only zero its own rows.
90 // This avoids all reductions in the zero row routines
91 // and thus improves performance for very large process counts.
92 // See PETSc doc about MAT_NO_OFF_PROC_ZERO_ROWS.
93 PetscCallAbort(PETSC_COMM_WORLD,
94 MatSetOption(A_, MAT_NO_OFF_PROC_ZERO_ROWS, PETSC_TRUE));
95
96 // Keep the non-zero pattern for the assignment operator.
97 PetscCallAbort(PETSC_COMM_WORLD,
98 MatSetOption(A_, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE));
99
100 if (nrows > 0)
101 {
102 PetscCallAbort(PETSC_COMM_WORLD,
103 MatZeroRows(A_, nrows, &row_pos[0], one, PETSC_NULLPTR,
104 PETSC_NULLPTR));
105 }
106 else
107 {
108 PetscCallAbort(PETSC_COMM_WORLD,
109 MatZeroRows(A_, 0, PETSC_NULLPTR, one, PETSC_NULLPTR,
110 PETSC_NULLPTR));
111 }
112}
113
114void PETScMatrix::viewer(const std::string& file_name,
115 const PetscViewerFormat vw_format)
116{
117 PetscViewer viewer;
118 PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &viewer);
119 PetscViewerPushFormat(viewer, vw_format);
120
122
123 PetscObjectSetName((PetscObject)A_, "Stiffness_matrix");
124 MatView(A_, viewer);
125
126// This preprocessor is only for debugging, e.g. dump the matrix and exit the
127// program.
128// #define EXIT_TEST
129#ifdef EXIT_TEST
130 MatDestroy(A_);
131 PetscFinalize();
132 exit(0);
133#endif
134}
135
136void PETScMatrix::create(const PetscInt d_nz, const PetscInt o_nz)
137{
138 PetscCallAbort(PETSC_COMM_WORLD, MatCreate(PETSC_COMM_WORLD, &A_));
139 PetscCallAbort(PETSC_COMM_WORLD,
140 MatSetSizes(A_, n_loc_rows_, n_loc_cols_, nrows_, ncols_));
141
142 PetscCallAbort(PETSC_COMM_WORLD, MatSetType(A_, MATAIJ));
143 PetscCallAbort(PETSC_COMM_WORLD, MatSetFromOptions(A_));
144
145 PetscCallAbort(PETSC_COMM_WORLD,
146 MatSeqAIJSetPreallocation(A_, d_nz, PETSC_NULLPTR));
147 PetscCallAbort(PETSC_COMM_WORLD,
148 MatMPIAIJSetPreallocation(A_, d_nz, PETSC_NULLPTR, o_nz,
149 PETSC_NULLPTR));
150 // If pre-allocation does not work one can use MatSetUp(A_), which is much
151 // slower.
152
153 PetscCallAbort(PETSC_COMM_WORLD,
154 MatGetOwnershipRange(A_, &start_rank_, &end_rank_));
155 PetscCallAbort(PETSC_COMM_WORLD, MatGetSize(A_, &nrows_, &ncols_));
156 PetscCallAbort(PETSC_COMM_WORLD,
157 MatGetLocalSize(A_, &n_loc_rows_, &n_loc_cols_));
158}
159
160bool finalizeMatrixAssembly(PETScMatrix& mat, const MatAssemblyType asm_type)
161{
162 mat.finalizeAssembly(asm_type);
163 return true;
164}
165
166} // namespace MathLib
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:56
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...