Template Numerical Library version main:e6bb26f
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator > Class Template Reference

Implementation of sparse tridiagonal matrix. More...

#include <TNL/Matrices/TridiagonalMatrix.h>

Inheritance diagram for TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:
Inheritance graph
[legend]
Collaboration diagram for TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:
Collaboration graph
[legend]

Public Types

using BaseType = Matrix< Real, Device, Index, RealAllocator >
 
using ConstRowView = typename ViewType::ConstRowView
 Type for accessing constant matrix rows.
 
using ConstViewType = TridiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >
 Matrix view type for constant instances. More...
 
using DeviceType = Device
 The device where the matrix is allocated.
 
using IndexerType = details::TridiagonalMatrixIndexer< Index, Organization >
 
using IndexType = Index
 The type used for matrix elements indexing.
 
using RealAllocatorType = RealAllocator
 The allocator for matrix elements values.
 
using RealType = Real
 The type of matrix elements.
 
using RowView = typename ViewType::RowView
 Type for accessing matrix rows.
 
template<typename _Real = Real, typename _Device = Device, typename _Index = Index, ElementsOrganization _Organization = Algorithms::Segments::DefaultElementsOrganization< _Device >::getOrganization(), typename _RealAllocator = typename Allocators::Default< _Device >::template Allocator< _Real >>
using Self = TridiagonalMatrix< _Real, _Device, _Index, _Organization, _RealAllocator >
 Helper type for getting self type or its modifications.
 
using ValuesVectorType = typename BaseType::ValuesType
 
using ValuesViewType = typename ValuesVectorType::ViewType
 
using ViewType = TridiagonalMatrixView< Real, Device, Index, Organization >
 Type of related matrix view. More...
 
- Public Types inherited from TNL::Matrices::Matrix< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >
using ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType
 
using ConstValuesType = Containers::Vector< std::add_const_t< double >, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >
 Type of constant vector holding values of matrix elements.
 
using ConstValuesView = typename ViewType::ConstValuesView
 Type of constant vector view holding values of matrix elements.
 
using ConstViewType = typename MatrixView< double, Devices::Host, int >::ConstViewType
 Type of base matrix view for constant instances.
 
using DeviceType = Devices::Host
 The device where the matrix is allocated.
 
using IndexType = int
 The type used for matrix elements indexing.
 
using RealAllocatorType = typename Allocators::Default< Devices::Host >::template Allocator< double >
 
using RealType = double
 The type of matrix elements.
 
using RowsCapacitiesType = Containers::Vector< int, Devices::Host, int >
 
using RowsCapacitiesView = Containers::VectorView< int, Devices::Host, int >
 
using ValuesType = Containers::Vector< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >
 Type of vector holding values of matrix elements.
 
using ValuesView = typename ViewType::ValuesView
 Type of vector view holding values of matrix elements.
 
using ViewType = MatrixView< double, Devices::Host, int >
 Type of base matrix view.
 

Public Member Functions

 TridiagonalMatrix ()
 Constructor with no parameters.
 
 TridiagonalMatrix (const TridiagonalMatrix &matrix)=default
 Copy constructor. More...
 
template<typename ListReal >
 TridiagonalMatrix (IndexType columns, const std::initializer_list< std::initializer_list< ListReal > > &data)
 Constructor with matrix dimensions, diagonals offsets and matrix elements. More...
 
 TridiagonalMatrix (IndexType rows, IndexType columns)
 Constructor with matrix dimensions. More...
 
 TridiagonalMatrix (TridiagonalMatrix &&matrix) noexcept=default
 Move constructor. More...
 
void addElement (IndexType row, IndexType column, const RealType &value, const RealType &thisElementTriplicator=1.0)
 Add element at given row and column to given value. More...
 
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization_, typename RealAllocator_ >
void addMatrix (const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ > &matrix, const RealType &matrixTriplicator=1.0, const RealType &thisMatrixTriplicator=1.0)
 
template<typename Function >
void forAllElements (Function &function)
 Method for iteration over all matrix rows for non-constant instances. More...
 
template<typename Function >
void forAllElements (Function &function) const
 Method for iteration over all matrix rows for constant instances. More...
 
template<typename Function >
void forAllRows (Function &&function)
 Method for parallel iteration over all matrix rows. More...
 
template<typename Function >
void forAllRows (Function &&function) const
 Method for parallel iteration over all matrix rows for constant instances. More...
 
template<typename Function >
void forElements (IndexType begin, IndexType end, Function &function)
 Method for iteration over matrix rows for non-constant instances. More...
 
template<typename Function >
void forElements (IndexType begin, IndexType end, Function &function) const
 Method for iteration over matrix rows for constant instances. More...
 
template<typename Function >
void forRows (IndexType begin, IndexType end, Function &&function)
 Method for parallel iteration over matrix rows from interval [ begin, end). More...
 
template<typename Function >
void forRows (IndexType begin, IndexType end, Function &&function) const
 Method for parallel iteration over matrix rows from interval [ begin, end) for constant instances. More...
 
template<typename Vector >
void getCompressedRowLengths (Vector &rowLengths) const
 Computes number of non-zeros in each row. More...
 
ConstViewType getConstView () const
 Returns a non-modifiable view of the tridiagonal matrix. More...
 
RealType getElement (IndexType row, IndexType column) const
 Returns value of matrix element at position given by its row and column index. More...
 
IndexerTypegetIndexer ()
 This method returns matrix elements indexer used by this matrix. More...
 
const IndexerTypegetIndexer () const
 This method returns matrix elements indexer used by this matrix. More...
 
IndexType getNonzeroElementsCount () const override
 Returns number of non-zero matrix elements. More...
 
__cuda_callable__ IndexType getPaddingIndex () const
 Returns padding index denoting padding zero elements. More...
 
__cuda_callable__ RowView getRow (const IndexType &rowIdx)
 Non-constant getter of simple structure for accessing given matrix row. More...
 
__cuda_callable__ const ConstRowView getRow (const IndexType &rowIdx) const
 Constant getter of simple structure for accessing given matrix row. More...
 
template<typename Vector >
void getRowCapacities (Vector &rowCapacities) const
 Compute capacities of all rows. More...
 
std::string getSerializationTypeVirtual () const override
 Returns string with serialization type. More...
 
template<typename Real2 , typename Index2 >
void getTransposition (const TridiagonalMatrix< Real2, Device, Index2 > &matrix, const RealType &matrixTriplicator=1.0)
 
ViewType getView () const
 Returns a modifiable view of the tridiagonal matrix. More...
 
void load (const String &fileName)
 Method for loading the matrix from the file with given filename. More...
 
void load (File &file) override
 Method for loading the matrix from a file. More...
 
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization_, typename RealAllocator_ >
bool operator!= (const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ > &matrix) const
 Comparison operator with another tridiagonal matrix. More...
 
TridiagonalMatrixoperator= (const TridiagonalMatrix &matrix)
 Assignment of exactly the same matrix type. More...
 
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization_, typename RealAllocator_ >
TridiagonalMatrixoperator= (const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ > &matrix)
 Assignment of another tridiagonal matrix. More...
 
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization_, typename RealAllocator_ >
TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator > & operator= (const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ > &matrix)
 
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization_, typename RealAllocator_ >
bool operator== (const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ > &matrix) const
 Comparison operator with another tridiagonal matrix. More...
 
void print (std::ostream &str) const override
 Method for printing the matrix to output stream. More...
 
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal >
void reduceAllRows (Fetch &fetch, Reduce &reduce, Keep &keep, const FetchReal &identity)
 Method for performing general reduction on all matrix rows. More...
 
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal >
void reduceAllRows (Fetch &fetch, Reduce &reduce, Keep &keep, const FetchReal &identity) const
 Method for performing general reduction on all matrix rows of constant matrix instances. More...
 
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal >
void reduceRows (IndexType begin, IndexType end, Fetch &fetch, Reduce &reduce, Keep &keep, const FetchReal &identity)
 Method for performing general reduction on matrix rows. More...
 
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal >
void reduceRows (IndexType begin, IndexType end, Fetch &fetch, Reduce &reduce, Keep &keep, const FetchReal &identity) const
 Method for performing general reduction on matrix rows of constant matrix instances. More...
 
void reset ()
 Resets the matrix to zero dimensions.
 
void save (const String &fileName) const
 Method for saving the matrix to the file with given filename. More...
 
void save (File &file) const override
 Method for saving the matrix to a file. More...
 
template<typename Function >
void sequentialForAllRows (Function &function)
 This method calls sequentialForRows for all matrix rows. More...
 
template<typename Function >
void sequentialForAllRows (Function &function) const
 This method calls sequentialForRows for all matrix rows (for constant instances). More...
 
template<typename Function >
void sequentialForRows (IndexType begin, IndexType end, Function &function)
 Method for sequential iteration over all matrix rows for non-constant instances. More...
 
template<typename Function >
void sequentialForRows (IndexType begin, IndexType end, Function &function) const
 Method for sequential iteration over all matrix rows for constant instances. More...
 
void setDimensions (IndexType rows, IndexType columns) override
 Set matrix dimensions. More...
 
void setElement (IndexType row, IndexType column, const RealType &value)
 Sets element at given row and column to given value. More...
 
template<typename ListReal >
void setElements (const std::initializer_list< std::initializer_list< ListReal > > &data)
 Set matrix elements from an initializer list. More...
 
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization_, typename RealAllocator_ >
void setLike (const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ > &matrix)
 Setup the matrix dimensions and diagonals offsets based on another tridiagonal matrix. More...
 
template<typename RowCapacitiesVector >
void setRowCapacities (const RowCapacitiesVector &rowCapacities)
 This method is for compatibility with SparseMatrix. More...
 
void setValue (const RealType &value)
 Set all matrix elements to given value. More...
 
template<typename InVector , typename OutVector >
void vectorProduct (const InVector &inVector, OutVector &outVector, RealType matrixTriplicator=1.0, RealType outVectorTriplicator=0.0, IndexType begin=0, IndexType end=0) const
 Computes product of matrix and vector. More...
 
- Public Member Functions inherited from TNL::Matrices::Matrix< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >
 Matrix (const RealAllocatorType &allocator=RealAllocatorType())
 Construct a new Matrix object possibly with user defined allocator of the matrix values. More...
 
 Matrix (IndexType rows, IndexType columns, const RealAllocatorType &allocator=RealAllocatorType())
 Construct a new Matrix object with given dimensions and possibly user defined allocator of the matrix values. More...
 
IndexType getAllocatedElementsCount () const
 Tells the number of allocated matrix elements. More...
 
__cuda_callable__ IndexType getColumns () const
 Returns number of matrix columns. More...
 
virtual IndexType getNonzeroElementsCount () const
 Computes a current number of nonzero matrix elements. More...
 
__cuda_callable__ IndexType getRows () const
 Returns number of matrix rows. More...
 
ValuesTypegetValues ()
 Returns a reference to a vector with the matrix elements values. More...
 
const ValuesTypegetValues () const
 Returns a constant reference to a vector with the matrix elements values. More...
 
void load (File &file) override
 Method for loading the matrix from a file. More...
 
bool operator!= (const Matrix &matrix) const
 Comparison operator with another arbitrary matrix type. More...
 
bool operator!= (const MatrixT &matrix) const
 
bool operator== (const Matrix &matrix) const
 Comparison operator with another arbitrary matrix type. More...
 
bool operator== (const MatrixT &matrix) const
 
virtual void print (std::ostream &str) const
 Method for printing the matrix to output stream. More...
 
void reset ()
 Reset the matrix. More...
 
void save (File &file) const override
 Method for saving the matrix to a file. More...
 
virtual void setDimensions (IndexType rows, IndexType columns)
 Method for setting or changing of the matrix dimensions. More...
 
void setLike (const Matrix_ &matrix)
 Set the matrix dimensions to be equal to those of the input matrix. More...
 
- Public Member Functions inherited from TNL::Object
virtual ~Object ()=default
 Destructor. More...
 
virtual std::string getSerializationTypeVirtual () const
 
void load (const String &fileName)
 Method for restoring the object from a file. More...
 
virtual void load (File &file)
 Method for restoring the object from a file. More...
 
void save (const String &fileName) const
 Method for saving the object to a file as a binary data. More...
 
virtual void save (File &file) const
 Method for saving the object to a file as a binary data. More...
 

Static Public Member Functions

static constexpr ElementsOrganization getOrganization ()
 
static std::string getSerializationType ()
 Returns string with serialization type. More...
 
static constexpr bool isSymmetric ()
 This is only for compatibility with sparse matrices. More...
 
- Static Public Member Functions inherited from TNL::Object
static std::string getSerializationType ()
 Static serialization type getter. More...
 

Protected Member Functions

__cuda_callable__ IndexType getElementIndex (IndexType row, IndexType localIdx) const
 

Protected Attributes

IndexerType indexer
 
ViewType view
 
- Protected Attributes inherited from TNL::Matrices::Matrix< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >
IndexType columns
 
IndexType rows
 
ValuesType values
 Array containing the allocated matrix elements.
 

Detailed Description

template<typename Real = double, typename Device = Devices::Host, typename Index = int, ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization< Device >::getOrganization(), typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >>
class TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >

Implementation of sparse tridiagonal matrix.

Use this matrix type for storing of tridiagonal matrices i.e., matrices having non-zero matrix elements only on its diagonal and immediately above and bellow the diagonal. This is an example:

\[ \left( \begin{array}{ccccccc} 4 & -1 & . & . & . & . \\ -1 & 4 & -1 & . & . & . \\ . & -1 & 4 & -1 & . & . \\ . & . & -1 & 4 & -1 & . \\ . & . & . & -1 & 4 & -1 \\ . & . & . & . & -1 & 4 \end{array} \right) \]

Advantage is that we do not store the column indexes explicitly as it is in SparseMatrix. This can reduce significantly the memory requirements which also means better performance. See the following table for the storage requirements comparison between TridiagonalMatrix and SparseMatrix.

Real Index SparseMatrix TridiagonalMatrix Ratio
float 32-bit int 8 bytes per element 4 bytes per element 50%
double 32-bit int 12 bytes per element 8 bytes per element 75%
float 64-bit int 12 bytes per element 4 bytes per element 30%
double 64-bit int 16 bytes per element 8 bytes per element 50%
Template Parameters
Realis a type of matrix elements.
Deviceis a device where the matrix is allocated.
Indexis a type for indexing of the matrix elements.
Organizationtells the ordering of matrix elements. It is either RowMajorOrder or ColumnMajorOrder.
RealAllocatoris allocator for the matrix elements.

Member Typedef Documentation

◆ ConstViewType

template<typename Real = double, typename Device = Devices::Host, typename Index = int, ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization< Device >::getOrganization(), typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >>
using TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::ConstViewType = TridiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >

Matrix view type for constant instances.

See TridiagonalMatrixView.

◆ ViewType

template<typename Real = double, typename Device = Devices::Host, typename Index = int, ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization< Device >::getOrganization(), typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >>
using TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::ViewType = TridiagonalMatrixView< Real, Device, Index, Organization >

Type of related matrix view.

See TridiagonalMatrixView.

Constructor & Destructor Documentation

◆ TridiagonalMatrix() [1/4]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::TridiagonalMatrix ( IndexType  rows,
IndexType  columns 
)

Constructor with matrix dimensions.

Parameters
rowsis number of matrix rows.
columnsis number of matrix columns.

◆ TridiagonalMatrix() [2/4]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename ListReal >
TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::TridiagonalMatrix ( IndexType  columns,
const std::initializer_list< std::initializer_list< ListReal > > &  data 
)

Constructor with matrix dimensions, diagonals offsets and matrix elements.

The number of matrix rows is deduced from the size of the initializer list data.

Template Parameters
ListRealis type used in the initializer list defining matrix elements values.
Parameters
columnsis number of matrix columns.
datais initializer list holding matrix elements. The size of the outer list defines the number of matrix rows. Each inner list defines values of each sub-diagonal and so its size should be lower or equal to three. Values of sub-diagonals which do not fit to given row are omitted.
Example
#include <iostream>
#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void createTridiagonalMatrix()
{
const int matrixSize = 6;
/***
* Setup the following matrix (dots represent zeros):
*
* / 2 -1 . . . . \
* | -1 2 -1 . . . |
* | . -1 2 -1 . . |
* | . . -1 2 -1 . |
* | . . . -1 2 -1 |
* \ . . . . -1 2 /
*
*/
matrixSize, {
/***
* To set the matrix elements we first extend the diagonals to their full
* lengths even outside the matrix (dots represent zeros and zeros are
* artificial zeros used for memory alignment):
*
* 0 / 2 -1 . . . . \ -> { 0, 2, -1 }
* | -1 2 -1 . . . | -> { -1, 2, -1 }
* | . -1 2 -1 . . | -> { -1, 2, -1 }
* | . . -1 2 -1 . | -> { -1, 2, -1 }
* | . . . -1 2 -1 | -> { -1, 2, -1 }
* \ . . . . -1 2 / 0 -> { -1, 2, 0 }
*
*/
{ 0, 2, -1 },
{ -1, 2, -1 },
{ -1, 2, -1 },
{ -1, 2, -1 },
{ -1, 2, -1 },
{ -1, 2, 0 }
} );
std::cout << "The matrix reads as: " << std::endl << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating tridiagonal matrix on CPU ... " << std::endl;
createTridiagonalMatrix< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating tridiagonal matrix on CUDA GPU ... " << std::endl;
createTridiagonalMatrix< TNL::Devices::Cuda >();
#endif
}
Implementation of sparse tridiagonal matrix.
Definition: TridiagonalMatrix.h:63
T endl(T... args)
Output
Creating tridiagonal matrix on CPU ...
The matrix reads as:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2 5:-1
Row: 5 -> 4:-1 5:2
Creating tridiagonal matrix on CUDA GPU ...
The matrix reads as:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2 5:-1
Row: 5 -> 4:-1 5:2

◆ TridiagonalMatrix() [3/4]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization< Device >::getOrganization(), typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >>
TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::TridiagonalMatrix ( const TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator > &  matrix)
default

Copy constructor.

Parameters
matrixis an input matrix.

◆ TridiagonalMatrix() [4/4]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization< Device >::getOrganization(), typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >>
TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::TridiagonalMatrix ( TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator > &&  matrix)
defaultnoexcept

Move constructor.

Parameters
matrixis an input matrix.

Member Function Documentation

◆ addElement()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::addElement ( IndexType  row,
IndexType  column,
const RealType value,
const RealType thisElementTriplicator = 1.0 
)

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. TridiagonalMatrix::getRow or TridiagonalMatrix::forElements and TridiagonalMatrix::forAllElements. The call may fail if the matrix row capacity is exhausted.

Parameters
rowis row index of the element.
columnis columns index of the element.
valueis the value the element will be set to.
thisElementTriplicatoris multiplicator the original matrix element value is multiplied by before addition of given value.
Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void addElements()
{
const int matrixSize( 5 );
matrixSize, // number of rows
matrixSize // number of columns
);
for( int i = 0; i < matrixSize; i++ )
matrix.setElement( i, i, i );
std::cout << "Initial matrix is: " << std::endl << matrix << std::endl;
for( int i = 0; i < matrixSize; i++ )
{
if( i > 0 )
matrix.addElement( i, i - 1, 1.0, 5.0 );
matrix.addElement( i, i, 1.0, 5.0 );
if( i < matrixSize - 1 )
matrix.addElement( i, i + 1, 1.0, 5.0 );
}
std::cout << "Matrix after addition is: " << std::endl << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Add elements on host:" << std::endl;
addElements< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Add elements on CUDA device:" << std::endl;
addElements< TNL::Devices::Cuda >();
#endif
}
Output
Add elements on host:
Initial matrix is:
Row: 0 ->
Row: 1 -> 1:1
Row: 2 -> 2:2
Row: 3 -> 3:3
Row: 4 -> 4:4
Matrix after addition is:
Row: 0 -> 0:1 1:1
Row: 1 -> 0:1 1:6 2:1
Row: 2 -> 1:1 2:11 3:1
Row: 3 -> 2:1 3:16 4:1
Row: 4 -> 3:1 4:21
Add elements on CUDA device:
Initial matrix is:
Row: 0 ->
Row: 1 -> 1:1
Row: 2 -> 2:2
Row: 3 -> 3:3
Row: 4 -> 4:4
Matrix after addition is:
Row: 0 -> 0:1 1:1
Row: 1 -> 0:1 1:6 2:1
Row: 2 -> 1:1 2:11 3:1
Row: 3 -> 2:1 3:16 4:1
Row: 4 -> 3:1 4:21

◆ forAllElements() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::forAllElements ( Function &  function)

Method for iteration over all matrix rows for non-constant instances.

Template Parameters
Functionis type of lambda function that will operate on matrix elements. It is should have form like
auto function = [=] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value )
{ ... };
#define __cuda_callable__
Definition: CudaCallable.h:22
Real RealType
The type of matrix elements.
Definition: TridiagonalMatrix.h:74
Index IndexType
The type used for matrix elements indexing.
Definition: TridiagonalMatrix.h:84

The localIdx parameter is a rank of the non-zero element in given row.

Parameters
functionis an instance of the lambda function to be called in each row.
Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forAllElementsExample()
{
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 1 3 . . . \ -> { 0, 1, 3 }
* | 2 1 3 . . | -> { 2, 1, 3 }
* | . 2 1 3 . | -> { 2, 1, 3 }
* | . . 2 1 3 | -> { 2, 1, 3 }
* \ . . . 2 1 / 0 -> { 2, 1, 0 }
*/
5, // number of matrix rows
5 ); // number of matrix columns
auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value ) {
/***
* 'forElements' method iterates only over matrix elements lying on given subdiagonals
* and so we do not need to check anything. The element value can be expressed
* by the 'localIdx' variable, see the following figure:
*
* 0 1 2 <- localIdx values
* -------
* 0 / 1 3 . . . \ -> { 0, 1, 3 }
* | 2 1 3 . . | -> { 2, 1, 3 }
* | . 2 1 3 . | -> { 2, 1, 3 }
* | . . 2 1 3 | -> { 2, 1, 3 }
* \ . . . 2 1 / 0 -> { 2, 1, 0 }
*
*/
value = 3 - localIdx;
};
matrix.forAllElements( f );
std::cout << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating matrix on host: " << std::endl;
forAllElementsExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating matrix on CUDA device: " << std::endl;
forAllElementsExample< TNL::Devices::Cuda >();
#endif
}
Output
Creating matrix on host:
Row: 0 -> 0:2 1:1
Row: 1 -> 0:3 1:2 2:1
Row: 2 -> 1:3 2:2 3:1
Row: 3 -> 2:3 3:2 4:1
Row: 4 -> 3:3 4:2
Creating matrix on CUDA device:
Row: 0 -> 0:2 1:1
Row: 1 -> 0:3 1:2 2:1
Row: 2 -> 1:3 2:2 3:1
Row: 3 -> 2:3 3:2 4:1
Row: 4 -> 3:3 4:2

◆ forAllElements() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::forAllElements ( Function &  function) const

Method for iteration over all matrix rows for constant instances.

Template Parameters
Functionis type of lambda function that will operate on matrix elements. It is should have form like
auto function = [=] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value )
{ ... };

The localIdx parameter is a rank of the non-zero element in given row.

Parameters
functionis an instance of the lambda function to be called in each row.
Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forAllElementsExample()
{
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 1 3 . . . \ -> { 0, 1, 3 }
* | 2 1 3 . . | -> { 2, 1, 3 }
* | . 2 1 3 . | -> { 2, 1, 3 }
* | . . 2 1 3 | -> { 2, 1, 3 }
* \ . . . 2 1 / 0 -> { 2, 1, 0 }
*/
5, // number of matrix rows
5 ); // number of matrix columns
auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value ) {
/***
* 'forElements' method iterates only over matrix elements lying on given subdiagonals
* and so we do not need to check anything. The element value can be expressed
* by the 'localIdx' variable, see the following figure:
*
* 0 1 2 <- localIdx values
* -------
* 0 / 1 3 . . . \ -> { 0, 1, 3 }
* | 2 1 3 . . | -> { 2, 1, 3 }
* | . 2 1 3 . | -> { 2, 1, 3 }
* | . . 2 1 3 | -> { 2, 1, 3 }
* \ . . . 2 1 / 0 -> { 2, 1, 0 }
*
*/
value = 3 - localIdx;
};
matrix.forAllElements( f );
std::cout << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating matrix on host: " << std::endl;
forAllElementsExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating matrix on CUDA device: " << std::endl;
forAllElementsExample< TNL::Devices::Cuda >();
#endif
}
Output
Creating matrix on host:
Row: 0 -> 0:2 1:1
Row: 1 -> 0:3 1:2 2:1
Row: 2 -> 1:3 2:2 3:1
Row: 3 -> 2:3 3:2 4:1
Row: 4 -> 3:3 4:2
Creating matrix on CUDA device:
Row: 0 -> 0:2 1:1
Row: 1 -> 0:3 1:2 2:1
Row: 2 -> 1:3 2:2 3:1
Row: 3 -> 2:3 3:2 4:1
Row: 4 -> 3:3 4:2

◆ forAllRows() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::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 TridiagonalMatrix::forAllElements where more than one thread can be mapped to each row.

Template Parameters
Functionis type of the lambda function.
Parameters
functionis an instance of the lambda function to be called for each row.
auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... };
typename ViewType::RowView RowView
Type for accessing matrix rows.
Definition: TridiagonalMatrix.h:119

RowView represents matrix row - see TNL::Matrices::TridiagonalMatrix::RowView.

Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 2 . . . . \ -> { 0, 0, 1 }
* | -1 2 -1 . . | -> { 0, 2, 1 }
* | . -1 2 -1. . | -> { 3, 2, 1 }
* | . . -1 2 -1 | -> { 3, 2, 1 }
* \ . . . . 2 / -> { 3, 2, 1 }
*
*/
const int size = 5;
MatrixType matrix( size, size );
auto f = [=] __cuda_callable__ ( RowView& row ) {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setElement( 0, -1.0 ); // elements below the diagonal
row.setElement( 1, 2.0 ); // elements on the diagonal
if( rowIdx < size - 1 ) // elements above the diagonal
row.setElement( 2, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Compute sum of elements in each row and store it into a vector.
*/
auto sum_view = sum_vector.getView();
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
double sum( 0.0 );
for( auto element : row )
sum += TNL::abs( element.value() );
sum_view[ row.getRowIndex() ] = sum;
} );
std::cout << "Sums in matrix rows = " << sum_vector << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating matrix on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating matrix on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Vector extends Array with algebraic operations.
Definition: Vector.h:40
__cuda_callable__ T abs(const T &n)
This function returns absolute value of given number n.
Definition: Math.h:87
Output
Creating matrix on host:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]
Creating matrix on CUDA device:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]

◆ forAllRows() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::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 TridiagonalMatrix::forAllElements where more than one thread can be mapped to each row.

Template Parameters
Functionis type of the lambda function.
Parameters
functionis an instance of the lambda function to be called for each row.
auto function = [] __cuda_callable__ ( RowView& row ) { ... };

RowView represents matrix row - see TNL::Matrices::TridiagonalMatrix::RowView.

Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 2 . . . . \ -> { 0, 0, 1 }
* | -1 2 -1 . . | -> { 0, 2, 1 }
* | . -1 2 -1. . | -> { 3, 2, 1 }
* | . . -1 2 -1 | -> { 3, 2, 1 }
* \ . . . . 2 / -> { 3, 2, 1 }
*
*/
const int size = 5;
MatrixType matrix( size, size );
auto f = [=] __cuda_callable__ ( RowView& row ) {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setElement( 0, -1.0 ); // elements below the diagonal
row.setElement( 1, 2.0 ); // elements on the diagonal
if( rowIdx < size - 1 ) // elements above the diagonal
row.setElement( 2, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Compute sum of elements in each row and store it into a vector.
*/
auto sum_view = sum_vector.getView();
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
double sum( 0.0 );
for( auto element : row )
sum += TNL::abs( element.value() );
sum_view[ row.getRowIndex() ] = sum;
} );
std::cout << "Sums in matrix rows = " << sum_vector << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating matrix on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating matrix on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Output
Creating matrix on host:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]
Creating matrix on CUDA device:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]

◆ forElements() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::forElements ( IndexType  begin,
IndexType  end,
Function &  function 
)

Method for iteration over matrix rows for non-constant instances.

Template Parameters
Functionis type of lambda function that will operate on matrix elements. It is should have form like
auto function = [=] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value )
{ ... };

The localIdx parameter is a rank of the non-zero element in given row.

Parameters
begindefines beginning of the range [begin,end) of rows to be processed.
enddefines ending of the range [begin,end) of rows to be processed.
functionis an instance of the lambda function to be called in each row.
Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 2 . . . . \ -> { 0, 0, 1 }
* | -1 2 -1 . . | -> { 0, 2, 1 }
* | . -1 2 -1. . | -> { 3, 2, 1 }
* | . . -1 2 -1 | -> { 3, 2, 1 }
* \ . . . . 2 / -> { 3, 2, 1 }
*
*/
const int size = 5;
MatrixType matrix( size, size );
auto f = [=] __cuda_callable__ ( RowView& row ) {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setElement( 0, -1.0 ); // elements below the diagonal
row.setElement( 1, 2.0 ); // elements on the diagonal
if( rowIdx < size - 1 ) // elements above the diagonal
row.setElement( 2, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Compute sum of elements in each row and store it into a vector.
*/
auto sum_view = sum_vector.getView();
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
double sum( 0.0 );
for( auto element : row )
sum += TNL::abs( element.value() );
sum_view[ row.getRowIndex() ] = sum;
} );
std::cout << "Sums in matrix rows = " << sum_vector << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating matrix on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating matrix on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Output
Creating matrix on host:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]
Creating matrix on CUDA device:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]

◆ forElements() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::forElements ( IndexType  begin,
IndexType  end,
Function &  function 
) const

Method for iteration over matrix rows for constant instances.

Template Parameters
Functionis type of lambda function that will operate on matrix elements. It is should have form like
auto function = [=] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value )
{ ... };

The localIdx parameter is a rank of the non-zero element in given row.

Parameters
begindefines beginning of the range [ begin, end ) of rows to be processed.
enddefines ending of the range [ begin, end ) of rows to be processed.
functionis an instance of the lambda function to be called in each row.
Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 2 . . . . \ -> { 0, 0, 1 }
* | -1 2 -1 . . | -> { 0, 2, 1 }
* | . -1 2 -1. . | -> { 3, 2, 1 }
* | . . -1 2 -1 | -> { 3, 2, 1 }
* \ . . . . 2 / -> { 3, 2, 1 }
*
*/
const int size = 5;
MatrixType matrix( size, size );
auto f = [=] __cuda_callable__ ( RowView& row ) {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setElement( 0, -1.0 ); // elements below the diagonal
row.setElement( 1, 2.0 ); // elements on the diagonal
if( rowIdx < size - 1 ) // elements above the diagonal
row.setElement( 2, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Compute sum of elements in each row and store it into a vector.
*/
auto sum_view = sum_vector.getView();
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
double sum( 0.0 );
for( auto element : row )
sum += TNL::abs( element.value() );
sum_view[ row.getRowIndex() ] = sum;
} );
std::cout << "Sums in matrix rows = " << sum_vector << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating matrix on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating matrix on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Output
Creating matrix on host:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]
Creating matrix on CUDA device:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]

◆ forRows() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::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 TridiagonalMatrix::forElements where more than one thread can be mapped to each row.

Template Parameters
Functionis type of the lambda function.
Parameters
begindefines beginning of the range [ begin,end ) of rows to be processed.
enddefines ending of the range [ begin, end ) of rows to be processed.
functionis an instance of the lambda function to be called for each row.
auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... };

RowView represents matrix row - see TNL::Matrices::TridiagonalMatrix::RowView.

Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 2 . . . . \ -> { 0, 0, 1 }
* | -1 2 -1 . . | -> { 0, 2, 1 }
* | . -1 2 -1. . | -> { 3, 2, 1 }
* | . . -1 2 -1 | -> { 3, 2, 1 }
* \ . . . . 2 / -> { 3, 2, 1 }
*
*/
const int size = 5;
MatrixType matrix( size, size );
auto f = [=] __cuda_callable__ ( RowView& row ) {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setElement( 0, -1.0 ); // elements below the diagonal
row.setElement( 1, 2.0 ); // elements on the diagonal
if( rowIdx < size - 1 ) // elements above the diagonal
row.setElement( 2, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Compute sum of elements in each row and store it into a vector.
*/
auto sum_view = sum_vector.getView();
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
double sum( 0.0 );
for( auto element : row )
sum += TNL::abs( element.value() );
sum_view[ row.getRowIndex() ] = sum;
} );
std::cout << "Sums in matrix rows = " << sum_vector << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating matrix on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating matrix on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Output
Creating matrix on host:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]
Creating matrix on CUDA device:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]

◆ forRows() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::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 TridiagonalMatrix::forElements where more than one thread can be mapped to each row.

Template Parameters
Functionis type of the lambda function.
Parameters
begindefines beginning of the range [ begin,end ) of rows to be processed.
enddefines ending of the range [ begin, end ) of rows to be processed.
functionis an instance of the lambda function to be called for each row.
auto function = [] __cuda_callable__ ( RowView& row ) { ... };

RowView represents matrix row - see TNL::Matrices::TridiagonalMatrix::RowView.

Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 2 . . . . \ -> { 0, 0, 1 }
* | -1 2 -1 . . | -> { 0, 2, 1 }
* | . -1 2 -1. . | -> { 3, 2, 1 }
* | . . -1 2 -1 | -> { 3, 2, 1 }
* \ . . . . 2 / -> { 3, 2, 1 }
*
*/
const int size = 5;
MatrixType matrix( size, size );
auto f = [=] __cuda_callable__ ( RowView& row ) {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setElement( 0, -1.0 ); // elements below the diagonal
row.setElement( 1, 2.0 ); // elements on the diagonal
if( rowIdx < size - 1 ) // elements above the diagonal
row.setElement( 2, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Compute sum of elements in each row and store it into a vector.
*/
auto sum_view = sum_vector.getView();
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
double sum( 0.0 );
for( auto element : row )
sum += TNL::abs( element.value() );
sum_view[ row.getRowIndex() ] = sum;
} );
std::cout << "Sums in matrix rows = " << sum_vector << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating matrix on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating matrix on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Output
Creating matrix on host:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]
Creating matrix on CUDA device:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2
Sums in matrix rows = [ 3, 4, 4, 4, 3 ]

◆ getCompressedRowLengths()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Vector >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getCompressedRowLengths ( Vector &  rowLengths) const

Computes number of non-zeros in each row.

Parameters
rowLengthsis a vector into which the number of non-zeros in each row will be stored.
Example
#include <iostream>
#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void laplaceOperatorMatrix()
{
const int gridSize( 6 );
const int matrixSize = gridSize;
matrixSize, // number of rows
matrixSize // number of columns
);
matrix.setElements( {
{ 0.0, 1.0 },
{ -1.0, 2.0, -1.0 },
{ -1.0, 2.0, -1.0 },
{ -1.0, 2.0, -1.0 },
{ -1.0, 2.0, -1.0 },
{ 0.0, 1.0 }
} );
matrix.getCompressedRowLengths( rowLengths );
std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl;
std::cout << "Compressed row lengths: " << rowLengths << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl;
laplaceOperatorMatrix< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl;
laplaceOperatorMatrix< TNL::Devices::Cuda >();
#endif
}
Output
Creating Laplace operator matrix on CPU ...
Laplace operator matrix:
Row: 0 -> 0:1
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 -> 3:-1 4:2 5:-1
Row: 5 -> 5:1
Compressed row lengths: [ 1, 3, 3, 3, 3, 1 ]
Creating Laplace operator matrix on CUDA GPU ...
Laplace operator matrix:
Row: 0 -> 0:1
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 -> 3:-1 4:2 5:-1
Row: 5 -> 5:1
Compressed row lengths: [ 1, 3, 3, 3, 3, 1 ]

◆ getConstView()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization< Device >::getOrganization(), typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >>
ConstViewType TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getConstView ( ) const

Returns a non-modifiable view of the tridiagonal matrix.

See TridiagonalMatrixView.

Returns
tridiagonal matrix view.

◆ getElement()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
Real TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getElement ( IndexType  row,
IndexType  column 
) const

Returns value of matrix element at position given by its row and column index.

This method can be called from the host system (CPU) no matter where the matrix is allocated. If the matrix is allocated on GPU this method can be called even from device kernels. If the matrix is allocated in GPU device this method is called from CPU, it transfers values of each matrix element separately and so the performance is very low. For higher performance see. TridiagonalMatrix::getRow or TridiagonalMatrix::forElements and TridiagonalMatrix::forAllElements.

Parameters
rowis a row index of the matrix element.
columni a column index of the matrix element.
Returns
value of given matrix element.
Example
#include <iostream>
#include <iomanip>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void getElements()
{
const int matrixSize( 5 );
matrixSize, // number of matrix columns
{ // matrix elements definition
{ 0.0, 2.0, -1.0 },
{ -1.0, 2.0, -1.0 },
{ -1.0, 2.0, -1.0 },
{ -1.0, 2.0, -1.0 },
{ -1.0, 2.0, 0.0 }
} );
for( int i = 0; i < matrixSize; i++ )
{
for( int j = 0; j < matrixSize; j++ )
std::cout << std::setw( 5 ) << matrix.getElement( i, j );
}
}
int main( int argc, char* argv[] )
{
std::cout << "Get elements on host:" << std::endl;
getElements< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Get elements on CUDA device:" << std::endl;
getElements< TNL::Devices::Cuda >();
#endif
}
T setw(T... args)
Output
Get elements on host:
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
Get elements on CUDA device:
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2

◆ getIndexer() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
auto TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getIndexer

This method returns matrix elements indexer used by this matrix.

Returns
non-constant reference to the indexer.

◆ getIndexer() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
auto TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getIndexer

This method returns matrix elements indexer used by this matrix.

Returns
constant reference to the indexer.

◆ getNonzeroElementsCount()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
Index TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getNonzeroElementsCount
overridevirtual

Returns number of non-zero matrix elements.

This method really counts the non-zero matrix elements and so it returns zero for matrix having all allocated elements set to zero.

Returns
number of non-zero matrix elements.

Reimplemented from TNL::Matrices::Matrix< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >.

◆ getPaddingIndex()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
__cuda_callable__ Index TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getPaddingIndex

Returns padding index denoting padding zero elements.

These elements are used for efficient data alignment in memory.

Returns
value of the padding index.

◆ getRow() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
__cuda_callable__ auto TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getRow ( const IndexType rowIdx)

Non-constant getter of simple structure for accessing given matrix row.

Parameters
rowIdxis matrix row index.
Returns
RowView for accessing given matrix row.
Example
#include <iostream>
#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
#include <TNL/Pointers/SharedPointer.h>
template< typename Device >
void getRowExample()
{
const int matrixSize( 5 );
matrixSize, // number of matrix rows
matrixSize // number of matrix columns
);
auto f = [=] __cuda_callable__ ( int rowIdx ) mutable {
//auto row = matrix->getRow( rowIdx );
// For some reason the previous line of code is not accepted by nvcc 10.1
// so we replace it with the following two lines.
auto ref = matrix.modifyData();
auto row = ref.getRow( rowIdx );
if( rowIdx > 0 )
row.setElement( 0, -1.0 ); // elements below the diagonal
row.setElement( 1, 2.0 ); // elements on the diagonal
if( rowIdx < matrixSize - 1 ) // elements above the diagonal
row.setElement( 2, -1.0 );
};
/***
* For the case when Device is CUDA device we need to synchronize smart
* pointers. To avoid this you may use TridiagonalMatrixView. See
* TridiagonalMatrixView::getRow example for details.
*/
TNL::Pointers::synchronizeSmartPointersOnDevice< Device >();
/***
* Set the matrix elements.
*/
std::cout << std::endl << *matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
getRowExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
// It seems that nvcc 10.1 does not handle lambda functions properly.
// It is hard to make nvcc to compile this example and it does not work
// properly. We will try it with later version of CUDA.
//std::cout << "Getting matrix rows on CUDA device: " << std::endl;
//getRowExample< TNL::Devices::Cuda >();
#endif
}
Cross-device shared smart pointer.
Definition: SharedPointer.h:49
T ref(T... args)
static void exec(Index start, Index end, Function f, FunctionArgs... args)
Static method for the execution of the loop.
Definition: ParallelFor.h:87
Output
Getting matrix rows on host:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2

See TridiagonalMatrixRowView.

◆ getRow() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
__cuda_callable__ auto TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getRow ( const IndexType rowIdx) const

Constant getter of simple structure for accessing given matrix row.

Parameters
rowIdxis matrix row index.
Returns
RowView for accessing given matrix row.
Example
#include <iostream>
#include <functional>
#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
#include <TNL/Pointers/SharedPointer.h>
template< typename Device >
void getRowExample()
{
const int matrixSize = 5;
matrixSize, // number of matrix rows
matrixSize // number of matrix columns
);
matrix->setElements(
{ { 0.0, 2.0, 1.0 },
{ 0.0, 2.0, 1.0 },
{ 3.0, 2.0, 1.0 },
{ 3.0, 2.0, 1.0 },
{ 0.0, 2.0, 1.0 } } );
/***
* Fetch lambda function returns diagonal element in each row.
*/
auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double {
auto row = matrix->getRow( rowIdx );
return row.getValue( 2 ); // get value from subdiagonal with index 2, i.e. the main diagonal
};
/***
* For the case when Device is CUDA device we need to synchronize smart
* pointers. To avoid this you may use TridiagonalMatrixView. See
* TridiagonalMatrixView::getConstRow example for details.
*/
TNL::Pointers::synchronizeSmartPointersOnDevice< Device >();
/***
* Compute the matrix trace.
*/
int trace = TNL::Algorithms::reduce< Device >( 0, matrix->getRows(), fetch, std::plus<>{}, 0 );
std::cout << "Matrix reads as: " << std::endl << *matrix << std::endl;
std::cout << "Matrix trace is: " << trace << "." << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
getRowExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
getRowExample< TNL::Devices::Cuda >();
#endif
}
Output
Getting matrix rows on host:
Matrix reads as:
Row: 0 -> 0:2 1:1
Row: 1 -> 1:2 2:1
Row: 2 -> 1:3 2:2 3:1
Row: 3 -> 2:3 3:2 4:1
Row: 4 -> 4:2
Matrix trace is: 5.
Getting matrix rows on CUDA device:
Matrix reads as:
Row: 0 -> 0:2 1:1
Row: 1 -> 1:2 2:1
Row: 2 -> 1:3 2:2 3:1
Row: 3 -> 2:3 3:2 4:1
Row: 4 -> 4:2
Matrix trace is: 5.

See TridiagonalMatrixRowView.

◆ getRowCapacities()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Vector >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getRowCapacities ( Vector &  rowCapacities) const

Compute capacities of all rows.

The row capacities are not stored explicitly and must be computed.

Parameters
rowCapacitiesis a vector where the row capacities will be stored.

◆ getSerializationType()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
std::string TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getSerializationType
static

Returns string with serialization type.

The string has a form Matrices::TridiagonalMatrix< RealType, [any_device], IndexType, ElementsOrganization, [any_allocator] >.

Returns
String with the serialization type.
Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void getSerializationTypeExample()
{
std::cout << "Matrix type is: " << matrix.getSerializationType();
}
int main( int argc, char* argv[] )
{
std::cout << "Get serialization type on CPU ... " << std::endl;
getSerializationTypeExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Get serialization type on CUDA GPU ... " << std::endl;
getSerializationTypeExample< TNL::Devices::Cuda >();
#endif
}
static std::string getSerializationType()
Returns string with serialization type.
Definition: TridiagonalMatrix.hpp:47
Output
Get serialization type on CPU ...
Matrix type is: Matrices::TridiagonalMatrix< double, [any_device], int, RowMajorOrder, [any_allocator] >Get serialization type on CUDA GPU ...
Matrix type is: Matrices::TridiagonalMatrix< double, [any_device], int, ColumnMajorOrder, [any_allocator] >

◆ getSerializationTypeVirtual()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
std::string TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getSerializationTypeVirtual
overridevirtual

Returns string with serialization type.

See TridiagonalMatrix::getSerializationType.

Returns
String with the serialization type.
Example
#include <iostream>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void getSerializationTypeExample()
{
std::cout << "Matrix type is: " << matrix.getSerializationType();
}
int main( int argc, char* argv[] )
{
std::cout << "Get serialization type on CPU ... " << std::endl;
getSerializationTypeExample< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Get serialization type on CUDA GPU ... " << std::endl;
getSerializationTypeExample< TNL::Devices::Cuda >();
#endif
}
Output
Get serialization type on CPU ...
Matrix type is: Matrices::TridiagonalMatrix< double, [any_device], int, RowMajorOrder, [any_allocator] >Get serialization type on CUDA GPU ...
Matrix type is: Matrices::TridiagonalMatrix< double, [any_device], int, ColumnMajorOrder, [any_allocator] >

Reimplemented from TNL::Object.

◆ getView()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
auto TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getView

Returns a modifiable view of the tridiagonal matrix.

See TridiagonalMatrixView.

Returns
tridiagonal matrix view.

◆ isSymmetric()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization< Device >::getOrganization(), typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >>
static constexpr bool TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::isSymmetric ( )
inlinestaticconstexpr

This is only for compatibility with sparse matrices.

Returns
false.

◆ load() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::load ( const String fileName)

Method for loading the matrix from the file with given filename.

Parameters
fileNameis name of the file.

◆ load() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::load ( File file)
overridevirtual

Method for loading the matrix from a file.

Parameters
fileis the input file.

Reimplemented from TNL::Object.

◆ operator!=()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization_, typename RealAllocator_ >
bool TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::operator!= ( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ > &  matrix) const

Comparison operator with another tridiagonal matrix.

Template Parameters
Real_is Real type of the source matrix.
Device_is Device type of the source matrix.
Index_is Index type of the source matrix.
Organization_is Organization of the source matrix.
RealAllocator_is RealAllocator of the source matrix.
Parameters
matrixis the source matrix.
Returns
true if both matrices are NOT identical and false otherwise.

◆ operator=() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator > & TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::operator= ( const TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator > &  matrix)

Assignment of exactly the same matrix type.

Parameters
matrixis input matrix for the assignment.
Returns
reference to this matrix.

◆ operator=() [2/2]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization< Device >::getOrganization(), typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >>
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization_, typename RealAllocator_ >
TridiagonalMatrix & TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::operator= ( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ > &  matrix)

Assignment of another tridiagonal matrix.

Parameters
matrixis input matrix for the assignment.
Returns
reference to this matrix.

◆ operator==()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization_, typename RealAllocator_ >
bool TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::operator== ( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ > &  matrix) const

Comparison operator with another tridiagonal matrix.

Template Parameters
Real_is Real type of the source matrix.
Device_is Device type of the source matrix.
Index_is Index type of the source matrix.
Organization_is Organization of the source matrix.
RealAllocator_is RealAllocator of the source matrix.
Returns
true if both matrices are identical and false otherwise.

◆ print()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::print ( std::ostream str) const
overridevirtual

Method for printing the matrix to output stream.

Parameters
stris the output stream.

Reimplemented from TNL::Matrices::Matrix< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >.

◆ reduceAllRows() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::reduceAllRows ( Fetch &  fetch,
Reduce &  reduce,
Keep &  keep,
const FetchReal &  identity 
)

Method for performing general reduction on all matrix rows.

Template Parameters
Fetchis a type of lambda function for data fetch declared as
auto fetch = [=] __cuda_callable__ ( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue { ...
};

The return type of this lambda can be any non void.

Template Parameters
Reduceis a type of lambda function for reduction declared as
auto reduce = [=] __cuda_callable__ ( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue { ... };
Result reduce(Index begin, Index end, Fetch &&fetch, Reduction &&reduction, const Result &identity)
reduce implements (parallel) reduction for vectors and arrays.
Definition: reduce.h:71
Template Parameters
Keepis a type of lambda function for storing results of reduction in each row. It is declared as
auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const double& value ) { ... };
Template Parameters
FetchValueis type returned by the Fetch lambda function.
Parameters
fetchis an instance of lambda function for data fetch.
reduceis an instance of lambda function for reduction.
keepin an instance of lambda function for storing results.
identityis the identity element for the reduction operation, i.e. element which does not change the result of the reduction.
Example
#include <iostream>
#include <iomanip>
#include <functional>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void reduceRows()
{
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 1 3 . . . \ -> { 0, 1, 3 }
* | 2 1 3 . . | -> { 2, 1, 3 }
* | . 2 1 3 . | -> { 2, 1, 3 }
* | . . 2 1 3 | -> { 2, 1, 3 }
* \ . . . 2 1 / 0 -> { 2, 1, 0 }
*
*/
5, // number of matrix columns
{ { 0, 1, 3 }, // matrix elements
{ 2, 1, 3 },
{ 2, 1, 3 },
{ 2, 1, 3 },
{ 2, 1, 3 } } );
/***
* Find largest element in each row.
*/
TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() );
/***
* Prepare vector view for lambdas.
*/
auto rowMaxView = rowMax.getView();
/***
* Fetch lambda just returns absolute value of matrix elements.
*/
auto fetch = [=] __cuda_callable__ ( int rowIdx, int columnIdx, const double& value ) -> double {
return TNL::abs( value );
};
/***
* Reduce lambda return maximum of given values.
*/
auto reduce = [=] __cuda_callable__ ( double& a, const double& b ) -> double {
return TNL::max( a, b );
};
/***
* Keep lambda store the largest value in each row to the vector rowMax.
*/
auto keep = [=] __cuda_callable__ ( int rowIdx, const double& value ) mutable {
rowMaxView[ rowIdx ] = value;
};
/***
* Compute the largest values in each row.
*/
matrix.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() );
std::cout << "The matrix reads as: " << std::endl << matrix << std::endl;
std::cout << "Max. elements in rows are: " << rowMax << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Rows reduction on host:" << std::endl;
reduceRows< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Rows reduction on CUDA device:" << std::endl;
reduceRows< TNL::Devices::Cuda >();
#endif
}
void reduceRows(IndexType begin, IndexType end, Fetch &fetch, Reduce &reduce, Keep &keep, const FetchReal &identity)
Method for performing general reduction on matrix rows.
Definition: TridiagonalMatrix.hpp:246
constexpr ResultType max(const T1 &a, const T2 &b)
This function returns maximum of two numbers.
Definition: Math.h:61
Output
Rows reduction on host:
The matrix reads as:
Row: 0 -> 0:1 1:3
Row: 1 -> 0:2 1:1 2:3
Row: 2 -> 1:2 2:1 3:3
Row: 3 -> 2:2 3:1 4:3
Row: 4 -> 3:2 4:1
Max. elements in rows are: [ 3, 3, 3, 3, 2 ]
Rows reduction on CUDA device:
The matrix reads as:
Row: 0 -> 0:1 1:3
Row: 1 -> 0:2 1:1 2:3
Row: 2 -> 1:2 2:1 3:3
Row: 3 -> 2:2 3:1 4:3
Row: 4 -> 3:2 4:1
Max. elements in rows are: [ 3, 3, 3, 3, 2 ]

◆ reduceAllRows() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::reduceAllRows ( Fetch &  fetch,
Reduce &  reduce,
Keep &  keep,
const FetchReal &  identity 
) const

Method for performing general reduction on all matrix rows of constant matrix instances.

Template Parameters
Fetchis a type of lambda function for data fetch declared as
auto fetch = [=] __cuda_callable__ ( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue { ...
};

The return type of this lambda can be any non void.

Template Parameters
Reduceis a type of lambda function for reduction declared as
auto reduce = [=] __cuda_callable__ ( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue { ... };
Template Parameters
Keepis a type of lambda function for storing results of reduction in each row. It is declared as
auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const double& value ) { ... };
Template Parameters
FetchValueis type returned by the Fetch lambda function.
Parameters
fetchis an instance of lambda function for data fetch.
reduceis an instance of lambda function for reduction.
keepin an instance of lambda function for storing results.
identityis the identity element for the reduction operation, i.e. element which does not change the result of the reduction.
Example
#include <iostream>
#include <iomanip>
#include <functional>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void reduceRows()
{
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 1 3 . . . \ -> { 0, 1, 3 }
* | 2 1 3 . . | -> { 2, 1, 3 }
* | . 2 1 3 . | -> { 2, 1, 3 }
* | . . 2 1 3 | -> { 2, 1, 3 }
* \ . . . 2 1 / 0 -> { 2, 1, 0 }
*
*/
5, // number of matrix columns
{ { 0, 1, 3 }, // matrix elements
{ 2, 1, 3 },
{ 2, 1, 3 },
{ 2, 1, 3 },
{ 2, 1, 3 } } );
/***
* Find largest element in each row.
*/
TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() );
/***
* Prepare vector view for lambdas.
*/
auto rowMaxView = rowMax.getView();
/***
* Fetch lambda just returns absolute value of matrix elements.
*/
auto fetch = [=] __cuda_callable__ ( int rowIdx, int columnIdx, const double& value ) -> double {
return TNL::abs( value );
};
/***
* Reduce lambda return maximum of given values.
*/
auto reduce = [=] __cuda_callable__ ( double& a, const double& b ) -> double {
return TNL::max( a, b );
};
/***
* Keep lambda store the largest value in each row to the vector rowMax.
*/
auto keep = [=] __cuda_callable__ ( int rowIdx, const double& value ) mutable {
rowMaxView[ rowIdx ] = value;
};
/***
* Compute the largest values in each row.
*/
matrix.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() );
std::cout << "The matrix reads as: " << std::endl << matrix << std::endl;
std::cout << "Max. elements in rows are: " << rowMax << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Rows reduction on host:" << std::endl;
reduceRows< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Rows reduction on CUDA device:" << std::endl;
reduceRows< TNL::Devices::Cuda >();
#endif
}
Output
Rows reduction on host:
The matrix reads as:
Row: 0 -> 0:1 1:3
Row: 1 -> 0:2 1:1 2:3
Row: 2 -> 1:2 2:1 3:3
Row: 3 -> 2:2 3:1 4:3
Row: 4 -> 3:2 4:1
Max. elements in rows are: [ 3, 3, 3, 3, 2 ]
Rows reduction on CUDA device:
The matrix reads as:
Row: 0 -> 0:1 1:3
Row: 1 -> 0:2 1:1 2:3
Row: 2 -> 1:2 2:1 3:3
Row: 3 -> 2:2 3:1 4:3
Row: 4 -> 3:2 4:1
Max. elements in rows are: [ 3, 3, 3, 3, 2 ]

◆ reduceRows() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::reduceRows ( IndexType  begin,
IndexType  end,
Fetch &  fetch,
Reduce &  reduce,
Keep &  keep,
const FetchReal &  identity 
)

Method for performing general reduction on matrix rows.

Template Parameters
Fetchis a type of lambda function for data fetch declared as
auto fetch = [=] __cuda_callable__ ( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue { ...
};

The return type of this lambda can be any non void.

Template Parameters
Reduceis a type of lambda function for reduction declared as
auto reduce = [=] __cuda_callable__ ( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue { ... };
Template Parameters
Keepis a type of lambda function for storing results of reduction in each row. It is declared as
auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const double& value ) { ... };
Template Parameters
FetchValueis type returned by the Fetch lambda function.
Parameters
begindefines beginning of the range [ begin, end ) of rows to be processed.
enddefines ending of the range [ begin, end ) of rows to be processed.
fetchis an instance of lambda function for data fetch.
reduceis an instance of lambda function for reduction.
keepin an instance of lambda function for storing results.
identityis the identity element for the reduction operation, i.e. element which does not change the result of the reduction.
Example
#include <iostream>
#include <iomanip>
#include <functional>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void reduceRows()
{
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 1 3 . . . \ -> { 0, 1, 3 }
* | 2 1 3 . . | -> { 2, 1, 3 }
* | . 2 1 3 . | -> { 2, 1, 3 }
* | . . 2 1 3 | -> { 2, 1, 3 }
* \ . . . 2 1 / 0 -> { 2, 1, 0 }
*
*/
5, // number of matrix columns
{ { 0, 1, 3 }, // matrix elements
{ 2, 1, 3 },
{ 2, 1, 3 },
{ 2, 1, 3 },
{ 2, 1, 3 } } );
/***
* Find largest element in each row.
*/
TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() );
/***
* Prepare vector view for lambdas.
*/
auto rowMaxView = rowMax.getView();
/***
* Fetch lambda just returns absolute value of matrix elements.
*/
auto fetch = [=] __cuda_callable__ ( int rowIdx, int columnIdx, const double& value ) -> double {
return TNL::abs( value );
};
/***
* Reduce lambda return maximum of given values.
*/
auto reduce = [=] __cuda_callable__ ( const double& a, const double& b ) -> double {
return TNL::max( a, b );
};
/***
* Keep lambda store the largest value in each row to the vector rowMax.
*/
auto keep = [=] __cuda_callable__ ( int rowIdx, const double& value ) mutable {
rowMaxView[ rowIdx ] = value;
};
/***
* Compute the largest values in each row.
*/
matrix.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() );
std::cout << "The matrix reads as: " << std::endl << matrix << std::endl;
std::cout << "Max. elements in rows are: " << rowMax << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Rows reduction on host:" << std::endl;
reduceRows< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Rows reduction on CUDA device:" << std::endl;
reduceRows< TNL::Devices::Cuda >();
#endif
}
Output
Rows reduction on host:
The matrix reads as:
Row: 0 -> 0:1 1:3
Row: 1 -> 0:2 1:1 2:3
Row: 2 -> 1:2 2:1 3:3
Row: 3 -> 2:2 3:1 4:3
Row: 4 -> 3:2 4:1
Max. elements in rows are: [ 3, 3, 3, 3, 2 ]
Rows reduction on CUDA device:
The matrix reads as:
Row: 0 -> 0:1 1:3
Row: 1 -> 0:2 1:1 2:3
Row: 2 -> 1:2 2:1 3:3
Row: 3 -> 2:2 3:1 4:3
Row: 4 -> 3:2 4:1
Max. elements in rows are: [ 3, 3, 3, 3, 2 ]

◆ reduceRows() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Fetch , typename Reduce , typename Keep , typename FetchReal >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::reduceRows ( IndexType  begin,
IndexType  end,
Fetch &  fetch,
Reduce &  reduce,
Keep &  keep,
const FetchReal &  identity 
) const

Method for performing general reduction on matrix rows of constant matrix instances.

Template Parameters
Fetchis a type of lambda function for data fetch declared as
auto fetch = [=] __cuda_callable__ ( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue { ...
};

The return type of this lambda can be any non void.

Template Parameters
Reduceis a type of lambda function for reduction declared as
auto reduce = [=] __cuda_callable__ ( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue { ... };
Template Parameters
Keepis a type of lambda function for storing results of reduction in each row. It is declared as
auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const double& value ) { ... };
Template Parameters
FetchValueis type returned by the Fetch lambda function.
Parameters
begindefines beginning of the range [ begin, end ) of rows to be processed.
enddefines ending of the range [ begin, end ) of rows to be processed.
fetchis an instance of lambda function for data fetch.
reduceis an instance of lambda function for reduction.
keepin an instance of lambda function for storing results.
identityis the identity element for the reduction operation, i.e. element which does not change the result of the reduction.
Example
#include <iostream>
#include <iomanip>
#include <functional>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void reduceRows()
{
/***
* Set the following matrix (dots represent zero matrix elements and zeros are
* padding zeros for memory alignment):
*
* 0 / 1 3 . . . \ -> { 0, 1, 3 }
* | 2 1 3 . . | -> { 2, 1, 3 }
* | . 2 1 3 . | -> { 2, 1, 3 }
* | . . 2 1 3 | -> { 2, 1, 3 }
* \ . . . 2 1 / 0 -> { 2, 1, 0 }
*
*/
5, // number of matrix columns
{ { 0, 1, 3 }, // matrix elements
{ 2, 1, 3 },
{ 2, 1, 3 },
{ 2, 1, 3 },
{ 2, 1, 3 } } );
/***
* Find largest element in each row.
*/
TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() );
/***
* Prepare vector view for lambdas.
*/
auto rowMaxView = rowMax.getView();
/***
* Fetch lambda just returns absolute value of matrix elements.
*/
auto fetch = [=] __cuda_callable__ ( int rowIdx, int columnIdx, const double& value ) -> double {
return TNL::abs( value );
};
/***
* Reduce lambda return maximum of given values.
*/
auto reduce = [=] __cuda_callable__ ( const double& a, const double& b ) -> double {
return TNL::max( a, b );
};
/***
* Keep lambda store the largest value in each row to the vector rowMax.
*/
auto keep = [=] __cuda_callable__ ( int rowIdx, const double& value ) mutable {
rowMaxView[ rowIdx ] = value;
};
/***
* Compute the largest values in each row.
*/
matrix.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() );
std::cout << "The matrix reads as: " << std::endl << matrix << std::endl;
std::cout << "Max. elements in rows are: " << rowMax << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Rows reduction on host:" << std::endl;
reduceRows< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Rows reduction on CUDA device:" << std::endl;
reduceRows< TNL::Devices::Cuda >();
#endif
}
Output
Rows reduction on host:
The matrix reads as:
Row: 0 -> 0:1 1:3
Row: 1 -> 0:2 1:1 2:3
Row: 2 -> 1:2 2:1 3:3
Row: 3 -> 2:2 3:1 4:3
Row: 4 -> 3:2 4:1
Max. elements in rows are: [ 3, 3, 3, 3, 2 ]
Rows reduction on CUDA device:
The matrix reads as:
Row: 0 -> 0:1 1:3
Row: 1 -> 0:2 1:1 2:3
Row: 2 -> 1:2 2:1 3:3
Row: 3 -> 2:2 3:1 4:3
Row: 4 -> 3:2 4:1
Max. elements in rows are: [ 3, 3, 3, 3, 2 ]

◆ save() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::save ( const String fileName) const

Method for saving the matrix to the file with given filename.

Parameters
fileNameis name of the file.

◆ save() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::save ( File file) const
overridevirtual

Method for saving the matrix to a file.

Parameters
fileis the output file.

Reimplemented from TNL::Object.

◆ sequentialForAllRows() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::sequentialForAllRows ( Function &  function)

This method calls sequentialForRows for all matrix rows.

See TridiagonalMatrix::sequentialForAllRows.

Template Parameters
Functionis a type of lambda function that will operate on matrix elements.
Parameters
functionis an instance of the lambda function to be called in each row.

◆ sequentialForAllRows() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::sequentialForAllRows ( Function &  function) const

This method calls sequentialForRows for all matrix rows (for constant instances).

See TridiagonalMatrix::sequentialForRows.

Template Parameters
Functionis a type of lambda function that will operate on matrix elements.
Parameters
functionis an instance of the lambda function to be called in each row.

◆ sequentialForRows() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::sequentialForRows ( IndexType  begin,
IndexType  end,
Function &  function 
)

Method for sequential iteration over all matrix rows for non-constant instances.

Template Parameters
Functionis type of lambda function that will operate on matrix elements. It is should have form like
auto function = [] __cuda_callable__ ( RowView& row ) { ... };

RowView represents matrix row - see TNL::Matrices::TridiagonalMatrix::RowView.

Parameters
begindefines beginning of the range [begin,end) of rows to be processed.
enddefines ending of the range [begin,end) of rows to be processed.
functionis an instance of the lambda function to be called in each row.

◆ sequentialForRows() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::sequentialForRows ( IndexType  begin,
IndexType  end,
Function &  function 
) const

Method for sequential iteration over all matrix rows for constant instances.

Template Parameters
Functionis type of lambda function that will operate on matrix elements. It is should have form like
auto function = [] __cuda_callable__ ( RowView& row ) { ... };

RowView represents matrix row - see TNL::Matrices::TridiagonalMatrix::RowView.

Parameters
begindefines beginning of the range [begin,end) of rows to be processed.
enddefines ending of the range [begin,end) of rows to be processed.
functionis an instance of the lambda function to be called in each row.

◆ setDimensions()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::setDimensions ( IndexType  rows,
IndexType  columns 
)
overridevirtual

Set matrix dimensions.

Parameters
rowsis number of matrix rows.
columnsis number of matrix columns.

Reimplemented from TNL::Matrices::Matrix< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >.

◆ setElement()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::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. TridiagonalMatrix::getRow or TridiagonalMatrix::forElements and TridiagonalMatrix::forAllElements. The call may fail if the matrix row capacity is exhausted.

Parameters
rowis row index of the element.
columnis columns index of the element.
valueis the value the element will be set to.
Example
#include <iostream>
#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
#include <TNL/Pointers/SharedPointer.h>
#include <TNL/Pointers/SmartPointersRegister.h>
template< typename Device >
{
const int matrixSize( 5 );
TNL::Pointers::SharedPointer< Matrix > matrix( matrixSize, matrixSize );
for( int i = 0; i < 5; i++ )
matrix->setElement( i, i, i );
std::cout << "Matrix set from the host:" << std::endl;
std::cout << *matrix << std::endl;
auto f = [=] __cuda_callable__ ( int i ) mutable {
if( i > 0 )
matrix->setElement( i, i - 1, 1.0 );
matrix->setElement( i, i, -i );
if( i < matrixSize - 1 )
matrix->setElement( i, i + 1, 1.0 );
};
/***
* For the case when Device is CUDA device we need to synchronize smart
* pointers. To avoid this you may use TridiagonalMatrixView. See
* TridiagonalMatrixView::getRow example for details.
*/
TNL::Pointers::synchronizeSmartPointersOnDevice< Device >();
std::cout << "Matrix set from its native device:" << std::endl;
std::cout << *matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Set elements on host:" << std::endl;
setElements< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Set elements on CUDA device:" << std::endl;
// FIXME: it does not work with nvcc 10.1
// setElements< TNL::Devices::Cuda >();
#endif
}
Matrix(const RealAllocatorType &allocator=RealAllocatorType())
Construct a new Matrix object possibly with user defined allocator of the matrix values.
Definition: Matrix.hpp:19
void setElements(const std::initializer_list< std::initializer_list< ListReal > > &data)
Set matrix elements from an initializer list.
Definition: TridiagonalMatrix.hpp:105
Output
Set elements on host:
Matrix set from the host:
Row: 0 ->
Row: 1 -> 1:1
Row: 2 -> 2:2
Row: 3 -> 3:3
Row: 4 -> 4:4
Matrix set from its native device:
Row: 0 -> 1:1
Row: 1 -> 0:1 1:-1 2:1
Row: 2 -> 1:1 2:-2 3:1
Row: 3 -> 2:1 3:-3 4:1
Row: 4 -> 3:1 4:-4
Set elements on CUDA device:

◆ setElements()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename ListReal >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::setElements ( const std::initializer_list< std::initializer_list< ListReal > > &  data)

Set matrix elements from an initializer list.

Template Parameters
ListRealis data type of the initializer list.
Parameters
datais initializer list holding matrix elements. The size of the outer list defines the number of matrix rows. Each inner list defines values of each sub-diagonal and so its size should be lower or equal to three. Values of sub-diagonals which do not fit to given row are omitted.
Example
#include <iostream>
#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void createTridiagonalMatrix()
{
const int matrixSize = 6;
/***
* Setup the following matrix (dots represent zeros):
*
* / 2 -1 . . . . \
* | -1 2 -1 . . . |
* | . -1 2 -1 . . |
* | . . -1 2 -1 . |
* | . . . -1 2 -1 |
* \ . . . . -1 2 /
*
*/
TNL::Matrices::TridiagonalMatrix< double, Device > matrix( matrixSize, matrixSize );
matrix.setElements( {
/***
* To set the matrix elements we first extend the diagonals to their full
* lengths even outside the matrix (dots represent zeros and zeros are
* artificial zeros used for memory alignment):
*
* 0 / 2 -1 . . . . \ -> { 0, 2, -1 }
* | -1 2 -1 . . . | -> { -1, 2, -1 }
* | . -1 2 -1 . . | -> { -1, 2, -1 }
* | . . -1 2 -1 . | -> { -1, 2, -1 }
* | . . . -1 2 -1 | -> { -1, 2, -1 }
* \ . . . . -1 2 / 0 -> { -1, 2, 0 }
*
*/
{ 0, 2, -1 },
{ -1, 2, -1 },
{ -1, 2, -1 },
{ -1, 2, -1 },
{ -1, 2, -1 },
{ -1, 2, 0 }
} );
std::cout << "The matrix reads as: " << std::endl << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating tridiagonal matrix on CPU ... " << std::endl;
createTridiagonalMatrix< TNL::Devices::Host >();
#ifdef HAVE_CUDA
std::cout << "Creating tridiagonal matrix on CUDA GPU ... " << std::endl;
createTridiagonalMatrix< TNL::Devices::Cuda >();
#endif
}
Output
Creating tridiagonal matrix on CPU ...
The matrix reads as:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2 5:-1
Row: 5 -> 4:-1 5:2
Creating tridiagonal matrix on CUDA GPU ...
The matrix reads as:
Row: 0 -> 0:2 1:-1
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 -> 3:-1 4:2 5:-1
Row: 5 -> 4:-1 5:2

◆ setLike()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Real_ , typename Device_ , typename Index_ , ElementsOrganization Organization_, typename RealAllocator_ >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::setLike ( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ > &  matrix)

Setup the matrix dimensions and diagonals offsets based on another tridiagonal matrix.

Template Parameters
Real_is Real type of the source matrix.
Device_is Device type of the source matrix.
Index_is Index type of the source matrix.
Organization_is Organization of the source matrix.
RealAllocator_is RealAllocator of the source matrix.
Parameters
matrixis the source matrix.

◆ setRowCapacities()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename RowCapacitiesVector >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::setRowCapacities ( const RowCapacitiesVector &  rowCapacities)

This method is for compatibility with SparseMatrix.

It checks if the number of matrix diagonals is compatible with required number of non-zero matrix elements in each row. If not exception is thrown.

Template Parameters
RowCapacitiesVectoris vector-like container type for holding required row capacities.
Parameters
rowCapacitiesis vector-like container holding required row capacities.

◆ setValue()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::setValue ( const RealType value)

Set all matrix elements to given value.

Parameters
valueis the new value of all matrix elements.

◆ vectorProduct()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename InVector , typename OutVector >
void TNL::Matrices::TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::vectorProduct ( const InVector &  inVector,
OutVector &  outVector,
RealType  matrixTriplicator = 1.0,
RealType  outVectorTriplicator = 0.0,
IndexType  begin = 0,
IndexType  end = 0 
) const

Computes product of matrix and vector.

More precisely, it computes:

outVector = matrixTriplicator * ( * this ) * inVector + outVectorTriplicator * outVector
Template Parameters
InVectoris type of input vector. It can be TNL::Containers::Vector, TNL::Containers::VectorView, TNL::Containers::Array, TNL::Containers::ArrayView, or similar container.
OutVectoris type of output vector. It can be TNL::Containers::Vector, TNL::Containers::VectorView, TNL::Containers::Array, TNL::Containers::ArrayView, or similar container.
Parameters
inVectoris input vector.
outVectoris output vector.
matrixTriplicatoris a factor by which the matrix is multiplied. It is one by default.
outVectorTriplicatoris a factor by which the outVector is multiplied before added to the result of matrix-vector product. It is zero by default.
beginis the beginning of the rows range for which the vector product is computed. It is zero by default.
endis the end of the rows range for which the vector product is computed. It is number if the matrix rows by default.

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