Template Numerical Library version\ main:8b8c8226
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView > Class Template Reference

RowView is a simple structure for accessing rows of sparse matrix. More...

#include <TNL/Matrices/SparseMatrixRowView.h>

Public Types

using ColumnsIndexesViewType = ColumnsIndexesView
 Type of container view used for storing the column indexes of the matrix elements.
 
using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType
 Type of constant container view used for storing the column indexes of the matrix elements.
 
using ConstRowView = SparseMatrixRowView< SegmentViewType, ConstValuesViewType, ConstColumnsIndexesViewType >
 Type of constant sparse matrix row view.
 
using ConstValuesViewType = typename ValuesViewType::ConstViewType
 Type of constant container view used for storing the matrix elements values.
 
using IndexType = typename ColumnsIndexesView::IndexType
 The type used for matrix elements indexing.
 
using IteratorType = MatrixRowViewIterator< RowView >
 Type of iterator for the matrix row.
 
using MatrixElementType = SparseMatrixElement< RealType, IndexType >
 The type of related matrix element.
 
using RealType = typename ValuesView::RealType
 The type of matrix elements.
 
using RowView = SparseMatrixRowView< SegmentViewType, ValuesViewType, ColumnsIndexesViewType >
 Type of sparse matrix row view.
 
using SegmentViewType = SegmentView
 Type representing matrix row format.
 
using ValueGetterType = details::SparseMatrixRowViewValueGetter< SegmentView, ValuesView, ColumnsIndexesView >
 
using ValuesViewType = ValuesView
 Type of container view used for storing the matrix elements values.
 

Public Member Functions

__cuda_callable__ SparseMatrixRowView (const SegmentViewType &segmentView, const ValuesViewType &values, const ColumnsIndexesViewType &columnIndexes)
 Constructor with segmentView, values and columnIndexes. More...
 
__cuda_callable__ IteratorType begin ()
 Returns iterator pointing at the beginning of the matrix row. More...
 
__cuda_callable__ const IteratorType cbegin () const
 Returns constant iterator pointing at the beginning of the matrix row. More...
 
__cuda_callable__ const IteratorType cend () const
 Returns constant iterator pointing at the end of the matrix row. More...
 
__cuda_callable__ IteratorType end ()
 Returns iterator pointing at the end of the matrix row. More...
 
__cuda_callable__ IndexTypegetColumnIndex (IndexType localIdx)
 Returns non-constants reference to a column index of an element with given rank in the row. More...
 
__cuda_callable__ const IndexTypegetColumnIndex (IndexType localIdx) const
 Returns constants reference to a column index of an element with given rank in the row. More...
 
__cuda_callable__ IndexType getPaddingIndex () const
 
__cuda_callable__ IndexType getRowIndex () const
 Returns the matrix row index. More...
 
__cuda_callable__ IndexType getSize () const
 Returns size of the matrix row, i.e. number of matrix elements in this row. More...
 
__cuda_callable__ auto getValue (IndexType localIdx) -> typename ValueGetterType::ResultType
 Returns non-constants reference to value of an element with given rank in the row. More...
 
__cuda_callable__ auto getValue (IndexType localIdx) const -> typename ValueGetterType::ConstResultType
 Returns constants reference to value of an element with given rank in the row. More...
 
template<typename _SegmentView , typename _ValuesView , typename _ColumnsIndexesView >
__cuda_callable__ bool operator== (const SparseMatrixRowView< _SegmentView, _ValuesView, _ColumnsIndexesView > &other) const
 Comparison of two matrix rows. More...
 
__cuda_callable__ void setColumnIndex (IndexType localIdx, const IndexType &columnIndex)
 Sets a column index of matrix element with given rank in the matrix row. More...
 
__cuda_callable__ void setElement (IndexType localIdx, IndexType columnIndex, const RealType &value)
 Sets both a value and a column index of matrix element with given rank in the matrix row. More...
 
__cuda_callable__ void setValue (IndexType localIdx, const RealType &value)
 Sets a value of matrix element with given rank in the matrix row. More...
 

Static Public Member Functions

static constexpr bool isBinary ()
 Tells whether the parent matrix is a binary matrix. More...
 

Protected Attributes

ColumnsIndexesViewType columnIndexes
 
SegmentViewType segmentView
 
ValuesViewType values
 

Detailed Description

template<typename SegmentView, typename ValuesView, typename ColumnsIndexesView>
class TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >

RowView is a simple structure for accessing rows of sparse matrix.

Template Parameters
SegmentViewis a segment view of segments representing the matrix format.
ValuesViewis a vector view storing the matrix elements values.
ColumnsIndexesViewis a vector view storing the column indexes of the matrix element.

See SparseMatrix and SparseMatrixView.

Example
#include <iostream>
#include <TNL/Algorithms/parallelFor.h>
#include <TNL/Matrices/SparseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
#include <TNL/Pointers/SharedPointer.h>
template< typename Device >
void getRowExample()
{
auto rowCapacities = { 1, 1, 1, 1, 1 }; // Variadic templates in SharedPointer
// constructor do not recognize initializer
// list so we give it a hint.
TNL::Pointers::SharedPointer< MatrixType > matrix( rowCapacities, 5 );
MatrixType* matrix_device = &matrix.template modifyData< Device >();
auto f = [=] __cuda_callable__ ( int rowIdx ) mutable {
auto row = matrix_device->getRow( rowIdx );
row.setElement( 0, rowIdx, 10 * ( rowIdx + 1 ) );
};
/***
* For the case when Device is CUDA device we need to synchronize smart
* pointers. To avoid this you may use SparseMatrixView. See
* SparseMatrixView::getRow example for details.
*/
TNL::Pointers::synchronizeSmartPointersOnDevice< Device >();
/***
* Set the matrix elements.
*/
TNL::Algorithms::parallelFor< Device >( 0, matrix->getRows(), f );
std::cout << *matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
getRowExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
getRowExample< TNL::Devices::Cuda >();
#endif
}
#define __cuda_callable__
Definition: CudaCallable.h:22
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Definition: SparseMatrix.h:57
Cross-device shared smart pointer.
Definition: SharedPointer.h:49
T endl(T... args)
Structure for specifying type of sparse matrix.
Definition: MatrixType.h:21
Output
Getting matrix rows on host:
Row: 0 -> 0:10
Row: 1 -> 1:20
Row: 2 -> 2:30
Row: 3 -> 3:40
Row: 4 -> 4:50
Getting matrix rows on CUDA device:
Row: 0 -> 0:10
Row: 1 -> 1:20
Row: 2 -> 2:30
Row: 3 -> 3:40
Row: 4 -> 4:50
Example
#include <iostream>
#include <TNL/Algorithms/parallelFor.h>
#include <TNL/Matrices/SparseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void getRowExample()
{
/***
* Set the following matrix (dots represent zero matrix elements):
*
* / 2 . . . . \
* | 1 2 1 . . |
* | . 1 2 1. . |
* | . . 1 2 1 |
* \ . . . . 2 /
*/
const int size = 5;
TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 3, 3, 3, 1 }, size );
auto view = matrix.getView();
/***
* Set the matrix elements.
*/
auto f = [=] __cuda_callable__ ( int rowIdx ) mutable {
auto row = view.getRow( rowIdx );
if( rowIdx == 0 )
row.setElement( 0, rowIdx, 2.0 ); // diagonal element
else if( rowIdx == size - 1 )
row.setElement( 0, rowIdx, 2.0 ); // diagonal element
else
{
row.setElement( 0, rowIdx - 1, 1.0 ); // elements below the diagonal
row.setElement( 1, rowIdx, 2.0 ); // diagonal element
row.setElement( 2, rowIdx + 1, 1.0 ); // elements above the diagonal
}
};
TNL::Algorithms::parallelFor< Device >( 0, matrix.getRows(), f );
std::cout << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
getRowExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
getRowExample< TNL::Devices::Cuda >();
#endif
}
__cuda_callable__ void setElement(IndexType localIdx, IndexType columnIndex, const RealType &value)
Sets both a value and a column index of matrix element with given rank in the matrix row.
Definition: SparseMatrixRowView.hpp:105
ViewType getView()
Returns a modifiable view of the sparse matrix.
Definition: SparseMatrix.hpp:156
__cuda_callable__ ConstRowView getRow(IndexType rowIdx) const
Constant getter of simple structure for accessing given matrix row.
Definition: SparseMatrix.hpp:468
Output
Getting matrix rows on host:
Row: 0 -> 0:2
Row: 1 -> 0:1 1:2 2:1
Row: 2 -> 1:1 2:2 3:1
Row: 3 -> 2:1 3:2 4:1
Row: 4 -> 4:2
Getting matrix rows on CUDA device:
Row: 0 -> 0:2
Row: 1 -> 0:1 1:2 2:1
Row: 2 -> 1:1 2:2 3:1
Row: 3 -> 2:1 3:2 4:1
Row: 4 -> 4:2

Constructor & Destructor Documentation

◆ SparseMatrixRowView()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::SparseMatrixRowView ( const SegmentViewType segmentView,
const ValuesViewType values,
const ColumnsIndexesViewType columnIndexes 
)

Constructor with segmentView, values and columnIndexes.

Parameters
segmentViewinstance of SegmentViewType representing matrix row.
valuesis a container view for storing the matrix elements values.
columnIndexesis a container view for storing the column indexes of the matrix elements.

Member Function Documentation

◆ begin()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ auto TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::begin

Returns iterator pointing at the beginning of the matrix row.

Returns
iterator pointing at the beginning.

◆ cbegin()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ auto TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::cbegin

Returns constant iterator pointing at the beginning of the matrix row.

Returns
iterator pointing at the beginning.

◆ cend()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ auto TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::cend

Returns constant iterator pointing at the end of the matrix row.

Returns
iterator pointing at the end.

◆ end()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ auto TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::end

Returns iterator pointing at the end of the matrix row.

Returns
iterator pointing at the end.

◆ getColumnIndex() [1/2]

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ auto TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::getColumnIndex ( IndexType  localIdx)

Returns non-constants reference to a column index of an element with given rank in the row.

Parameters
localIdxis the rank of the non-zero element in given row.
Returns
non-constant reference to the matrix element column index.

◆ getColumnIndex() [2/2]

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ auto TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::getColumnIndex ( IndexType  localIdx) const

Returns constants reference to a column index of an element with given rank in the row.

Parameters
localIdxis the rank of the non-zero element in given row.
Returns
constant reference to the matrix element column index.

◆ getRowIndex()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ auto TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::getRowIndex

Returns the matrix row index.

Returns
matrix row index.

◆ getSize()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ auto TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::getSize

Returns size of the matrix row, i.e. number of matrix elements in this row.

Returns
Size of the matrix row.

◆ getValue() [1/2]

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ auto TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::getValue ( IndexType  localIdx) -> typename ValueGetterType::ResultType

Returns non-constants reference to value of an element with given rank in the row.

Parameters
localIdxis the rank of the non-zero element in given row.
Returns
non-constant reference to the matrix element value.

◆ getValue() [2/2]

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ auto TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::getValue ( IndexType  localIdx) const -> typename ValueGetterType::ConstResultType

Returns constants reference to value of an element with given rank in the row.

Parameters
localIdxis the rank of the non-zero element in given row.
Returns
constant reference to the matrix element value.

◆ isBinary()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
static constexpr bool TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::isBinary ( )
inlinestaticconstexpr

Tells whether the parent matrix is a binary matrix.

Returns
true if the matrix is binary.

◆ operator==()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
template<typename _SegmentView , typename _ValuesView , typename _ColumnsIndexesView >
__cuda_callable__ bool TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::operator== ( const SparseMatrixRowView< _SegmentView, _ValuesView, _ColumnsIndexesView > &  other) const

Comparison of two matrix rows.

The other matrix row can be from any other matrix.

Parameters
otheris another matrix row.
Returns
true if both rows are the same, false otherwise.

◆ setColumnIndex()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ void TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::setColumnIndex ( IndexType  localIdx,
const IndexType columnIndex 
)

Sets a column index of matrix element with given rank in the matrix row.

Parameters
localIdxis the rank of the matrix element in the row.
columnIndexis the new column index of the matrix element.

◆ setElement()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ void TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::setElement ( IndexType  localIdx,
IndexType  columnIndex,
const RealType value 
)

Sets both a value and a column index of matrix element with given rank in the matrix row.

Parameters
localIdxis the rank of the matrix element in the row.
columnIndexis the new column index of the matrix element.
valueis the new value of the matrix element.

◆ setValue()

template<typename SegmentView , typename ValuesView , typename ColumnsIndexesView >
__cuda_callable__ void TNL::Matrices::SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView >::setValue ( IndexType  localIdx,
const RealType value 
)

Sets a value of matrix element with given rank in the matrix row.

Parameters
localIdxis the rank of the matrix element in the row.
valueis the new value of the matrix element.

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