Template Numerical Library version\ main:4e58ea6
|
"Matrix-free matrix" based on lambda functions. More...
#include <TNL/Matrices/LambdaMatrix.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 | DeviceType = Device |
The device where the matrix is allocated. | |
using | IndexType = Index |
The type used for matrix elements indexing. | |
using | MatrixElementsLambdaType = MatrixElementsLambda |
Type of the lambda function returning the matrix elements. | |
using | RealType = Real |
The type of matrix elements. | |
using | RowView = LambdaMatrixRowView< MatrixElementsLambdaType, CompressedRowLengthsLambdaType, RealType, IndexType > |
Type of Lambda matrix row view. | |
Public Member Functions | |
LambdaMatrix (const LambdaMatrix &matrix)=default | |
Copy constructor. | |
LambdaMatrix (IndexType rows, IndexType columns, MatrixElementsLambda &matrixElements, CompressedRowLengthsLambda &compressedRowLengths) | |
Constructor with matrix dimensions and lambda functions defining the matrix elements. | |
LambdaMatrix (LambdaMatrix &&matrix) noexcept=default | |
Move constructor. | |
LambdaMatrix (MatrixElementsLambda &matrixElements, CompressedRowLengthsLambda &compressedRowLengths) | |
Constructor with lambda functions defining the matrix elements. | |
template<typename Function > | |
void | forAllElements (Function &function) const |
This method calls forElements for all matrix rows (for constant instances). | |
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) const |
Method for iteration over all matrix rows for constant instances. | |
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. | |
__cuda_callable__ IndexType | getColumns () const |
Returns a number of matrix columns. | |
template<typename RowLengthsVector > | |
void | getCompressedRowLengths (RowLengthsVector &rowLengths) const |
Computes number of non-zeros in each row. | |
template<typename Vector > | |
void | getCompressedRowLengths (Vector &rowLengths) const |
__cuda_callable__ const CompressedRowLengthsLambda & | getCompressedRowLengthsLambda () const |
Get reference to the lambda function returning number of non-zero elements in each row. | |
RealType | getElement (IndexType row, IndexType column) const |
Returns value of matrix element at position given by its row and column index. | |
__cuda_callable__ const MatrixElementsLambda & | getMatrixElementsLambda () const |
Get reference to the lambda function returning the matrix elements values and column indexes. | |
IndexType | getNonzeroElementsCount () const |
Returns number of non-zero matrix elements. | |
__cuda_callable__ ConstRowView | getRow (IndexType rowIdx) const |
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 | getRows () const |
Returns a number of matrix rows. | |
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 &identity) const |
Method for performing general reduction on ALL matrix rows. | |
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. | |
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) 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. | |
template<typename InVector , typename OutVector > | |
void | vectorProduct (const InVector &inVector, OutVector &outVector, const RealType &matrixMultiplicator=1.0, const RealType &outVectorMultiplicator=0.0, IndexType begin=0, IndexType end=0) const |
Computes product of matrix and vector. | |
Static Public Member Functions | |
static constexpr bool | isBinary () |
static constexpr bool | isSymmetric () |
Protected Attributes | |
IndexType | columns |
CompressedRowLengthsLambda | compressedRowLengthsLambda |
MatrixElementsLambda | matrixElementsLambda |
IndexType | rows |
"Matrix-free matrix" based on lambda functions.
The elements of this matrix are not stored explicitly in memory but implicitly on a form of lambda functions.
MatrixElementsLambda | is a lambda function returning matrix elements values and positions. |
MatrixElementsLambda | is a lambda function returning matrix elements values and positions. |
It has the following form:
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.
CompressedRowLengthsLambda | is a lambda function returning a number of non-zero elements in each row. |
It has the following form:
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.
Real | is a type of matrix elements values. |
Device | is a device on which the lambda functions will be evaluated. |
Index | is a type to be used for indexing. |
TNL::Matrices::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::LambdaMatrix | ( | MatrixElementsLambda & | matrixElements, |
CompressedRowLengthsLambda & | compressedRowLengths ) |
Constructor with lambda functions defining the matrix elements.
Note: It might be difficult to express the types of the lambdas. For easier creation of LambdaMatrix you may use LambdaMatrixFactory.
matrixElements | is a lambda function giving matrix elements position and value. |
compressedRowLengths | is a lambda function returning how many non-zero matrix elements are in given row. |
TNL::Matrices::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::LambdaMatrix | ( | IndexType | rows, |
IndexType | columns, | ||
MatrixElementsLambda & | matrixElements, | ||
CompressedRowLengthsLambda & | compressedRowLengths ) |
Constructor with matrix dimensions and lambda functions defining the matrix elements.
Note: It might be difficult to express the types of the lambdas. For easier creation of LambdaMatrix you may use LambdaMatrixFactory.
rows | is a number of the matrix rows. |
columns | is a number of the matrix columns. |
matrixElements | is a lambda function giving matrix elements position and value. |
compressedRowLengths | is a lambda function returning how many non-zero matrix elements are in given row. |
|
default |
Copy constructor.
matrix | is input matrix. |
|
defaultnoexcept |
Move constructor.
matrix | is input matrix. |
void TNL::Matrices::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::forAllElements | ( | Function & | function | ) | const |
This method calls forElements for all matrix rows (for constant instances).
See LambdaMatrix::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 in each row. |
void TNL::Matrices::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::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 LambdaMatrix::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::LambdaMatrix::RowView.
void TNL::Matrices::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::forElements | ( | IndexType | begin, |
IndexType | end, | ||
Function & | function ) const |
Method for 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 |
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::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::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 LambdaMatrix::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::LambdaMatrix::RowView.
|
nodiscard |
Returns a number of matrix columns.
void TNL::Matrices::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::getCompressedRowLengths | ( | RowLengthsVector & | 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 |
Get reference to the lambda function returning number of non-zero elements in each row.
|
nodiscard |
Returns value of matrix element at position given by its row and column index.
row | is a row index of the matrix element. |
column | i a column index of the matrix element. |
|
nodiscard |
Get reference to the lambda function returning the matrix elements values and column indexes.
|
nodiscard |
Returns number of non-zero matrix elements.
|
nodiscard |
Getter of simple structure for accessing given matrix row.
rowIdx | is matrix row index. |
See LambdaMatrixRowView.
void TNL::Matrices::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::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 a number of matrix rows.
void TNL::Matrices::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::print | ( | std::ostream & | str | ) | const |
Method for printing the matrix to output stream.
str | is the output stream. |
void TNL::Matrices::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::reduceAllRows | ( | Fetch && | fetch, |
const Reduce & | reduce, | ||
Keep && | keep, | ||
const FetchReal & | identity ) const |
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::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::reduceRows | ( | IndexType | begin, |
IndexType | end, | ||
Fetch && | fetch, | ||
const Reduce & | reduce, | ||
Keep && | keep, | ||
const FetchReal & | identity ) const |
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::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::sequentialForAllRows | ( | Function && | function | ) | const |
This method calls sequentialForRows for all matrix rows (for constant instances).
See LambdaMatrix::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::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::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::LambdaMatrix::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::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::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. |
void TNL::Matrices::LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >::vectorProduct | ( | const InVector & | inVector, |
OutVector & | outVector, | ||
const RealType & | matrixMultiplicator = 1.0, | ||
const RealType & | 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. |