OGS
PETScMatrix.h
Go to the documentation of this file.
1 
15 #pragma once
16 
17 #include <string>
18 #include <vector>
19 
21 #include "PETScMatrixOption.h"
22 #include "PETScVector.h"
23 
24 typedef Mat PETSc_Mat;
25 
26 namespace MathLib
27 {
32 {
33 public:
34  using IndexType = PetscInt;
35 
36 public:
44  PETScMatrix(const PetscInt nrows,
45  const PETScMatrixOption& mat_op = PETScMatrixOption());
46 
54  PETScMatrix(const PetscInt nrows, const PetscInt ncols,
55  const PETScMatrixOption& mat_op = PETScMatrixOption());
56 
58  PETScMatrix(PETScMatrix const& A);
59 
61 
67  void finalizeAssembly(const MatAssemblyType asm_type = MAT_FINAL_ASSEMBLY)
68  {
69  MatAssemblyBegin(A_, asm_type);
70  MatAssemblyEnd(A_, asm_type);
71  }
72 
74  PetscInt getNumberOfRows() const { return nrows_; }
76  PetscInt getNumberOfColumns() const { return ncols_; }
78  PetscInt getNumberOfLocalRows() const { return n_loc_rows_; }
80  PetscInt getNumberOfLocalColumns() const { return n_loc_cols_; }
82  PetscInt getRangeBegin() const { return start_rank_; }
84  PetscInt getRangeEnd() const { return end_rank_; }
86  Mat& getRawMatrix() { return A_; }
93  Mat const& getRawMatrix() const { return A_; }
95  void setZero() { MatZeroEntries(A_); }
105  void setRowsColumnsZero(std::vector<PetscInt> const& row_pos);
106 
113  void set(const PetscInt i, const PetscInt j, const PetscScalar value)
114  {
115  MatSetValue(A_, i, j, value, INSERT_VALUES);
116  }
117 
124  void add(const PetscInt i, const PetscInt j, const PetscScalar value)
125  {
126  MatSetValue(A_, i, j, value, ADD_VALUES);
127  }
128 
145  template <class T_DENSE_MATRIX>
146  void add(RowColumnIndices<PetscInt> const& indices,
147  const T_DENSE_MATRIX& sub_matrix)
148  {
149  // Set global column indices to positive to allow all entries of columns
150  // to be added to the global matrix. For the ghost columns, only the
151  // off diagonal entries are added due to the negative indices of the
152  // corresponding rows.
153  std::vector<PetscInt> cols;
154  cols.reserve(indices.columns.size());
155  for (auto col : indices.columns)
156  {
157  // Ghost entries, and its original index is 0.
158  if (col == -ncols_)
159  cols.push_back(0);
160  else
161  cols.push_back(std::abs(col));
162  }
163 
164  add(indices.rows, cols, sub_matrix);
165  }
166 
174  template <class T_DENSE_MATRIX>
175  void add(std::vector<PetscInt> const& row_pos,
176  std::vector<PetscInt> const& col_pos,
177  const T_DENSE_MATRIX& sub_mat);
178 
208  void viewer(const std::string& file_name,
209  const PetscViewerFormat vw_format = PETSC_VIEWER_ASCII_MATLAB);
210 
211 private:
212  void destroy()
213  {
214  if (A_)
215  MatDestroy(&A_);
216  A_ = nullptr;
217  }
218 
220  Mat A_ = nullptr;
221 
223  PetscInt nrows_;
224 
226  PetscInt ncols_;
227 
229  PetscInt n_loc_rows_;
230 
232  PetscInt n_loc_cols_;
233 
235  PetscInt start_rank_;
236 
238  PetscInt end_rank_;
239 
248  void create(const PetscInt d_nz, const PetscInt o_nz);
249 
250  friend bool finalizeMatrixAssembly(PETScMatrix& mat,
251  const MatAssemblyType asm_type);
252 };
253 
254 template <class T_DENSE_MATRIX>
255 void PETScMatrix::add(std::vector<PetscInt> const& row_pos,
256  std::vector<PetscInt> const& col_pos,
257  const T_DENSE_MATRIX& sub_mat)
258 {
259  const PetscInt nrows = static_cast<PetscInt>(row_pos.size());
260  const PetscInt ncols = static_cast<PetscInt>(col_pos.size());
261 
262  MatSetValues(A_, nrows, &row_pos[0], ncols, &col_pos[0], &sub_mat(0, 0),
263  ADD_VALUES);
264 };
265 
273  PETScMatrix& mat, const MatAssemblyType asm_type = MAT_FINAL_ASSEMBLY);
274 
275 } // namespace MathLib
Define data for the configuration of PETSc matrix and linear solver.
Mat PETSc_Mat
Definition: PETScMatrix.h:24
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.
Definition: PETScMatrix.h:232
PetscInt getNumberOfLocalRows() const
Get the number of local rows.
Definition: PETScMatrix.h:78
PETScMatrix & operator=(PETScMatrix const &A)
Definition: PETScMatrix.cpp:67
PetscInt getNumberOfColumns() const
Get the number of columns.
Definition: PETScMatrix.h:76
void add(RowColumnIndices< PetscInt > const &indices, const T_DENSE_MATRIX &sub_matrix)
Add sub-matrix at positions given by global indices, in which negative index indicates ghost entry.
Definition: PETScMatrix.h:146
void setZero()
Set all entries to zero.
Definition: PETScMatrix.h:95
void create(const PetscInt d_nz, const PetscInt o_nz)
Create the matrix, configure memory allocation and set the related member data.
Mat const & getRawMatrix() const
Definition: PETScMatrix.h:93
PetscInt getRangeEnd() const
Get the end global index of the rows in the same rank.
Definition: PETScMatrix.h:84
void set(const PetscInt i, const PetscInt j, const PetscScalar value)
Set a single entry with a value.
Definition: PETScMatrix.h:113
PetscInt end_rank_
Ending index in a rank.
Definition: PETScMatrix.h:238
Mat & getRawMatrix()
Get matrix reference.
Definition: PETScMatrix.h:86
PetscInt getRangeBegin() const
Get the start global index of the rows of the same rank.
Definition: PETScMatrix.h:82
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)
friend bool finalizeMatrixAssembly(PETScMatrix &mat, const MatAssemblyType asm_type)
General interface for the matrix assembly.
void add(const PetscInt i, const PetscInt j, const PetscScalar value)
Add value to a single entry.
Definition: PETScMatrix.h:124
PetscInt getNumberOfRows() const
Get the number of rows.
Definition: PETScMatrix.h:74
PetscInt start_rank_
Starting index in a rank.
Definition: PETScMatrix.h:235
PetscInt getNumberOfLocalColumns() const
Get the number of local columns.
Definition: PETScMatrix.h:80
bool finalizeMatrixAssembly(MAT_T &)
This a struct data containing the configuration information to create a PETSc type matrix.
LineIndex const & columns