Advanced Multi-Physics (AMP)
On-Line Documentation
Matrix.h
Go to the documentation of this file.
1#ifndef included_AMP_Matrix
2#define included_AMP_Matrix
3
4#include "AMP/matrices/MatrixParametersBase.h"
5#include "AMP/matrices/data/MatrixData.h"
6#include "AMP/matrices/operations/MatrixOperations.h"
7#include "AMP/utils/ParameterBase.h"
8#include "AMP/utils/enable_shared_from_this.h"
9#include "AMP/utils/typeid.h"
10#include "AMP/vectors/Vector.h"
11#include <memory>
12
13
14namespace AMP::LinearAlgebra {
15
16
27// commented out for now
28// class Matrix : public AMP::enable_shared_from_this<Matrix>
29class Matrix
30{
31public:
33 using shared_ptr = std::shared_ptr<Matrix>;
34 using const_shared_ptr = std::shared_ptr<const Matrix>;
35
39 explicit Matrix( std::shared_ptr<MatrixParametersBase> params );
40
41
45 explicit Matrix( std::shared_ptr<MatrixData> data );
46
48 virtual ~Matrix();
49
51 virtual std::string type() const = 0;
52
54 virtual std::uint16_t mode() const;
55
58
64 void mult( std::shared_ptr<const Vector> in, std::shared_ptr<Vector> out );
65
71 void multTranspose( std::shared_ptr<const Vector> in, std::shared_ptr<Vector> out );
72
77 void scale( AMP::Scalar alpha );
78
85
92
98 void axpy( AMP::Scalar alpha, const Matrix &X );
99
103 void setScalar( AMP::Scalar alpha );
104
108 void zero();
109
114
118
123
130
137
143 void axpy( AMP::Scalar alpha, std::shared_ptr<const Matrix> X );
144
148 virtual std::shared_ptr<Matrix> transpose() const;
149
153 virtual shared_ptr clone() const = 0;
154
158 virtual void copy( std::shared_ptr<const Matrix> X );
159
163 void copyCast( std::shared_ptr<const Matrix> X );
164
169 virtual Vector::shared_ptr
171
177
184 const bool remove_zeros = false ) const;
185
190
196
197public:
208 template<typename T>
209 void
210 addValuesByGlobalID( size_t num_rows, size_t num_cols, size_t *rows, size_t *cols, T *values )
211 {
212 d_matrixData->addValuesByGlobalID( num_rows, num_cols, rows, cols, values );
213 }
214
225 template<typename T>
227 size_t num_rows, size_t num_cols, const size_t *rows, const size_t *cols, const T *values )
228 {
229 d_matrixData->setValuesByGlobalID( num_rows, num_cols, rows, cols, values );
230 }
231
237 template<typename T = double>
238 void
239 setRowByGlobalID( size_t row, const std::vector<size_t> &cols, const std::vector<T> &values )
240 {
241 AMP_ASSERT( cols.size() == values.size() );
242 d_matrixData->setValuesByGlobalID<T>( 1, cols.size(), &row, cols.data(), values.data() );
243 }
244
254 template<typename T>
256 size_t num_rows, size_t num_cols, const size_t *rows, const size_t *cols, T *values ) const
257 {
258 d_matrixData->getValuesByGlobalID( num_rows, num_cols, rows, cols, values );
259 }
260
266 template<typename T = double>
267 void getRowByGlobalID( size_t row, std::vector<size_t> &cols, std::vector<T> &values ) const
268 {
269 d_matrixData->getRowByGlobalID( row, cols, values );
270 }
271
272
276 size_t numberColumnIDs( size_t row ) const { return d_matrixData->numberColumnIDs( row ); }
277
281 std::vector<size_t> getColumnIDs( size_t row ) const
282 {
283 return d_matrixData->getColumnIDs( row );
284 }
285
290 {
291 return d_matrixData->makeConsistent( t );
292 }
293
297 size_t numLocalRows() const { return d_matrixData->numLocalRows(); }
298
302 size_t numGlobalRows() const { return d_matrixData->numGlobalRows(); }
303
307 size_t numLocalColumns() const { return d_matrixData->numLocalColumns(); }
308
312 size_t numGlobalColumns() const { return d_matrixData->numGlobalColumns(); }
313
317 size_t beginRow() const { return d_matrixData->beginRow(); }
318
322 size_t endRow() const { return d_matrixData->endRow(); }
323
325 AMP_MPI getComm() const { return d_matrixData->getComm(); }
326
331 virtual std::shared_ptr<Discretization::DOFManager> getRightDOFManager() const
332 {
333 return d_matrixData->getRightDOFManager();
334 }
335
340 virtual std::shared_ptr<Discretization::DOFManager> getLeftDOFManager() const
341 {
342 return d_matrixData->getLeftDOFManager();
343 }
344
345public:
354 template<typename T = double>
355 void addValueByGlobalID( size_t row, size_t col, T value )
356 {
357 addValuesByGlobalID( 1, 1, &row, &col, &value );
358 }
359
368 template<typename T = double>
369 void setValueByGlobalID( size_t row, size_t col, T value )
370 {
371 setValuesByGlobalID( 1, 1, &row, &col, &value );
372 }
373
379 template<typename T = double>
380 T getValueByGlobalID( size_t row, size_t col ) const
381 {
382 T ans;
383 getValuesByGlobalID( 1, 1, &row, &col, &ans );
384 return ans;
385 }
386
388 std::shared_ptr<MatrixData> getMatrixData() { return d_matrixData; }
389
391 std::shared_ptr<const MatrixData> getMatrixData() const { return d_matrixData; }
392
395
396public: // Write/read restart data
403
409 virtual void writeRestart( int64_t fid ) const;
410
418
419protected:
422
424 Matrix( const Matrix & );
425
427 Matrix &operator=( const Matrix & ) = delete;
428
429
430protected:
436 virtual void multiply( shared_ptr other_op, shared_ptr &result ) = 0;
437
439 std::shared_ptr<MatrixData> d_matrixData;
440 std::shared_ptr<MatrixOperations> d_matrixOps;
441};
442
443inline std::shared_ptr<Matrix> Matrix::transpose() const
444{
445 AMP_ERROR( "not implemented" );
446 return std::shared_ptr<Matrix>();
447}
448
449std::ostream &operator<<( std::ostream &out, const Matrix &p );
450
451inline std::ostream &operator<<( std::ostream &out, std::shared_ptr<const Matrix> p )
452{
453 return operator<<( out, *p );
454}
455
456} // namespace AMP::LinearAlgebra
457
458#endif
#define X(C)
Provides C++ wrapper around MPI routines.
Definition AMP_MPI.h:63
Class to manage reading/writing restart data.
An abstract interface for using and manipulating matrices.
Definition Matrix.h:30
Vector::shared_ptr getRowSums(Vector::shared_ptr buf=Vector::shared_ptr()) const
Get sum of each row in matrix.
T getValueByGlobalID(size_t row, size_t col) const
Get values in the matrix.
Definition Matrix.h:380
std::vector< size_t > getColumnIDs(size_t row) const
Given a row, retrieve the non-zero column indices of the matrix in compressed format.
Definition Matrix.h:281
virtual ~Matrix()
Destructor.
virtual Vector::shared_ptr createOutputVector() const =0
Get a left vector ( For , is a left vector )
size_t numLocalRows() const
Get the number of local rows in the matrix.
Definition Matrix.h:297
size_t numGlobalRows() const
Get the number of global rows in the matrix.
Definition Matrix.h:302
void scaleInv(AMP::Scalar alpha, Vector::const_shared_ptr D)
Scale matrix by inverse of diagonal matrix.
size_t numLocalColumns() const
Get the number of local columns in the matrix.
Definition Matrix.h:307
virtual void multiply(shared_ptr other_op, shared_ptr &result)=0
Multiply two matrices and store in a third result = this * other_op.
Matrix(const Matrix &)
Protected copy constructor.
void getRowByGlobalID(size_t row, std::vector< size_t > &cols, std::vector< T > &values) const
Retrieve a row of the matrix in compressed format.
Definition Matrix.h:267
virtual std::uint16_t mode() const
Return CSR mode of the matrix.
void setRowByGlobalID(size_t row, const std::vector< size_t > &cols, const std::vector< T > &values)
Set values for a row in the matrix.
Definition Matrix.h:239
virtual void copy(std::shared_ptr< const Matrix > X)
Set this matrix with the same non-zero and distributed structure as x and copy the coefficients.
Matrix & operator=(const Matrix &)=delete
Protected assignment operator.
void mult(std::shared_ptr< const Vector > in, std::shared_ptr< Vector > out)
Matrix-vector multiplication.
static shared_ptr matMatMult(shared_ptr A, shared_ptr B)
Compute the product of two matrices.
virtual std::shared_ptr< Matrix > transpose() const
Return a new matrix that is the transpose of this one.
Definition Matrix.h:443
void makeConsistent(AMP::LinearAlgebra::ScatterType t)
Perform communication to ensure values in the matrix are the same across cores.
Definition Matrix.h:289
virtual std::shared_ptr< Discretization::DOFManager > getRightDOFManager() const
Get the DOFManager associated with a right vector. For , is a right vector.
Definition Matrix.h:331
void setDiagonal(Vector::const_shared_ptr in)
Set the diagonal to the values in a vector.
void setValueByGlobalID(size_t row, size_t col, T value)
Set values in the matrix.
Definition Matrix.h:369
void setValuesByGlobalID(size_t num_rows, size_t num_cols, const size_t *rows, const size_t *cols, const T *values)
Set values in the matrix.
Definition Matrix.h:226
virtual void registerChildObjects(AMP::IO::RestartManager *manager) const
Register any child objects.
virtual std::shared_ptr< Discretization::DOFManager > getLeftDOFManager() const
Get the DOFManager associated with a left vector. For , is a left vector.
Definition Matrix.h:340
size_t numberColumnIDs(size_t row) const
Given a row, retrieve the number of non-zero column indices of the matrix.
Definition Matrix.h:276
void addValueByGlobalID(size_t row, size_t col, T value)
Add values to those in the matrix.
Definition Matrix.h:355
std::shared_ptr< Matrix > shared_ptr
Convenience typedef.
Definition Matrix.h:33
void multTranspose(std::shared_ptr< const Vector > in, std::shared_ptr< Vector > out)
Matrix transpose-vector multiplication.
size_t beginRow() const
Get the global id of the beginning row.
Definition Matrix.h:317
AMP_MPI getComm() const
Get the comm.
Definition Matrix.h:325
AMP::Scalar LinfNorm() const
Compute the maximum row sum.
void setIdentity()
Set the matrix to the identity matrix.
virtual Vector::shared_ptr createInputVector() const =0
Get a right vector ( For , is a right vector )
void scale(AMP::Scalar alpha, Vector::const_shared_ptr D)
Diagonally scale matrix.
virtual std::string type() const =0
Return the type of the matrix.
Matrix(std::shared_ptr< MatrixData > data)
Constructor.
void addValuesByGlobalID(size_t num_rows, size_t num_cols, size_t *rows, size_t *cols, T *values)
Add values to those in the matrix.
Definition Matrix.h:210
void getValuesByGlobalID(size_t num_rows, size_t num_cols, const size_t *rows, const size_t *cols, T *values) const
Get values in the matrix.
Definition Matrix.h:255
static void matMatMult(shared_ptr A, shared_ptr B, shared_ptr C)
Compute the product of two matrices.
std::shared_ptr< MatrixOperations > d_matrixOps
Definition Matrix.h:440
void scale(AMP::Scalar alpha)
Scale the matrix by a scalar.
void copyCast(std::shared_ptr< const Matrix > X)
Set this matrix with the same non-zero and distributed structure as x and copy the coefficients after...
Matrix(std::shared_ptr< MatrixParametersBase > params)
Constructor.
Matrix(int64_t fid, AMP::IO::RestartManager *manager)
Read restart data to file.
void zero()
Set the non-zeros of the matrix to zero.
std::shared_ptr< MatrixData > getMatrixData()
Return the pointer to the MatrixData.
Definition Matrix.h:388
virtual shared_ptr clone() const =0
Return a matrix with the same non-zero and distributed structure.
size_t endRow() const
Get the global id of the ending row.
Definition Matrix.h:322
virtual void setBackend(AMP::Utilities::Backend)
Replace current backend with different one, no-op if same, no-op if not a CSRMatrix.
virtual Vector::shared_ptr extractDiagonal(Vector::shared_ptr buf=Vector::shared_ptr()) const =0
Extract the diagonal from a matrix.
std::shared_ptr< MatrixData > d_matrixData
Pointer to data.
Definition Matrix.h:439
virtual void writeRestart(int64_t fid) const
Write restart data to file.
Vector::shared_ptr getRowSumsAbsolute(Vector::shared_ptr buf=Vector::shared_ptr(), const bool remove_zeros=false) const
Get absolute sum of each row in matrix.
std::shared_ptr< const Matrix > const_shared_ptr
Definition Matrix.h:34
Matrix()
Protected constructor.
void axpy(AMP::Scalar alpha, std::shared_ptr< const Matrix > X)
Compute the linear combination of two matrices.
size_t numGlobalColumns() const
Get the number of global columns in the matrix.
Definition Matrix.h:312
uint64_t getID() const
Get a unique id hash for the matrix.
void axpy(AMP::Scalar alpha, const Matrix &X)
Compute the linear combination of two matrices.
std::shared_ptr< const MatrixData > getMatrixData() const
Return the pointer to the MatrixData.
Definition Matrix.h:391
void setScalar(AMP::Scalar alpha)
Set the non-zeros of the matrix to a scalar.
std::shared_ptr< Vector > shared_ptr
Shorthand for shared pointer to Vector.
Definition Vector.h:60
std::shared_ptr< const Vector > const_shared_ptr
Definition Vector.h:65
Scalar is a class used to store a scalar variable that may be different types/precision.
Definition Scalar.h:21
#define AMP_ASSERT(EXP)
Assert error.
#define AMP_ERROR(MSG)
Throw error.
ScatterType
Flag to choose algorithm for makeConsistent.
Definition Variable.h:21
std::ostream & operator<<(std::ostream &out, const Matrix &p)
Backend
Enum to store the backend used for gpu acceleration.
Definition Backend.h:21



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:40.
Comments on this page