Template Numerical Library: TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization > Class Template Reference

Template Numerical Library version\ main:c7077e4fd

Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization > Class Template Reference

A common base class for MultidiagonalMatrix and MultidiagonalMatrixView. More...

`#include <TNL/Matrices/MultidiagonalMatrixBase.h>`

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

## Public Types
using | ConstRowView = typename RowView::ConstRowView
| Type for accessing constant matrix rows.
using | DeviceType = Device
| The device where the matrix is allocated.
using | DiagonalOffsetsView
using | HostDiagonalOffsetsView
using | IndexerType = detail::MultidiagonalMatrixIndexer< Index, Organization == Algorithms::Segments::RowMajorOrder >
using | IndexType = Index
| The type used for matrix elements indexing.
using | MatrixType = GeneralMatrix
| The type of matrix can be only general currently.
using | RealType = typename Base::RealType
| The type of matrix elements.
using | RowView = MultidiagonalMatrixRowView< typename Base::ValuesViewType, IndexerType, DiagonalOffsetsView >
| Type for accessing matrix rows.
Public Types inherited from TNL::Matrices::MatrixBase< Real, Device, Index, GeneralMatrix, Organization >
using | ConstValuesViewType
| Type of constant vector view holding values of matrix elements.
using | DeviceType
| The device where the matrix is allocated.
using | IndexType
| The type used for matrix elements indexing.
using | RealType
| The type of matrix elements.
using | RowCapacitiesType
using | ValuesViewType
| Type of vector view holding values of matrix elements.

## Public Member Functions
__cuda_callable__ | MultidiagonalMatrixBase ()=default
| Constructor with no parameters.
__cuda_callable__ | MultidiagonalMatrixBase (const MultidiagonalMatrixBase &)=default
| Copy constructor.
__cuda_callable__ | MultidiagonalMatrixBase (MultidiagonalMatrixBase &&) noexcept=default
| Move constructor.
__cuda_callable__ | MultidiagonalMatrixBase (typename Base::ValuesViewType values, DiagonalOffsetsView diagonalOffsets, HostDiagonalOffsetsView hostDiagonalOffsets, IndexerType indexer)
| Constructor with all necessary data and views.
__cuda_callable__ void | addElement (IndexType row, IndexType column, const RealType &value, const RealType &thisElementMultiplicator=1.0)
| Add element at given row and column to given value.
template<typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_>
void | addMatrix (const MultidiagonalMatrixBase< Real_, Device_, Index_, Organization_ > &matrix, const RealType &matrixMultiplicator=1.0, const RealType &thisMatrixMultiplicator=1.0)
template<typename Function>
void | forAllElements (Function &&function)
| This method calls forElements for all matrix rows.
template<typename Function>
void | forAllElements (Function &&function) const
| This method calls forElements for all matrix rows (for constant instances).
template<typename Condition, typename Function>
void | forAllElementsIf (Condition &&condition, Function &&function)
| This method calls forElementsIf for all matrix rows (for non-constant instances).
template<typename Condition, typename Function>
void | forAllElementsIf (Condition &&condition, Function &&function) const
| This method calls forElementsIf for all matrix rows (for constant instances).
template<typename Function>
void | forAllRows (Function &&function)
| Method for parallel iteration over all matrix rows.
template<typename Function>
void | forAllRows (Function &&function) const
| Method for parallel iteration over all matrix rows for constant instances.
template<typename Array, typename Function>
void | forElements (const Array &rowIndexes, Function &&function)
| Method for iteration over all matrix elements in the rows enlisted in the array rowIndexes. This is variant for non-constant instances.
template<typename Array, typename Function>
void | forElements (const Array &rowIndexes, Function &&function) const
| Method for iteration over all matrix elements in the rows enlisted in the array rowIndexes. This is variant for constant instances.
template<typename Array, typename Function>
void | forElements (const Array &rowIndexes, IndexType begin, IndexType end, Function &&function)
| Method for iteration over all matrix elements in the rows enlisted in the array rowIndexes. This is variant for non-constant instances.
template<typename Array, typename Function>
void | forElements (const Array &rowIndexes, IndexType begin, IndexType end, Function &&function) const
| Method for iteration over all matrix elements in the rows enlisted in the array rowIndexes. This is variant for constant instances.
template<typename Function>
void | forElements (IndexType begin, IndexType end, Function &&function)
| Method for iteration over all matrix rows for non-constant instances.
template<typename Function>
void | forElements (IndexType begin, IndexType end, Function &&function) const
| Method for iteration over all matrix rows for constant instances.
template<typename Condition, typename Function>
void | forElementsIf (IndexType begin, IndexType end, Condition &&condition, Function &&function)
| Method for iterating over all matrix elements that meet a condition based on the row index (for constant instances).
template<typename Condition, typename Function>
void | forElementsIf (IndexType begin, IndexType end, Condition &&condition, Function &&function) const
| Method for iterating over all matrix elements that meet a condition based on the row index (for constant instances).
template<typename Function>
void | forRows (IndexType begin, IndexType end, Function &&function)
| Method for parallel iteration over matrix rows from interval [begin, end).
template<typename Function>
void | forRows (IndexType begin, IndexType end, Function &&function) const
| Method for parallel iteration over matrix rows from interval [begin, end) for constant instances.
template<typename Vector>
void | getCompressedRowLengths (Vector &rowLengths) const
| Computes number of non-zeros in each row.
__cuda_callable__ DiagonalOffsetsView | getDiagonalOffsets ()
| Returns a view for the vector of diagonal offsets.
__cuda_callable__ DiagonalOffsetsView::ConstViewType | getDiagonalOffsets () const
| Returns a view for the vector of diagonal offsets.
__cuda_callable__ IndexType | getDiagonalsCount () const
| Returns number of diagonals.
__cuda_callable__ RealType | getElement (IndexType row, IndexType column) const
| Returns value of matrix element at position given by its row and column index.
__cuda_callable__ IndexerType & | getIndexer ()
| This method returns matrix elements indexer used by this matrix.
__cuda_callable__ const IndexerType & | getIndexer () const
| This method returns matrix elements indexer used by this matrix.
IndexType | getNonzeroElementsCount () const
| Returns number of non-zero matrix elements.
__cuda_callable__ RowView | getRow (IndexType rowIdx)
| Non-constant getter of simple structure for accessing given matrix row.
__cuda_callable__ ConstRowView | getRow (IndexType rowIdx) const
| Constant getter of simple structure for accessing given matrix row.
template<typename Vector>
void | getRowCapacities (Vector &rowCapacities) const
| Compute capacities of all rows.
template<typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_>
bool | operator!= (const MultidiagonalMatrixBase< Real_, Device_, Index_, Organization_ > &matrix) const
| Comparison operator with another multidiagonal matrix.
MultidiagonalMatrixBase & | operator= (const MultidiagonalMatrixBase &)=delete
| Copy-assignment operator.
MultidiagonalMatrixBase & | operator= (MultidiagonalMatrixBase &&)=delete
| Move-assignment operator.
template<typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_>
bool | operator== (const MultidiagonalMatrixBase< Real_, Device_, Index_, Organization_ > &matrix) const
| Comparison operator with another multidiagonal matrix.
void | print (std::ostream &str) const
| Method for printing the matrix to output stream.
template<typename Fetch, typename Reduce, typename Keep, typename FetchReal>
void | reduceAllRows (Fetch &&fetch, const Reduce &reduce, Keep &&keep, const FetchReal &identity) const
| Method for performing general reduction on all matrix rows for constant instances.
template<typename Fetch, typename Reduce, typename Keep, typename FetchReal>
void | reduceRows (IndexType begin, IndexType end, Fetch &&fetch, const Reduce &reduce, Keep &&keep, const FetchReal &identity) const
| Method for performing general reduction on matrix rows for constant instances.
template<typename Function>
void | sequentialForAllRows (Function &function)
| This method calls sequentialForRows for all matrix rows.
template<typename Function>
void | sequentialForAllRows (Function &function) const
| This method calls sequentialForRows for all matrix rows (for constant instances).
template<typename Function>
void | sequentialForRows (IndexType begin, IndexType end, Function &function)
| Method for sequential iteration over all matrix rows for non-constant instances.
template<typename Function>
void | sequentialForRows (IndexType begin, IndexType end, Function &function) const
| Method for sequential iteration over all matrix rows for constant instances.
__cuda_callable__ void | setElement (IndexType row, IndexType column, const RealType &value)
| Sets element at given row and column to given value.
void | setValue (const RealType &value)
| Set all matrix elements to given value.
template<typename InVector, typename OutVector>
void | vectorProduct (const InVector &inVector, OutVector &outVector, RealType matrixMultiplicator=1.0, RealType outVectorMultiplicator=0.0, IndexType begin=0, IndexType end=0) const
| Computes product of matrix and vector.
Public Member Functions inherited from TNL::Matrices::MatrixBase< Real, Device, Index, GeneralMatrix, Organization >
__cuda_callable__ | MatrixBase ()=default
| Basic constructor with no parameters.
__cuda_callable__ | MatrixBase (const MatrixBase &view)=default
| Shallow copy constructor.
__cuda_callable__ | MatrixBase (IndexType rows, IndexType columns, ValuesViewType values)
| Constructor with matrix dimensions and matrix elements values.
__cuda_callable__ | MatrixBase (MatrixBase &&view) noexcept=default
| Move constructor.
IndexType | getAllocatedElementsCount () const
| Tells the number of allocated matrix elements.
__cuda_callable__ IndexType | getColumns () const
| Returns number of matrix columns.
__cuda_callable__ IndexType | getRows () const
| Returns number of matrix rows.
__cuda_callable__ ValuesViewType & | getValues ()
| Returns a reference to a vector with the matrix elements values.
__cuda_callable__ const ValuesViewType & | getValues () const
| Returns a constant reference to a vector with the matrix elements values.
__cuda_callable__ MatrixBase & | operator= (const MatrixBase &)=delete
| Copy-assignment operator.
__cuda_callable__ MatrixBase & | operator= (MatrixBase &&)=delete
| Move-assignment operator.

## Static Public Member Functions
static std::string | getSerializationType ()
| Returns string with serialization type.
Static Public Member Functions inherited from TNL::Matrices::MatrixBase< Real, Device, Index, GeneralMatrix, Organization >
static constexpr ElementsOrganization | getOrganization ()
| Matrix elements organization getter.
static constexpr bool | isBinary ()
| Test of binary matrix type.
static constexpr bool | isSymmetric ()
| Test of symmetric matrix type.

## Protected Member Functions
__cuda_callable__ void | bind (typename Base::ValuesViewType values, DiagonalOffsetsView diagonalOffsets, HostDiagonalOffsetsView hostDiagonalOffsets, IndexerType indexer)
| Re-initializes the internal attributes of the base class.
Protected Member Functions inherited from TNL::Matrices::MatrixBase< Real, Device, Index, GeneralMatrix, Organization >
__cuda_callable__ void | bind (IndexType rows, IndexType columns, ValuesViewType values)
| Re-initializes the internal attributes of the base class.

## Protected Attributes
DiagonalOffsetsView | diagonalOffsets
HostDiagonalOffsetsView | hostDiagonalOffsets
IndexerType | indexer
Protected Attributes inherited from TNL::Matrices::MatrixBase< Real, Device, Index, GeneralMatrix, Organization >
IndexType | columns
IndexType | rows
ValuesViewType | values

## Detailed Description

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
class TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >

A common base class for MultidiagonalMatrix and MultidiagonalMatrixView.

Template Parameters

  Real | is a type of matrix elements.
  Device | is a device where the matrix is allocated.
  Index | is a type for indexing of the matrix elements.
  Organization | tells the ordering of matrix elements. It is either RowMajorOrder or ColumnMajorOrder.

## Member Typedef Documentation

## ◆ DiagonalOffsetsView

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

using TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::DiagonalOffsetsView

Initial value:
Containers::VectorView< std::conditional_t< std::is_const_v< Real >, std::add_const_t< Index >, Index >, Device, Index >
TNL::Containers::VectorView
VectorView extends ArrayView with algebraic operations.
Definition VectorView.h:24

## ◆ HostDiagonalOffsetsView

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

using TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::HostDiagonalOffsetsView

Initial value:
Containers::
VectorView< std::conditional_t< std::is_const_v< Real >, std::add_const_t< Index >, Index >, Devices::Host, Index >
TNL::Devices::Host
Definition Host.h:19

## Constructor & Destructor Documentation

## ◆ MultidiagonalMatrixBase()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

__cuda_callable__ TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::MultidiagonalMatrixBase | ( | typename Base::ValuesViewType | values,
|  | DiagonalOffsetsView | diagonalOffsets,
|  | HostDiagonalOffsetsView | hostDiagonalOffsets,
|  | IndexerType | indexer )

Constructor with all necessary data and views.

Parameters

  values | is a vector view with matrix elements values
  diagonalOffsets | is a vector view with diagonals offsets
  hostDiagonalOffsets | is a vector view with a copy of diagonals offsets on the host
  indexer | is an indexer of matrix elements

## Member Function Documentation

## ◆ addElement()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

__cuda_callable__ void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::addElement | ( | IndexType | row,
|  | IndexType | column,
|  | const RealType & | value,
|  | const RealType & | thisElementMultiplicator = 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. MultidiagonalMatrix::getRow or MultidiagonalMatrix::forElements and MultidiagonalMatrix::forAllElements. The call may fail if the matrix row capacity is exhausted.

Parameters

  row | is row index of the element.
  column | is columns index of the element.
  value | is the value the element will be set to.
  thisElementMultiplicator | is multiplicator the original matrix element value is multiplied by before addition of given value.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  template< typename Device >
  void
  addElements()
  {
  const int matrixSize( 5 );
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  matrixSize, // number of rows
  matrixSize, // number of columns
  { -1, 0, 1 } ); // diagonals offsets
  auto view = matrix.getView();
  for( int i = 0; i < matrixSize; i++ )
  view.setElement( i, i, i ); // or matrix.setElement
  std::cout << "Initial matrix is:\n" << matrix << '\n';
  for( int i = 0; i < matrixSize; i++ ) {
  if( i > 0 )
  view.addElement( i, i - 1, 1.0, 5.0 ); // or matrix.addElement
  view.addElement( i, i, 1.0, 5.0 ); // or matrix.addElement
  if( i < matrixSize - 1 )
  view.addElement( i, i + 1, 1.0, 5.0 ); // or matrix.addElement
  }
  std::cout << "Matrix after addition is:\n" << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Add elements on host:\n";
  addElements< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Add elements on CUDA device:\n";
  addElements< TNL::Devices::Cuda >();
  #endif
  }
  std::cout
  TNL::Matrices::MultidiagonalMatrixBase::addElement
  __cuda_callable__ void addElement(IndexType row, IndexType column, const RealType &value, const RealType &thisElementMultiplicator=1.0)
  Add element at given row and column to given value.
  TNL::Matrices::MultidiagonalMatrixBase::setElement
  __cuda_callable__ void setElement(IndexType row, IndexType column, const RealType &value)
  Sets element at given row and column to given value.
  TNL::Matrices::MultidiagonalMatrix
  Implementation of sparse multidiagonal matrix.
  Definition MultidiagonalMatrix.h:64
  TNL::Matrices::MultidiagonalMatrix::getView
  ViewType getView()
  Returns a modifiable view of the multidiagonal matrix.

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

## ◆ bind()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

__cuda_callable__ void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::bind | ( | typename Base::ValuesViewType | values,
|  | DiagonalOffsetsView | diagonalOffsets,
|  | HostDiagonalOffsetsView | hostDiagonalOffsets,
|  | IndexerType | indexer )
| protected

Re-initializes the internal attributes of the base class.

Note that this function is protected to ensure that the user cannot modify the base class of a matrix. For the same reason, in future code development we also need to make sure that all non-const functions in the base class return by value and not by reference.

## ◆ forAllElements() [1/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forAllElements | ( | Function && | function | ) |

This method calls forElements for all matrix rows.

See MultidiagonalMatrix::forElements.

Template Parameters

  Function | is a type of lambda function that will operate on matrix elements.

Parameters

  function | is an instance of the lambda function to be called in each row.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.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 0 / 1 . . . . \ -> { 0, 0, 1 }
  * 0 | 2 1 . . . | -> { 0, 2, 1 }
  * | 3 2 1 . . | -> { 3, 2, 1 }
  * | . 3 2 1 . | -> { 3, 2, 1 }
  * \ . . 3 2 1 / -> { 3, 2, 1 }
  *
  * The diagonals offsets are { -2, -1, 0 }.
  */
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  5, // number of matrix rows
  5, // number of matrix columns
  { -2, -1, 0 } ); // matrix diagonals offsets
  auto view = matrix.getView();
  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 0 / 1 . . . . \ -> { 0, 0, 1 }
  * 0 | 2 1 . . . | -> { 0, 2, 1 }
  * | 3 2 1 . . | -> { 3, 2, 1 }
  * | . 3 2 1 . | -> { 3, 2, 1 }
  * \ . . 3 2 1 / -> { 3, 2, 1 }
  *
  */
  value = 3 - localIdx;
  };
  view.forAllElements( f ); // or matrix.forAllElements
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on host:\n";
  forAllElementsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA device:\n";
  forAllElementsExample< TNL::Devices::Cuda >();
  #endif
  }
  __cuda_callable__
  #define __cuda_callable__
  Definition Macros.h:49
  TNL::Matrices::MultidiagonalMatrixBase::forAllElements
  void forAllElements(Function &&function) const
  This method calls forElements for all matrix rows (for constant instances).

Output
  Creating matrix on host:
  Row: 0 -> 0:1
  Row: 1 -> 0:2 1:1
  Row: 2 -> 0:3 1:2 2:1
  Row: 3 -> 1:3 2:2 3:1
  Row: 4 -> 2:3 3:2 4:1

## ◆ forAllElements() [2/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forAllElements | ( | Function && | function | ) | const

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

See MultidiagonalMatrix::forElements.

Template Parameters

  Function | is a type of lambda function that will operate on matrix elements.

Parameters

  function | is an instance of the lambda function to be called in each row.

## ◆ forAllElementsIf() [1/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Condition, typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forAllElementsIf | ( | Condition && | condition,
|  | Function && | function )

This method calls forElementsIf for all matrix rows (for non-constant instances).

See MultidiagonalMatrix::forElementsIf.

Template Parameters

  Condition | is a type of lambda function representing the condition based on the row index.
  Function | is a type of lambda function that will operate on matrix elements.

Parameters

  condition | is an instance of the lambda function representing the condition based on the row index.
  function | is an instance of the lambda function to be called in each row.

## ◆ forAllElementsIf() [2/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Condition, typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forAllElementsIf | ( | Condition && | condition,
|  | Function && | function ) const

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

See MultidiagonalMatrix::forElementsIf.

Template Parameters

  Condition | is a type of lambda function representing the condition based on the row index.
  Function | is a type of lambda function that will operate on matrix elements.

Parameters

  condition | is an instance of the lambda function representing the condition based on the row index.
  function | is an instance of the lambda function to be called in each row.

## ◆ forAllRows() [1/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

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

Template Parameters

  Function | is type of the lambda function.

Parameters

  function | is an instance of the lambda function to be called for each row.

auto function = [] __cuda_callable__ ( RowView& row ) { ... };
TNL::Matrices::MultidiagonalMatrixBase::RowView
MultidiagonalMatrixRowView< typename Base::ValuesViewType, IndexerType, DiagonalOffsetsView > RowView
Type for accessing matrix rows.
Definition MultidiagonalMatrixBase.h:56

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

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  forRowsExample()
  {
  using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >;
  /***
  * 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 }
  *
  * The diagonals offsets are { -1, 0, 1 }.
  */
  const int size = 5;
  MatrixType matrix(
  size, // number of matrix rows
  size, // number of matrix columns
  { -1, 0, 1 } ); // matrix diagonals offsets
  auto view = matrix.getView();
  auto f = [] __cuda_callable__( typename MatrixType::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 );
  };
  view.forAllRows( f ); // or matrix.forAllRows
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on host:\n";
  forRowsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA device:\n";
  forRowsExample< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Matrices::MultidiagonalMatrixBase::MatrixType
  GeneralMatrix MatrixType
  The type of matrix can be only general currently.
  Definition MultidiagonalMatrixBase.h:44

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

## ◆ forAllRows() [2/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

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

Template Parameters

  Function | is type of the lambda function.

Parameters

  function | is an instance of the lambda function to be called for each row.

auto function = [] __cuda_callable__ ( const ConstRowView& row ) { ... };
TNL::Matrices::MultidiagonalMatrixBase::ConstRowView
typename RowView::ConstRowView ConstRowView
Type for accessing constant matrix rows.
Definition MultidiagonalMatrixBase.h:61

ConstRowView represents matrix row - see TNL::Matrices::MultidiagonalMatrixBase::ConstRowView.

## ◆ forElements() [1/6]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Array, typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forElements | ( | const Array & | rowIndexes,
|  | Function && | function )

Method for iteration over all matrix elements in the rows enlisted in the array rowIndexes. This is variant for non-constant instances.

Template Parameters

  Array | is a type of the array (or vector) with row indexes.
  Function | is type of lambda function that will operate on matrix elements. It should have form like

auto function = [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value )
{ ... };
TNL::Matrices::MultidiagonalMatrixBase::IndexType
Index IndexType
The type used for matrix elements indexing.
Definition MultidiagonalMatrixBase.h:41
TNL::Matrices::MultidiagonalMatrixBase::RealType
typename Base::RealType RealType
The type of matrix elements.
Definition MultidiagonalMatrixBase.h:31

The localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, index of the matrix subdiagonal.

Parameters

  rowIndexes | is an array with row indexes.
  function | is an instance of the lambda function to be called for each element.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  forElementsExample()
  {
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  5, // number of matrix rows
  5, // number of matrix columns
  { 0, 1, 2 } ); // matrix diagonals offsets
  auto matrixView = matrix.getView();
  auto f = [] __cuda_callable__( int rowIdx, int localIdx, int columnIdx, double& value )
  {
  value = rowIdx + 1;
  };
  TNL::Containers::Array< double, Device > rowIndexes{ 0, 2, 4 };
  matrixView.forElements( rowIndexes, f ); // or matrix.forElements
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on host:\n";
  forElementsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA device:\n";
  forElementsExample< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Containers::Array
  Array is responsible for memory management, access to array elements, and general array operations.
  Definition Array.h:65
  TNL::Matrices::MultidiagonalMatrixBase::forElements
  void forElements(IndexType begin, IndexType end, Function &&function) const
  Method for iteration over all matrix rows for constant instances.

Output
  Creating matrix on host:
  Row: 0 -> 0:1 1:1 2:1
  Row: 1 ->
  Row: 2 -> 2:3 3:3 4:3
  Row: 3 ->
  Row: 4 -> 4:5

## ◆ forElements() [2/6]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Array, typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forElements | ( | const Array & | rowIndexes,
|  | Function && | function ) const

Method for iteration over all matrix elements in the rows enlisted in the array rowIndexes. This is variant for constant instances.

Template Parameters

  Array | is a type of the array (or vector) with row indexes.
  Function | is type of lambda function that will operate on matrix elements. It should have form like

auto function = [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value )
{ ... };

The localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, index of the matrix subdiagonal.

Parameters

  rowIndexes | is an array with row indexes.
  function | is an instance of the lambda function to be called for each element.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  forElementsExample()
  {
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  5, // number of matrix rows
  5, // number of matrix columns
  { 0, 1, 2 } ); // matrix diagonals offsets
  auto matrixView = matrix.getView();
  auto f = [] __cuda_callable__( int rowIdx, int localIdx, int columnIdx, double& value )
  {
  value = rowIdx + 1;
  };
  TNL::Containers::Array< double, Device > rowIndexes{ 0, 2, 4 };
  matrixView.forElements( rowIndexes, f ); // or matrix.forElements
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on host:\n";
  forElementsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA device:\n";
  forElementsExample< TNL::Devices::Cuda >();
  #endif
  }

Output
  Creating matrix on host:
  Row: 0 -> 0:1 1:1 2:1
  Row: 1 ->
  Row: 2 -> 2:3 3:3 4:3
  Row: 3 ->
  Row: 4 -> 4:5

## ◆ forElements() [3/6]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Array, typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forElements | ( | const Array & | rowIndexes,
|  | IndexType | begin,
|  | IndexType | end,
|  | Function && | function )

Method for iteration over all matrix elements in the rows enlisted in the array rowIndexes. This is variant for non-constant instances.

Template Parameters

  Array | is a type of the array (or vector) with row indexes.
  Function | is type of lambda function that will operate on matrix elements. It should have form like

auto function = [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value )
{ ... };

The localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, index of the matrix subdiagonal.

Parameters

  rowIndexes | is an array with row indexes.
  begin | defines beginning of the range [begin, end) of row indexes in the array rowIndexes to be processed.
  end | defines ending of the range [begin, end) of row indexes in the array rowIndexes to be processed.
  function | is an instance of the lambda function to be called for each element.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  forElementsExample()
  {
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  5, // number of matrix rows
  5, // number of matrix columns
  { 0, 1, 2 } ); // matrix diagonals offsets
  auto matrixView = matrix.getView();
  auto f = [] __cuda_callable__( int rowIdx, int localIdx, int columnIdx, double& value )
  {
  value = rowIdx + localIdx + 1;
  };
  TNL::Containers::Array< double, Device > rowIndexes{ 0, 2, 4, 0, 1, 3, 5, 9 };
  // The following iterates only over the rows with indexes 0, 2, and 4, i.e. indexes
  // at positions 0, 1, and 2 in the rowIndexes array. The rest is ignored.
  matrixView.forElements( rowIndexes, 0, 2, f ); // or matrix.forElements
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on host:\n";
  forElementsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA device:\n";
  forElementsExample< TNL::Devices::Cuda >();
  #endif
  }

Output
  Creating matrix on host:
  Row: 0 -> 0:1 1:2 2:3
  Row: 1 ->
  Row: 2 -> 2:3 3:4 4:5
  Row: 3 ->
  Row: 4 ->

## ◆ forElements() [4/6]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Array, typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forElements | ( | const Array & | rowIndexes,
|  | IndexType | begin,
|  | IndexType | end,
|  | Function && | function ) const

Method for iteration over all matrix elements in the rows enlisted in the array rowIndexes. This is variant for constant instances.

Template Parameters

  Array | is a type of the array (or vector) with row indexes.
  Function | is type of lambda function that will operate on matrix elements. It should have form like

auto function = [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value )
{ ... };

The localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, index of the matrix subdiagonal.

Parameters

  rowIndexes | is an array with row indexes.
  begin | defines beginning of the range [begin, end) of row indexes in the array rowIndexes to be processed.
  end | defines ending of the range [begin, end) of row indexes in the array rowIndexes to be processed.
  function | is an instance of the lambda function to be called for each element.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  forElementsExample()
  {
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  5, // number of matrix rows
  5, // number of matrix columns
  { 0, 1, 2 } ); // matrix diagonals offsets
  auto matrixView = matrix.getView();
  auto f = [] __cuda_callable__( int rowIdx, int localIdx, int columnIdx, double& value )
  {
  value = rowIdx + localIdx + 1;
  };
  TNL::Containers::Array< double, Device > rowIndexes{ 0, 2, 4, 0, 1, 3, 5, 9 };
  // The following iterates only over the rows with indexes 0, 2, and 4, i.e. indexes
  // at positions 0, 1, and 2 in the rowIndexes array. The rest is ignored.
  matrixView.forElements( rowIndexes, 0, 2, f ); // or matrix.forElements
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on host:\n";
  forElementsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA device:\n";
  forElementsExample< TNL::Devices::Cuda >();
  #endif
  }

Output
  Creating matrix on host:
  Row: 0 -> 0:1 1:2 2:3
  Row: 1 ->
  Row: 2 -> 2:3 3:4 4:5
  Row: 3 ->
  Row: 4 ->

## ◆ forElements() [5/6]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forElements | ( | IndexType | begin,
|  | IndexType | end,
|  | Function && | function )

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

Template Parameters

  Function | is type of lambda function that will operate on matrix elements. It should have form like

auto function = [=] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, RealType& value )
{ ... };

where

rowIdx is an index of the matrix row.

localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, index of the matrix subdiagonal.

columnIdx is a column index of the matrix element.

value is a reference to the matrix element value. It can be used even for changing the matrix element value.

Parameters

  begin | defines beginning of the range [begin, end) of rows to be processed.
  end | defines ending of the range [begin, end) of rows to be processed.
  function | is an instance of the lambda function to be called in each row.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  forElementsExample()
  {
  /***
  * Set the following matrix (dots represent zero matrix elements and zeros are
  * padding zeros for memory alignment):
  *
  * 0 0 / 1 . . . . \ -> { 0, 0, 1 }
  * 0 | 2 1 . . . | -> { 0, 2, 1 }
  * | 3 2 1 . . | -> { 3, 2, 1 }
  * | . 3 2 1 . | -> { 3, 2, 1 }
  * \ . . 3 2 1 / -> { 3, 2, 1 }
  *
  * The diagonals offsets are { -2, -1, 0 }.
  */
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  5, // number of matrix rows
  5, // number of matrix columns
  { -2, -1, 0 } ); // matrix diagonals offsets
  auto view = matrix.getView();
  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 0 / 1 . . . . \ -> { 0, 0, 1 }
  * 0 | 2 1 . . . | -> { 0, 2, 1 }
  * | 3 2 1 . . | -> { 3, 2, 1 }
  * | . 3 2 1 . | -> { 3, 2, 1 }
  * \ . . 3 2 1 / -> { 3, 2, 1 }
  *
  */
  value = 3 - localIdx;
  };
  view.forElements( 0, matrix.getRows(), f ); // or matrix.forElements
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on host:\n";
  forElementsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA device:\n";
  forElementsExample< TNL::Devices::Cuda >();
  #endif
  }

Output
  Creating matrix on host:
  Row: 0 -> 0:1
  Row: 1 -> 0:2 1:1
  Row: 2 -> 0:3 1:2 2:1
  Row: 3 -> 1:3 2:2 3:1
  Row: 4 -> 2:3 3:2 4:1

## ◆ forElements() [6/6]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forElements | ( | IndexType | begin,
|  | IndexType | end,
|  | Function && | function ) const

Method for iteration over all matrix rows for constant instances.

Template Parameters

  Function | is type of lambda function that will operate on matrix elements. It should have form like

auto function = [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value )
{ ... };

where

rowIdx is an index of the matrix row.

localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, index of the matrix subdiagonal.

columnIdx is a column index of the matrix element.

value is the matrix element value.

Parameters

  begin | defines beginning of the range [begin, end) of rows to be processed.
  end | defines ending of the range [begin, end) of rows to be processed.
  function | is an instance of the lambda function to be called in each row.

## ◆ forElementsIf() [1/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Condition, typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forElementsIf | ( | IndexType | begin,
|  | IndexType | end,
|  | Condition && | condition,
|  | Function && | function )

Method for iterating over all matrix elements that meet a condition based on the row index (for constant instances).

Template Parameters

  Condition | is a type of lambda function that will be used to check if the element meets the condition. It should have form like

auto condition = [] __cuda_callable__ ( IndexType rowIdx ) -> bool { ... };

Template Parameters

  Function | is type of lambda function that will operate on matrix elements. It should have form like

auto function = [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value )
{ ... };

The localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, index of the matrix subdiagonal.

Parameters

  begin | defines beginning of the range [begin, end) of rows to be processed.
  end | defines ending of the range [begin, end) of rows to be processed.
  condition | is an instance of the lambda function representing the condition based on the row index.
  function | is an instance of the lambda function to be called for each matrix element.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  forElementsExample()
  {
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  5, // number of matrix rows
  5, // number of matrix columns
  { 0, 1, 2 } ); // matrix diagonals offsets
  auto matrixView = matrix.getView();
  auto condition = [] __cuda_callable__( int rowIdx )
  {
  return rowIdx % 2 == 0;
  };
  auto f = [] __cuda_callable__( int rowIdx, int localIdx, int columnIdx, double& value )
  {
  value = rowIdx + 1;
  };
  matrixView.forElementsIf( 0, matrix.getRows(), condition, f ); // or matrix.forElements
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on host:\n";
  forElementsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA device:\n";
  forElementsExample< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Matrices::MultidiagonalMatrixBase::forElementsIf
  void forElementsIf(IndexType begin, IndexType end, Condition &&condition, Function &&function) const
  Method for iterating over all matrix elements that meet a condition based on the row index (for const...

Output
  Creating matrix on host:
  Row: 0 -> 0:1 1:1 2:1
  Row: 1 ->
  Row: 2 -> 2:3 3:3 4:3
  Row: 3 ->
  Row: 4 -> 4:5

## ◆ forElementsIf() [2/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Condition, typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::forElementsIf | ( | IndexType | begin,
|  | IndexType | end,
|  | Condition && | condition,
|  | Function && | function ) const

Method for iterating over all matrix elements that meet a condition based on the row index (for constant instances).

Template Parameters

  Condition | is a type of lambda function that will be used to check if the element meets the condition. It should have form like

auto condition = [] __cuda_callable__ ( IndexType rowIdx ) -> bool { ... };

Template Parameters

  Function | is type of lambda function that will operate on matrix elements. It should have form like

auto function = [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value )
{ ... };

The localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, index of the matrix subdiagonal.

Parameters

  begin | defines beginning of the range [begin, end) of rows to be processed.
  end | defines ending of the range [begin, end) of rows to be processed.
  condition | is an instance of the lambda function representing the condition based on the row index.
  function | is an instance of the lambda function to be called for each matrix element.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  forElementsExample()
  {
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  5, // number of matrix rows
  5, // number of matrix columns
  { 0, 1, 2 } ); // matrix diagonals offsets
  auto matrixView = matrix.getView();
  auto condition = [] __cuda_callable__( int rowIdx )
  {
  return rowIdx % 2 == 0;
  };
  auto f = [] __cuda_callable__( int rowIdx, int localIdx, int columnIdx, double& value )
  {
  value = rowIdx + 1;
  };
  matrixView.forElementsIf( 0, matrix.getRows(), condition, f ); // or matrix.forElements
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on host:\n";
  forElementsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA device:\n";
  forElementsExample< TNL::Devices::Cuda >();
  #endif
  }

Output
  Creating matrix on host:
  Row: 0 -> 0:1 1:1 2:1
  Row: 1 ->
  Row: 2 -> 2:3 3:3 4:3
  Row: 3 ->
  Row: 4 -> 4:5

## ◆ forRows() [1/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

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

Template Parameters

  Function | is type of the lambda function.

Parameters

  begin | defines beginning of the range [begin, end) of rows to be processed.
  end | defines ending of the range [begin, end) of rows to be processed.
  function | is an instance of the lambda function to be called for each row.

auto function = [] __cuda_callable__ ( RowView& row ) { ... };

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

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  forRowsExample()
  {
  using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >;
  /***
  * 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 }
  *
  * The diagonals offsets are { -1, 0, 1 }.
  */
  const int size = 5;
  MatrixType matrix(
  size, // number of matrix rows
  size, // number of matrix columns
  { -1, 0, 1 } ); // matrix diagonals offsets
  auto view = matrix.getView();
  auto f = [] __cuda_callable__( typename MatrixType::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 );
  };
  view.forAllRows( f ); // or matrix.forAllRows
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating matrix on host:\n";
  forRowsExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating matrix on CUDA device:\n";
  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

## ◆ forRows() [2/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

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

Template Parameters

  Function | is type of the lambda function.

Parameters

  begin | defines beginning of the range [begin, end) of rows to be processed.
  end | defines ending of the range [begin, end) of rows to be processed.
  function | is an instance of the lambda function to be called for each row.

auto function = [] __cuda_callable__ ( const ConstRowView& row ) { ... };

ConstRowView represents matrix row - see TNL::Matrices::MultidiagonalMatrixBase::ConstRowView.

## ◆ getCompressedRowLengths()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Vector>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::getCompressedRowLengths | ( | Vector & | rowLengths | ) | const

Computes number of non-zeros in each row.

Parameters

  rowLengths | is a vector into which the number of non-zeros in each row will be stored.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  laplaceOperatorMatrix()
  {
  const int gridSize( 4 );
  const int matrixSize = gridSize * gridSize;
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  matrixSize, // number of rows
  matrixSize, // number of columns
  { -gridSize, -1, 0, 1, gridSize } // diagonals offsets
  );
  matrix.setElements(
  { { 0.0, 0.0, 1.0 }, // set matrix elements corresponding to boundary grid nodes
  { 0.0, 0.0, 1.0 }, // and Dirichlet boundary conditions, i.e. 1 on the main diagonal
  { 0.0, 0.0, 1.0 }, // which is the third one
  { 0.0, 0.0, 1.0 },
  { 0.0, 0.0, 1.0 },
  { -1.0, -1.0, 4.0, -1.0, -1.0 }, // set matrix elements corresponding to inner grid nodes, i.e. 4 on the main diagonal
  { -1.0, -1.0, 4.0, -1.0, -1.0 }, // (the third one) and -1 to the other sub-diagonals
  { 0.0, 0.0, 1.0 },
  { 0.0, 0.0, 1.0 },
  { -1.0, -1.0, 4.0, -1.0, -1.0 },
  { -1.0, -1.0, 4.0, -1.0, -1.0 },
  { 0.0, 0.0, 1.0 },
  { 0.0, 0.0, 1.0 },
  { 0.0, 0.0, 1.0 },
  { 0.0, 0.0, 1.0 },
  { 0.0, 0.0, 1.0 } } );
  auto view = matrix.getView();
  TNL::Containers::Vector< int, Device > rowLengths;
  view.getCompressedRowLengths( rowLengths ); // or matrix.getCompressedRowLengths
  std::cout << "Laplace operator matrix:\n" << matrix << '\n';
  std::cout << "Compressed row lengths: " << rowLengths << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Creating Laplace operator matrix on CPU ...\n";
  laplaceOperatorMatrix< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Creating Laplace operator matrix on CUDA GPU ...\n";
  laplaceOperatorMatrix< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Containers::Vector
  Vector extends Array with algebraic operations.
  Definition Vector.h:37
  TNL::Matrices::MultidiagonalMatrix::setElements
  void setElements(const std::initializer_list< std::initializer_list< ListReal > > &data)
  Set matrix elements from an initializer list.

Output
  Creating Laplace operator matrix on CPU ...
  Laplace operator matrix:
  Row: 0 -> 0:1
  Row: 1 -> 1:1
  Row: 2 -> 2:1
  Row: 3 -> 3:1
  Row: 4 -> 4:1
  Row: 5 -> 1:-1 4:-1 5:4 6:-1 9:-1
  Row: 6 -> 2:-1 5:-1 6:4 7:-1 10:-1
  Row: 7 -> 7:1
  Row: 8 -> 8:1
  Row: 9 -> 5:-1 8:-1 9:4 10:-1 13:-1
  Row: 10 -> 6:-1 9:-1 10:4 11:-1 14:-1
  Row: 11 -> 11:1
  Row: 12 -> 12:1
  Row: 13 -> 13:1
  Row: 14 -> 14:1
  Row: 15 -> 15:1
  Compressed row lengths: [ 1, 1, 1, 1, 1, 5, 5, 1, 1, 5, 5, 1, 1, 1, 1, 1 ]

## ◆ getDiagonalsCount()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

__cuda_callable__ IndexType TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::getDiagonalsCount | ( |  | ) | const
| nodiscard

Returns number of diagonals.

Returns
  Number of diagonals.

## ◆ getElement()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

__cuda_callable__ RealType TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::getElement | ( | IndexType | row,
|  | IndexType | column ) const
| nodiscard

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

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

Parameters

  row | is a row index of the matrix element.
  column | i a column index of the matrix element.

Returns
  value of given matrix element.

Example
  #include <iostream>
  #include <iomanip>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  template< typename Device >
  void
  getElements()
  {
  const int matrixSize( 5 );
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  matrixSize, // number of matrix columns
  { -1, 0, 1 }, // matrix diagonals offsets
  { // 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 } } );
  auto view = matrix.getView();
  for( int i = 0; i < matrixSize; i++ ) {
  for( int j = 0; j < matrixSize; j++ )
  std::cout << std::setw( 5 ) << view.getElement( i, j ); // or matrix.getElement
  std::cout << '\n';
  }
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Get elements on host:\n";
  getElements< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Get elements on CUDA device:\n";
  getElements< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Matrices::MultidiagonalMatrixBase::getElement
  __cuda_callable__ RealType getElement(IndexType row, IndexType column) const
  Returns value of matrix element at position given by its row and column index.
  std::setw
  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

## ◆ getIndexer() [1/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

__cuda_callable__ IndexerType & TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::getIndexer | ( |  | ) |
| nodiscard

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>

__cuda_callable__ const IndexerType & TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::getIndexer | ( |  | ) | const
| nodiscard

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>

IndexType TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::getNonzeroElementsCount | ( |  | ) | const
| nodiscard

Returns number of non-zero matrix elements.

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

Returns
  number of non-zero matrix elements.

## ◆ getRow() [1/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

__cuda_callable__ RowView TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::getRow | ( | IndexType | rowIdx | ) |
| nodiscard

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

Parameters

  rowIdx | is matrix row index.

Returns
  RowView for accessing given matrix row.

Example
  #include <iostream>
  #include <TNL/Algorithms/parallelFor.h>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  getRowExample()
  {
  const int matrixSize( 5 );
  using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >;
  MatrixType matrix(
  matrixSize, // number of matrix rows
  matrixSize, // number of matrix columns
  { -1, 0, 1 } );
  auto view = matrix.getView();
  auto f = [ = ] __cuda_callable__( int rowIdx ) mutable
  {
  auto row = view.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 );
  };
  /***
  * Set the matrix elements.
  */
  TNL::Algorithms::parallelFor< Device >( 0, matrix.getRows(), f );
  std::cout << '\n' << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Getting matrix rows on host:\n";
  getRowExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Getting matrix rows on CUDA device:\n";
  getRowExample< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Algorithms::parallelFor
  std::enable_if_t< std::is_integral_v< Begin > &&std::is_integral_v< End > > parallelFor(const Begin &begin, const End &end, typename Device::LaunchConfiguration launch_config, Function f, FunctionArgs... args)
  Parallel for-loop function for 1D range specified with integral values.
  Definition parallelFor.h:41

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 MultidiagonalMatrixRowView.

## ◆ getRow() [2/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

__cuda_callable__ ConstRowView TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::getRow | ( | IndexType | rowIdx | ) | const
| nodiscard

Constant getter of simple structure for accessing given matrix row.

Parameters

  rowIdx | is matrix row index.

Returns
  RowView for accessing given matrix row.

Example
  #include <iostream>
  #include <functional>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  template< typename Device >
  void
  getRowExample()
  {
  const int matrixSize = 5;
  auto diagonalsOffsets = { -2, -1, 0 };
  using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >;
  MatrixType matrix(
  matrixSize, // number of matrix columns
  diagonalsOffsets,
  { { 0.0, 0.0, 1.0 }, // matrix elements
  { 0.0, 2.0, 1.0 },
  { 3.0, 2.0, 1.0 },
  { 3.0, 2.0, 1.0 },
  { 3.0, 2.0, 1.0 } } );
  auto view = matrix.getView();
  /***
  * Fetch lambda function returns diagonal element in each row.
  */
  auto fetch = [ = ] __cuda_callable__( int rowIdx ) -> double
  {
  auto row = view.getRow( rowIdx );
  return row.getValue( 2 ); // get value from subdiagonal with index 2, i.e. the main diagonal
  };
  /***
  * Compute the matrix trace.
  */
  int trace = TNL::Algorithms::reduce< Device >( 0, matrix.getRows(), fetch, std::plus<>{}, 0 );
  std::cout << "Matrix reads as:\n" << matrix << '\n';
  std::cout << "Matrix trace is: " << trace << ".\n";
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Getting matrix rows on host:\n";
  getRowExample< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Getting matrix rows on CUDA device:\n";
  getRowExample< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Algorithms::reduce
  Result reduce(Index begin, Index end, Fetch &&fetch, Reduction &&reduction, const Result &identity)
  reduce implements (parallel) reduction for vectors and arrays.
  Definition reduce.h:66
  std::plus
  TNL::Matrices::MatrixType
  Structure for specifying type of sparse matrix.
  Definition MatrixType.h:17

Output
  Getting matrix rows on host:
  Matrix reads as:
  Row: 0 -> 0:1
  Row: 1 -> 0:2 1:1
  Row: 2 -> 0:3 1:2 2:1
  Row: 3 -> 1:3 2:2 3:1
  Row: 4 -> 2:3 3:2 4:1
  Matrix trace is: 5.

See MultidiagonalMatrixRowView.

## ◆ getRowCapacities()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Vector>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::getRowCapacities | ( | Vector & | rowCapacities | ) | const

Compute capacities of all rows.

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

Parameters

  rowCapacities | is a vector where the row capacities will be stored.

## ◆ getSerializationType()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

std::string TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::getSerializationType | ( |  | ) |
| staticnodiscard

Returns string with serialization type.

Returns
  String with the serialization type.

## ◆ operator!=()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_>

bool TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::operator!= | ( | const MultidiagonalMatrixBase< Real_, Device_, Index_, Organization_ > & | matrix | ) | const
| nodiscard

Comparison operator with another multidiagonal 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.

Parameters

  matrix | is the source matrix view.

Returns
  true if both matrices are NOT identical and false otherwise.

## ◆ operator=()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

MultidiagonalMatrixBase & TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::operator= | ( | const MultidiagonalMatrixBase< Real, Device, Index, Organization > & |  | ) |
| delete

Copy-assignment operator.

It is a deleted function, because matrix assignment in general requires reallocation.

## ◆ operator==()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_>

bool TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::operator== | ( | const MultidiagonalMatrixBase< Real_, Device_, Index_, Organization_ > & | matrix | ) | const
| nodiscard

Comparison operator with another multidiagonal 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.

Parameters

  matrix | is the source matrix view.

Returns
  true if both matrices are identical and false otherwise.

## ◆ print()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::print | ( | std::ostream & | str | ) | const

Method for printing the matrix to output stream.

Parameters

  str | is the output stream.

## ◆ reduceAllRows()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Fetch, typename Reduce, typename Keep, typename FetchReal>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::reduceAllRows | ( | Fetch && | fetch,
|  | const Reduce & | reduce,
|  | Keep && | keep,
|  | const FetchReal & | identity ) const

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

Template Parameters

  Fetch | is 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

  Reduce | is a type of lambda function for reduction declared as

auto reduce = [] __cuda_callable__ ( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue { ... };

Template Parameters

  Keep | is a type of lambda function for storing results of reduction in each row. It is declared as

auto keep = [=] __cuda_callable__ ( IndexType rowIdx, const RealType& value ) { ... };

Template Parameters

  FetchValue | is type returned by the Fetch lambda function.

Parameters

  fetch | is an instance of lambda function for data fetch.
  reduce | is an instance of lambda function for reduction.
  keep | in an instance of lambda function for storing results.
  identity | is the identity element for the reduction operation, i.e. element which does not change the result of the reduction.

Example
  #include <iostream>
  #include <iomanip>
  #include <functional>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  template< typename Device >
  void
  reduceAllRows()
  {
  /***
  * Set the following matrix (dots represent zero matrix elements and zeros are
  * padding zeros for memory alignment):
  *
  * 0 0 / 1 . . . . \ -> { 0, 0, 1 }
  * 0 | 2 1 . . . | -> { 0, 2, 1 }
  * | 3 2 1 . . | -> { 3, 2, 1 }
  * | . 3 2 1 . | -> { 3, 2, 1 }
  * \ . . 3 2 1 / -> { 3, 2, 1 }
  *
  * The diagonals offsets are { -2, -1, 0 }.
  */
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  5, // number of matrix columns
  { -2, -1, 0 }, // diagonals offsets
  { { 0, 0, 1 }, // matrix elements
  { 0, 2, 1 },
  { 3, 2, 1 },
  { 3, 2, 1 },
  { 3, 2, 1 } } );
  auto view = matrix.getView();
  /***
  * 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.
  */
  view.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); // or matrix.reduceAllRows
  std::cout << "The matrix reads as:\n" << matrix << '\n';
  std::cout << "Max. elements in rows are: " << rowMax << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Rows reduction on host:\n";
  reduceAllRows< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Rows reduction on CUDA device:\n";
  reduceAllRows< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Matrices::MultidiagonalMatrixBase::reduceAllRows
  void reduceAllRows(Fetch &&fetch, const Reduce &reduce, Keep &&keep, const FetchReal &identity) const
  Method for performing general reduction on all matrix rows for constant instances.
  std::numeric_limits::lowest
  T lowest(T... args)
  TNL::abs
  __cuda_callable__ T abs(const T &n)
  This function returns absolute value of given number n.
  Definition Math.h:75
  TNL::max
  constexpr ResultType max(const T1 &a, const T2 &b)
  This function returns maximum of two numbers.
  Definition Math.h:49

Output
  Rows reduction on host:
  The matrix reads as:
  Row: 0 -> 0:1
  Row: 1 -> 0:2 1:1
  Row: 2 -> 0:3 1:2 2:1
  Row: 3 -> 1:3 2:2 3:1
  Row: 4 -> 2:3 3:2 4:1
  Max. elements in rows are: [ -1.79769e+308, -1.79769e+308, -1.79769e+308, -1.79769e+308, -1.79769e+308 ]

## ◆ reduceRows()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Fetch, typename Reduce, typename Keep, typename FetchReal>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::reduceRows | ( | IndexType | begin,
|  | IndexType | end,
|  | Fetch && | fetch,
|  | const Reduce & | reduce,
|  | Keep && | keep,
|  | const FetchReal & | identity ) const

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

Template Parameters

  Fetch | is 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

  Reduce | is a type of lambda function for reduction declared as

auto reduce = [] __cuda_callable__ ( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue { ... };

Template Parameters

  Keep | is a type of lambda function for storing results of reduction in each row. It is declared as

auto keep = [=] __cuda_callable__ ( IndexType rowIdx, const RealType& value ) { ... };

Template Parameters

  FetchValue | is type returned by the Fetch lambda function.

Parameters

  begin | defines beginning of the range [begin, end) of rows to be processed.
  end | defines ending of the range [begin, end) of rows to be processed.
  fetch | is an instance of lambda function for data fetch.
  reduce | is an instance of lambda function for reduction.
  keep | in an instance of lambda function for storing results.
  identity | is the identity element for the reduction operation, i.e. element which does not change the result of the reduction.

Example
  #include <iostream>
  #include <TNL/Matrices/MultidiagonalMatrix.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 0 / 1 . . . . \ -> { 0, 0, 1 }
  * 0 | 2 1 . . . | -> { 0, 2, 1 }
  * | 3 2 1 . . | -> { 3, 2, 1 }
  * | . 3 2 1 . | -> { 3, 2, 1 }
  * \ . . 3 2 1 / -> { 3, 2, 1 }
  *
  * The diagonals offsets are { -2, -1, 0 }.
  */
  TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
  5, // number of matrix columns
  { -2, -1, 0 }, // diagonals offsets
  { { 0, 0, 1 }, // matrix elements
  { 0, 2, 1 },
  { 3, 2, 1 },
  { 3, 2, 1 },
  { 3, 2, 1 } } );
  auto view = matrix.getView();
  /***
  * 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.
  */
  view.reduceRows(
  0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); // or matrix.reduceRows
  std::cout << "The matrix reads as:\n" << matrix << '\n';
  std::cout << "Max. elements in rows are: " << rowMax << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Rows reduction on host:\n";
  reduceRows< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Rows reduction on CUDA device:\n";
  reduceRows< TNL::Devices::Cuda >();
  #endif
  }
  TNL::Matrices::MultidiagonalMatrixBase::reduceRows
  void reduceRows(IndexType begin, IndexType end, Fetch &&fetch, const Reduce &reduce, Keep &&keep, const FetchReal &identity) const
  Method for performing general reduction on matrix rows for constant instances.

Output
  Rows reduction on host:
  The matrix reads as:
  Row: 0 -> 0:1
  Row: 1 -> 0:2 1:1
  Row: 2 -> 0:3 1:2 2:1
  Row: 3 -> 1:3 2:2 3:1
  Row: 4 -> 2:3 3:2 4:1
  Max. elements in rows are: [ -1.79769e+308, -1.79769e+308, -1.79769e+308, -1.79769e+308, -1.79769e+308 ]

## ◆ sequentialForAllRows() [1/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::sequentialForAllRows | ( | Function & | function | ) |

This method calls sequentialForRows for all matrix rows.

See MultidiagonalMatrixBase::sequentialForAllRows.

Template Parameters

  Function | is a type of lambda function that will operate on matrix elements.

Parameters

  function | is an instance of the lambda function to be called in each row.

## ◆ sequentialForAllRows() [2/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::sequentialForAllRows | ( | Function & | function | ) | const

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

See MultidiagonalMatrixBase::sequentialForRows.

Template Parameters

  Function | is a type of lambda function that will operate on matrix elements.

Parameters

  function | is an instance of the lambda function to be called in each row.

## ◆ sequentialForRows() [1/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::sequentialForRows | ( | IndexType | begin,
|  | IndexType | end,
|  | Function & | function )

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

Template Parameters

  Function | is type of lambda function that will operate on matrix elements. It should have form like

auto function = [] __cuda_callable__ ( RowView& row ) { ... };

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

Parameters

  begin | defines beginning of the range [begin, end) of rows to be processed.
  end | defines ending of the range [begin, end) of rows to be processed.
  function | is an instance of the lambda function to be called in each row.

## ◆ sequentialForRows() [2/2]

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename Function>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::sequentialForRows | ( | IndexType | begin,
|  | IndexType | end,
|  | Function & | function ) const

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

Template Parameters

  Function | is type of lambda function that will operate on matrix elements. It should have form like

auto function = [] __cuda_callable__ ( const ConstRowView& row ) { ... };

ConstRowView represents matrix row - see TNL::Matrices::MultidiagonalMatrixBase::ConstRowView.

Parameters

  begin | defines beginning of the range [begin, end) of rows to be processed.
  end | defines ending of the range [begin, end) of rows to be processed.
  function | is an instance of the lambda function to be called in each row.

## ◆ setElement()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

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

Parameters

  row | is row index of the element.
  column | is columns index of the element.
  value | is the value the element will be set to.

Example
  #include <iostream>
  #include <TNL/Algorithms/parallelFor.h>
  #include <TNL/Matrices/MultidiagonalMatrix.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  #include <TNL/Pointers/SharedPointer.h>
  #include <TNL/Pointers/SmartPointersRegister.h>
  template< typename Device >
  void
  setElements()
  {
  const int matrixSize( 5 );
  auto diagonalsOffsets = { -1, 0, 1 }; // offsets of tridiagonal matrix
  using Matrix = TNL::Matrices::MultidiagonalMatrix< double, Device >;
  Matrix matrix( matrixSize, matrixSize, diagonalsOffsets );
  auto view = matrix.getView();
  for( int i = 0; i < 5; i++ )
  view.setElement( i, i, i );
  std::cout << "Matrix set from the host:\n";
  std::cout << matrix << '\n';
  auto f = [ = ] __cuda_callable__( int i ) mutable
  {
  if( i > 0 )
  view.setElement( i, i - 1, 1.0 );
  view.setElement( i, i, -i );
  if( i < matrixSize - 1 )
  view.setElement( i, i + 1, 1.0 );
  };
  TNL::Algorithms::parallelFor< Device >( 0, matrixSize, f );
  std::cout << "Matrix set from its native device:\n";
  std::cout << matrix << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Set elements on host:\n";
  setElements< TNL::Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Set elements on CUDA device:\n";
  setElements< TNL::Devices::Cuda >();
  #endif
  }

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

## ◆ setValue()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::setValue | ( | const RealType & | value | ) |

Set all matrix elements to given value.

Parameters

  value | is the new value of all matrix elements.

## ◆ vectorProduct()

template<typename Real, typename Device, typename Index, ElementsOrganization Organization>
template<typename InVector, typename OutVector>

void TNL::Matrices::MultidiagonalMatrixBase< Real, Device, Index, Organization >::vectorProduct | ( | const InVector & | inVector,
|  | OutVector & | outVector,
|  | RealType | matrixMultiplicator = 1.0,
|  | RealType | outVectorMultiplicator = 0.0,
|  | IndexType | begin = 0,
|  | IndexType | end = 0 ) const

Computes product of matrix and vector.

More precisely, it computes:

outVector = matrixMultiplicator * ( * this ) * inVector + outVectorMultiplicator * outVector

Template Parameters

  InVector | is type of input vector. It can be TNL::Containers::Vector, TNL::Containers::VectorView, TNL::Containers::Array, TNL::Containers::ArrayView, or similar container.
  OutVector | is type of output vector. It can be TNL::Containers::Vector, TNL::Containers::VectorView, TNL::Containers::Array, TNL::Containers::ArrayView, or similar container.

Parameters

  inVector | is input vector.
  outVector | is output vector.
  matrixMultiplicator | is a factor by which the matrix is multiplied. It is one by default.
  outVectorMultiplicator | is a factor by which the outVector is multiplied before added to the result of matrix-vector product. It is zero by default.
  begin | is the beginning of the rows range for which the vector product is computed. It is zero by default.
  end | is the end of the rows range for which the vector product is computed. It is number if the matrix rows by default.

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

- src/TNL/Matrices/MultidiagonalMatrixBase.h