Advanced Multi-Physics (AMP)
On-Line Documentation
CSRLocalMatrixData.h
Go to the documentation of this file.
1#ifndef included_AMP_CSRLocalMatrixData_h
2#define included_AMP_CSRLocalMatrixData_h
3
4#include "AMP/matrices/MatrixParametersBase.h"
5#include "AMP/matrices/data/MatrixData.h"
6#include "AMP/utils/Memory.h"
7#include "AMP/utils/Utilities.h"
8
9#include <algorithm>
10#include <functional>
11#include <map>
12#include <numeric>
13#include <tuple>
14#include <unordered_map>
15
16namespace AMP::IO {
17class RestartManager;
18}
19
20namespace AMP::Discretization {
21class DOFManager;
22}
23
24namespace AMP::LinearAlgebra {
25
26// Forward declare CSRMatrix{Data,Communicator} to make them friends
27template<typename C>
28class CSRMatrixData;
29template<typename C>
30class CSRMatrixCommunicator;
31template<typename C>
32class CSRMatrixSpGEMMDefault;
33template<typename C>
34class CSRMatrixSpGEMMDevice;
35
36template<typename Config>
37class CSRLocalMatrixData : public AMP::enable_shared_from_this<CSRLocalMatrixData<Config>>
38{
39public:
40 template<typename C>
41 friend class CSRMatrixData;
42 template<typename C>
43 friend class CSRLocalMatrixData;
44 template<typename C>
46 template<typename C>
48 template<typename C>
50
51 using mask_t = unsigned char;
52 using gidx_t = typename Config::gidx_t;
53 using lidx_t = typename Config::lidx_t;
54 using scalar_t = typename Config::scalar_t;
55 using allocator_type = typename Config::allocator_type;
56 static_assert( std::is_same_v<typename allocator_type::value_type, void> );
57
69 explicit CSRLocalMatrixData( std::shared_ptr<MatrixParametersBase> params,
70 AMP::Utilities::MemoryType memory_location,
71 typename Config::gidx_t first_row,
72 typename Config::gidx_t last_row,
73 typename Config::gidx_t first_col,
74 typename Config::gidx_t last_col,
75 bool is_diag,
76 bool is_symbolic = false,
77 uint64_t hash = 0 );
78
81
82 std::string type() const;
83
85 std::uint16_t mode() const { return static_cast<std::uint16_t>( Config::mode ); }
86
88 std::tuple<lidx_t *, gidx_t *, lidx_t *, scalar_t *> getDataFields()
89 {
90 return std::make_tuple(
91 d_row_starts.get(), d_cols.get(), d_cols_loc.get(), d_coeffs.get() );
92 }
93
94 std::tuple<const lidx_t *, const gidx_t *, const lidx_t *, const scalar_t *>
96 {
97 return std::make_tuple(
98 d_row_starts.get(), d_cols.get(), d_cols_loc.get(), d_coeffs.get() );
99 }
100
103
105 lidx_t *getRowStarts() { return d_row_starts.get(); }
106
108 auto getMemoryLocation() const { return d_memory_location; }
109
111 bool isDiag() const { return d_is_diag; }
112
114 bool isEmpty() const { return d_is_empty; }
115
117 lidx_t numberOfNonZeros() const { return d_nnz; }
118
121
124
127 {
129 }
130
132 gidx_t beginRow() const { return d_first_row; }
133
135 gidx_t endRow() const { return d_last_row; }
136
138 gidx_t beginCol() const { return d_first_col; }
139
141 gidx_t endCol() const { return d_last_col; }
142
145
148 {
149 if ( d_is_diag ) {
150 return nullptr;
151 }
152 return d_cols_unq.get();
153 }
154
156 size_t *getColumnMapSizeT() const;
157
160
168 template<typename idx_t>
169 void getColumnMap( std::vector<idx_t> &colMap ) const
170 {
171 // Don't do anything if offd and empty
172 if ( !d_is_diag && d_is_empty ) {
173 return;
174 }
175
176 colMap.resize( d_is_diag ? ( d_last_col - d_first_col ) : d_ncols_unq, 0 );
177
178 if ( d_is_diag ) {
179 std::iota( colMap.begin(), colMap.end(), d_first_col );
180 } else {
181 AMP::Utilities::copy<gidx_t, idx_t>( d_ncols_unq, d_cols_unq.get(), colMap.data() );
182 }
183 }
184
185
187 void setNNZ( lidx_t tot_nnz );
188
190 void setNNZ( const lidx_t *nnz );
191
193 void setNNZ( bool do_accum );
194
196 void removeRange( const scalar_t bnd_lo, const scalar_t bnd_up );
197
199 void getColPtrs( std::vector<gidx_t *> &col_ptrs );
200
202 void printStats( bool verbose, bool show_zeros ) const;
203
205 void printAll( bool force = false ) const;
206
208 std::shared_ptr<CSRLocalMatrixData> maskMatrixData( const mask_t *mask,
209 const bool is_symbolic ) const;
210
211 static std::shared_ptr<CSRLocalMatrixData>
212 ConcatHorizontal( std::shared_ptr<MatrixParametersBase> params,
213 std::map<int, std::shared_ptr<CSRLocalMatrixData>> blocks );
214
215 static std::shared_ptr<CSRLocalMatrixData>
216 ConcatVertical( std::shared_ptr<MatrixParametersBase> params,
217 std::map<int, std::shared_ptr<CSRLocalMatrixData>> blocks,
218 const gidx_t first_col,
219 const gidx_t last_col,
220 const bool is_diag );
221
222 template<typename U>
223 static std::shared_ptr<U[]> sharedArrayBuilder( const size_t N )
224 {
225 using alloc_t = typename std::allocator_traits<allocator_type>::template rebind_alloc<U>;
226 alloc_t alloc;
227 return std::shared_ptr<typename alloc_t::value_type[]>(
228 alloc.allocate( N ), [N, &alloc]( auto p ) -> void { alloc.deallocate( p, N ); } );
229 }
230
231 static std::shared_ptr<lidx_t[]> makeLidxArray( const size_t N )
232 {
233 return sharedArrayBuilder<lidx_t>( N );
234 }
235
236 static std::shared_ptr<gidx_t[]> makeGidxArray( const size_t N )
237 {
238 return sharedArrayBuilder<gidx_t>( N );
239 }
240
241 static std::shared_ptr<scalar_t[]> makeScalarArray( const size_t N )
242 {
243 return sharedArrayBuilder<scalar_t>( N );
244 }
245
246public: // Non virtual functions
248 uint64_t getID() const { return d_hash; }
249
250public: // Write/read restart data
257
263 void writeRestart( int64_t fid ) const;
264
269
270protected:
278
280 std::shared_ptr<CSRLocalMatrixData> cloneMatrixData();
281
283 template<typename ConfigOut>
284 std::shared_ptr<CSRLocalMatrixData<ConfigOut>> migrate() const;
285
287 std::shared_ptr<CSRLocalMatrixData>
288 transpose( std::shared_ptr<MatrixParametersBase> params ) const;
289
295 void getRowByGlobalID( const size_t local_row,
296 std::vector<size_t> &cols,
297 std::vector<double> &values ) const;
298
307 void getValuesByGlobalID( const size_t local_row,
308 const size_t num_cols,
309 const size_t *cols,
310 scalar_t *values ) const;
311
319 void addValuesByGlobalID( const size_t local_row,
320 const size_t num_cols,
321 const size_t *cols,
322 const scalar_t *vals );
323
331 void setValuesByGlobalID( const size_t local_row,
332 const size_t num_cols,
333 const size_t *cols,
334 const scalar_t *vals );
335
339 size_t numberColumnIDs( size_t local_row ) const;
340
345 std::vector<size_t> getColumnIDs( const size_t local_row ) const;
346
347 // Data members passed from outer CSRMatrixData object
358
362 bool d_is_empty = true;
364 bool d_is_symbolic = false;
365
367 std::shared_ptr<lidx_t[]> d_row_starts;
369 std::shared_ptr<gidx_t[]> d_cols;
371 std::shared_ptr<gidx_t[]> d_cols_unq;
373 std::shared_ptr<lidx_t[]> d_cols_loc;
375 std::shared_ptr<scalar_t[]> d_coeffs;
376
384 uint64_t d_hash = 0;
385
387 mutable std::shared_ptr<size_t[]> d_cols_unq_size_t;
388
390 mutable std::shared_ptr<scalar_t[]> d_ghost_cache;
391};
392
393} // namespace AMP::LinearAlgebra
394
395#endif
Class to manage reading/writing restart data.
gidx_t endCol() const
Get global index of last column in diagonal region (exclusive)
lidx_t d_ncols_unq
Number of unique columns referenced by block.
void getRowByGlobalID(const size_t local_row, std::vector< size_t > &cols, std::vector< double > &values) const
Get columns and values from one row.
uint64_t getID() const
Get a unique id hash for the vector.
gidx_t d_last_row
Global index of last row of this block.
static std::shared_ptr< lidx_t[]> makeLidxArray(const size_t N)
lidx_t numUniqueColumns() const
Get number of unique columns.
void globalToLocalColumns()
Convert global column ids to local and free global columns.
std::shared_ptr< gidx_t[]> d_cols_unq
Unique set of indices from d_cols, unused for diagonal block.
lidx_t d_nnz
Total number of nonzeros.
gidx_t d_first_row
Global index of first row of this block.
void setNNZ(const lidx_t *nnz)
Set number of nonzeros in each row and allocate space accordingly.
size_t numberColumnIDs(size_t local_row) const
Given a row, retrieve the number of non-zero column indices of the matrix.
std::shared_ptr< CSRLocalMatrixData< ConfigOut > > migrate() const
Migrate data to new memory space.
std::shared_ptr< CSRLocalMatrixData > transpose(std::shared_ptr< MatrixParametersBase > params) const
Make matrix data for transpose.
void setNNZ(lidx_t tot_nnz)
Set total number of nonzeros and allocate space accordingly.
gidx_t d_first_col
Global index of first column of diagonal block.
std::tuple< lidx_t *, gidx_t *, lidx_t *, scalar_t * > getDataFields()
Get all data fields as tuple.
lidx_t * getRowStarts()
Get row pointers.
std::shared_ptr< lidx_t[]> d_row_starts
Starting index of each row within other data arrays.
void getValuesByGlobalID(const size_t local_row, const size_t num_cols, const size_t *cols, scalar_t *values) const
Get values from the matrix.
void removeRange(const scalar_t bnd_lo, const scalar_t bnd_up)
Remove matrix entries from within given range.
gidx_t beginRow() const
Get global index of first row in block (inclusive)
void addValuesByGlobalID(const size_t local_row, const size_t num_cols, const size_t *cols, const scalar_t *vals)
Add to existing values at given column locations in a row.
static std::shared_ptr< scalar_t[]> makeScalarArray(const size_t N)
gidx_t * getColumnMap() const
Get pointer to unique columns, only useful for off-diagonal block.
std::shared_ptr< CSRLocalMatrixData > cloneMatrixData()
Make a clone of this matrix data.
void setValuesByGlobalID(const size_t local_row, const size_t num_cols, const size_t *cols, const scalar_t *vals)
Overwrite existing values at given column locations in a row.
std::uint16_t mode() const
Return CSR mode of the matrix.
lidx_t numLocalColumns() const
Get number of local columns in diagonal region.
std::shared_ptr< CSRLocalMatrixData > maskMatrixData(const mask_t *mask, const bool is_symbolic) const
Apply a mask to the entries of the matrix and return a new one.
gidx_t d_last_col
Global index of last column of diagonal block.
void getColPtrs(std::vector< gidx_t * > &col_ptrs)
Get pointers into d_cols at start of each row.
typename Config::allocator_type allocator_type
bool isDiag() const
Check if this is a diagonal block.
std::shared_ptr< lidx_t[]> d_cols_loc
Local column indices for nonzeros in each row.
auto getMemoryLocation() const
Get the memory space where data is stored.
std::shared_ptr< scalar_t[]> d_ghost_cache
Storage space for vector ghost information, allocated lazily.
std::shared_ptr< scalar_t[]> d_coeffs
Nonzero values in each row.
void printAll(bool force=false) const
Print all information in a matrix block.
lidx_t numberOfNonZeros() const
Get total number of nonzeros in block.
void registerChildObjects(AMP::IO::RestartManager *manager) const
Register any child objects.
virtual ~CSRLocalMatrixData()
Destructor.
uint64_t d_hash
hash to uniquely identify this object during restart
scalar_t * getGhostCache() const
Get pointer to unique columns as size_t's.
void setNNZ(bool do_accum)
setNNZ function that references d_row_starts and optionally does scan
AMP::Utilities::MemoryType d_memory_location
Memory space where data lives, compatible with allocator template parameter.
lidx_t numLocalRows() const
Get number of local rows.
size_t * getColumnMapSizeT() const
Get pointer to unique columns as size_t's.
void sortColumns()
Sort the columns/values within each row.
CSRLocalMatrixData(std::shared_ptr< MatrixParametersBase > params, AMP::Utilities::MemoryType memory_location, typename Config::gidx_t first_row, typename Config::gidx_t last_row, typename Config::gidx_t first_col, typename Config::gidx_t last_col, bool is_diag, bool is_symbolic=false, uint64_t hash=0)
Constructor.
bool d_is_diag
Flag to indicate if this is a diagonal block or not.
static std::shared_ptr< CSRLocalMatrixData > ConcatVertical(std::shared_ptr< MatrixParametersBase > params, std::map< int, std::shared_ptr< CSRLocalMatrixData > > blocks, const gidx_t first_col, const gidx_t last_col, const bool is_diag)
static std::shared_ptr< U[]> sharedArrayBuilder(const size_t N)
std::shared_ptr< gidx_t[]> d_cols
Global column indices for nonzeros in each row.
void getColumnMap(std::vector< idx_t > &colMap) const
Copy unique columns into.
void swapDataFields(CSRLocalMatrixData< Config > &other)
Swap data fields with another CSRLocalMatrix.
gidx_t beginCol() const
Get global index of first column in diagonal region (inclusive)
std::tuple< const lidx_t *, const gidx_t *, const lidx_t *, const scalar_t * > getDataFields() const
std::shared_ptr< size_t[]> d_cols_unq_size_t
column map as size_t, generated lazily only if needed
bool d_is_empty
Flag to indicate if this is empty.
CSRLocalMatrixData(int64_t fid, AMP::IO::RestartManager *manager)
Constructor from restart data.
lidx_t d_num_rows
Number of locally stored rows, (d_local_row - d_first_row)
void printStats(bool verbose, bool show_zeros) const
Print information about matrix block.
std::vector< size_t > getColumnIDs(const size_t local_row) const
Get columns and values from one row.
static std::shared_ptr< CSRLocalMatrixData > ConcatHorizontal(std::shared_ptr< MatrixParametersBase > params, std::map< int, std::shared_ptr< CSRLocalMatrixData > > blocks)
void writeRestart(int64_t fid) const
Write restart data to file.
gidx_t endRow() const
Get global index of last row in block (exclusive)
bool d_is_symbolic
Flag to indicate if this is a symbolic matrix.
static std::shared_ptr< gidx_t[]> makeGidxArray(const size_t N)
Enhancement of std::enable_shared_from_this.
MemoryType
Enum to store pointer type.
Definition Memory.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