Template Numerical Library version\ main:8b8c8226
|
Implementation of sparse matrix, i.e. matrix storing only non-zero elements. More...
#include <TNL/Matrices/SparseMatrix.h>
Public Types | |
using | BaseType = Matrix< Real, Device, Index, RealAllocator > |
using | ColumnsIndexesVectorType = Containers::Vector< typename TNL::copy_const< Index >::template from< Real >::type, Device, Index, IndexAllocator > |
using | ColumnsIndexesViewType = typename ColumnsIndexesVectorType::ViewType |
using | ComputeRealType = ComputeReal |
using | ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType |
using | ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType |
using | ConstRowView = typename ViewType::ConstRowView |
Type for accessing constant matrix rows. | |
using | ConstValuesViewType = typename ValuesViewType::ConstViewType |
using | ConstViewType = SparseMatrixView< std::add_const_t< Real >, Device, Index, MatrixType, SegmentsViewTemplate, ComputeRealType > |
Matrix view type for constant instances. More... | |
using | DeviceType = Device |
The device where the matrix is allocated. | |
using | IndexAllocatorType = IndexAllocator |
The allocator for matrix elements column indexes. | |
using | IndexType = Index |
The type used for matrix elements indexing. | |
using | RealAllocatorType = RealAllocator |
The allocator for matrix elements values. | |
using | RealType = Real |
The type of matrix elements. | |
using | RowsCapacitiesType = Containers::Vector< std::remove_const_t< Index >, Device, Index, IndexAllocator > |
using | RowsCapacitiesView = Containers::VectorView< std::remove_const_t< Index >, Device, Index > |
using | RowView = typename ViewType::RowView |
Type for accessing matrix rows. | |
template<typename Device_ , typename Index_ , typename IndexAllocator_ > | |
using | SegmentsTemplate = Segments< Device_, Index_, IndexAllocator_ > |
Templated type of segments, i.e. sparse matrix format. | |
using | SegmentsType = Segments< Device, Index, IndexAllocator > |
Type of segments used by this matrix. It represents the sparse matrix format. | |
template<typename Device_ , typename Index_ > | |
using | SegmentsViewTemplate = typename SegmentsType::template ViewTemplate< Device_, Index > |
Templated view type of segments, i.e. sparse matrix format. | |
using | SegmentsViewType = typename SegmentsType::ViewType |
Type of segments view used by the related matrix view. It represents the sparse matrix format. | |
template<typename _Real = Real, typename _Device = Device, typename _Index = Index, typename _MatrixType = MatrixType, template< typename, typename, typename > class _Segments = Segments, typename _ComputeReal = ComputeReal, typename _RealAllocator = typename Allocators::Default< _Device >::template Allocator< _Real >, typename _IndexAllocator = typename Allocators::Default< _Device >::template Allocator< _Index >> | |
using | Self = SparseMatrix< _Real, _Device, _Index, _MatrixType, _Segments, _ComputeReal, _RealAllocator, _IndexAllocator > |
Helper type for getting self type or its modifications. | |
using | ValuesVectorType = typename Matrix< Real, Device, Index, RealAllocator >::ValuesType |
using | ValuesViewType = typename ValuesVectorType::ViewType |
using | ViewType = SparseMatrixView< Real, Device, Index, MatrixType, SegmentsViewTemplate, ComputeRealType > |
Type of related matrix view. More... | |
![]() | |
using | ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType |
using | ConstValuesType = Containers::Vector< std::add_const_t< Real >, Device, Index, RealAllocator > |
Type of constant vector holding values of matrix elements. | |
using | ConstValuesView = typename ViewType::ConstValuesView |
Type of constant vector view holding values of matrix elements. | |
using | ConstViewType = typename MatrixView< Real, Device, Index >::ConstViewType |
Type of base matrix view for constant instances. More... | |
using | DeviceType = Device |
The device where the matrix is allocated. | |
using | IndexType = Index |
The type used for matrix elements indexing. | |
using | RealAllocatorType = RealAllocator |
using | RealType = Real |
The type of matrix elements. | |
using | RowsCapacitiesType = Containers::Vector< Index, Device, Index > |
using | RowsCapacitiesView = Containers::VectorView< Index, Device, Index > |
using | ValuesType = Containers::Vector< Real, Device, Index, RealAllocator > |
Type of vector holding values of matrix elements. | |
using | ValuesView = typename ViewType::ValuesView |
Type of vector view holding values of matrix elements. | |
using | ViewType = MatrixView< Real, Device, Index > |
Type of base matrix view. More... | |
Public Member Functions | |
SparseMatrix (const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType()) | |
Constructor only with values and column indexes allocators. More... | |
template<typename RowCapacitiesVector , std::enable_if_t< TNL::IsArrayType< RowCapacitiesVector >::value, int > = 0> | |
SparseMatrix (const RowCapacitiesVector &rowCapacities, IndexType columns, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType()) | |
Constructor with matrix rows capacities given as a vector and number of columns. More... | |
SparseMatrix (const SparseMatrix &matrix) | |
Copy constructor. More... | |
template<typename ListIndex > | |
SparseMatrix (const std::initializer_list< ListIndex > &rowCapacities, IndexType columns, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType()) | |
Constructor with matrix rows capacities and number of columns. More... | |
template<typename Index_t , std::enable_if_t< std::is_integral< Index_t >::value, int > = 0> | |
SparseMatrix (Index_t rows, Index_t columns, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType()) | |
Constructor with matrix dimensions. More... | |
SparseMatrix (IndexType rows, IndexType columns, const std::initializer_list< std::tuple< IndexType, IndexType, RealType > > &data, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType()) | |
Constructor with matrix dimensions and data in initializer list. More... | |
template<typename MapIndex , typename MapValue > | |
SparseMatrix (IndexType rows, IndexType columns, const std::map< std::pair< MapIndex, MapIndex >, MapValue > &map, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType()) | |
Constructor with matrix dimensions and data in std::map. More... | |
SparseMatrix (SparseMatrix &&matrix) noexcept=default | |
Move constructor. More... | |
__cuda_callable__ void | addElement (IndexType row, IndexType column, const RealType &value, const RealType &thisElementMultiplicator) |
Add element at given row and column to given value. More... | |
template<typename Function > | |
void | forAllElements (Function &&function) |
Method for parallel iteration over all matrix elements for non-constant instances. More... | |
template<typename Function > | |
void | forAllElements (Function &&function) const |
Method for parallel iteration over all matrix elements for constant instances. More... | |
template<typename Function > | |
void | forAllRows (Function &&function) |
Method for parallel iteration over all matrix rows. More... | |
template<typename Function > | |
void | forAllRows (Function &&function) const |
Method for parallel iteration over all matrix rows for constant instances. More... | |
template<typename Function > | |
void | forElements (IndexType begin, IndexType end, Function &&function) |
Method for parallel iteration over all matrix elements of given rows for non-constant instances. More... | |
template<typename Function > | |
void | forElements (IndexType begin, IndexType end, Function &&function) const |
Method for parallel iteration over matrix elements of given rows for constant instances. More... | |
template<typename Function > | |
void | forRows (IndexType begin, IndexType end, Function &&function) |
Method for parallel iteration over matrix rows from interval [ begin, end). More... | |
template<typename Function > | |
void | forRows (IndexType begin, IndexType end, Function &&function) const |
Method for parallel iteration over matrix rows from interval [ begin, end) for constant instances. More... | |
ColumnsIndexesVectorType & | getColumnIndexes () |
Getter of column indexes for nonconstant instances. More... | |
const ColumnsIndexesVectorType & | getColumnIndexes () const |
Getter of column indexes for constant instances. More... | |
template<typename Vector > | |
void | getCompressedRowLengths (Vector &rowLengths) const |
Computes number of non-zeros in each row. More... | |
ConstViewType | getConstView () const |
Returns a non-modifiable view of the sparse matrix. More... | |
__cuda_callable__ RealType | getElement (IndexType row, IndexType column) const |
Returns value of matrix element at position given by its row and column index. More... | |
IndexType | getNonzeroElementsCount () const override |
Returns number of non-zero matrix elements. More... | |
__cuda_callable__ IndexType | getPaddingIndex () const |
Returns a padding index value. More... | |
__cuda_callable__ RowView | getRow (IndexType rowIdx) |
Non-constant getter of simple structure for accessing given matrix row. More... | |
__cuda_callable__ ConstRowView | getRow (IndexType rowIdx) const |
Constant getter of simple structure for accessing given matrix row. More... | |
template<typename Vector > | |
void | getRowCapacities (Vector &rowCapacities) const |
Compute capacities of all rows. More... | |
__cuda_callable__ IndexType | getRowCapacity (IndexType row) const |
Returns capacity of given matrix row. More... | |
SegmentsType & | getSegments () |
Getter of segments for non-constant instances. More... | |
const SegmentsType & | getSegments () const |
Getter of segments for constant instances. More... | |
std::string | getSerializationTypeVirtual () const override |
Returns string with serialization type. More... | |
template<typename Real2 , typename Index2 , template< typename, typename, typename > class Segments2> | |
void | getTransposition (const SparseMatrix< Real2, Device, Index2, MatrixType, Segments2 > &matrix, const ComputeRealType &matrixMultiplicator=1.0) |
ViewType | getView () |
Returns a modifiable view of the sparse matrix. More... | |
void | load (const String &fileName) |
Method for loading the matrix from the file with given filename. More... | |
void | load (File &file) override |
Method for loading the matrix from a file. More... | |
template<typename Matrix > | |
bool | operator!= (const Matrix &matrix) const |
Comparison operator with another arbitrary matrix type. More... | |
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization, typename RealAllocator_ > | |
SparseMatrix & | operator= (const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ > &matrix) |
Assignment of dense matrix. More... | |
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization, typename RealAllocator_ > | |
SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator > & | operator= (const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ > &matrix) |
template<typename RHSMatrix > | |
SparseMatrix & | operator= (const RHSMatrix &matrix) |
Assignment of any matrix type other then this and dense. More... | |
template<typename RHSMatrix > | |
SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator > & | operator= (const RHSMatrix &matrix) |
SparseMatrix & | operator= (const SparseMatrix &matrix) |
Copy-assignment of exactly the same matrix type. More... | |
SparseMatrix & | operator= (SparseMatrix &&matrix) noexcept(false) |
Move-assignment of exactly the same matrix type. More... | |
template<typename Matrix > | |
bool | operator== (const Matrix &matrix) const |
Comparison operator with another arbitrary matrix type. More... | |
void | print (std::ostream &str) const override |
Method for printing the matrix to output stream. More... | |
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal > | |
void | reduceAllRows (Fetch &fetch, const Reduce &reduce, Keep &keep, const FetchReal &identity) |
Method for performing general reduction on all matrix rows. More... | |
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal > | |
void | reduceAllRows (Fetch &fetch, const Reduce &reduce, Keep &keep, const FetchReal &identity) const |
Method for performing general reduction on all matrix rows for constant instances. More... | |
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal > | |
void | reduceRows (IndexType begin, IndexType end, Fetch &fetch, const Reduce &reduce, Keep &keep, const FetchReal &identity) |
Method for performing general reduction on matrix rows. More... | |
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal > | |
void | reduceRows (IndexType begin, IndexType end, Fetch &fetch, const Reduce &reduce, Keep &keep, const FetchReal &identity) const |
Method for performing general reduction on matrix rows for constant instances. More... | |
template<typename Fetch , typename Reduce , typename Keep , typename FetchValue > | |
void | reduceRows (IndexType begin, IndexType end, Fetch &fetch, const Reduce &reduce, Keep &keep, const FetchValue &identity) |
template<typename Fetch , typename Reduce , typename Keep , typename FetchValue > | |
void | reduceRows (IndexType begin, IndexType end, Fetch &fetch, const Reduce &reduce, Keep &keep, const FetchValue &identity) const |
void | reset () |
Resets the matrix to zero dimensions. | |
void | save (const String &fileName) const |
Method for saving the matrix to the file with given filename. More... | |
void | save (File &file) const override |
Method for saving the matrix to a file. More... | |
template<typename Function > | |
void | sequentialForAllRows (Function &function) |
This method calls sequentialForRows for all matrix rows. More... | |
template<typename Function > | |
void | sequentialForAllRows (Function &function) const |
This method calls sequentialForRows for all matrix rows (for constant instances). More... | |
template<typename Function > | |
void | sequentialForRows (IndexType begin, IndexType end, Function &function) |
Method for sequential iteration over all matrix rows for non-constant instances. More... | |
template<typename Function > | |
void | sequentialForRows (IndexType begin, IndexType end, Function &function) const |
Method for sequential iteration over all matrix rows for constant instances. More... | |
virtual void | setColumnsWithoutReset (IndexType columns) |
Set number of columns of this matrix. More... | |
void | setDimensions (IndexType rows, IndexType columns) override |
Set number of rows and columns of this matrix. More... | |
__cuda_callable__ void | setElement (IndexType row, IndexType column, const RealType &value) |
Sets element at given row and column to given value. More... | |
void | setElements (const std::initializer_list< std::tuple< IndexType, IndexType, RealType > > &data) |
This method sets the sparse matrix elements from initializer list. More... | |
template<typename MapIndex , typename MapValue > | |
void | setElements (const std::map< std::pair< MapIndex, MapIndex >, MapValue > &map) |
This method sets the sparse matrix elements from std::map. More... | |
template<typename Matrix > | |
void | setLike (const Matrix &matrix) |
Set the number of matrix rows and columns by the given matrix. More... | |
template<typename Matrix_ > | |
void | setLike (const Matrix_ &matrix) |
template<typename RowsCapacitiesVector > | |
void | setRowCapacities (const RowsCapacitiesVector &rowCapacities) |
Allocates memory for non-zero matrix elements. More... | |
template<typename InVector , typename OutVector > | |
void | vectorProduct (const InVector &inVector, OutVector &outVector, ComputeRealType matrixMultiplicator=1.0, ComputeRealType outVectorMultiplicator=0.0, IndexType begin=0, IndexType end=0) const |
Computes product of matrix and vector. More... | |
![]() | |
Matrix (const RealAllocatorType &allocator=RealAllocatorType()) | |
Construct a new Matrix object possibly with user defined allocator of the matrix values. More... | |
Matrix (IndexType rows, IndexType columns, const RealAllocatorType &allocator=RealAllocatorType()) | |
Construct a new Matrix object with given dimensions and possibly user defined allocator of the matrix values. More... | |
IndexType | getAllocatedElementsCount () const |
Tells the number of allocated matrix elements. More... | |
__cuda_callable__ IndexType | getColumns () const |
Returns number of matrix columns. More... | |
virtual IndexType | getNonzeroElementsCount () const |
Computes a current number of nonzero matrix elements. More... | |
__cuda_callable__ IndexType | getRows () const |
Returns number of matrix rows. More... | |
ValuesType & | getValues () |
Returns a reference to a vector with the matrix elements values. More... | |
const ValuesType & | getValues () const |
Returns a constant reference to a vector with the matrix elements values. More... | |
void | load (File &file) override |
Method for loading the matrix from a file. More... | |
template<typename Matrix > | |
bool | operator!= (const Matrix &matrix) const |
Comparison operator with another arbitrary matrix type. More... | |
template<typename MatrixT > | |
bool | operator!= (const MatrixT &matrix) const |
template<typename Matrix > | |
bool | operator== (const Matrix &matrix) const |
Comparison operator with another arbitrary matrix type. More... | |
template<typename MatrixT > | |
bool | operator== (const MatrixT &matrix) const |
virtual void | print (std::ostream &str) const |
Method for printing the matrix to output stream. More... | |
void | reset () |
Reset the matrix. More... | |
void | save (File &file) const override |
Method for saving the matrix to a file. More... | |
virtual void | setDimensions (IndexType rows, IndexType columns) |
Method for setting or changing of the matrix dimensions. More... | |
template<typename Matrix_ > | |
void | setLike (const Matrix_ &matrix) |
Set the matrix dimensions to be equal to those of the input matrix. More... | |
![]() | |
virtual | ~Object ()=default |
Destructor. More... | |
virtual std::string | getSerializationTypeVirtual () const |
void | load (const String &fileName) |
Method for restoring the object from a file. More... | |
virtual void | load (File &file) |
Method for restoring the object from a file. More... | |
void | save (const String &fileName) const |
Method for saving the object to a file as a binary data. More... | |
virtual void | save (File &file) const |
Method for saving the object to a file as a binary data. More... | |
Static Public Member Functions | |
static std::string | getSerializationType () |
Returns string with serialization type. More... | |
static constexpr bool | isBinary () |
Test of binary matrix type. More... | |
static constexpr bool | isSymmetric () |
Test of symmetric matrix type. More... | |
![]() | |
static std::string | getSerializationType () |
Static serialization type getter. More... | |
Protected Attributes | |
ColumnsIndexesVectorType | columnIndexes |
IndexAllocator | indexAllocator |
SegmentsType | segments |
ViewType | view |
![]() | |
IndexType | columns |
IndexType | rows |
ValuesType | values |
Array containing the allocated matrix elements. | |
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Real | is a type of matrix elements. If Real equals bool the matrix is treated as binary and so the matrix elements values are not stored in the memory since we need to remember only coordinates of non-zero elements( which equal one). |
Device | is a device where the matrix is allocated. |
Index | is a type for indexing of the matrix elements. |
MatrixType | specifies a symmetry of matrix. See MatrixType. Symmetric matrices store only lower part of the matrix and its diagonal. The upper part is reconstructed on the fly. GeneralMatrix with no symmetry is used by default. |
Segments | is a structure representing the sparse matrix format. Depending on the pattern of the non-zero elements different matrix formats can perform differently especially on GPUs. By default Algorithms::Segments::CSR format is used. See also Algorithms::Segments::Ellpack, Algorithms::Segments::SlicedEllpack, Algorithms::Segments::ChunkedEllpack, and Algorithms::Segments::BiEllpack. |
ComputeReal | is the same as Real mostly but for binary matrices it is set to Index type. This can be changed by the user, of course. |
RealAllocator | is allocator for the matrix elements values. |
IndexAllocator | is allocator for the matrix elements column indexes. |
using TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::ConstViewType = SparseMatrixView< std::add_const_t< Real >, Device, Index, MatrixType, SegmentsViewTemplate, ComputeRealType > |
Matrix view type for constant instances.
See SparseMatrixView.
using TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::ViewType = SparseMatrixView< Real, Device, Index, MatrixType, SegmentsViewTemplate, ComputeRealType > |
Type of related matrix view.
See SparseMatrixView.
TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::SparseMatrix | ( | const RealAllocatorType & | realAllocator = RealAllocatorType() , |
const IndexAllocatorType & | indexAllocator = IndexAllocatorType() |
||
) |
Constructor only with values and column indexes allocators.
realAllocator | is used for allocation of matrix elements values. |
indexAllocator | is used for allocation of matrix elements column indexes. |
|
explicit |
Copy constructor.
matrix | is the source matrix |
|
defaultnoexcept |
Move constructor.
matrix | is the source matrix |
TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::SparseMatrix | ( | Index_t | rows, |
Index_t | columns, | ||
const RealAllocatorType & | realAllocator = RealAllocatorType() , |
||
const IndexAllocatorType & | indexAllocator = IndexAllocatorType() |
||
) |
Constructor with matrix dimensions.
rows | is number of matrix rows. |
columns | is number of matrix columns. |
realAllocator | is used for allocation of matrix elements values. |
indexAllocator | is used for allocation of matrix elements column indexes. |
|
explicit |
Constructor with matrix rows capacities and number of columns.
The number of matrix rows is given by the size of rowCapacities list.
ListIndex | is the initializer list values type. |
rowCapacities | is a list telling how many matrix elements must be allocated in each row. |
columns | is the number of matrix columns. |
realAllocator | is used for allocation of matrix elements values. |
indexAllocator | is used for allocation of matrix elements column indexes. |
|
explicit |
Constructor with matrix rows capacities given as a vector and number of columns.
The number of matrix rows is given by the size of rowCapacities vector.
RowCapacitiesVector | is the row capacities vector type. Usually it is some of TNL::Containers::Array, TNL::Containers::ArrayView, TNL::Containers::Vector or TNL::Containers::VectorView. |
rowCapacities | is a vector telling how many matrix elements must be allocated in each row. |
columns | is the number of matrix columns. |
realAllocator | is used for allocation of matrix elements values. |
indexAllocator | is used for allocation of matrix elements column indexes. |
|
explicit |
Constructor with matrix dimensions and data in initializer list.
The matrix elements values are given as a list data of triples: { { row1, column1, value1 }, { row2, column2, value2 }, ... }.
rows | is number of matrix rows. |
columns | is number of matrix columns. |
data | is a list of matrix elements values. |
realAllocator | is used for allocation of matrix elements values. |
indexAllocator | is used for allocation of matrix elements column indexes. |
|
explicit |
Constructor with matrix dimensions and data in std::map.
The matrix elements values are given as a map data where keys are std::pair of matrix coordinates ( {row, column} ) and value is the matrix element value.
MapIndex | is a type for indexing rows and columns. |
MapValue | is a type for matrix elements values in the map. |
rows | is number of matrix rows. |
columns | is number of matrix columns. |
map | is std::map containing matrix elements. |
realAllocator | is used for allocation of matrix elements values. |
indexAllocator | is used for allocation of matrix elements column indexes. |
__cuda_callable__ void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::addElement | ( | IndexType | row, |
IndexType | column, | ||
const RealType & | value, | ||
const RealType & | thisElementMultiplicator | ||
) |
Add element at given row and column to given value.
This method can be called from the host system (CPU) no matter where the matrix is allocated. If the matrix is allocated on GPU this method can be called even from device kernels. If the matrix is allocated in GPU device this method is called from CPU, it transfers values of each matrix element separately and so the performance is very low. For higher performance see. SparseMatrix::getRow or SparseMatrix::forElements and SparseMatrix::forAllElements. The call may fail if the matrix row capacity is exhausted.
row | is row index of the element. |
column | is columns index of the element. |
value | is the value the element will be set to. |
thisElementMultiplicator | is multiplicator the original matrix element value is multiplied by before addition of given value. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::forAllElements | ( | Function && | function | ) |
Method for parallel iteration over all matrix elements for non-constant instances.
See SparseMatrix::forElements.
Function | is a type of lambda function that will operate on matrix elements. |
function | is an instance of the lambda function to be called for each matrix element. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::forAllElements | ( | Function && | function | ) | const |
Method for parallel iteration over all matrix elements for constant instances.
See SparseMatrix::forElements.
Function | is a type of lambda function that will operate on matrix elements. |
function | is an instance of the lambda function to be called for each matrix element. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::forAllRows | ( | Function && | function | ) |
Method for parallel iteration over all matrix rows.
In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method SparseMatrix::forAllElements where more than one thread can be mapped to each row.
Function | is type of the lambda function. |
function | is an instance of the lambda function to be called for each row. |
RowView represents matrix row - see TNL::Matrices::SparseMatrix::RowView.
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::forAllRows | ( | Function && | function | ) | const |
Method for parallel iteration over all matrix rows for constant instances.
In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method SparseMatrix::forAllElements where more than one thread can be mapped to each row.
Function | is type of the lambda function. |
function | is an instance of the lambda function to be called for each row. |
RowView represents matrix row - see TNL::Matrices::SparseMatrix::RowView.
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::forElements | ( | IndexType | begin, |
IndexType | end, | ||
Function && | function | ||
) |
Method for parallel iteration over all matrix elements of given rows for non-constant instances.
Function | is type of lambda function that will operate on matrix elements. |
begin | defines beginning of the range [ begin,end ) of rows to be processed. |
end | defines ending of the range [ begin, end ) of rows to be processed. |
function | is an instance of the lambda function to be called for each element of given rows. |
The lambda function function
should be declared like follows:
The localIdx parameter is a rank of the non-zero element in given row.
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::forElements | ( | IndexType | begin, |
IndexType | end, | ||
Function && | function | ||
) | const |
Method for parallel iteration over matrix elements of given rows for constant instances.
Function | is type of lambda function that will operate on matrix elements. |
begin | defines beginning of the range [ begin, end ) of rows to be processed. |
end | defines ending of the range [ begin, end ) of rows to be processed. |
function | is an instance of the lambda function to be called for element of given rows. |
The lambda function function
should be declared like follows:
The localIdx parameter is a rank of the non-zero element in given row.
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::forRows | ( | IndexType | begin, |
IndexType | end, | ||
Function && | function | ||
) |
Method for parallel iteration over matrix rows from interval [ begin, end).
In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method SparseMatrix::forElements where more than one thread can be mapped to each row.
Function | is type of the lambda function. |
begin | defines beginning of the range [ begin,end ) of rows to be processed. |
end | defines ending of the range [ begin, end ) of rows to be processed. |
function | is an instance of the lambda function to be called for each row. |
RowView represents matrix row - see TNL::Matrices::SparseMatrix::RowView.
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::forRows | ( | IndexType | begin, |
IndexType | end, | ||
Function && | function | ||
) | const |
Method for parallel iteration over matrix rows from interval [ begin, end) for constant instances.
In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method SparseMatrix::forElements where more than one thread can be mapped to each row.
Function | is type of the lambda function. |
begin | defines beginning of the range [ begin,end ) of rows to be processed. |
end | defines ending of the range [ begin, end ) of rows to be processed. |
function | is an instance of the lambda function to be called for each row. |
RowView represents matrix row - see TNL::Matrices::SparseMatrix::RowView.
auto TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getColumnIndexes |
Getter of column indexes for nonconstant instances.
auto TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getColumnIndexes |
Getter of column indexes for constant instances.
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getCompressedRowLengths | ( | Vector & | rowLengths | ) | const |
Computes number of non-zeros in each row.
rowLengths | is a vector into which the number of non-zeros in each row will be stored. |
auto TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getConstView |
Returns a non-modifiable view of the sparse matrix.
See SparseMatrixView.
__cuda_callable__ auto TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getElement | ( | IndexType | row, |
IndexType | column | ||
) | const |
Returns value of matrix element at position given by its row and column index.
This method can be called from the host system (CPU) no matter where the matrix is allocated. If the matrix is allocated on GPU this method can be called even from device kernels. If the matrix is allocated in GPU device this method is called from CPU, it transfers values of each matrix element separately and so the performance is very low. For higher performance see. SparseMatrix::getRow or SparseMatrix::forElements and SparseMatrix::forAllElements.
row | is a row index of the matrix element. |
column | i a column index of the matrix element. |
|
overridevirtual |
Returns number of non-zero matrix elements.
This method really counts the non-zero matrix elements and so it returns zero for matrix having all allocated elements set to zero.
Reimplemented from TNL::Matrices::Matrix< Real, Device, Index, RealAllocator >.
__cuda_callable__ Index TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getPaddingIndex |
Returns a padding index value.
Padding index is used for column indexes of padding zeros. Padding zeros are used in some sparse matrix formats for better data alignment in memory.
__cuda_callable__ auto TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getRow | ( | IndexType | rowIdx | ) |
Non-constant getter of simple structure for accessing given matrix row.
rowIdx | is matrix row index. |
See SparseMatrixRowView.
__cuda_callable__ auto TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getRow | ( | IndexType | rowIdx | ) | const |
Constant getter of simple structure for accessing given matrix row.
rowIdx | is matrix row index. |
See SparseMatrixRowView.
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getRowCapacities | ( | Vector & | rowCapacities | ) | const |
Compute capacities of all rows.
The row capacities are not stored explicitly and must be computed.
rowCapacities | is a vector where the row capacities will be stored. |
__cuda_callable__ Index TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getRowCapacity | ( | IndexType | row | ) | const |
Returns capacity of given matrix row.
row | index of matrix row. |
auto TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getSegments |
Getter of segments for non-constant instances.
Segments are a structure for addressing the matrix elements columns and values. In fact, Segments represent the sparse matrix format.
auto TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getSegments |
Getter of segments for constant instances.
Segments are a structure for addressing the matrix elements columns and values. In fact, Segments represent the sparse matrix format.
|
static |
Returns string with serialization type.
The string has a form Matrices::SparseMatrix< RealType, [any_device], IndexType, General/Symmetric, Format, [any_allocator] >
.
|
overridevirtual |
Returns string with serialization type.
See SparseMatrix::getSerializationType.
Reimplemented from TNL::Object.
auto TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getView |
|
inlinestaticconstexpr |
Test of binary matrix type.
|
inlinestaticconstexpr |
Test of symmetric matrix type.
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::load | ( | const String & | fileName | ) |
Method for loading the matrix from the file with given filename.
fileName | is name of the file. |
|
overridevirtual |
Method for loading the matrix from a file.
file | is the input file. |
Reimplemented from TNL::Matrices::Matrix< Real, Device, Index, RealAllocator >.
bool TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::operator!= | ( | const Matrix & | matrix | ) | const |
Comparison operator with another arbitrary matrix type.
matrix | is the right-hand side matrix. |
SparseMatrix & TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::operator= | ( | const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ > & | matrix | ) |
Assignment of dense matrix.
matrix | is input matrix for the assignment. |
SparseMatrix & TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::operator= | ( | const RHSMatrix & | matrix | ) |
Assignment of any matrix type other then this and dense.
Warning: Assignment of symmetric sparse matrix to general sparse matrix does not give correct result, currently. Only the diagonal and the lower part of the matrix is assigned.
matrix | is input matrix for the assignment. |
SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator > & TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::operator= | ( | const SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator > & | matrix | ) |
Copy-assignment of exactly the same matrix type.
matrix | is input matrix for the assignment. |
|
noexcept |
Move-assignment of exactly the same matrix type.
matrix | is input matrix for the assignment. |
bool TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::operator== | ( | const Matrix & | matrix | ) | const |
Comparison operator with another arbitrary matrix type.
matrix | is the right-hand side matrix. |
|
overridevirtual |
Method for printing the matrix to output stream.
str | is the output stream. |
Reimplemented from TNL::Matrices::Matrix< Real, Device, Index, RealAllocator >.
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::reduceAllRows | ( | Fetch & | fetch, |
const Reduce & | reduce, | ||
Keep & | keep, | ||
const FetchReal & | identity | ||
) |
Method for performing general reduction on all matrix rows.
Fetch | is a type of lambda function for data fetch declared as |
The return type of this lambda can be any non void.
Reduce | is a type of lambda function for reduction declared as |
Keep | is a type of lambda function for storing results of reduction in each row. It is declared as |
FetchValue | is type returned by the Fetch lambda function. |
fetch | is an instance of lambda function for data fetch. |
reduce | is an instance of lambda function for reduction. |
keep | in an instance of lambda function for storing results. |
identity | is the identity element for the reduction operation, i.e. element which does not change the result of the reduction. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::reduceAllRows | ( | Fetch & | fetch, |
const Reduce & | reduce, | ||
Keep & | keep, | ||
const FetchReal & | identity | ||
) | const |
Method for performing general reduction on all matrix rows for constant instances.
Fetch | is a type of lambda function for data fetch declared as |
The return type of this lambda can be any non void.
Reduce | is a type of lambda function for reduction declared as |
Keep | is a type of lambda function for storing results of reduction in each row. It is declared as |
FetchValue | is type returned by the Fetch lambda function. |
fetch | is an instance of lambda function for data fetch. |
reduce | is an instance of lambda function for reduction. |
keep | in an instance of lambda function for storing results. |
identity | is the identity element for the reduction operation, i.e. element which does not change the result of the reduction. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::reduceRows | ( | IndexType | begin, |
IndexType | end, | ||
Fetch & | fetch, | ||
const Reduce & | reduce, | ||
Keep & | keep, | ||
const FetchReal & | identity | ||
) |
Method for performing general reduction on matrix rows.
Fetch | is a type of lambda function for data fetch declared as |
The return type of this lambda can be any non void.
Reduce | is a type of lambda function for reduction declared as |
Keep | is a type of lambda function for storing results of reduction in each row. It is declared as |
FetchValue | is type returned by the Fetch lambda function. |
begin | defines beginning of the range [ begin, end ) of rows to be processed. |
end | defines ending of the range [ begin, end ) of rows to be processed. |
fetch | is an instance of lambda function for data fetch. |
reduce | is an instance of lambda function for reduction. |
keep | in an instance of lambda function for storing results. |
identity | is the identity element for the reduction operation, i.e. element which does not change the result of the reduction. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::reduceRows | ( | IndexType | begin, |
IndexType | end, | ||
Fetch & | fetch, | ||
const Reduce & | reduce, | ||
Keep & | keep, | ||
const FetchReal & | identity | ||
) | const |
Method for performing general reduction on matrix rows for constant instances.
Fetch | is a type of lambda function for data fetch declared as |
The return type of this lambda can be any non void.
Reduce | is a type of lambda function for reduction declared as |
Keep | is a type of lambda function for storing results of reduction in each row. It is declared as |
FetchValue | is type returned by the Fetch lambda function. |
begin | defines beginning of the range [ begin, end ) of rows to be processed. |
end | defines ending of the range [ begin, end ) of rows to be processed. |
fetch | is an instance of lambda function for data fetch. |
reduce | is an instance of lambda function for reduction. |
keep | in an instance of lambda function for storing results. |
identity | is the identity element for the reduction operation, i.e. element which does not change the result of the reduction. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::save | ( | const String & | fileName | ) | const |
Method for saving the matrix to the file with given filename.
fileName | is name of the file. |
|
overridevirtual |
Method for saving the matrix to a file.
file | is the output file. |
Reimplemented from TNL::Matrices::Matrix< Real, Device, Index, RealAllocator >.
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::sequentialForAllRows | ( | Function & | function | ) |
This method calls sequentialForRows for all matrix rows.
See SparseMatrix::sequentialForAllRows.
Function | is a type of lambda function that will operate on matrix elements. |
function | is an instance of the lambda function to be called in each row. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::sequentialForAllRows | ( | Function & | function | ) | const |
This method calls sequentialForRows for all matrix rows (for constant instances).
See SparseMatrix::sequentialForRows.
Function | is a type of lambda function that will operate on matrix elements. |
function | is an instance of the lambda function to be called in each row. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::sequentialForRows | ( | IndexType | begin, |
IndexType | end, | ||
Function & | function | ||
) |
Method for sequential iteration over all matrix rows for non-constant instances.
Function | is type of lambda function that will operate on matrix elements. It is should have form like |
RowView represents matrix row - see TNL::Matrices::SparseMatrix::RowView.
begin | defines beginning of the range [ begin, end ) of rows to be processed. |
end | defines ending of the range [ begin, end ) of rows to be processed. |
function | is an instance of the lambda function to be called in each row. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::sequentialForRows | ( | IndexType | begin, |
IndexType | end, | ||
Function & | function | ||
) | const |
Method for sequential iteration over all matrix rows for constant instances.
Function | is type of lambda function that will operate on matrix elements. It is should have form like |
RowView represents matrix row - see TNL::Matrices::SparseMatrix::RowView.
begin | defines beginning of the range [ begin, end ) of rows to be processed. |
end | defines ending of the range [ begin, end ) of rows to be processed. |
function | is an instance of the lambda function to be called in each row. |
|
virtual |
Set number of columns of this matrix.
Unlike setDimensions, the storage is not reset in this operation. It is the user's responsibility to update the column indices stored in the matrix to be consistent with the new number of columns.
columns | is the number of matrix columns. |
|
overridevirtual |
Set number of rows and columns of this matrix.
rows | is the number of matrix rows. |
columns | is the number of matrix columns. |
Reimplemented from TNL::Matrices::Matrix< Real, Device, Index, RealAllocator >.
__cuda_callable__ void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::setElement | ( | IndexType | row, |
IndexType | column, | ||
const RealType & | value | ||
) |
Sets element at given row and column to given value.
This method can be called from the host system (CPU) no matter where the matrix is allocated. If the matrix is allocated on GPU this method can be called even from device kernels. If the matrix is allocated in GPU device this method is called from CPU, it transfers values of each matrix element separately and so the performance is very low. For higher performance see. SparseMatrix::getRow or SparseMatrix::forElements and SparseMatrix::forAllElements. The call may fail if the matrix row capacity is exhausted.
row | is row index of the element. |
column | is columns index of the element. |
value | is the value the element will be set to. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::setElements | ( | const std::initializer_list< std::tuple< IndexType, IndexType, RealType > > & | data | ) |
This method sets the sparse matrix elements from initializer list.
The number of matrix rows and columns must be set already. The matrix elements values are given as a list data of triples: { { row1, column1, value1 }, { row2, column2, value2 }, ... }.
data | is a initializer list of initializer lists representing list of matrix rows. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::setElements | ( | const std::map< std::pair< MapIndex, MapIndex >, MapValue > & | map | ) |
This method sets the sparse matrix elements from std::map.
The matrix elements values are given as a map data where keys are std::pair of matrix coordinates ( {row, column} ) and value is the matrix element value.
MapIndex | is a type for indexing rows and columns. |
MapValue | is a type for matrix elements values in the map. |
map | is std::map containing matrix elements. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::setLike | ( | const Matrix & | matrix | ) |
Set the number of matrix rows and columns by the given matrix.
Matrix | is matrix type. This can be any matrix having methods getRows and getColumns. |
matrix | in the input matrix dimensions of which are to be adopted. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::setRowCapacities | ( | const RowsCapacitiesVector & | rowCapacities | ) |
Allocates memory for non-zero matrix elements.
The size of the input vector must be equal to the number of matrix rows. The number of allocated matrix elements for each matrix row depends on the sparse matrix format. Some formats may allocate more elements than required.
RowsCapacitiesVector | is a type of vector/array used for row capacities setting. |
rowCapacities | is a vector telling the number of required non-zero matrix elements in each row. |
void TNL::Matrices::SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::vectorProduct | ( | const InVector & | inVector, |
OutVector & | outVector, | ||
ComputeRealType | matrixMultiplicator = 1.0 , |
||
ComputeRealType | outVectorMultiplicator = 0.0 , |
||
IndexType | begin = 0 , |
||
IndexType | end = 0 |
||
) | const |
Computes product of matrix and vector.
More precisely, it computes:
InVector | is type of input vector. It can be TNL::Containers::Vector, TNL::Containers::VectorView, TNL::Containers::Array, TNL::Containers::ArrayView, or similar container. |
OutVector | is type of output vector. It can be TNL::Containers::Vector, TNL::Containers::VectorView, TNL::Containers::Array, TNL::Containers::ArrayView, or similar container. |
inVector | is input vector. |
outVector | is output vector. |
matrixMultiplicator | is a factor by which the matrix is multiplied. It is one by default. |
outVectorMultiplicator | is a factor by which the outVector is multiplied before added to the result of matrix-vector product. It is zero by default. |
begin | is the beginning of the rows range for which the vector product is computed. It is zero by default. |
end | is the end of the rows range for which the vector product is computed. It is number if the matrix rows by default. |