GEOS
HypreMatrix.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef GEOS_LINEARALGEBRA_INTERFACES_HYPREMATRIX_HPP_
21 #define GEOS_LINEARALGEBRA_INTERFACES_HYPREMATRIX_HPP_
22 
23 #include "common/DataTypes.hpp"
29 
37 
39 extern "C" struct hypre_IJMatrix_struct;
40 
42 extern "C" struct hypre_ParCSRMatrix_struct;
43 
45 
46 namespace geos
47 {
48 
55 class HypreMatrix final : public virtual LinearOperator< HypreVector >,
56  private MatrixBase< HypreMatrix, HypreVector >
57 {
58 public:
59 
62 
65 
67  using HYPRE_IJMatrix = hypre_IJMatrix_struct *;
68 
70  using HYPRE_ParCSRMatrix = hypre_ParCSRMatrix_struct *;
71 
76 
83 
88  HypreMatrix( HypreMatrix const & src );
89 
94  HypreMatrix( HypreMatrix && src ) noexcept;
95 
102 
108  HypreMatrix & operator=( HypreMatrix && src ) noexcept;
109 
113  ~HypreMatrix() override;
114 
116 
121 
124  using MatrixBase::closed;
125  using MatrixBase::assembled;
128  using MatrixBase::ready;
129  using MatrixBase::residual;
132  using MatrixBase::create;
133 
134  virtual void create( CRSMatrixView< real64 const, globalIndex const > const & localMatrix,
135  localIndex const numLocalColumns,
136  MPI_Comm const & comm ) override;
137 
138  virtual void createWithLocalSize( localIndex const localRows,
139  localIndex const localCols,
140  localIndex const maxEntriesPerRow,
141  MPI_Comm const & comm ) override;
142 
143  virtual void createWithGlobalSize( globalIndex const globalRows,
144  globalIndex const globalCols,
145  localIndex const maxEntriesPerRow,
146  MPI_Comm const & comm ) override;
147 
148  virtual void open() override;
149 
150  virtual void close() override;
151 
155  virtual bool created() const override;
156 
157  virtual void reset() override;
158 
159  virtual void set( real64 const value ) override;
160 
161  virtual void zero() override;
162 
163  virtual void add( globalIndex const rowIndex,
164  globalIndex const colIndex,
165  real64 const value ) override;
166 
167  virtual void set( globalIndex const rowIndex,
168  globalIndex const colIndex,
169  real64 const value ) override;
170 
171  virtual void insert( globalIndex const rowIndex,
172  globalIndex const colIndex,
173  real64 const value ) override;
174 
175  virtual void add( globalIndex const rowIndex,
176  globalIndex const * colIndices,
177  real64 const * values,
178  localIndex const size ) override;
179 
180  virtual void set( globalIndex const rowIndex,
181  globalIndex const * colIndices,
182  real64 const * values,
183  localIndex const size ) override;
184 
185  virtual void insert( globalIndex const rowIndex,
186  globalIndex const * colIndices,
187  real64 const * values,
188  localIndex const size ) override;
189 
190  virtual void add( globalIndex const rowIndex,
191  arraySlice1d< globalIndex const > const & colIndices,
192  arraySlice1d< real64 const > const & values ) override;
193 
194  virtual void set( globalIndex const rowIndex,
195  arraySlice1d< globalIndex const > const & colIndices,
196  arraySlice1d< real64 const > const & values ) override;
197 
198  virtual void insert( globalIndex const rowIndex,
199  arraySlice1d< globalIndex const > const & colIndices,
200  arraySlice1d< real64 const > const & values ) override;
201 
202  virtual void add( arraySlice1d< globalIndex const > const & rowIndices,
203  arraySlice1d< globalIndex const > const & colIndices,
204  arraySlice2d< real64 const > const & values ) override;
205 
206  virtual void set( arraySlice1d< globalIndex const > const & rowIndices,
207  arraySlice1d< globalIndex const > const & colIndices,
208  arraySlice2d< real64 const > const & values ) override;
209 
210  virtual void insert( arraySlice1d< globalIndex const > const & rowIndices,
211  arraySlice1d< globalIndex const > const & colIndices,
212  arraySlice2d< real64 const > const & values ) override;
213 
214  virtual void add( globalIndex const * rowIndices,
215  globalIndex const * colIndices,
216  real64 const * values,
217  localIndex const numRows,
218  localIndex const numCols ) override;
219 
220  virtual void set( globalIndex const * rowIndices,
221  globalIndex const * colIndices,
222  real64 const * values,
223  localIndex const numRows,
224  localIndex const numCols ) override;
225 
226  virtual void insert( globalIndex const * rowIndices,
227  globalIndex const * colIndices,
228  real64 const * values,
229  localIndex const numRows,
230  localIndex const numCols ) override;
231 
232  virtual void insert( arrayView1d< globalIndex const > const & rowIndices,
233  arrayView1d< globalIndex const > const & colIndices,
234  arrayView1d< real64 const > const & values ) override;
235 
236  virtual void apply( HypreVector const & src,
237  HypreVector & dst ) const override;
238 
239  virtual void applyTranspose( HypreVector const & src,
240  HypreVector & dst ) const override;
241 
242  virtual void multiply( HypreMatrix const & src,
243  HypreMatrix & dst ) const override;
244 
245  virtual void leftMultiplyTranspose( HypreMatrix const & src,
246  HypreMatrix & dst ) const override;
247 
248  virtual void rightMultiplyTranspose( HypreMatrix const & src,
249  HypreMatrix & dst ) const override;
250 
251  virtual void multiplyRAP( HypreMatrix const & R,
252  HypreMatrix const & P,
253  HypreMatrix & dst ) const override;
254 
255  virtual void multiplyPtAP( HypreMatrix const & P,
256  HypreMatrix & dst ) const override;
257 
258  virtual void gemv( real64 const alpha,
259  HypreVector const & x,
260  real64 const beta,
261  HypreVector & y,
262  bool useTranspose = false ) const override;
263 
264  virtual void scale( real64 const scalingFactor ) override;
265 
266  virtual void leftScale( HypreVector const & vec ) override;
267 
268  virtual void rescaleRows( arrayView1d< globalIndex const > const & rowIndices,
269  RowSumType const rowSumType ) override;
270 
271  virtual void rightScale( HypreVector const & vec ) override;
272 
276  virtual void computeScalingVector( HypreVector & scaling ) const override;
277 
282  virtual void leftRightScale( HypreVector const & vecLeft,
283  HypreVector const & vecRight ) override;
284 
285  virtual void transpose( HypreMatrix & dst ) const override;
286 
288  integer const dofPerPoint ) const override;
289 
290  virtual real64 clearRow( globalIndex const row,
291  bool const keepDiag = false,
292  real64 const diagValue = 0.0 ) override;
293 
294  virtual void addEntries( HypreMatrix const & src,
295  MatrixPatternOp const op,
296  real64 const scale ) override;
297 
298  virtual void addDiagonal( HypreVector const & src,
299  real64 const scale ) override;
300 
301  virtual void clampEntries( real64 const lo,
302  real64 const hi,
303  bool const excludeDiag ) override;
304 
308  virtual localIndex maxRowLength() const override;
309 
310  virtual localIndex rowLength( globalIndex const globalRowIndex ) const override;
311 
312  virtual void getRowLengths( arrayView1d< localIndex > const & lengths ) const override;
313 
314  virtual void getRowCopy( globalIndex globalRowIndex,
315  arraySlice1d< globalIndex > const & colIndices,
316  arraySlice1d< real64 > const & values ) const override;
317 
318  virtual void extractDiagonal( HypreVector & dst ) const override;
319 
320  virtual void getRowSums( HypreVector & dst,
321  RowSumType const rowSumType ) const override;
322 
326  virtual globalIndex numGlobalRows() const override;
327 
331  virtual globalIndex numGlobalCols() const override;
332 
336  virtual localIndex numLocalRows() const override;
337 
341  virtual localIndex numLocalCols() const override;
342 
346  virtual globalIndex ilower() const override;
347 
351  virtual globalIndex iupper() const override;
352 
356  virtual globalIndex jlower() const override;
357 
361  virtual globalIndex jupper() const override;
362 
366  virtual localIndex numLocalNonzeros() const override;
367 
371  virtual globalIndex numGlobalNonzeros() const override;
372 
376  virtual real64 normInf() const override;
377 
381  virtual real64 norm1() const override;
382 
386  virtual real64 normFrobenius() const override;
387 
391  virtual real64 normMax() const override;
392 
393  virtual real64 normMax( arrayView1d< globalIndex const > const & rowIndices ) const override;
394 
395  virtual localIndex getLocalRowID( globalIndex const index ) const override;
396 
397  virtual globalIndex getGlobalRowID( localIndex const index ) const override;
398 
402  virtual MPI_Comm comm() const override;
403 
404  virtual void print( std::ostream & os = std::cout ) const override;
405 
406  virtual void write( string const & filename,
407  LAIOutputFormat const format = LAIOutputFormat::MATRIX_MARKET ) const override;
408 
410 
415  HYPRE_ParCSRMatrix const & unwrapped() const;
416 
421  HYPRE_IJMatrix const & unwrappedIJ() const;
422 
423 private:
424 
428  void parCSRtoIJ( HYPRE_ParCSRMatrix const & parCSRMatrix );
429 
433  HYPRE_IJMatrix m_ij_mat{};
434 
438  HYPRE_ParCSRMatrix m_parcsr_mat{};
439 
440 };
441 
442 } // namespace geos
443 
444 #endif /*GEOS_LINEARALGEBRA_INTERFACES_HYPREMATRIX_HPP_*/
Facilitates exporting Hypre matrix and associated vector objects (either in parallel or serial).
Definition: HypreExport.hpp:38
Wrapper class for hypre's ParCSRMatrix.
Definition: HypreMatrix.hpp:57
virtual real64 normFrobenius() const override
Returns the Frobenius norm of the matrix.
virtual void rightMultiplyTranspose(HypreMatrix const &src, HypreMatrix &dst) const override
Matrix/Matrix transpose multiplication.
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 write(string const &filename, LAIOutputFormat const format=LAIOutputFormat::MATRIX_MARKET) const override
Write the matrix to filename in a matlab-compatible format.
virtual void clampEntries(real64 const lo, real64 const hi, bool const excludeDiag) override
Clamp each matrix value between values of lo and hi.
virtual globalIndex numGlobalRows() const override
Get the number of global rows.
virtual localIndex rowLength(globalIndex const globalRowIndex) const override
Get row length via global row index.
virtual void leftScale(HypreVector const &vec) override
Pre-multiplies (left) with diagonal matrix consisting of the values in vec.
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.
hypre_ParCSRMatrix_struct * HYPRE_ParCSRMatrix
ParCSRMatrix pointer alias.
Definition: HypreMatrix.hpp:70
virtual globalIndex jupper() const override
Returns index one past the last global col owned by that processor.
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 void multiply(HypreMatrix const &src, HypreMatrix &dst) const override
Matrix/Matrix multiplication.
virtual void multiplyRAP(HypreMatrix const &R, HypreMatrix const &P, HypreMatrix &dst) const override
Compute the triple product dst = R * this * P
hypre_IJMatrix_struct * HYPRE_IJMatrix
IJMatrix pointer alias.
Definition: HypreMatrix.hpp:67
virtual bool created() const override
Query matrix creation status.
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 add(globalIndex const rowIndex, globalIndex const *colIndices, real64 const *values, localIndex const size) override
Add elements to one row using c-style arrays.
virtual void print(std::ostream &os=std::cout) const override
Print the matrix in Trilinos format to a stream.
HypreMatrix & operator=(HypreMatrix &&src) noexcept
Move assignment.
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 void getRowLengths(arrayView1d< localIndex > const &lengths) const override
Get the row lengths of every local row.
virtual localIndex numLocalNonzeros() const override
Returns the number of nonzeros in the local portion of the matrix.
virtual globalIndex ilower() const override
Returns the index of the first global row owned by that processor.
virtual void apply(HypreVector const &src, HypreVector &dst) const override
Apply operator to a vector, dst = this(src).
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 real64 norm1() const override
Returns the one norm of the matrix.
virtual void reset() override
Reset the matrix to default state.
HYPRE_IJMatrix const & unwrappedIJ() const
Returns a pointer to implementation.
virtual void zero() override
Set all elements to zero.
HypreMatrix(HypreMatrix const &src)
Copy constructor.
virtual void multiplyPtAP(HypreMatrix const &P, HypreMatrix &dst) const override
Compute the triple product dst = P^T * this * P
virtual void leftRightScale(HypreVector const &vecLeft, HypreVector const &vecRight) override
Post-multiplies (right) with diagonal matrix consisting of the values in vecRight and pre-multiplies ...
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 gemv(real64 const alpha, HypreVector const &x, real64 const beta, HypreVector &y, bool useTranspose=false) const override
Compute gemv y = alpha*A*x + beta*y.
virtual real64 normMax() const override
Returns the max norm of the matrix (the largest absolute element value).
virtual real64 normInf() const override
Returns the infinity norm of the matrix.
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 numLocalRows() const override
Get the number of local rows.
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 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 real64 normMax(arrayView1d< globalIndex const > const &rowIndices) const override
Returns the max norm of the matrix on a subset of rows.
virtual globalIndex numGlobalCols() const override
Get the number of global 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.
HypreMatrix(HypreMatrix &&src) noexcept
Move constructor.
virtual void open() override
Open matrix for adding new entries.
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 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 applyTranspose(HypreVector const &src, HypreVector &dst) const override
Apply transpose of the matrix to a vector.
virtual void rightScale(HypreVector const &vec) override
Post-multiplies (right) with diagonal matrix consisting of the values in vec.
virtual void getRowSums(HypreVector &dst, RowSumType const rowSumType) const override
Populate a vector with row sums of this.
HypreMatrix()
Empty matrix constructor.
virtual globalIndex getGlobalRowID(localIndex const index) const override
Map a local row index to global row index.
virtual void separateComponentFilter(HypreMatrix &dst, integer const dofPerPoint) const override
Apply a separate component approximation (filter) to this matrix.
virtual void transpose(HypreMatrix &dst) const override
Matrix transposition.
virtual globalIndex numGlobalNonzeros() const override
Returns the total number of nonzeros in the matrix.
virtual globalIndex jlower() const override
Returns the index of the first global col owned by that processor.
HYPRE_ParCSRMatrix const & unwrapped() const
Returns a pointer to implementation.
~HypreMatrix() override
Virtual destructor.
virtual void set(real64 const value) override
Set all non-zero elements to a value.
virtual void insert(globalIndex const rowIndex, globalIndex const colIndex, real64 const value) override
Insert one element.
HypreMatrix & operator=(HypreMatrix const &src)
Copy assignment.
virtual void addEntries(HypreMatrix const &src, MatrixPatternOp const op, real64 const scale) override
Add entries of another matrix to this.
virtual void addDiagonal(HypreVector const &src, real64 const scale) override
Add (scaled) entries of a vector to the diagonal of this matrix.
virtual void close() override
Assemble and compress the matrix.
virtual void set(globalIndex const rowIndex, globalIndex const colIndex, real64 const value) override
Set one element.
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 extractDiagonal(HypreVector &dst) const override
Extract diagonal values into a vector.
virtual void leftMultiplyTranspose(HypreMatrix const &src, HypreMatrix &dst) const override
Matrix/Matrix transpose multiplication.
virtual void create(CRSMatrixView< real64 const, globalIndex const > const &localMatrix, localIndex const numLocalColumns, MPI_Comm const &comm) override
Create parallel matrix from a local CRS matrix.
virtual localIndex maxRowLength() const override
Returns the number of nonzero entries in the longest row of the matrix.
virtual void scale(real64 const scalingFactor) override
Multiply all elements by scalingFactor.
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 void insert(globalIndex const *rowIndices, globalIndex const *colIndices, real64 const *values, localIndex const numRows, localIndex const numCols) override
Insert dense matrix.
virtual localIndex getLocalRowID(globalIndex const index) const override
Map a global row index to local row index.
virtual void getRowCopy(globalIndex globalRowIndex, arraySlice1d< globalIndex > const &colIndices, arraySlice1d< real64 > const &values) const override
Returns a copy of the data in row globalRow.
virtual MPI_Comm comm() const override
Get the MPI communicator the matrix was created with.
virtual localIndex numLocalCols() const override
Get the number of local columns.
virtual void computeScalingVector(HypreVector &scaling) const override
Compute left and right scaling vectors for diagonal scaling.
virtual void add(globalIndex const rowIndex, globalIndex const colIndex, real64 const value) override
Add to one element.
virtual globalIndex iupper() const override
Returns index one past the last global row owned by that processor.
Wrapper class for hypre's ParVector.
Definition: HypreVector.hpp:51
Abstract base class for linear operators.
Common base template for all matrix wrapper types.
Definition: MatrixBase.hpp:76
virtual void residual(Vector const &x, Vector const &b, Vector &r) const override
Compute residual r = b - A * x.
Definition: MatrixBase.hpp:551
void setDofManager(DofManager const *const dofManager)
Associate a DofManager with this matrix.
Definition: MatrixBase.hpp:157
bool assembled() const
Query matrix assembled status.
Definition: MatrixBase.hpp:108
bool closed() const
Query matrix closed status.
Definition: MatrixBase.hpp:102
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:204
bool insertable() const
Query matrix status.
Definition: MatrixBase.hpp:130
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:186
DofManager const * dofManager() const
Definition: MatrixBase.hpp:165
bool ready() const
Query matrix ready status.
Definition: MatrixBase.hpp:115
bool modifiable() const
Query matrix status.
Definition: MatrixBase.hpp:123
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:248
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:179
RowSumType
Type of row sum to compute.
Definition: MatrixBase.hpp:36
LvArray::CRSMatrixView< T, COL_INDEX, localIndex const, LvArray::ChaiBuffer > CRSMatrixView
Alias for CRS Matrix View.
Definition: DataTypes.hpp:309
GEOS_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:87
LAIOutputFormat
Definition: common.hpp:142
ArraySlice< T, 2, USD > arraySlice2d
Alias for 2D array slice.
Definition: DataTypes.hpp:199
double real64
64-bit floating point type.
Definition: DataTypes.hpp:98
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:84
ArraySlice< T, 1, USD > arraySlice1d
Alias for 1D array slice.
Definition: DataTypes.hpp:183
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:81
MatrixPatternOp
Describes relationship between and treatment of nonzero patterns of arguments in matrix functions lik...
Definition: MatrixBase.hpp:48