GEOSX
PetscMatrix.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOS_LINEARALGEBRA_INTERFACES_PETSCSPARSEMATRIX_HPP_
20 #define GEOS_LINEARALGEBRA_INTERFACES_PETSCSPARSEMATRIX_HPP_
21 
22 #include "common/DataTypes.hpp"
27 
35 
37 extern "C" struct _p_Mat;
38 
40 
41 namespace geos
42 {
43 
48 class PetscMatrix final : public virtual LinearOperator< PetscVector >,
49  private MatrixBase< PetscMatrix, PetscVector >
50 {
51 public:
52 
55 
58 
60  using Mat = struct _p_Mat *;
61 
66 
71 
76  PetscMatrix( PetscMatrix const & src );
77 
82  PetscMatrix( PetscMatrix && src ) noexcept;
83 
89  PetscMatrix & operator=( PetscMatrix const & src );
90 
96  PetscMatrix & operator=( PetscMatrix && src ) noexcept;
97 
101  ~PetscMatrix() override;
102 
104 
109 
112  using MatrixBase::create;
113  using MatrixBase::closed;
114  using MatrixBase::assembled;
117  using MatrixBase::ready;
118  using MatrixBase::residual;
121 
122  virtual void createWithLocalSize( localIndex const localRows,
123  localIndex const localCols,
124  localIndex const maxEntriesPerRow,
125  MPI_Comm const & comm ) override;
126 
127  virtual void createWithGlobalSize( globalIndex const globalRows,
128  globalIndex const globalCols,
129  localIndex const maxEntriesPerRow,
130  MPI_Comm const & comm ) override;
131 
135  virtual bool created() const override;
136 
137  virtual void reset() override;
138 
139  virtual void set( real64 const value ) override;
140 
141  virtual void zero() override;
142 
143  virtual void open() override;
144 
145  virtual void close() override;
146 
147  virtual void add( globalIndex const rowIndex,
148  globalIndex const colIndex,
149  real64 const value ) override;
150 
151  virtual void set( globalIndex const rowIndex,
152  globalIndex const colIndex,
153  real64 const value ) override;
154 
155  virtual void insert( globalIndex const rowIndex,
156  globalIndex const colIndex,
157  real64 const value ) override;
158 
159  virtual void add( globalIndex const rowIndex,
160  globalIndex const * colIndices,
161  real64 const * values,
162  localIndex const size ) override;
163 
164  virtual void set( globalIndex const rowIndex,
165  globalIndex const * colIndices,
166  real64 const * values,
167  localIndex const size ) override;
168 
169  virtual void insert( globalIndex const rowIndex,
170  globalIndex const * colIndices,
171  real64 const * values,
172  localIndex const size ) override;
173 
174  virtual void add( globalIndex const rowIndex,
175  arraySlice1d< globalIndex const > const & colIndices,
176  arraySlice1d< real64 const > const & values ) override;
177 
178  virtual void set( globalIndex const rowIndex,
179  arraySlice1d< globalIndex const > const & colIndices,
180  arraySlice1d< real64 const > const & values ) override;
181 
182  virtual void insert( globalIndex const rowIndex,
183  arraySlice1d< globalIndex const > const & colIndices,
184  arraySlice1d< real64 const > const & values ) override;
185 
186  virtual void add( arraySlice1d< globalIndex const > const & rowIndices,
187  arraySlice1d< globalIndex const > const & colIndices,
188  arraySlice2d< real64 const > const & values ) override;
189 
190  virtual void set( arraySlice1d< globalIndex const > const & rowIndices,
191  arraySlice1d< globalIndex const > const & colIndices,
192  arraySlice2d< real64 const > const & values ) override;
193 
194  virtual void insert( arraySlice1d< globalIndex const > const & rowIndices,
195  arraySlice1d< globalIndex const > const & colIndices,
196  arraySlice2d< real64 const > const & values ) override;
197 
198  virtual void add( globalIndex const * rowIndices,
199  globalIndex const * colIndices,
200  real64 const * values,
201  localIndex const numRows,
202  localIndex const numCols ) override;
203 
204  virtual void set( globalIndex const * rowIndices,
205  globalIndex const * colIndices,
206  real64 const * values,
207  localIndex const numRows,
208  localIndex const numCols ) override;
209 
210  virtual void insert( globalIndex const * rowIndices,
211  globalIndex const * colIndices,
212  real64 const * values,
213  localIndex const numRows,
214  localIndex const numCols ) override;
215 
216  virtual void insert( arrayView1d< globalIndex const > const & rowIndices,
217  arrayView1d< globalIndex const > const & colIndices,
218  arrayView1d< real64 const > const & values ) override;
219 
220  virtual void apply( PetscVector const & src,
221  PetscVector & dst ) const override;
222 
223  virtual void multiply( PetscMatrix const & src,
224  PetscMatrix & dst ) const override;
225 
226  virtual void applyTranspose( Vector const & src,
227  Vector & dst ) const override;
228 
229  virtual void leftMultiplyTranspose( PetscMatrix const & src,
230  PetscMatrix & dst ) const override;
231 
232  virtual void rightMultiplyTranspose( PetscMatrix const & src,
233  PetscMatrix & dst ) const override;
234 
235  virtual void multiplyRAP( PetscMatrix const & R,
236  PetscMatrix const & P,
237  PetscMatrix & dst ) const override;
238 
239  virtual void multiplyPtAP( PetscMatrix const & P,
240  PetscMatrix & dst ) const override;
241 
242  virtual void gemv( real64 const alpha,
243  PetscVector const & x,
244  real64 const beta,
245  PetscVector & y,
246  bool useTranspose = false ) const override;
247 
248  virtual void scale( real64 const scalingFactor ) override;
249 
250  virtual void leftScale( PetscVector const & vec ) override;
251 
252  virtual void rightScale( PetscVector const & vec ) override;
253 
254  virtual void leftRightScale( PetscVector const & vecLeft,
255  PetscVector const & vecRight ) override;
256 
257  virtual void rescaleRows( arrayView1d< globalIndex const > const & rowIndices,
258  RowSumType const rowSumType ) override;
259 
260  virtual void transpose( PetscMatrix & dst ) const override;
261 
263  integer const dofsPerNode ) const override;
264 
265  virtual real64 clearRow( globalIndex const row,
266  bool const keepDiag = false,
267  real64 const diagValue = 0.0 ) override;
268 
269  virtual void addEntries( PetscMatrix const & src,
270  MatrixPatternOp const op,
271  real64 const scale = 1.0 ) override;
272 
273  virtual void addDiagonal( PetscVector const & src,
274  real64 const scale ) override;
275 
276  virtual void clampEntries( real64 const lo,
277  real64 const hi,
278  bool const excludeDiag ) override;
279 
283  virtual localIndex maxRowLength() const override;
284 
285  virtual localIndex rowLength( globalIndex const globalRowIndex ) const override;
286 
287  virtual void getRowLengths( arrayView1d< localIndex > const & lengths ) const override;
288 
289  virtual void extractDiagonal( PetscVector & dst ) const override;
290 
291  virtual void getRowSums( PetscVector & dst,
292  RowSumType const rowSumType ) const override;
293 
294  virtual void getRowCopy( globalIndex globalRow,
295  arraySlice1d< globalIndex > const & colIndices,
296  arraySlice1d< real64 > const & values ) const override;
297 
301  virtual globalIndex numGlobalRows() const override;
302 
306  virtual globalIndex numGlobalCols() const override;
307 
311  virtual localIndex numLocalRows() const override;
312 
316  virtual localIndex numLocalCols() const override;
317 
321  virtual globalIndex ilower() const override;
322 
326  virtual globalIndex iupper() const override;
327 
331  virtual globalIndex jlower() const override;
332 
336  virtual globalIndex jupper() const override;
337 
341  virtual localIndex numLocalNonzeros() const override;
342 
346  virtual globalIndex numGlobalNonzeros() const override;
347 
351  virtual real64 normInf() const override;
352 
356  virtual real64 norm1() const override;
357 
361  virtual real64 normFrobenius() const override;
362 
366  virtual real64 normMax() const override;
367 
368  virtual real64 normMax( arrayView1d< globalIndex const > const & m ) const override;
369 
370  virtual localIndex getLocalRowID( globalIndex const index ) const override;
371 
372  virtual globalIndex getGlobalRowID( localIndex const index ) const override;
373 
377  virtual MPI_Comm comm() const override;
378 
379  virtual void print( std::ostream & os = std::cout ) const override;
380 
381  virtual void write( string const & filename,
382  LAIOutputFormat const format = LAIOutputFormat::MATRIX_MARKET ) const override;
383 
385 
390  const Mat & unwrapped() const;
391 
397 
398 private:
399 
401  Mat m_mat{};
402 
404  array1d< globalIndex > m_rowsToClear;
405 
407  array1d< real64 > m_diagValues;
408 
409 };
410 
411 } // namespace geos
412 
413 #endif /*GEOS_LINEARALGEBRA_INTERFACES_PETSCSPARSEMATRIX_HPP_*/
Abstract base class for linear operators.
Common base template for all matrix wrapper types.
Definition: MatrixBase.hpp:75
virtual void residual(Vector const &x, Vector const &b, Vector &r) const override
Compute residual r = b - A * x.
Definition: MatrixBase.hpp:550
void setDofManager(DofManager const *const dofManager)
Associate a DofManager with this matrix.
Definition: MatrixBase.hpp:156
bool assembled() const
Query matrix assembled status.
Definition: MatrixBase.hpp:107
bool closed() const
Query matrix closed status.
Definition: MatrixBase.hpp:101
virtual void createWithGlobalSize(globalIndex const globalSize, localIndex const maxEntriesPerRow, MPI_Comm const &comm)
Create a square matrix from global number of rows.
Definition: MatrixBase.hpp:203
bool insertable() const
Query matrix status.
Definition: MatrixBase.hpp:129
virtual void createWithLocalSize(localIndex const localSize, localIndex const maxEntriesPerRow, MPI_Comm const &comm)
Create a square matrix from local number of rows.
Definition: MatrixBase.hpp:185
DofManager const * dofManager() const
Definition: MatrixBase.hpp:164
bool ready() const
Query matrix ready status.
Definition: MatrixBase.hpp:114
bool modifiable() const
Query matrix status.
Definition: MatrixBase.hpp:122
virtual void create(CRSMatrixView< real64 const, globalIndex const > const &localMatrix, localIndex const numLocalColumns, MPI_Comm const &comm)
Create parallel matrix from a local CRS matrix.
Definition: MatrixBase.hpp:247
Facilitates exporting Petsc matrix and associated vector objects (either in parallel or serial).
Definition: PetscExport.hpp:43
This class creates and provides basic support for the Mat matrix object type used in PETSc.
Definition: PetscMatrix.hpp:50
virtual localIndex numLocalCols() const override
Get the number of local columns.
virtual void set(globalIndex const *rowIndices, globalIndex const *colIndices, real64 const *values, localIndex const numRows, localIndex const numCols) override
Set a dense block of values.
virtual globalIndex numGlobalNonzeros() const override
Returns the total number of nonzeros in the matrix.
virtual localIndex numLocalRows() const override
Get the number of local rows.
PetscMatrix & operator=(PetscMatrix const &src)
Copy assignment.
virtual globalIndex numGlobalRows() const override
Get the number of global rows.
struct _p_Mat * Mat
Alias for PETSc matrix struct pointer.
Definition: PetscMatrix.hpp:60
virtual void clampEntries(real64 const lo, real64 const hi, bool const excludeDiag) override
Clamp each matrix value between values of lo and hi.
PetscMatrix(PetscMatrix const &src)
Copy constructor.
virtual real64 normMax(arrayView1d< globalIndex const > const &m) const override
Returns the max norm of the matrix on a subset of rows.
virtual void transpose(PetscMatrix &dst) const override
Matrix transposition.
virtual void set(globalIndex const rowIndex, globalIndex const *colIndices, real64 const *values, localIndex const size) override
Set elements to one row using c-style arrays.
virtual void insert(globalIndex const rowIndex, arraySlice1d< globalIndex const > const &colIndices, arraySlice1d< real64 const > const &values) override
Insert elements of one row using array1d.
virtual void zero() override
Set all elements to zero.
virtual globalIndex numGlobalCols() const override
Get the number of global columns.
Mat & unwrapped()
Returns a non-const pointer to the underlying matrix.
virtual void scale(real64 const scalingFactor) override
Multiply all elements by scalingFactor.
virtual void close() override
Assemble and compress the matrix.
PetscMatrix()
Empty matrix constructor.
virtual void leftMultiplyTranspose(PetscMatrix const &src, PetscMatrix &dst) const override
Matrix/Matrix transpose multiplication.
virtual globalIndex jupper() const override
Returns index one past the last global col owned by that processor.
virtual void insert(globalIndex const rowIndex, globalIndex const *colIndices, real64 const *values, localIndex const size) override
Insert elements to one row using c-style arrays.
virtual void getRowSums(PetscVector &dst, RowSumType const rowSumType) const override
Populate a vector with row sums of this.
virtual void rescaleRows(arrayView1d< globalIndex const > const &rowIndices, RowSumType const rowSumType) override
Rescales selected rows of matrix using row sum reciprocal as a factor.
virtual void add(globalIndex const *rowIndices, globalIndex const *colIndices, real64 const *values, localIndex const numRows, localIndex const numCols) override
Add a dense block of values.
virtual void apply(PetscVector const &src, PetscVector &dst) const override
Apply operator to a vector, dst = this(src).
virtual void add(globalIndex const rowIndex, arraySlice1d< globalIndex const > const &colIndices, arraySlice1d< real64 const > const &values) override
Add elements to one row using array1d.
virtual localIndex maxRowLength() const override
Returns the number of nonzero entries in the longest row of the matrix.
virtual void set(globalIndex const rowIndex, arraySlice1d< globalIndex const > const &colIndices, arraySlice1d< real64 const > const &values) override
Set elements of one row using array1d.
virtual void open() override
Open matrix for adding new entries.
virtual void getRowLengths(arrayView1d< localIndex > const &lengths) const override
Get the row lengths of every local row.
virtual void set(arraySlice1d< globalIndex const > const &rowIndices, arraySlice1d< globalIndex const > const &colIndices, arraySlice2d< real64 const > const &values) override
Set a dense block of values.
virtual void leftScale(PetscVector const &vec) override
Pre-multiplies (left) with diagonal matrix consisting of the values in vec.
PetscMatrix(PetscMatrix &&src) noexcept
Move constructor.
virtual void createWithLocalSize(localIndex const localRows, localIndex const localCols, localIndex const maxEntriesPerRow, MPI_Comm const &comm) override
Create a rectangular matrix from number of rows/columns.
virtual void print(std::ostream &os=std::cout) const override
Print the matrix in Trilinos format to a stream.
virtual void separateComponentFilter(PetscMatrix &dst, integer const dofsPerNode) const override
Apply a separate component approximation (filter) to this matrix.
virtual void rightScale(PetscVector const &vec) override
Post-multiplies (right) with diagonal matrix consisting of the values in vec.
virtual MPI_Comm comm() const override
Get the MPI communicator the matrix was created with.
virtual real64 clearRow(globalIndex const row, bool const keepDiag=false, real64 const diagValue=0.0) override
Clear a row, and optionally set diagonal element to diagValue.
virtual void multiplyPtAP(PetscMatrix const &P, PetscMatrix &dst) const override
Compute the triple product dst = P^T * this * P
virtual real64 normFrobenius() const override
Returns the Frobenius norm of the matrix.
virtual void leftRightScale(PetscVector const &vecLeft, PetscVector const &vecRight) override
Post-multiplies (right) with diagonal matrix consisting of the values in vecRight and pre-multiplies ...
virtual void insert(globalIndex const *rowIndices, globalIndex const *colIndices, real64 const *values, localIndex const numRows, localIndex const numCols) override
Insert dense matrix.
virtual void insert(arrayView1d< globalIndex const > const &rowIndices, arrayView1d< globalIndex const > const &colIndices, arrayView1d< real64 const > const &values) override
Insert values stored in 3 linear vectors.
virtual localIndex rowLength(globalIndex const globalRowIndex) const override
Get row length via global row index.
virtual void multiplyRAP(PetscMatrix const &R, PetscMatrix const &P, PetscMatrix &dst) const override
Compute the triple product dst = R * this * P
~PetscMatrix() override
Destructor.
virtual void add(globalIndex const rowIndex, globalIndex const colIndex, real64 const value) override
Add to one element.
virtual globalIndex ilower() const override
Returns the index of the first global row owned by that processor.
virtual real64 normMax() const override
Returns the max norm of the matrix (the largest absolute element value).
virtual void getRowCopy(globalIndex globalRow, arraySlice1d< globalIndex > const &colIndices, arraySlice1d< real64 > const &values) const override
Returns a copy of the data in row globalRow.
virtual globalIndex jlower() const override
Returns the index of the first global col owned by that processor.
const Mat & unwrapped() const
Returns a const pointer to the underlying matrix.
virtual void applyTranspose(Vector const &src, Vector &dst) const override
Apply transpose of the matrix to a vector.
virtual void insert(globalIndex const rowIndex, globalIndex const colIndex, real64 const value) override
Insert one element.
virtual void add(arraySlice1d< globalIndex const > const &rowIndices, arraySlice1d< globalIndex const > const &colIndices, arraySlice2d< real64 const > const &values) override
Add a dense block of values.
virtual real64 normInf() const override
Returns the infinity norm of the matrix.
virtual void createWithGlobalSize(globalIndex const globalRows, globalIndex const globalCols, localIndex const maxEntriesPerRow, MPI_Comm const &comm) override
Create a rectangular matrix from number of rows/columns.
virtual globalIndex iupper() const override
Returns index one past the last global row owned by that processor.
virtual bool created() const override
Get the number of global rows.
virtual void insert(arraySlice1d< globalIndex const > const &rowIndices, arraySlice1d< globalIndex const > const &colIndices, arraySlice2d< real64 const > const &values) override
Insert a dense block of values.
virtual real64 norm1() const override
Returns the one norm of the matrix.
virtual void gemv(real64 const alpha, PetscVector const &x, real64 const beta, PetscVector &y, bool useTranspose=false) const override
Compute gemv y = alpha*A*x + beta*y.
virtual void rightMultiplyTranspose(PetscMatrix const &src, PetscMatrix &dst) const override
Matrix/Matrix transpose multiplication.
virtual localIndex getLocalRowID(globalIndex const index) const override
Map a global row index to local row index.
virtual void addEntries(PetscMatrix const &src, MatrixPatternOp const op, real64 const scale=1.0) override
Add entries of another matrix to this.
virtual localIndex numLocalNonzeros() const override
Returns the number of nonzeros in the local portion of the matrix.
virtual void set(globalIndex const rowIndex, globalIndex const colIndex, real64 const value) override
Set one element.
virtual void add(globalIndex const rowIndex, globalIndex const *colIndices, real64 const *values, localIndex const size) override
Add elements to one row using c-style arrays.
virtual globalIndex getGlobalRowID(localIndex const index) const override
Map a local row index to global row index.
virtual void write(string const &filename, LAIOutputFormat const format=LAIOutputFormat::MATRIX_MARKET) const override
Write the matrix to filename in a matlab-compatible format.
PetscMatrix & operator=(PetscMatrix &&src) noexcept
Move assignment.
virtual void multiply(PetscMatrix const &src, PetscMatrix &dst) const override
Matrix/Matrix multiplication.
virtual void addDiagonal(PetscVector const &src, real64 const scale) override
Add (scaled) entries of a vector to the diagonal of this matrix.
virtual void reset() override
Reset the matrix to default state.
virtual void set(real64 const value) override
Set all non-zero elements to a value.
virtual void extractDiagonal(PetscVector &dst) const override
Extract diagonal values into a vector.
This class creates and provides basic support for Vec vector object type used in PETSc.
Definition: PetscVector.hpp:45
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:220
RowSumType
Type of row sum to compute.
Definition: MatrixBase.hpp:35
LAIOutputFormat
Definition: common.hpp:140
ArraySlice< T, 2, USD > arraySlice2d
Alias for 2D array slice.
Definition: DataTypes.hpp:240
double real64
64-bit floating point type.
Definition: DataTypes.hpp:139
ArraySlice< T, 1, USD > arraySlice1d
Alias for 1D array slice.
Definition: DataTypes.hpp:224
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:122
GEOSX_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:128
GEOSX_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:216
MatrixPatternOp
Describes relationship between and treatment of nonzero patterns of arguments in matrix functions lik...
Definition: MatrixBase.hpp:47