1#ifndef included_AMP_CSRLocalMatrixData_h
2#define included_AMP_CSRLocalMatrixData_h
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"
14#include <unordered_map>
30class CSRMatrixCommunicator;
32class CSRMatrixSpGEMMDefault;
34class CSRMatrixSpGEMMDevice;
36template<
typename Config>
52 using gidx_t =
typename Config::gidx_t;
53 using lidx_t =
typename Config::lidx_t;
56 static_assert( std::is_same_v<typename allocator_type::value_type, void> );
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,
76 bool is_symbolic =
false,
85 std::uint16_t
mode()
const {
return static_cast<std::uint16_t
>( Config::mode ); }
90 return std::make_tuple(
94 std::tuple<const lidx_t *, const gidx_t *, const lidx_t *, const scalar_t *>
97 return std::make_tuple(
168 template<
typename idx_t>
179 std::iota( colMap.begin(), colMap.end(),
d_first_col );
209 const bool is_symbolic )
const;
211 static std::shared_ptr<CSRLocalMatrixData>
213 std::map<
int, std::shared_ptr<CSRLocalMatrixData>> blocks );
215 static std::shared_ptr<CSRLocalMatrixData>
217 std::map<
int, std::shared_ptr<CSRLocalMatrixData>> blocks,
220 const bool is_diag );
225 using alloc_t =
typename std::allocator_traits<allocator_type>::template rebind_alloc<U>;
227 return std::shared_ptr<typename alloc_t::value_type[]>(
228 alloc.allocate( N ), [N, &
alloc](
auto p ) ->
void { alloc.deallocate( p, N ); } );
233 return sharedArrayBuilder<lidx_t>( N );
238 return sharedArrayBuilder<gidx_t>( N );
243 return sharedArrayBuilder<scalar_t>( N );
283 template<
typename ConfigOut>
284 std::shared_ptr<CSRLocalMatrixData<ConfigOut>>
migrate()
const;
287 std::shared_ptr<CSRLocalMatrixData>
288 transpose( std::shared_ptr<MatrixParametersBase> params )
const;
296 std::vector<size_t> &cols,
297 std::vector<double> &values )
const;
308 const size_t num_cols,
320 const size_t num_cols,
332 const size_t num_cols,
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.
typename Config::lidx_t lidx_t
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.
typename Config::gidx_t gidx_t
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.
bool isEmpty() const
Check if empty.
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.
typename Config::scalar_t scalar_t
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.