Advanced Multi-Physics (AMP)
On-Line Documentation
CSRMatrixData.h
Go to the documentation of this file.
1#ifndef included_AMP_CSRMatrixData_h
2#define included_AMP_CSRMatrixData_h
3
4#include "AMP/matrices/MatrixParametersBase.h"
5#include "AMP/matrices/RawCSRMatrixParameters.h"
6#include "AMP/matrices/data/CSRLocalMatrixData.h"
7#include "AMP/matrices/data/MatrixData.h"
8#include "AMP/utils/Memory.h"
9#include "AMP/utils/Utilities.h"
10
11#include <functional>
12#include <map>
13#include <tuple>
14#include <vector>
15
16namespace AMP::IO {
17class RestartManager;
18}
19
20namespace AMP::Discretization {
21class DOFManager;
22}
23
24namespace AMP::LinearAlgebra {
25
26template<typename C>
27class CSRMatrixSpGEMMDefault;
28
29template<typename Config>
31{
32public:
33 template<typename C>
35 template<typename C>
36 friend class CSRMatrixData;
37
38 using gidx_t = typename Config::gidx_t;
39 using lidx_t = typename Config::lidx_t;
40 using scalar_t = typename Config::scalar_t;
41 using allocator_type = typename Config::allocator_type;
42 static_assert( std::is_same_v<typename allocator_type::value_type, void> );
44 typename std::allocator_traits<allocator_type>::template rebind_alloc<gidx_t>;
46 typename std::allocator_traits<allocator_type>::template rebind_alloc<lidx_t>;
48 typename std::allocator_traits<allocator_type>::template rebind_alloc<scalar_t>;
51
55 explicit CSRMatrixData( std::shared_ptr<MatrixParametersBase> params );
56
58 virtual ~CSRMatrixData();
59
62
64 CSRMatrixData( const CSRMatrixData & ) = delete;
65
67 std::shared_ptr<MatrixData> cloneMatrixData() const override;
68
70 template<typename ConfigOut>
71 std::shared_ptr<CSRMatrixData<ConfigOut>> migrate( AMP::Utilities::Backend backend ) const;
72
74 std::shared_ptr<MatrixData> transpose() const override;
75
77 std::string type() const override { return "CSRMatrixData"; }
78
80 virtual std::uint16_t mode() const override
81 {
82 return static_cast<std::uint16_t>( Config::mode );
83 }
84
90 void getRowByGlobalID( size_t row,
91 std::vector<size_t> &cols,
92 std::vector<double> &values ) const override;
93
104 void addValuesByGlobalID( size_t num_rows,
105 size_t num_cols,
106 const size_t *rows,
107 const size_t *cols,
108 const void *values,
109 const typeID &id ) override;
110
121 void setValuesByGlobalID( size_t num_rows,
122 size_t num_cols,
123 const size_t *rows,
124 const size_t *cols,
125 const void *values,
126 const typeID &id ) override;
127
138 void getValuesByGlobalID( size_t num_rows,
139 size_t num_cols,
140 const size_t *rows,
141 const size_t *cols,
142 void *values,
143 const typeID &id ) const override;
144
148 size_t numberColumnIDs( size_t row ) const override;
149
151 std::vector<size_t> getColumnIDs( size_t row ) const override;
152
155
161 std::shared_ptr<Discretization::DOFManager> getLeftDOFManager() const override;
162
168 std::shared_ptr<Discretization::DOFManager> getRightDOFManager() const override;
169
175 std::shared_ptr<CommunicationList> getLeftCommList() const;
176
182 std::shared_ptr<CommunicationList> getRightCommList() const;
183
185 size_t numLocalRows() const override;
186
188 size_t numGlobalRows() const override;
189
191 size_t numLocalColumns() const override;
192
194 size_t numGlobalColumns() const override;
195
197 size_t beginRow() const override;
198
200 size_t endRow() const override;
201
203 size_t beginCol() const override;
204
206 size_t endCol() const override;
207
209 typeID getCoeffType() const override
210 {
211 constexpr auto type = getTypeID<scalar_t>();
212 return type;
213 }
214
216 std::shared_ptr<localmatrixdata_t> getDiagMatrix() { return d_diag_matrix; }
217 std::shared_ptr<const localmatrixdata_t> getDiagMatrix() const { return d_diag_matrix; }
218
220 std::shared_ptr<localmatrixdata_t> getOffdMatrix() { return d_offd_matrix; }
221 std::shared_ptr<const localmatrixdata_t> getOffdMatrix() const { return d_offd_matrix; }
222
224 lidx_t *getDiagRowStarts() { return d_diag_matrix->d_row_starts.get(); }
225
227 lidx_t *getOffDiagRowStarts() { return d_offd_matrix->d_row_starts.get(); }
228
230 bool isSquare() const noexcept { return d_is_square; }
231
233 bool isEmpty() const noexcept { return d_diag_matrix->d_is_empty && d_offd_matrix->d_is_empty; }
234
236 auto numberOfNonZeros() const { return d_diag_matrix->d_nnz + d_offd_matrix->d_nnz; }
237
239 auto numberOfNonZerosDiag() const { return d_diag_matrix->d_nnz; }
240
242 auto numberOfNonZerosOffDiag() const { return d_offd_matrix->d_nnz; }
243
245 bool hasOffDiag() const { return !d_offd_matrix->d_is_empty; }
246
248 auto getMemoryLocation() const { return d_memory_location; }
249
254 void setNNZ( lidx_t tot_nnz_diag, lidx_t tot_nnz_offd );
255
260 void setNNZ( const lidx_t *nnz_diag, const lidx_t *nnz_offd );
261
268 void setNNZ( bool do_accum );
269
272
278 void resetDOFManagers( bool force_dm_reset = false );
279
284 void assemble( bool force_dm_reset = false );
285
290 void removeRange( AMP::Scalar bnd_lo, AMP::Scalar bnd_up ) override;
291
293 void printStats( bool verbose, bool show_zeros ) const
294 {
295 std::cout << "CSRMatrixData stats:" << std::endl;
296 std::cout << " Memory location: " << AMP::Utilities::getString( d_memory_location )
297 << std::endl;
298 std::cout << " Global size: (" << numGlobalRows() << " x " << numGlobalColumns() << ")"
299 << std::endl;
300 d_diag_matrix->printStats( verbose, show_zeros );
301 d_offd_matrix->printStats( verbose, show_zeros );
302 }
303
311 std::shared_ptr<localmatrixdata_t> subsetRows( const std::vector<gidx_t> &rows ) const;
312
322 std::shared_ptr<localmatrixdata_t>
323 subsetCols( const gidx_t idx_lo, const gidx_t idx_up, const bool is_diag ) const;
324
325public: // Write/read restart data
331 void registerChildObjects( AMP::IO::RestartManager *manager ) const override;
332
338 void writeRestart( int64_t fid ) const override;
339
343 CSRMatrixData( int64_t fid, AMP::IO::RestartManager *manager );
344
345protected:
347 void setOtherData( std::map<gidx_t, std::map<gidx_t, scalar_t>> &,
349
350 std::shared_ptr<localmatrixdata_t>
351 transposeOffd( std::shared_ptr<MatrixParametersBase> params ) const;
352
353 void writeRestartMapData( const int64_t fid,
354 const std::string &prefix,
355 const std::map<gidx_t, std::map<gidx_t, scalar_t>> &data ) const;
356
357 void readRestartMapData( const int64_t fid,
358 const std::string &prefix,
359 std::map<gidx_t, std::map<gidx_t, scalar_t>> &data );
360
361public:
364
365protected:
367 bool d_is_square = true;
376
383
385 std::shared_ptr<localmatrixdata_t> d_diag_matrix;
387 std::shared_ptr<localmatrixdata_t> d_offd_matrix;
388
390 std::shared_ptr<Discretization::DOFManager> d_leftDOFManager;
392 std::shared_ptr<Discretization::DOFManager> d_rightDOFManager;
393
395 std::shared_ptr<CommunicationList> d_leftCommList;
397 std::shared_ptr<CommunicationList> d_rightCommList;
398
400 std::map<gidx_t, std::map<gidx_t, scalar_t>> d_other_data;
401
403 std::map<gidx_t, std::map<gidx_t, scalar_t>> d_ghost_data;
404};
405
406template<typename Config>
408{
409 auto ptr = dynamic_cast<CSRMatrixData<Config> const *>( &A );
410 AMP_INSIST( ptr, "dynamic cast from const MatrixData to const CSRMatrixData failed" );
411 return ptr;
412}
413
414template<typename Config>
416{
417 auto ptr = dynamic_cast<CSRMatrixData<Config> *>( &A );
418 AMP_INSIST( ptr, "dynamic cast from MatrixData to CSRMatrixData failed" );
419 return ptr;
420}
421
422} // namespace AMP::LinearAlgebra
423
424#endif
Class to manage reading/writing restart data.
auto numberOfNonZeros() const
Get total number of nonzeros in both blocks.
std::map< gidx_t, std::map< gidx_t, scalar_t > > d_ghost_data
storage of off core matrix data to set
lidxAllocator_t d_lidxAllocator
Allocator for lidx_t matched to template parameter.
typename Config::gidx_t gidx_t
void globalToLocalColumns()
Convert global columns in blocks to local columns and free global columns.
AMP::Utilities::MemoryType d_memory_location
Memory location, set by examining type of Allocator.
gidx_t d_last_col
Global index of last column of diagonal block.
std::shared_ptr< localmatrixdata_t > d_offd_matrix
Diagonal matrix block [d_first_row,d_last_row] x ]d_first_col,d_last_col[.
typename Config::lidx_t lidx_t
std::shared_ptr< localmatrixdata_t > transposeOffd(std::shared_ptr< MatrixParametersBase > params) const
auto numberOfNonZerosOffDiag() const
Get total number of nonzeros in off-diagonal block.
void setNNZ(lidx_t tot_nnz_diag, lidx_t tot_nnz_offd)
Set the number of nonzeros in each block and allocate space internally.
std::shared_ptr< localmatrixdata_t > getDiagMatrix()
Get pointer to diagonal block.
void registerChildObjects(AMP::IO::RestartManager *manager) const override
Register any child objects.
std::shared_ptr< const localmatrixdata_t > getOffdMatrix() const
std::shared_ptr< CommunicationList > d_rightCommList
CommunicationList for right vectors.
typeID getCoeffType() const override
Return the typeid of the matrix coeffs.
typename std::allocator_traits< allocator_type >::template rebind_alloc< gidx_t > gidxAllocator_t
lidx_t * getDiagRowStarts()
Get row pointers from diagonal block.
std::shared_ptr< MatrixData > transpose() const override
Transpose.
void writeRestart(int64_t fid) const override
Write restart data to file.
CSRMatrixData(std::shared_ptr< MatrixParametersBase > params)
Constructor.
void readRestartMapData(const int64_t fid, const std::string &prefix, std::map< gidx_t, std::map< gidx_t, scalar_t > > &data)
gidxAllocator_t d_gidxAllocator
Allocator for gidx_t matched to template parameter.
gidx_t d_last_row
Global index of last row of this block.
std::map< gidx_t, std::map< gidx_t, scalar_t > > d_other_data
storage of off core matrix data
typename std::allocator_traits< allocator_type >::template rebind_alloc< scalar_t > scalarAllocator_t
virtual ~CSRMatrixData()
Destructor.
size_t numLocalColumns() const override
Get the number of local columns in the matrix.
void writeRestartMapData(const int64_t fid, const std::string &prefix, const std::map< gidx_t, std::map< gidx_t, scalar_t > > &data) const
bool d_is_square
Matrix is square if true.
CSRMatrixData()
Empty constructor.
std::shared_ptr< localmatrixdata_t > subsetCols(const gidx_t idx_lo, const gidx_t idx_up, const bool is_diag) const
Extract subset of each row containing global columns in some range.
std::shared_ptr< CommunicationList > d_leftCommList
CommunicationList for left vectors.
void assemble(bool force_dm_reset=false)
Convenience function to triger internal setup.
size_t beginCol() const override
Get the global id of the first column in diagonal block (inclusive)
CSRMatrixData(int64_t fid, AMP::IO::RestartManager *manager)
Constructor from restart data.
bool isEmpty() const noexcept
Check if matrix is globally square.
void setValuesByGlobalID(size_t num_rows, size_t num_cols, const size_t *rows, const size_t *cols, const void *values, const typeID &id) override
Set values in the matrix.
std::shared_ptr< localmatrixdata_t > subsetRows(const std::vector< gidx_t > &rows) const
Extract subset of locally owned rows into new local matrix.
std::string type() const override
Return the type of the matrix.
gidx_t d_first_row
Global index of first row of this block.
size_t beginRow() const override
Get the global id of the first stored row (inclusive)
std::shared_ptr< localmatrixdata_t > d_diag_matrix
Diagonal matrix block [d_first_row,d_last_row] x [d_first_col,d_last_col].
typename localmatrixdata_t::mask_t mask_t
void removeRange(AMP::Scalar bnd_lo, AMP::Scalar bnd_up) override
Remove matrix entries within given range.
size_t numberColumnIDs(size_t row) const override
Given a row, retrieve the number of non-zero column indices of the matrix.
virtual std::uint16_t mode() const override
Return CSR mode of the matrix.
std::shared_ptr< const localmatrixdata_t > getDiagMatrix() const
bool isSquare() const noexcept
Check if matrix is globally square.
void getValuesByGlobalID(size_t num_rows, size_t num_cols, const size_t *rows, const size_t *cols, void *values, const typeID &id) const override
Get values from the matrix.
std::shared_ptr< localmatrixdata_t > getOffdMatrix()
Get pointer to off-diagonal block.
typename std::allocator_traits< allocator_type >::template rebind_alloc< lidx_t > lidxAllocator_t
std::shared_ptr< MatrixData > cloneMatrixData() const override
Clone the data.
std::shared_ptr< Discretization::DOFManager > d_rightDOFManager
DOFManager for right vectors.
std::shared_ptr< Discretization::DOFManager > d_leftDOFManager
DOFManager for left vectors.
typename Config::scalar_t scalar_t
void resetDOFManagers(bool force_dm_reset=false)
Replace left/right DOFManagers and CommunicationLists to match NNZ structure.
std::shared_ptr< CommunicationList > getRightCommList() const
Get the CommList associated with a right vector ( For , is a right vector )
size_t numGlobalColumns() const override
Get the number of global columns in the matrix.
size_t endRow() const override
Get the global id of the last stored row (exclusive)
size_t numGlobalRows() const override
Get the number of global rows in the matrix.
auto numberOfNonZerosDiag() const
Get total number of nonzeros in diagonal block.
size_t numLocalRows() const override
Get the number of local rows in the matrix.
scalarAllocator_t d_scalarAllocator
Allocator for scalar_t matched to template parameter.
void setNNZ(const lidx_t *nnz_diag, const lidx_t *nnz_offd)
Set the number of nonzeros in each block and allocate space internally.
gidx_t d_first_col
Global index of first column of diagonal block.
bool hasOffDiag() const
Check if off-diagonal block is non-empty.
CSRMatrixData(const CSRMatrixData &)=delete
Copy constructor.
std::vector< size_t > getColumnIDs(size_t row) const override
Get the global indices of nonzeros in a given row.
std::shared_ptr< Discretization::DOFManager > getRightDOFManager() const override
Get the DOFManager associated with a right vector ( For , is a right vector )
void makeConsistent(AMP::LinearAlgebra::ScatterType t) override
Perform communication to ensure values in the matrix are the same across cores.
std::shared_ptr< Discretization::DOFManager > getLeftDOFManager() const override
Get the DOFManager associated with a left vector ( For , is a left vector )
auto getMemoryLocation() const
Get the memory space where data is stored.
void getRowByGlobalID(size_t row, std::vector< size_t > &cols, std::vector< double > &values) const override
Retrieve a row of the matrix in compressed format.
std::shared_ptr< CSRMatrixData< ConfigOut > > migrate(AMP::Utilities::Backend backend) const
Migrate data to different configuration, mostly for moving memory spaces.
void printStats(bool verbose, bool show_zeros) const
Print information about matrix blocks.
lidx_t * getOffDiagRowStarts()
Get row pointers from off-diagonal block.
void setNNZ(bool do_accum)
Set the number of nonzeros in each block and allocate space internally.
typename Config::allocator_type allocator_type
std::shared_ptr< CommunicationList > getLeftCommList() const
Get the CommList associated with a left vector ( For , is a left vector )
void setOtherData(std::map< gidx_t, std::map< gidx_t, scalar_t > > &, AMP::LinearAlgebra::ScatterType)
Update matrix data off-core.
size_t endCol() const override
Get the global id of the last column in diagonal block (exclusive)
void addValuesByGlobalID(size_t num_rows, size_t num_cols, const size_t *rows, const size_t *cols, const void *values, const typeID &id) override
Add values to those in the matrix.
Scalar is a class used to store a scalar variable that may be different types/precision.
Definition Scalar.h:21
#define AMP_INSIST(EXP, MSG)
Insist error.
static CSRMatrixData< Config > const * getCSRMatrixData(MatrixData const &A)
ScatterType
Flag to choose algorithm for makeConsistent.
Definition Variable.h:21
Backend
Enum to store the backend used for gpu acceleration.
Definition Backend.h:21
std::string_view getString(const ExecutionSpace exec_space)
MemoryType
Enum to store pointer type.
Definition Memory.h:21
Class to store type info.
Definition typeid.h:210



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