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 MatConvert(A.A_, MATSAME, MAT_INITIAL_MATRIX, &A_);
54}
55
57{
58 nrows_ = A.nrows_;
59 ncols_ = A.ncols_;
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}
78
79void PETScMatrix::setRowsColumnsZero(std::vector<PetscInt> const& row_pos)
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}
104
105void PETScMatrix::viewer(const std::string& file_name,
106 const PetscViewerFormat vw_format)
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}
126
127void PETScMatrix::create(const PetscInt d_nz, const PetscInt o_nz)
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}
144
145bool finalizeMatrixAssembly(PETScMatrix& mat, const MatAssemblyType asm_type)
146{
147 mat.finalizeAssembly(asm_type);
148 return true;
149}
150
151} // 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...