Template Numerical Library version\ main:bb09b17
|
Template of a sparse matrix that can be used for testing of new sparse-matrix formats. More...
#include <TNL/Matrices/Sandbox/SparseSandboxMatrix.h>
Public Types | |
using | ColumnsIndexesVectorType |
using | ColumnsIndexesViewType = typename ColumnsIndexesVectorType::ViewType |
using | ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType |
using | ConstRowCapacitiesView = typename RowCapacitiesView::ConstViewType |
using | ConstRowView = SparseSandboxMatrixRowView< ConstValuesViewType, ConstColumnsIndexesViewType, isBinary() > |
Type for accessing constant matrix rows. | |
using | ConstValuesViewType = typename ValuesViewType::ConstViewType |
using | ConstViewType = SparseSandboxMatrixView< std::add_const_t< Real >, Device, Index, MatrixType > |
Matrix view type for constant instances. | |
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 = std::remove_const_t< Real > |
The type of matrix elements. | |
using | RowCapacitiesType = Containers::Vector< std::remove_const_t< Index >, Device, Index, IndexAllocator > |
using | RowCapacitiesView = Containers::VectorView< std::remove_const_t< Index >, Device, Index > |
using | RowPointers = TNL::Containers::Vector< IndexType, DeviceType, IndexType > |
Type of container for CSR row pointers. | |
using | RowView = SparseSandboxMatrixRowView< ValuesViewType, ColumnsIndexesViewType, isBinary() > |
Type for accessing matrix rows. | |
template<typename _Real = Real, typename _Device = Device, typename _Index = Index, typename _MatrixType = MatrixType, typename _RealAllocator = typename Allocators::Default< _Device >::template Allocator< _Real >, typename _IndexAllocator = typename Allocators::Default< _Device >::template Allocator< _Index >> | |
using | Self = SparseSandboxMatrix< _Real, _Device, _Index, _MatrixType, _RealAllocator, _IndexAllocator > |
Helper type for getting self type or its modifications. | |
using | ValuesVectorType = Containers::Vector< Real, Device, Index, RealAllocator > |
using | ValuesViewType = typename ValuesVectorType::ViewType |
using | ViewType = SparseSandboxMatrixView< Real, Device, Index, MatrixType > |
Type of related matrix view. | |
Public Member Functions | |
SparseSandboxMatrix (const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType()) | |
Constructor only with values and column indexes allocators. | |
template<typename RowCapacitiesVector , std::enable_if_t< TNL::IsArrayType< RowCapacitiesVector >::value, int > = 0> | |
SparseSandboxMatrix (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. | |
SparseSandboxMatrix (const SparseSandboxMatrix &matrix)=default | |
Copy constructor. | |
template<typename ListIndex > | |
SparseSandboxMatrix (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. | |
template<typename Index_t , std::enable_if_t< std::is_integral< Index_t >::value, int > = 0> | |
SparseSandboxMatrix (Index_t rows, Index_t columns, const RealAllocatorType &realAllocator=RealAllocatorType(), const IndexAllocatorType &indexAllocator=IndexAllocatorType()) | |
Constructor with matrix dimensions. | |
SparseSandboxMatrix (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. | |
template<typename MapIndex , typename MapValue > | |
SparseSandboxMatrix (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. | |
SparseSandboxMatrix (SparseSandboxMatrix &&matrix) noexcept=default | |
Move constructor. | |
__cuda_callable__ void | addElement (IndexType row, IndexType column, const RealType &value, const RealType &thisElementMultiplicator) |
Add element at given row and column to given value. | |
template<typename Function > | |
void | forAllElements (Function &&function) |
Method for parallel iteration over all matrix elements for non-constant instances. | |
template<typename Function > | |
void | forAllElements (Function &&function) const |
Method for parallel iteration over all matrix elements for constant instances. | |
template<typename Function > | |
void | forAllRows (Function &&function) |
Method for parallel iteration over all matrix rows. | |
template<typename Function > | |
void | forAllRows (Function &&function) const |
Method for parallel iteration over all matrix rows for constant instances. | |
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. | |
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. | |
template<typename Function > | |
void | forRows (IndexType begin, IndexType end, Function &&function) |
Method for parallel iteration over matrix rows from interval [begin, end) . | |
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. | |
ColumnsIndexesVectorType & | getColumnIndexes () |
Getter of column indexes for nonconstant instances. | |
const ColumnsIndexesVectorType & | getColumnIndexes () const |
Getter of segments for non-constant instances. | |
__cuda_callable__ IndexType | getColumns () const |
Returns number of matrix columns. | |
template<typename Vector > | |
void | getCompressedRowLengths (Vector &rowLengths) const |
Computes number of non-zeros in each row. | |
ConstViewType | getConstView () const |
Returns a non-modifiable view of the sparse matrix. | |
__cuda_callable__ RealType | getElement (IndexType row, IndexType column) const |
Returns value of matrix element at position given by its row and column index. | |
IndexType | getNonzeroElementsCount () const |
Returns number of non-zero matrix elements. | |
__cuda_callable__ RowView | getRow (const IndexType &rowIdx) |
Non-constant getter of simple structure for accessing given matrix row. | |
__cuda_callable__ ConstRowView | getRow (const IndexType &rowIdx) const |
Constant getter of simple structure for accessing given matrix row. | |
template<typename Vector > | |
void | getRowCapacities (Vector &rowCapacities) const |
Compute capacities of all rows. | |
__cuda_callable__ IndexType | getRowCapacity (IndexType row) const |
Returns capacity of given matrix row. | |
__cuda_callable__ IndexType | getRows () const |
Returns number of matrix rows. | |
std::string | getSerializationTypeVirtual () const override |
Returns string with serialization type. | |
template<typename Real2 , typename Index2 > | |
void | getTransposition (const SparseSandboxMatrix< Real2, Device, Index2, MatrixType > &matrix, const RealType &matrixMultiplicator=1.0) |
ValuesVectorType & | getValues () |
Returns a reference to a vector with the matrix elements values. | |
const ValuesVectorType & | getValues () const |
Returns a constant reference to a vector with the matrix elements values. | |
ViewType | getView () |
Returns a modifiable view of the sparse matrix. | |
void | load (const String &fileName) |
Method for loading the matrix from the file with given filename. | |
void | load (File &file) override |
Method for loading the matrix from a file. | |
template<typename Matrix > | |
bool | operator!= (const Matrix &matrix) const |
Comparison operator with another arbitrary matrix type. | |
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization, typename RealAllocator_ > | |
SparseSandboxMatrix & | operator= (const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ > &matrix) |
Assignment of dense matrix. | |
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization, typename RealAllocator_ > | |
SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator > & | operator= (const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ > &matrix) |
template<typename RHSMatrix > | |
SparseSandboxMatrix & | operator= (const RHSMatrix &matrix) |
Assignment of any matrix type other then this and dense. | |
template<typename RHSMatrix > | |
SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator > & | operator= (const RHSMatrix &matrix) |
SparseSandboxMatrix & | operator= (const SparseSandboxMatrix &matrix) |
Assignment of exactly the same matrix type. | |
template<typename Device_ > | |
SparseSandboxMatrix & | operator= (const SparseSandboxMatrix< RealType, Device_, IndexType, MatrixType, RealAllocator, IndexAllocator > &matrix) |
Assignment of exactly the same matrix type but different device. | |
template<typename Device_ > | |
SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator > & | operator= (const SparseSandboxMatrix< RealType, Device_, IndexType, MatrixType, RealAllocator, IndexAllocator > &matrix) |
template<typename Matrix > | |
bool | operator== (const Matrix &matrix) const |
Comparison operator with another arbitrary matrix type. | |
void | print (std::ostream &str) const |
Method for printing the matrix to output stream. | |
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal > | |
void | reduceAllRows (Fetch &&fetch, const Reduce &reduce, Keep &&keep, const FetchReal &zero) const |
Method for performing general reduction on all matrix rows for constant instances. | |
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal > | |
void | reduceRows (IndexType begin, IndexType end, Fetch &&fetch, const Reduce &reduce, Keep &&keep, const FetchReal &zero) const |
Method for performing general reduction on matrix rows for constant instances. | |
template<typename Fetch , typename Reduce , typename Keep , typename FetchValue > | |
void | reduceRows (IndexType begin, IndexType end, Fetch &&fetch, const Reduce &reduce, Keep &&keep, const FetchValue &zero) 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. | |
void | save (File &file) const override |
Method for saving the matrix to a file. | |
template<typename Function > | |
void | sequentialForAllRows (Function &function) |
This method calls sequentialForRows for all matrix rows. | |
template<typename Function > | |
void | sequentialForAllRows (Function &function) const |
This method calls sequentialForRows for all matrix rows (for constant instances). | |
template<typename Function > | |
void | sequentialForRows (IndexType begin, IndexType end, Function &function) |
Method for sequential iteration over all matrix rows for non-constant instances. | |
template<typename Function > | |
void | sequentialForRows (IndexType begin, IndexType end, Function &function) const |
Method for sequential iteration over all matrix rows for constant instances. | |
void | setDimensions (IndexType rows, IndexType columns) |
Set number of rows and columns of this matrix. | |
__cuda_callable__ void | setElement (IndexType row, IndexType column, const RealType &value) |
Sets element at given row and column to given value. | |
void | setElements (const std::initializer_list< std::tuple< IndexType, IndexType, RealType > > &data) |
This method sets the sparse matrix elements from initializer list. | |
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. | |
template<typename Matrix > | |
void | setLike (const Matrix &matrix) |
Set the number of matrix rows and columns by the given matrix. | |
template<typename Matrix_ > | |
void | setLike (const Matrix_ &matrix) |
template<typename RowCapacitiesVector > | |
void | setRowCapacities (const RowCapacitiesVector &rowCapacities) |
Allocates memory for non-zero matrix elements. | |
template<typename InVector , typename OutVector > | |
void | vectorProduct (const InVector &inVector, OutVector &outVector, RealType matrixMultiplicator=1.0, RealType outVectorMultiplicator=0.0, IndexType begin=0, IndexType end=0) const |
Computes product of matrix and vector. | |
Public Member Functions inherited from TNL::Object | |
virtual | ~Object ()=default |
Destructor. | |
void | load (const String &fileName) |
Method for restoring the object from a file. | |
void | save (const String &fileName) const |
Method for saving the object to a file as a binary data. | |
Static Public Member Functions | |
static std::string | getSerializationType () |
Returns string with serialization type. | |
static constexpr bool | isBinary () |
Test of binary matrix type. | |
static constexpr bool | isSymmetric () |
Test of symmetric matrix type. | |
Static Public Member Functions inherited from TNL::Object | |
static std::string | getSerializationType () |
Static serialization type getter. | |
Protected Attributes | |
ColumnsIndexesVectorType | columnIndexes |
Vector containing the column indices of non-zero matrix elements. | |
IndexType | columns |
IndexAllocator | indexAllocator |
RowPointers | rowPointers |
Container for CSR row pointers. | |
IndexType | rows |
ValuesVectorType | values |
Array containing the allocated matrix elements. | |
ViewType | view |
Instance of the sparse matrix view. | |
Template of a sparse matrix that can be used for testing of new sparse-matrix formats.
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. |
RealAllocator | is allocator for the matrix elements values. |
IndexAllocator | is allocator for the matrix elements column indexes. |
This class can be used for rapid testing and development of new formats for sparse matrices. One may profit from several TNL tools compatible with interface of this templated class like:
In the core of this class there is:
(\ref SparseSandboxMatrix::values) which is used for storing of matrix elements values.
Vector
columnIndexes` (SparseSandboxMatrix::columnIndexes). This vector is used for storing of matrix elements column indexes.This class contains fully functional implementation of CSR format and so the user have to replace just what he needs to. Once you have successfully implemented the sparse matrix format in this form, you may consider to extract it into a form of segments to make it accessible even for other algorithms then SpMV.
Parts of the code, that need to be modified are marked by SANDBOX_TODO tag. The whole implementation consits of the following classes:
SparseSandboxMatrix
are common with SparseSandboxMatrixView
and in this case they are implemented in the view class (and there is just redirection from this class). For this reason, SparseSandboxMatrix
contains instance of the view class (SparseSandboxMatrix::view) which needs to be regularly updated each time when metadata are changed. This is usually done by the means of method SparseSandboxMatrix::getView.We suggest the following way of implementation of the new sparse matrix format:
src/UnitTests/Matrices/SparseMatrixTests_SandboxMatrix.h
and src/UnitTests/Matrices/SparseMatrixVectorProductTests_SandboxMatrix.h
rowPointers
.tnl-benchmark-spmv
- you need to include your new matrix to src/Benchmarks/SpMV/spmv.h
and modify this file accordingly.reduceRows
implemented, you may use the original implementation of SpMV based just on the reduceRows
method.During the implementation some unit tests may crash. If you do not need them at the moment, you may comment them in files src/UnitTests/Matrices/SparseMatrixTests.h
and src/UnitTests/Matrices/SparseMatrixVectorProductTests.h
using TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::ColumnsIndexesVectorType |
using TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::ConstViewType = SparseSandboxMatrixView< std::add_const_t< Real >, Device, Index, MatrixType > |
Matrix view type for constant instances.
using TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::RowPointers = TNL::Containers::Vector< IndexType, DeviceType, IndexType > |
Type of container for CSR row pointers.
SANDBOX_TODO: You may replace it with containers for metadata of your format.
using TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::ViewType = SparseSandboxMatrixView< Real, Device, Index, MatrixType > |
Type of related matrix view.
TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::SparseSandboxMatrix | ( | 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. |
|
default |
Copy constructor.
matrix | is the source matrix |
|
defaultnoexcept |
Move constructor.
matrix | is the source matrix |
TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::SparseSandboxMatrix | ( | 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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. |
ConstRowView represents matrix row - see TNL::Matrices::SparseMatrix::ConstRowView.
void TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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. |
ConstRowView represents matrix row - see TNL::Matrices::SparseMatrix::ConstRowView.
|
nodiscard |
Getter of column indexes for nonconstant instances.
|
nodiscard |
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.
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.
Getter of column indexes for constant instances.
|
nodiscard |
Returns number of matrix columns.
void TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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. |
|
nodiscard |
Returns a non-modifiable view of the sparse matrix.
|
nodiscard |
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. |
|
nodiscard |
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.
|
nodiscard |
Non-constant getter of simple structure for accessing given matrix row.
rowIdx | is matrix row index. |
See SparseMatrixRowView.
|
nodiscard |
Constant getter of simple structure for accessing given matrix row.
rowIdx | is matrix row index. |
See SparseMatrixRowView.
void TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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. |
|
nodiscard |
Returns capacity of given matrix row.
row | index of matrix row. |
|
nodiscard |
Returns number of matrix rows.
|
staticnodiscard |
Returns string with serialization type.
The string has a form Matrices::SparseSandboxMatrix< RealType, [any_device], IndexType, General/Symmetric, Format, [any_allocator] >
.
|
nodiscardoverridevirtual |
Returns string with serialization type.
See SparseSandboxMatrix::getSerializationType.
Reimplemented from TNL::Object.
|
nodiscard |
Returns a reference to a vector with the matrix elements values.
|
nodiscard |
Returns a constant reference to a vector with the matrix elements values.
|
nodiscard |
Returns a modifiable view of the sparse matrix.
|
inlinestaticconstexpr |
Test of binary matrix type.
|
inlinestaticconstexpr |
Test of symmetric matrix type.
void TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Object.
|
nodiscard |
Comparison operator with another arbitrary matrix type.
matrix | is the right-hand side matrix. |
SparseSandboxMatrix & TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::operator= | ( | const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ > & | matrix | ) |
Assignment of dense matrix.
matrix | is input matrix for the assignment. |
SparseSandboxMatrix & TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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. |
SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator > & TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::operator= | ( | const SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator > & | matrix | ) |
Assignment of exactly the same matrix type.
matrix | is input matrix for the assignment. |
SparseSandboxMatrix & TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::operator= | ( | const SparseSandboxMatrix< RealType, Device_, IndexType, MatrixType, RealAllocator, IndexAllocator > & | matrix | ) |
Assignment of exactly the same matrix type but different device.
matrix | is input matrix for the assignment. |
|
nodiscard |
Comparison operator with another arbitrary matrix type.
matrix | is the right-hand side matrix. |
void TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::print | ( | std::ostream & | str | ) | const |
Method for printing the matrix to output stream.
str | is the output stream. |
void TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::reduceAllRows | ( | Fetch && | fetch, |
const Reduce & | reduce, | ||
Keep && | keep, | ||
const FetchReal & | zero ) 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 fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue . The return type of this lambda can be any non void. |
Reduce | is a type of lambda function for reduction declared as reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue . |
Keep | is a type of lambda function for storing results of reduction in each row. It is declared as keep( const IndexType rowIdx, const double& value ) . |
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. |
zero | is the identity element of given reduction operation. |
void TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::reduceRows | ( | IndexType | begin, |
IndexType | end, | ||
Fetch && | fetch, | ||
const Reduce & | reduce, | ||
Keep && | keep, | ||
const FetchReal & | zero ) const |
Method for performing general reduction on matrix rows for constant instances.
Fetch | is a type of lambda function for data fetch declared as fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue . The return type of this lambda can be any non void. |
Reduce | is a type of lambda function for reduction declared as reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue . |
Keep | is a type of lambda function for storing results of reduction in each row. It is declared as keep( const IndexType rowIdx, const double& value ) . |
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. |
zero | is the identity element of given reduction operation. |
void TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Object.
void TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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 should have form like function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx_, RealType& value ) . The column index repeats twice only for compatibility with sparse matrices. |
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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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 should have form like function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx_, const RealType& value ) . The column index repeats twice only for compatibility with sparse matrices. |
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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::setDimensions | ( | IndexType | rows, |
IndexType | columns ) |
Set number of rows and columns of this matrix.
rows | is the number of matrix rows. |
columns | is the number of matrix columns. |
__cuda_callable__ void TNL::Matrices::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::setRowCapacities | ( | const RowCapacitiesVector & | 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.
RowCapacitiesVector | 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::Sandbox::SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::vectorProduct | ( | const InVector & | inVector, |
OutVector & | outVector, | ||
RealType | matrixMultiplicator = 1.0, | ||
RealType | outVectorMultiplicator = 0.0, | ||
IndexType | begin = 0, | ||
IndexType | end = 0 ) const |
Computes product of matrix and vector.
More precisely, it computes:
outVector = matrixMultiplicator * ( * this ) * inVector + outVectorMultiplicator * outVector
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. |
|
protected |
Container for CSR row pointers.
SANDBOX_TODO: You may replace it with containers and metadata required by you format.