OGS
PETScMatrix.cpp
Go to the documentation of this file.
1 
17 #include "PETScMatrix.h"
18 
19 namespace MathLib
20 {
21 PETScMatrix::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;
31  nrows_ = PETSC_DECIDE;
32  ncols_ = PETSC_DECIDE;
33  }
34 
35  create(mat_opt.d_nz, mat_opt.o_nz);
36 }
37 
38 PETScMatrix::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  {
47  nrows_ = PETSC_DECIDE;
48  ncols_ = PETSC_DECIDE;
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 {
64  MatConvert(A.A_, MATSAME, MAT_INITIAL_MATRIX, &A_);
65 }
66 
68 {
69  nrows_ = A.nrows_;
70  ncols_ = A.ncols_;
74  end_rank_ = A.end_rank_;
75 
76  if (A_)
77  {
78  // TODO this is the slowest option for copying
79  MatCopy(A.A_, A_, DIFFERENT_NONZERO_PATTERN);
80  }
81  else
82  {
83  destroy();
84  MatConvert(A.A_, MATSAME, MAT_INITIAL_MATRIX, &A_);
85  }
86 
87  return *this;
88 }
89 
90 void 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.
101  MatSetOption(A_, MAT_NO_OFF_PROC_ZERO_ROWS, PETSC_TRUE);
102 
103  // Keep the non-zero pattern for the assignment operator.
104  MatSetOption(A_, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE);
105 
106  if (nrows > 0)
107  MatZeroRows(A_, nrows, &row_pos[0], one, PETSC_NULL, PETSC_NULL);
108  else
109  MatZeroRows(A_, 0, PETSC_NULL, one, PETSC_NULL, PETSC_NULL);
110 }
111 
112 void PETScMatrix::viewer(const std::string& file_name,
113  const PetscViewerFormat vw_format)
114 {
115  PetscViewer viewer;
116  PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &viewer);
117  PetscViewerPushFormat(viewer, vw_format);
118 
120 
121  PetscObjectSetName((PetscObject)A_, "Stiffness_matrix");
122  MatView(A_, viewer);
123 
124 // This preprocessor is only for debugging, e.g. dump the matrix and exit the
125 // program.
126 //#define EXIT_TEST
127 #ifdef EXIT_TEST
128  MatDestroy(A_);
129  PetscFinalize();
130  exit(0);
131 #endif
132 }
133 
134 void PETScMatrix::create(const PetscInt d_nz, const PetscInt o_nz)
135 {
136  MatCreate(PETSC_COMM_WORLD, &A_);
137  MatSetSizes(A_, n_loc_rows_, n_loc_cols_, nrows_, ncols_);
138 
139  MatSetType(A_, MATAIJ);
140  MatSetFromOptions(A_);
141 
142  MatSeqAIJSetPreallocation(A_, d_nz, PETSC_NULL);
143  MatMPIAIJSetPreallocation(A_, d_nz, PETSC_NULL, o_nz, PETSC_NULL);
144  // If pre-allocation does not work one can use MatSetUp(A_), which is much
145  // slower.
146 
147  MatGetOwnershipRange(A_, &start_rank_, &end_rank_);
148  MatGetSize(A_, &nrows_, &ncols_);
149  MatGetLocalSize(A_, &n_loc_rows_, &n_loc_cols_);
150 }
151 
152 bool finalizeMatrixAssembly(PETScMatrix& mat, const MatAssemblyType asm_type)
153 {
154  mat.finalizeAssembly(asm_type);
155  return true;
156 }
157 
158 } // 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.
Definition: PETScMatrix.h:232
PETScMatrix & operator=(PETScMatrix const &A)
Definition: PETScMatrix.cpp:67
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.
Definition: PETScMatrix.h:238
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.
Definition: PETScMatrix.h:229
Mat A_
PETSc matrix.
Definition: PETScMatrix.h:220
PetscInt nrows_
Number of the global rows.
Definition: PETScMatrix.h:223
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 by...
Definition: PETScMatrix.cpp:90
PetscInt ncols_
Number of the global columns.
Definition: PETScMatrix.h:226
void viewer(const std::string &file_name, const PetscViewerFormat vw_format=PETSC_VIEWER_ASCII_MATLAB)
PetscInt start_rank_
Starting index in a rank.
Definition: PETScMatrix.h:235
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...