Template Numerical Library version\ main:94209208
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Attributes | List of all members
TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index > Class Template Reference

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

#include <TNL/Matrices/LambdaMatrixRowView.h>

Public Types

using CompressedRowLengthsLambdaType = CompressedRowLengthsLambda
 Type of the lambda function returning the number of non-zero elements in each row.
 
using ConstRowView = RowView
 Type of constant Lambda matrix row view.
 
using IndexType = Index
 The type used for matrix elements indexing.
 
using IteratorType = LambdaMatrixRowViewIterator< RowView >
 Type of iterator for the matrix row.
 
using MatrixElementsLambdaType = MatrixElementsLambda
 Type of the lambda function returning the matrix elements.
 
using MatrixElementType = LambdaMatrixElement< RealType, IndexType >
 The type of related matrix element.
 
using RealType = Real
 The type of matrix elements.
 
using RowView = LambdaMatrixRowView< MatrixElementsLambdaType, CompressedRowLengthsLambdaType, RealType, IndexType >
 Type of Lambda matrix row view.
 

Public Member Functions

__cuda_callable__ LambdaMatrixRowView (const MatrixElementsLambdaType &matrixElementsLambda, const CompressedRowLengthsLambdaType &compressedRowLengthsLambda, const IndexType &rows, const IndexType &columns, const IndexType &rowIdx)
 Constructor with related lambda functions, matrix dimensions and row index.
 
__cuda_callable__ IteratorType begin () const
 Returns non-constant iterator pointing at the beginning of the matrix row.
 
__cuda_callable__ IteratorType cbegin () const
 Returns constant iterator pointing at the beginning of the matrix row.
 
__cuda_callable__ IteratorType cend () const
 Returns constant iterator pointing at the end of the matrix row.
 
__cuda_callable__ IteratorType end () const
 Returns non-constant iterator pointing at the end of the matrix row.
 
__cuda_callable__ IndexType getColumnIndex (IndexType localIdx) const
 Returns constants reference to a column index of an element with given rank in the row.
 
__cuda_callable__ const IndexTypegetRowIndex () const
 Returns the matrix row index.
 
__cuda_callable__ IndexType getSize () const
 Returns size of the matrix row, i.e. number of matrix elements in this row.
 
__cuda_callable__ RealType getValue (IndexType localIdx) const
 Returns constants reference to value of an element with given rank in the row.
 
template<typename MatrixElementsLambda_ , typename CompressedRowLengthsLambda_ , typename Real_ , typename Index_ >
__cuda_callable__ bool operator== (const LambdaMatrixRowView< MatrixElementsLambda_, CompressedRowLengthsLambda_, Real_, Index_ > &other) const
 Comparison of two matrix rows.
 

Protected Attributes

IndexType columns
 
const CompressedRowLengthsLambda & compressedRowLengthsLambda
 
const MatrixElementsLambda & matrixElementsLambda
 
IndexType rowIdx
 
IndexType rows
 

Detailed Description

template<typename MatrixElementsLambda, typename CompressedRowLengthsLambda, typename Real = double, typename Index = int>
class TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >

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

Template Parameters
MatrixElementsLambdais a lambda function returning matrix elements values and positions.

It has the following form:

auto matrixElements = [] __cuda_callable__ ( Index rows, Index columns, Index rowIdx, Index localIdx, Index& columnIdx, Real&
value ) { ... }
#define __cuda_callable__
Definition Macros.h:49
Definition Real.h:14

where rows is the number of matrix rows, columns is the number of matrix columns, rowIdx is the index of matrix row being queried, localIdx is the rank of the non-zero element in given row, columnIdx is a column index of the matrix element computed by this lambda and value is a value of the matrix element computed by this lambda.

Template Parameters
CompressedRowLengthsLambdais a lambda function returning a number of non-zero elements in each row.

It has the following form:

auto rowLengths = [] __cuda_callable__ ( Index rows, Index columns, Index rowIdx ) -> IndexType { ... }
Index IndexType
The type used for matrix elements indexing.
Definition LambdaMatrixRowView.h:60

where rows is the number of matrix rows, columns is the number of matrix columns and rowIdx is an index of the row being queried.

Template Parameters
Realis a type of matrix elements values.
Indexis a type to be used for indexing.
Example
#include <iostream>
#include <TNL/Algorithms/parallelFor.h>
#include <TNL/Matrices/LambdaMatrix.h>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void
getRowExample()
{
/***
* Set matrix representing approximation of the Laplace operator on regular
* grid using the finite difference method.
*/
const int gridSize( 4 );
const int matrixSize = gridSize * gridSize;
auto rowLengths = [ = ] __cuda_callable__( const int rows, const int columns, const int rowIdx ) -> int
{
const int gridRow = rowIdx / gridSize; // coordinates in the numerical grid
const int gridColumn = rowIdx % gridSize;
if( gridRow == 0 || gridRow == gridSize - 1 || // boundary grid node
gridColumn == 0 || gridColumn == gridSize - 1 )
return 1;
return 5;
};
auto matrixElements =
const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value )
{
const int gridRow = rowIdx / gridSize; // coordinates in the numerical grid
const int gridColumn = rowIdx % gridSize;
if( gridRow == 0 || gridRow == gridSize - 1 || // boundary grid node
gridColumn == 0 || gridColumn == gridSize - 1 )
{
columnIdx = rowIdx; // diagonal element ....
value = 1.0; // ... is set to 1
}
else // interior grid node
{
switch( localIdx ) // set diagonal element to 4
{ // and the others to -1
case 0:
columnIdx = rowIdx - gridSize;
value = 1;
break;
case 1:
columnIdx = rowIdx - 1;
value = 1;
break;
case 2:
columnIdx = rowIdx;
value = -4;
break;
case 3:
columnIdx = rowIdx + 1;
value = 1;
break;
case 4:
columnIdx = rowIdx + gridSize;
value = 1;
break;
}
}
};
auto matrix =
TNL::Matrices::LambdaMatrixFactory< double, Device, int >::create( matrixSize, matrixSize, matrixElements, rowLengths );
TNL::Matrices::DenseMatrix< double, Device > denseMatrix( matrixSize, matrixSize );
denseMatrix.setValue( 0.0 );
auto dense_view = denseMatrix.getView();
auto f = [ = ] __cuda_callable__( const int& rowIdx ) mutable
{
auto row = matrix.getRow( rowIdx );
auto dense_row = dense_view.getRow( rowIdx );
for( int localIdx = 0; localIdx < row.getSize(); localIdx++ )
dense_row.setValue( row.getColumnIndex( localIdx ), row.getValue( localIdx ) );
};
TNL::Algorithms::parallelFor< Device >( 0, matrixSize, f );
std::cout << "Laplace operator lambda matrix: " << std::endl << matrix << std::endl;
std::cout << "Laplace operator dense matrix: " << std::endl << denseMatrix << std::endl;
}
int
main( int argc, char* argv[] )
{
std::cout << "Running example on CPU ... " << std::endl;
getRowExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Running example on CUDA GPU ... " << std::endl;
getRowExample< TNL::Devices::Cuda >();
#endif
}
Implementation of dense matrix, i.e. matrix storing explicitly all of its elements including zeros.
Definition DenseMatrix.h:31
T endl(T... args)
static auto create(MatrixElementsLambda &matrixElementsLambda, CompressedRowLengthsLambda &compressedRowLengthsLambda) -> LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >
Creates lambda matrix with given lambda functions.
Definition LambdaMatrix.h:571
Output
Running example on CPU ...
Laplace operator lambda matrix:
Row: 0 -> 0:1
Row: 1 -> 1:1
Row: 2 -> 2:1
Row: 3 -> 3:1
Row: 4 -> 4:1
Row: 5 -> 1:1 4:1 5:-4 6:1 9:1
Row: 6 -> 2:1 5:1 6:-4 7:1 10:1
Row: 7 -> 7:1
Row: 8 -> 8:1
Row: 9 -> 5:1 8:1 9:-4 10:1 13:1
Row: 10 -> 6:1 9:1 10:-4 11:1 14:1
Row: 11 -> 11:1
Row: 12 -> 12:1
Row: 13 -> 13:1
Row: 14 -> 14:1
Row: 15 -> 15:1
Laplace operator dense matrix:
Row: 0 -> 0:1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 1 -> 0:0 1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 2 -> 0:0 1:0 2:1 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 3 -> 0:0 1:0 2:0 3:1 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 4 -> 0:0 1:0 2:0 3:0 4:1 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 5 -> 0:0 1:1 2:0 3:0 4:1 5:-4 6:1 7:0 8:0 9:1 10:0 11:0 12:0 13:0 14:0 15:0
Row: 6 -> 0:0 1:0 2:1 3:0 4:0 5:1 6:-4 7:1 8:0 9:0 10:1 11:0 12:0 13:0 14:0 15:0
Row: 7 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:1 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 8 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:1 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 9 -> 0:0 1:0 2:0 3:0 4:0 5:1 6:0 7:0 8:1 9:-4 10:1 11:0 12:0 13:1 14:0 15:0
Row: 10 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:1 10:-4 11:1 12:0 13:0 14:1 15:0
Row: 11 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:1 12:0 13:0 14:0 15:0
Row: 12 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:1 13:0 14:0 15:0
Row: 13 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:1 14:0 15:0
Row: 14 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:1 15:0
Row: 15 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:1
Running example on CUDA GPU ...
Laplace operator lambda matrix:
Row: 0 -> 0:1
Row: 1 -> 1:1
Row: 2 -> 2:1
Row: 3 -> 3:1
Row: 4 -> 4:1
Row: 5 -> 1:1 4:1 5:-4 6:1 9:1
Row: 6 -> 2:1 5:1 6:-4 7:1 10:1
Row: 7 -> 7:1
Row: 8 -> 8:1
Row: 9 -> 5:1 8:1 9:-4 10:1 13:1
Row: 10 -> 6:1 9:1 10:-4 11:1 14:1
Row: 11 -> 11:1
Row: 12 -> 12:1
Row: 13 -> 13:1
Row: 14 -> 14:1
Row: 15 -> 15:1
Laplace operator dense matrix:
Row: 0 -> 0:1 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 1 -> 0:0 1:1 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 2 -> 0:0 1:0 2:1 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 3 -> 0:0 1:0 2:0 3:1 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 4 -> 0:0 1:0 2:0 3:0 4:1 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 5 -> 0:0 1:1 2:0 3:0 4:1 5:-4 6:1 7:0 8:0 9:1 10:0 11:0 12:0 13:0 14:0 15:0
Row: 6 -> 0:0 1:0 2:1 3:0 4:0 5:1 6:-4 7:1 8:0 9:0 10:1 11:0 12:0 13:0 14:0 15:0
Row: 7 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:1 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 8 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:1 9:0 10:0 11:0 12:0 13:0 14:0 15:0
Row: 9 -> 0:0 1:0 2:0 3:0 4:0 5:1 6:0 7:0 8:1 9:-4 10:1 11:0 12:0 13:1 14:0 15:0
Row: 10 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:1 7:0 8:0 9:1 10:-4 11:1 12:0 13:0 14:1 15:0
Row: 11 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:1 12:0 13:0 14:0 15:0
Row: 12 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:1 13:0 14:0 15:0
Row: 13 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:1 14:0 15:0
Row: 14 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:1 15:0
Row: 15 -> 0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:1

Constructor & Destructor Documentation

◆ LambdaMatrixRowView()

template<typename MatrixElementsLambda , typename CompressedRowLengthsLambda , typename Real , typename Index >
__cuda_callable__ TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::LambdaMatrixRowView ( const MatrixElementsLambdaType & matrixElementsLambda,
const CompressedRowLengthsLambdaType & compressedRowLengthsLambda,
const IndexType & rows,
const IndexType & columns,
const IndexType & rowIdx )

Constructor with related lambda functions, matrix dimensions and row index.

Parameters
matrixElementsLambdais a constant reference to the lambda function evaluating matrix elements.
compressedRowLengthsLambdais a constant reference to the lambda function returning the number of nonzero elements in each row.
rowsis number of matrix rows.
columnsis number of matrix columns.
rowIdxis the matrix row index.

Member Function Documentation

◆ begin()

template<typename MatrixElementsLambda , typename CompressedRowLengthsLambda , typename Real , typename Index >
__cuda_callable__ auto TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::begin ( ) const

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

Returns
iterator pointing at the beginning.

◆ cbegin()

template<typename MatrixElementsLambda , typename CompressedRowLengthsLambda , typename Real , typename Index >
__cuda_callable__ auto TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::cbegin ( ) const

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

Returns
iterator pointing at the beginning.

◆ cend()

template<typename MatrixElementsLambda , typename CompressedRowLengthsLambda , typename Real , typename Index >
__cuda_callable__ auto TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::cend ( ) const

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

Returns
iterator pointing at the end.

◆ end()

template<typename MatrixElementsLambda , typename CompressedRowLengthsLambda , typename Real , typename Index >
__cuda_callable__ auto TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::end ( ) const

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

Returns
iterator pointing at the end.

◆ getColumnIndex()

template<typename MatrixElementsLambda , typename CompressedRowLengthsLambda , typename Real , typename Index >
__cuda_callable__ auto TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::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 MatrixElementsLambda , typename CompressedRowLengthsLambda , typename Real , typename Index >
__cuda_callable__ auto TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::getRowIndex ( ) const

Returns the matrix row index.

Returns
matrix row index.

◆ getSize()

template<typename MatrixElementsLambda , typename CompressedRowLengthsLambda , typename Real , typename Index >
__cuda_callable__ auto TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::getSize ( ) const

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

Returns
Size of the matrix row.

◆ getValue()

template<typename MatrixElementsLambda , typename CompressedRowLengthsLambda , typename Real , typename Index >
__cuda_callable__ auto TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::getValue ( IndexType localIdx) const

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.

◆ operator==()

template<typename MatrixElementsLambda , typename CompressedRowLengthsLambda , typename Real , typename Index >
template<typename MatrixElementsLambda_ , typename CompressedRowLengthsLambda_ , typename Real_ , typename Index_ >
__cuda_callable__ bool TNL::Matrices::LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::operator== ( const LambdaMatrixRowView< MatrixElementsLambda_, CompressedRowLengthsLambda_, Real_, Index_ > & 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.

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