MoCSI API Reference
Loading...
Searching...
No Matches
Public Member Functions | List of all members
CsrSparseMatrix< T > Class Template Reference

Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix. This class provides basic element-wise arithmetic operations and access functionality. It also includes a build-up feature that allows elements to be registered using the Coordinate (COO) format prior to finalization. Note: Adding elements after the initial build-up is not supported, as it is inefficient and unnecessary for static mesh use cases. More...

#include <CsrMatrix.h>

Inheritance diagram for CsrSparseMatrix< T >:
Inheritance graph
Collaboration diagram for CsrSparseMatrix< T >:
Collaboration graph

Public Member Functions

 CsrSparseMatrix ()
 Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to allow dynamic creation.
 
 CsrSparseMatrix (const std::valarray< T > &values, const std::valarray< int > &column_indices, const std::valarray< int > &row_pointers, int rows, int cols)
 Constructor for an CsrSparseMatrix object from given CSR arrays and row and column numbers. Disables dynamic creation.
 
void registerElement (T value, int rows, int cols) override
 Registers the element and it's initial value within a COO style vector for dynamic creation.
 
void buildAtRuntime ()
 Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by rows (ascending) and within one row by columns (ascending) and marks the beginning of the rows while it progresses through the column sorting. Afterwards it simply writes the values and column indices into the corresponding layer. The sorting of the column indices it done to fully comply with the CSR format and allow for O( log(m) ) random access time (m = elements within a row). The flag m_dynamic_creation is always true, when using the default constructor and not passing already prebuild CSR arrays to the constructor.
 
void buildAtRuntime (int rows, int cols) override
 Function overload for enforcing matrix boundaries. Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by rows (ascending) and within one row by columns (ascending) and marks the beginning of the rows while it progresses through the column sorting. Afterwards it simply writes the values and column indices into the corresponding layer. The sorting of the column indices it done to fully comply with the CSR format and allow for O( log(m) ) random access time (m = elements within a row).
 
Toperator() (int rows, int cols) override
 Parentheses operator to allow access to the non-zero matrix values as Matrix(row,column). The static CSR matrix only supports writing to predefined non-zero positions. Right now throws an error (int) when trying to access a zero element. Uses divide and conquer to allow access in O( log(m) ) time (m = elements within a row).
 
T operator() (int rows, int cols) const override
 Parentheses operator to allow access to the non-zero matrix values as Matrix(row,column) in const contexts. Uses divide and conquer to allow access in O( log(m) ) time (m = elements within a row).
 
std::valarray< TscalarAdd (T value) const override
 Addition of a scalar to the matrix values element-wise.
 
std::valarray< TscalarSubtract (T value) const override
 Subtraction of a scalar from the matrix values element-wise.
 
std::valarray< TscalarMultiply (T value) const override
 Multiplication of a scalar to the matrix values element-wise.
 
std::valarray< TscalarDivide (T value) const override
 Division of a scalar from the matrix values element-wise.
 
std::valarray< TvectorElemAdd (const std::valarray< T > &value) const override
 Addition of a vector to the matrix values element-wise.
 
std::valarray< TvectorElemSubtract (const std::valarray< T > &value) const override
 Subtraction of a vector from the matrix values element-wise.
 
std::valarray< TvectorElemMultiply (const std::valarray< T > &value) const override
 Multiplication of a vector to the matrix values element-wise.
 
std::valarray< TvectorElemDivide (const std::valarray< T > &value) const override
 Division of a vector from the matrix values element-wise.
 
std::valarray< TmatrixVectorMultiplication (const std::valarray< T > &value) const override
 Matrix-vector-multiplication for sparse matrices.
 
const MatrixType getMatrixType () override
 
void printMatrixInDenseFormat ()
 Prints the entire matrix with zero values.
 
const std::vector< std::pair< int, int > > & getNonZeroElements () override
 
int getNonZeroElementNumber () const
 
const std::valarray< int > & getColumnIndices ()
 
const std::valarray< int > & getRowPointers ()
 
const std::valarray< T > & getMatrixElements ()
 
void updateNonZeroElements () override
 Sets the m_non_zero_elememts parameter of the parent class.
 
 ~CsrSparseMatrix () override=default
 
- Public Member Functions inherited from GenericMatrix< T >
 GenericMatrix ()
 Constructor for an empty GenericMatrix object. This should only be used for dynamically created matrices.
 
 GenericMatrix (int rows, int cols)
 Constructor for an empty GenericMatrix object whose value array holds rows*cols values.
 
 GenericMatrix (int rows, int cols, const std::valarray< T > &values, bool allow_mismatched_size)
 Constructor for a GenericMatrix object whose value array holds rows*cols values given by the user. Can throw an std::ivalid_argument if provided with a value array of different size than rows*cols.
 
 GenericMatrix (const GenericMatrix &)=default
 
GenericMatrixoperator= (const GenericMatrix &)=default
 
 GenericMatrix (GenericMatrix &&)=default
 
GenericMatrixoperator= (GenericMatrix &&)=default
 
std::pair< int, intsize () const
 Function that returns the size of the matrix as a pair in (rows, columns) format.
 
void zeroMatrix ()
 Function that zeros out the matrix.
 
void overwriteMatrixValues (const std::valarray< T > &value)
 Overwrites the matrix values with a new valarray. This is used for optimized overwrite when arithmetic operations on multiple matrices are used.
 
virtual ~GenericMatrix ()=default
 

Additional Inherited Members

- Protected Attributes inherited from GenericMatrix< T >
int m_rows {0}
 
int m_cols {0}
 
std::valarray< Tm_matrix {}
 
std::vector< std::pair< int, int > > m_non_zero_elements
 

Detailed Description

template<typename T>
class CsrSparseMatrix< T >

Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix. This class provides basic element-wise arithmetic operations and access functionality. It also includes a build-up feature that allows elements to be registered using the Coordinate (COO) format prior to finalization. Note: Adding elements after the initial build-up is not supported, as it is inefficient and unnecessary for static mesh use cases.

Constructor & Destructor Documentation

◆ CsrSparseMatrix() [1/2]

Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to allow dynamic creation.

◆ CsrSparseMatrix() [2/2]

template<typename T >
CsrSparseMatrix< T >::CsrSparseMatrix ( const std::valarray< T > &  values,
const std::valarray< int > &  column_indices,
const std::valarray< int > &  row_pointers,
int  rows,
int  cols 
)

Constructor for an CsrSparseMatrix object from given CSR arrays and row and column numbers. Disables dynamic creation.

Parameters
valuesThe value array of the CSR format with the non-zero values of the sparse matrix.
column_indicesThe array holding all the column index values of the CSR format.
row_pointersThe array with the indices to the elements that mark the beginning of a new row of the CSR format.
rowsThe number of rows of the matrix.
colsThe number of columns of the matrix.

◆ ~CsrSparseMatrix()

template<typename T >
CsrSparseMatrix< T >::~CsrSparseMatrix ( )
overridedefault

Member Function Documentation

◆ buildAtRuntime() [1/2]

template<typename T >
void CsrSparseMatrix< T >::buildAtRuntime ( )

Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by rows (ascending) and within one row by columns (ascending) and marks the beginning of the rows while it progresses through the column sorting. Afterwards it simply writes the values and column indices into the corresponding layer. The sorting of the column indices it done to fully comply with the CSR format and allow for O( log(m) ) random access time (m = elements within a row). The flag m_dynamic_creation is always true, when using the default constructor and not passing already prebuild CSR arrays to the constructor.

◆ buildAtRuntime() [2/2]

template<typename T >
void CsrSparseMatrix< T >::buildAtRuntime ( int  rows,
int  cols 
)
overridevirtual

Function overload for enforcing matrix boundaries. Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by rows (ascending) and within one row by columns (ascending) and marks the beginning of the rows while it progresses through the column sorting. Afterwards it simply writes the values and column indices into the corresponding layer. The sorting of the column indices it done to fully comply with the CSR format and allow for O( log(m) ) random access time (m = elements within a row).

Parameters
rowsthe row to access.
colsthe column to access.

Implements GenericMatrix< T >.

◆ getColumnIndices()

template<typename T >
const std::valarray< int > & CsrSparseMatrix< T >::getColumnIndices ( )
inline

◆ getMatrixElements()

template<typename T >
const std::valarray< T > & CsrSparseMatrix< T >::getMatrixElements ( )
inline

◆ getMatrixType()

template<typename T >
const MatrixType CsrSparseMatrix< T >::getMatrixType ( )
inlineoverridevirtual

Implements GenericMatrix< T >.

◆ getNonZeroElementNumber()

template<typename T >
int CsrSparseMatrix< T >::getNonZeroElementNumber ( ) const
inline

◆ getNonZeroElements()

template<typename T >
const std::vector< std::pair< int, int > > & CsrSparseMatrix< T >::getNonZeroElements ( )
inlineoverridevirtual

Implements GenericMatrix< T >.

◆ getRowPointers()

template<typename T >
const std::valarray< int > & CsrSparseMatrix< T >::getRowPointers ( )
inline

◆ matrixVectorMultiplication()

template<typename T >
std::valarray< T > CsrSparseMatrix< T >::matrixVectorMultiplication ( const std::valarray< T > &  value) const
overridevirtual

Matrix-vector-multiplication for sparse matrices.

Template Parameters
TNumeric type used in the simulation (e.g., float, double)
Parameters
valueThe vector with the values to multiply to the matrix from the right.
Returns
std::valarray<T> The resulting vector.

Implements GenericMatrix< T >.

◆ operator()() [1/2]

template<typename T >
T CsrSparseMatrix< T >::operator() ( int  rows,
int  cols 
) const
overridevirtual

Parentheses operator to allow access to the non-zero matrix values as Matrix(row,column) in const contexts. Uses divide and conquer to allow access in O( log(m) ) time (m = elements within a row).

Parameters
rowsthe row to access.
colsthe column to access.

Implements GenericMatrix< T >.

◆ operator()() [2/2]

template<typename T >
T & CsrSparseMatrix< T >::operator() ( int  rows,
int  cols 
)
overridevirtual

Parentheses operator to allow access to the non-zero matrix values as Matrix(row,column). The static CSR matrix only supports writing to predefined non-zero positions. Right now throws an error (int) when trying to access a zero element. Uses divide and conquer to allow access in O( log(m) ) time (m = elements within a row).

Parameters
rowsthe row to access.
colsthe column to access.

Implements GenericMatrix< T >.

◆ printMatrixInDenseFormat()

template<typename T >
void CsrSparseMatrix< T >::printMatrixInDenseFormat ( )

Prints the entire matrix with zero values.

◆ registerElement()

template<typename T >
void CsrSparseMatrix< T >::registerElement ( T  value,
int  rows,
int  cols 
)
overridevirtual

Registers the element and it's initial value within a COO style vector for dynamic creation.

Parameters
valueThe initial value of the element.
rowsThe row index of that element within the matrix.
colsThe column index of that element within the matrix.

Reimplemented from GenericMatrix< T >.

◆ scalarAdd()

template<typename T >
std::valarray< T > CsrSparseMatrix< T >::scalarAdd ( T  value) const
overridevirtual

Addition of a scalar to the matrix values element-wise.

Parameters
valueThe scalar value to be added to the calling object.

Implements GenericMatrix< T >.

◆ scalarDivide()

template<typename T >
std::valarray< T > CsrSparseMatrix< T >::scalarDivide ( T  value) const
overridevirtual

Division of a scalar from the matrix values element-wise.

Parameters
valueThe scalar value to divide to the calling object by.

Implements GenericMatrix< T >.

◆ scalarMultiply()

template<typename T >
std::valarray< T > CsrSparseMatrix< T >::scalarMultiply ( T  value) const
overridevirtual

Multiplication of a scalar to the matrix values element-wise.

Parameters
valueThe scalar value to be multiplied to the calling object.

Implements GenericMatrix< T >.

◆ scalarSubtract()

template<typename T >
std::valarray< T > CsrSparseMatrix< T >::scalarSubtract ( T  value) const
overridevirtual

Subtraction of a scalar from the matrix values element-wise.

Parameters
valueThe scalar value to be subtracted from the calling object.

Implements GenericMatrix< T >.

◆ updateNonZeroElements()

template<typename T >
void CsrSparseMatrix< T >::updateNonZeroElements ( )
overridevirtual

Sets the m_non_zero_elememts parameter of the parent class.

Template Parameters
TNumeric type used in the simulation (e.g., float, double)

Implements GenericMatrix< T >.

◆ vectorElemAdd()

template<typename T >
std::valarray< T > CsrSparseMatrix< T >::vectorElemAdd ( const std::valarray< T > &  value) const
overridevirtual

Addition of a vector to the matrix values element-wise.

Parameters
valueThe vector values to be added to the calling object.

Implements GenericMatrix< T >.

◆ vectorElemDivide()

template<typename T >
std::valarray< T > CsrSparseMatrix< T >::vectorElemDivide ( const std::valarray< T > &  value) const
overridevirtual

Division of a vector from the matrix values element-wise.

Parameters
valueThe vector value to divide to the calling object by.

Implements GenericMatrix< T >.

◆ vectorElemMultiply()

template<typename T >
std::valarray< T > CsrSparseMatrix< T >::vectorElemMultiply ( const std::valarray< T > &  value) const
overridevirtual

Multiplication of a vector to the matrix values element-wise.

Parameters
valueThe vector value to be multiplied to the calling object.

Implements GenericMatrix< T >.

◆ vectorElemSubtract()

template<typename T >
std::valarray< T > CsrSparseMatrix< T >::vectorElemSubtract ( const std::valarray< T > &  value) const
overridevirtual

Subtraction of a vector from the matrix values element-wise.

Parameters
valueThe vector value to be subtracted from the calling object.

Implements GenericMatrix< T >.


The documentation for this class was generated from the following file: