Template Numerical Library version\ main:8b8c8226
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator > Class Template Reference

Implementation of dense matrix, i.e. matrix storing explicitly all of its elements including zeros. More...

#include <TNL/Matrices/DenseMatrix.h>

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

Public Types

using ConstRowView = typename ConstViewType::ConstRowView
 Type for accessing immutable matrix rows.
 
using ConstViewType = typename DenseMatrixView< Real, Device, Index, Organization >::ConstViewType
 Matrix view type for constant instances. More...
 
using DeviceType = Device
 The device where the matrix is allocated.
 
using IndexType = Index
 The type used for matrix elements indexing.
 
using RealAllocatorType = RealAllocator
 The allocator for matrix elements.
 
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 = DenseMatrix< _Real, _Device, _Index, _Organization, _RealAllocator >
 Helper type for getting self type or its modifications.
 
using ViewType = DenseMatrixView< Real, Device, Index, Organization >
 Type of related matrix view. More...
 
- Public Types inherited from TNL::Matrices::Matrix< Real, Device, Index, RealAllocator >
using ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType
 
using ConstValuesType = Containers::Vector< std::add_const_t< Real >, Device, Index, RealAllocator >
 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< Real, Device, Index >::ConstViewType
 Type of base matrix view for constant instances. More...
 
using DeviceType = Device
 The device where the matrix is allocated.
 
using IndexType = Index
 The type used for matrix elements indexing.
 
using RealAllocatorType = RealAllocator
 
using RealType = Real
 The type of matrix elements.
 
using RowsCapacitiesType = Containers::Vector< Index, Device, Index >
 
using RowsCapacitiesView = Containers::VectorView< Index, Device, Index >
 
using ValuesType = Containers::Vector< Real, Device, Index, RealAllocator >
 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< Real, Device, Index >
 Type of base matrix view. More...
 

Public Member Functions

 DenseMatrix (const DenseMatrix &matrix)=default
 Copy constructor. More...
 
 DenseMatrix (const RealAllocatorType &allocator=RealAllocatorType())
 Constructor only with values allocator. More...
 
 DenseMatrix (DenseMatrix &&matrix) noexcept=default
 Move constructor. More...
 
 DenseMatrix (IndexType rows, IndexType columns, const RealAllocatorType &allocator=RealAllocatorType())
 Constructor with matrix dimensions. More...
 
template<typename Value >
 DenseMatrix (std::initializer_list< std::initializer_list< Value > > data, const RealAllocatorType &allocator=RealAllocatorType())
 Constructor with 2D initializer list. More...
 
__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. More...
 
template<typename Matrix >
void addMatrix (const Matrix &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. More...
 
template<typename Function >
void forAllElements (Function &&function) const
 This method calls forElements for 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 all matrix rows for non-constant instances. More...
 
template<typename Function >
void forElements (IndexType begin, IndexType end, Function &&function) const
 Method for iteration over all 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 RowLengthsVector >
void getCompressedRowLengths (RowLengthsVector &rowLengths) const
 Computes number of non-zeros in each row. More...
 
ConstViewType getConstView () const
 Returns a non-modifiable view of the dense matrix. More...
 
__cuda_callable__ Real getElement (IndexType row, IndexType column) const
 Returns value of matrix element at position given by its row and column index. More...
 
template<typename Matrix1 , typename Matrix2 , int tileDim = 32>
void getMatrixProduct (const Matrix1 &matrix1, const Matrix2 &matrix2, const RealType &matrixMultiplicator=1.0)
 
IndexType getNonzeroElementsCount () const override
 Returns number of non-zero matrix elements. More...
 
__cuda_callable__ RowView getRow (const IndexType &rowIdx)
 Non-constant getter of simple structure for accessing given matrix row. More...
 
__cuda_callable__ 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 Matrix , int tileDim = 32>
void getTransposition (const Matrix &matrix, const RealType &matrixMultiplicator=1.0)
 
ViewType getView ()
 Returns a modifiable view of the dense 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_ , typename RealAllocator_ >
bool operator!= (const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ > &matrix) const
 Comparison operator with another dense matrix. More...
 
template<typename Real_ , typename Device_ , typename Index_ >
bool operator!= (const DenseMatrixView< Real_, Device_, Index_, Organization > &matrix) const
 Comparison operator with another dense matrix view. More...
 
template<typename Matrix >
bool operator!= (const Matrix &matrix) const
 Comparison operator with another arbitrary matrix type. More...
 
__cuda_callable__ Realoperator() (IndexType row, IndexType column)
 Returns non-constant reference to element at row row and column column. More...
 
__cuda_callable__ const Realoperator() (IndexType row, IndexType column) const
 Returns constant reference to element at row row and column column. More...
 
DenseMatrixoperator= (const DenseMatrix &matrix)
 Assignment operator with exactly the same type of the dense matrix. More...
 
template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
DenseMatrix< Real, Device, Index, Organization, RealAllocator > & operator= (const DenseMatrix< Real, Device, Index, Organization, RealAllocator > &matrix)
 
template<typename RHSReal , typename RHSDevice , typename RHSIndex , typename RHSRealAllocator >
DenseMatrixoperator= (const DenseMatrix< RHSReal, RHSDevice, RHSIndex, Organization, RHSRealAllocator > &matrix)
 Assignment operator with the same organization. More...
 
template<typename RHSReal , typename RHSDevice , typename RHSIndex , typename RHSRealAllocator >
DenseMatrix< Real, Device, Index, Organization, RealAllocator > & operator= (const DenseMatrix< RHSReal, RHSDevice, RHSIndex, Organization, RHSRealAllocator > &matrix)
 
template<typename RHSReal , typename RHSDevice , typename RHSIndex , ElementsOrganization RHSOrganization, typename RHSRealAllocator >
DenseMatrixoperator= (const DenseMatrix< RHSReal, RHSDevice, RHSIndex, RHSOrganization, RHSRealAllocator > &matrix)
 Assignment operator with other dense matrices. More...
 
template<typename RHSReal , typename RHSDevice , typename RHSIndex , ElementsOrganization RHSOrganization, typename RHSRealAllocator >
DenseMatrix< Real, Device, Index, Organization, RealAllocator > & operator= (const DenseMatrix< RHSReal, RHSDevice, RHSIndex, RHSOrganization, RHSRealAllocator > &matrix)
 
template<typename RHSReal , typename RHSDevice , typename RHSIndex >
DenseMatrixoperator= (const DenseMatrixView< RHSReal, RHSDevice, RHSIndex, Organization > &matrix)
 Assignment operator with matrix view having the same elements organization. More...
 
template<typename RHSReal , typename RHSDevice , typename RHSIndex >
DenseMatrix< Real, Device, Index, Organization, RealAllocator > & operator= (const DenseMatrixView< RHSReal, RHSDevice, RHSIndex, Organization > &matrix)
 
template<typename RHSReal , typename RHSDevice , typename RHSIndex , ElementsOrganization RHSOrganization>
DenseMatrixoperator= (const DenseMatrixView< RHSReal, RHSDevice, RHSIndex, RHSOrganization > &matrix)
 Assignment operator with other dense matrices. More...
 
template<typename RHSReal , typename RHSDevice , typename RHSIndex , ElementsOrganization RHSOrganization>
DenseMatrix< Real, Device, Index, Organization, RealAllocator > & operator= (const DenseMatrixView< RHSReal, RHSDevice, RHSIndex, RHSOrganization > &matrix)
 
template<typename RHSMatrix >
DenseMatrixoperator= (const RHSMatrix &matrix)
 Assignment operator with other (sparse) types of matrices. More...
 
template<typename RHSMatrix >
DenseMatrix< Real, Device, Index, Organization, RealAllocator > & operator= (const RHSMatrix &matrix)
 
template<typename Real_ , typename Device_ , typename Index_ , typename RealAllocator_ >
bool operator== (const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ > &matrix) const
 Comparison operator with another dense matrix. More...
 
template<typename Real_ , typename Device_ , typename Index_ >
bool operator== (const DenseMatrixView< Real_, Device_, Index_, Organization > &matrix) const
 Comparison operator with another dense matrix view. More...
 
template<typename Matrix >
bool operator== (const Matrix &matrix) const
 Comparison operator with another arbitrary matrix type. 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, const 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, const Reduce &reduce, Keep &keep, const FetchReal &identity) const
 Method for performing general reduction on ALL matrix rows for constant instances. More...
 
template<typename Fetch , typename Reduce , typename Keep , typename FetchValue >
void reduceRows (IndexType begin, IndexType end, Fetch &fetch, const Reduce &reduce, Keep &keep, const FetchValue &identity)
 Method for performing general reduction on matrix rows. More...
 
template<typename Fetch , typename Reduce , typename Keep , typename FetchValue >
void reduceRows (IndexType begin, IndexType end, Fetch &fetch, const Reduce &reduce, Keep &keep, const FetchValue &identity) const
 Method for performing general reduction on matrix rows for constant 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 number of rows and columns of this matrix. More...
 
__cuda_callable__ void setElement (IndexType row, IndexType column, const RealType &value)
 Sets element at given row and column to given value. More...
 
template<typename Value >
void setElements (std::initializer_list< std::initializer_list< Value > > data)
 This method recreates the dense matrix from 2D initializer list. More...
 
template<typename Matrix >
void setLike (const Matrix &matrix)
 Set the number of matrix rows and columns by the given matrix. More...
 
template<typename Matrix_ >
void setLike (const Matrix_ &matrix)
 
template<typename RowCapacitiesVector >
void setRowCapacities (const RowCapacitiesVector &rowCapacities)
 This method is only for the compatibility with the sparse matrices. More...
 
void setValue (const RealType &v)
 Sets all matrix elements to value v. More...
 
template<typename InVector , typename OutVector >
void vectorProduct (const InVector &inVector, OutVector &outVector, const RealType &matrixMultiplicator=1.0, const RealType &outVectorMultiplicator=0.0, IndexType begin=0, IndexType end=0) const
 Computes product of matrix and vector. More...
 
- Public Member Functions inherited from TNL::Matrices::Matrix< Real, Device, Index, RealAllocator >
 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...
 
template<typename Matrix >
bool operator!= (const Matrix &matrix) const
 Comparison operator with another arbitrary matrix type. More...
 
template<typename MatrixT >
bool operator!= (const MatrixT &matrix) const
 
template<typename Matrix >
bool operator== (const Matrix &matrix) const
 Comparison operator with another arbitrary matrix type. More...
 
template<typename MatrixT >
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...
 
template<typename Matrix_ >
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 ()
 Matrix elements organization getter. More...
 
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 Types

using BaseType = Matrix< Real, Device, Index, RealAllocator >
 
using SegmentsType = Algorithms::Segments::Ellpack< Device, Index, typename Allocators::Default< Device >::template Allocator< Index >, Organization, 1 >
 
using SegmentViewType = typename SegmentsType::SegmentViewType
 
using ValuesVectorType = typename BaseType::ValuesType
 
using ValuesViewType = typename ValuesVectorType::ViewType
 

Protected Member Functions

__cuda_callable__ IndexType getElementIndex (IndexType row, IndexType column) const
 

Protected Attributes

SegmentsType segments
 
ViewType view
 
- Protected Attributes inherited from TNL::Matrices::Matrix< Real, Device, Index, RealAllocator >
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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >

Implementation of dense matrix, i.e. matrix storing explicitly all of its elements including zeros.

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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::ConstViewType = typename DenseMatrixView< Real, Device, Index, Organization >::ConstViewType

Matrix view type for constant instances.

See DenseMatrixView.

◆ 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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::ViewType = DenseMatrixView< Real, Device, Index, Organization >

Type of related matrix view.

See DenseMatrixView.

Constructor & Destructor Documentation

◆ DenseMatrix() [1/5]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::DenseMatrix ( const RealAllocatorType allocator = RealAllocatorType())

Constructor only with values allocator.

Parameters
allocatoris used for allocation of matrix elements values.

◆ DenseMatrix() [2/5]

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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::DenseMatrix ( const DenseMatrix< Real, Device, Index, Organization, RealAllocator > &  matrix)
default

Copy constructor.

Parameters
matrixis the source matrix

◆ DenseMatrix() [3/5]

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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::DenseMatrix ( DenseMatrix< Real, Device, Index, Organization, RealAllocator > &&  matrix)
defaultnoexcept

Move constructor.

Parameters
matrixis the source matrix

◆ DenseMatrix() [4/5]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::DenseMatrix ( IndexType  rows,
IndexType  columns,
const RealAllocatorType allocator = RealAllocatorType() 
)

Constructor with matrix dimensions.

Parameters
rowsis number of matrix rows.
columnsis number of matrix columns.
allocatoris used for allocation of matrix elements values.

◆ DenseMatrix() [5/5]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Value >
TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::DenseMatrix ( std::initializer_list< std::initializer_list< Value > >  data,
const RealAllocatorType allocator = RealAllocatorType() 
)

Constructor with 2D initializer list.

The number of matrix rows is set to the outer list size and the number of matrix columns is set to maximum size of inner lists. Missing elements are filled in with zeros.

Parameters
datais a initializer list of initializer lists representing list of matrix rows.
allocatoris used for allocation of matrix elements values.
Example
#include <iostream>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void initializerListExample()
{
{ 1, 2, 3, 4, 5, 6 },
{ 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18 }
};
std::cout << "General dense matrix: " << std::endl << matrix << std::endl;
{ 1 },
{ 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 }
};
std::cout << "Triangular dense matrix: " << std::endl << triangularMatrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Creating matrices on CPU ... " << std::endl;
initializerListExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Creating matrices on CUDA GPU ... " << std::endl;
initializerListExample< TNL::Devices::Cuda >();
#endif
}
Implementation of dense matrix, i.e. matrix storing explicitly all of its elements including zeros.
Definition: DenseMatrix.h:35
T endl(T... args)
Output
Creating matrices on CPU ...
General dense matrix:
Row: 0 -> 0:1 1:2 2:3 3:4 4:5 5:6
Row: 1 -> 0:7 1:8 2:9 3:10 4:11 5:12
Row: 2 -> 0:13 1:14 2:15 3:16 4:17 5:18
Triangular dense matrix:
Row: 0 -> 0:1 1:0 2:0 3:0 4:0
Row: 1 -> 0:2 1:3 2:0 3:0 4:0
Row: 2 -> 0:4 1:5 2:6 3:0 4:0
Row: 3 -> 0:7 1:8 2:9 3:10 4:0
Row: 4 -> 0:11 1:12 2:13 3:14 4:15
Creating matrices on CUDA GPU ...
General dense matrix:
Row: 0 -> 0:1 1:2 2:3 3:4 4:5 5:6
Row: 1 -> 0:7 1:8 2:9 3:10 4:11 5:12
Row: 2 -> 0:13 1:14 2:15 3:16 4:17 5:18
Triangular dense matrix:
Row: 0 -> 0:1 1:0 2:0 3:0 4:0
Row: 1 -> 0:2 1:3 2:0 3:0 4:0
Row: 2 -> 0:4 1:5 2:6 3:0 4:0
Row: 3 -> 0:7 1:8 2:9 3:10 4:0
Row: 4 -> 0:11 1:12 2:13 3:14 4:15

Member Function Documentation

◆ addElement()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
__cuda_callable__ void TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::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. DenseMatrix::getRow or DenseMatrix::forElements and DenseMatrix::forAllElements.

Parameters
rowis row index of the element.
columnis columns index of the element.
valueis the value the element will be set to.
thisElementMultiplicatoris multiplicator the original matrix element value is multiplied by before addition of given value.
Example
#include <iostream>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void addElements()
{
for( int i = 0; i < 5; i++ )
matrix.setElement( i, i, i );
std::cout << "Initial matrix is: " << std::endl << matrix << std::endl;
for( int i = 0; i < 5; i++ )
for( int j = 0; j < 5; j++ )
matrix.addElement( i, j, 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 __CUDACC__
std::cout << "Add elements on CUDA device:" << std::endl;
addElements< TNL::Devices::Cuda >();
#endif
}
Output
Add elements on host:
Initial matrix is:
Row: 0 -> 0:0 1:0 2:0 3:0 4:0
Row: 1 -> 0:0 1:1 2:0 3:0 4:0
Row: 2 -> 0:0 1:0 2:2 3:0 4:0
Row: 3 -> 0:0 1:0 2:0 3:3 4:0
Row: 4 -> 0:0 1:0 2:0 3:0 4:4
Matrix after addition is:
Row: 0 -> 0:1 1:1 2:1 3:1 4:1
Row: 1 -> 0:1 1:6 2:1 3:1 4:1
Row: 2 -> 0:1 1:1 2:11 3:1 4:1
Row: 3 -> 0:1 1:1 2:1 3:16 4:1
Row: 4 -> 0:1 1:1 2:1 3:1 4:21
Add elements on CUDA device:
Initial matrix is:
Row: 0 -> 0:0 1:0 2:0 3:0 4:0
Row: 1 -> 0:0 1:1 2:0 3:0 4:0
Row: 2 -> 0:0 1:0 2:2 3:0 4:0
Row: 3 -> 0:0 1:0 2:0 3:3 4:0
Row: 4 -> 0:0 1:0 2:0 3:0 4:4
Matrix after addition is:
Row: 0 -> 0:1 1:1 2:1 3:1 4:1
Row: 1 -> 0:1 1:6 2:1 3:1 4:1
Row: 2 -> 0:1 1:1 2:11 3:1 4:1
Row: 3 -> 0:1 1:1 2:1 3:16 4:1
Row: 4 -> 0:1 1:1 2:1 3:1 4:21

◆ forAllElements() [1/2]

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

This method calls forElements for all matrix rows.

See DenseMatrix::forAllElements.

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.
Example
#include <iostream>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forAllElementsExample()
{
auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int columnIdx_, double& value ) {
if( rowIdx >= columnIdx )
value = rowIdx + columnIdx;
};
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 __CUDACC__
std::cout << "Creating matrix on CUDA device: " << std::endl;
forAllElementsExample< TNL::Devices::Cuda >();
#endif
}
#define __cuda_callable__
Definition: CudaCallable.h:22
Output
Creating matrix on host:
Row: 0 -> 0:0 1:0 2:0 3:0 4:0
Row: 1 -> 0:1 1:2 2:0 3:0 4:0
Row: 2 -> 0:2 1:3 2:4 3:0 4:0
Row: 3 -> 0:3 1:4 2:5 3:6 4:0
Row: 4 -> 0:4 1:5 2:6 3:7 4:8
Creating matrix on CUDA device:
Row: 0 -> 0:0 1:0 2:0 3:0 4:0
Row: 1 -> 0:1 1:2 2:0 3:0 4:0
Row: 2 -> 0:2 1:3 2:4 3:0 4:0
Row: 3 -> 0:3 1:4 2:5 3:6 4:0
Row: 4 -> 0:4 1:5 2:6 3:7 4:8

◆ forAllElements() [2/2]

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

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

See DenseMatrix::forElements.

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.
Example
#include <iostream>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forAllElementsExample()
{
auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int columnIdx_, double& value ) {
if( rowIdx >= columnIdx )
value = rowIdx + columnIdx;
};
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 __CUDACC__
std::cout << "Creating matrix on CUDA device: " << std::endl;
forAllElementsExample< TNL::Devices::Cuda >();
#endif
}
Output
Creating matrix on host:
Row: 0 -> 0:0 1:0 2:0 3:0 4:0
Row: 1 -> 0:1 1:2 2:0 3:0 4:0
Row: 2 -> 0:2 1:3 2:4 3:0 4:0
Row: 3 -> 0:3 1:4 2:5 3:6 4:0
Row: 4 -> 0:4 1:5 2:6 3:7 4:8
Creating matrix on CUDA device:
Row: 0 -> 0:0 1:0 2:0 3:0 4:0
Row: 1 -> 0:1 1:2 2:0 3:0 4:0
Row: 2 -> 0:2 1:3 2:4 3:0 4:0
Row: 3 -> 0:3 1:4 2:5 3:6 4:0
Row: 4 -> 0:4 1:5 2:6 3:7 4:8

◆ forAllRows() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::DenseMatrix< 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 DenseMatrix::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: DenseMatrix.h:104

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

Example
#include <iostream>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
const int size = 5;
MatrixType matrix( size, size );
/***
* Set the matrix elements.
*/
auto f = [=] __cuda_callable__ ( RowView& row ) mutable {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setValue( rowIdx - 1, -1.0 );
row.setValue( rowIdx, rowIdx + 1.0 );
if( rowIdx < size - 1 )
row.setValue( rowIdx + 1, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Now divide each matrix row by its largest element with use of iterators.
*/
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
for( auto element : row )
largest = TNL::max( largest, element.value() );
for( auto element : row )
element.value() /= largest;
} );
std::cout << "Divide each matrix row by its largest element... " << std::endl;
std::cout << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
T lowest(T... args)
constexpr ResultType max(const T1 &a, const T2 &b)
This function returns maximum of two numbers.
Definition: Math.h:65
Structure for specifying type of sparse matrix.
Definition: MatrixType.h:21
Output
Getting matrix rows on host:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1
Getting matrix rows on CUDA device:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1

◆ forAllRows() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::DenseMatrix< 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 DenseMatrix::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::DenseMatrix::RowView.

Example
#include <iostream>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
const int size = 5;
MatrixType matrix( size, size );
/***
* Set the matrix elements.
*/
auto f = [=] __cuda_callable__ ( RowView& row ) mutable {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setValue( rowIdx - 1, -1.0 );
row.setValue( rowIdx, rowIdx + 1.0 );
if( rowIdx < size - 1 )
row.setValue( rowIdx + 1, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Now divide each matrix row by its largest element with use of iterators.
*/
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
for( auto element : row )
largest = TNL::max( largest, element.value() );
for( auto element : row )
element.value() /= largest;
} );
std::cout << "Divide each matrix row by its largest element... " << std::endl;
std::cout << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Output
Getting matrix rows on host:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1
Getting matrix rows on CUDA device:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1

◆ forElements() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::forElements ( IndexType  begin,
IndexType  end,
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 columnIdx, IndexType columnIdx_, RealType& value )
{ ... };
Index IndexType
The type used for matrix elements indexing.
Definition: DenseMatrix.h:58
Definition: Real.h:17

The column index repeats twice only for compatibility with sparse matrices.

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/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
const int size = 5;
MatrixType matrix( size, size );
/***
* Set the matrix elements.
*/
auto f = [=] __cuda_callable__ ( RowView& row ) mutable {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setValue( rowIdx - 1, -1.0 );
row.setValue( rowIdx, rowIdx + 1.0 );
if( rowIdx < size - 1 )
row.setValue( rowIdx + 1, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Now divide each matrix row by its largest element with use of iterators.
*/
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
for( auto element : row )
largest = TNL::max( largest, element.value() );
for( auto element : row )
element.value() /= largest;
} );
std::cout << "Divide each matrix row by its largest element... " << std::endl;
std::cout << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Output
Getting matrix rows on host:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1
Getting matrix rows on CUDA device:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1

◆ forElements() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::forElements ( IndexType  begin,
IndexType  end,
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 columnIdx, IndexType columnIdx_, const RealType& value )
{ ... };

The column index repeats twice only for compatibility with sparse matrices.

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/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
const int size = 5;
MatrixType matrix( size, size );
/***
* Set the matrix elements.
*/
auto f = [=] __cuda_callable__ ( RowView& row ) mutable {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setValue( rowIdx - 1, -1.0 );
row.setValue( rowIdx, rowIdx + 1.0 );
if( rowIdx < size - 1 )
row.setValue( rowIdx + 1, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Now divide each matrix row by its largest element with use of iterators.
*/
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
for( auto element : row )
largest = TNL::max( largest, element.value() );
for( auto element : row )
element.value() /= largest;
} );
std::cout << "Divide each matrix row by its largest element... " << std::endl;
std::cout << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Output
Getting matrix rows on host:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1
Getting matrix rows on CUDA device:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1

◆ forRows() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::DenseMatrix< 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 DenseMatrix::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::DenseMatrix::RowView.

Example
#include <iostream>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
const int size = 5;
MatrixType matrix( size, size );
/***
* Set the matrix elements.
*/
auto f = [=] __cuda_callable__ ( RowView& row ) mutable {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setValue( rowIdx - 1, -1.0 );
row.setValue( rowIdx, rowIdx + 1.0 );
if( rowIdx < size - 1 )
row.setValue( rowIdx + 1, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Now divide each matrix row by its largest element with use of iterators.
*/
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
for( auto element : row )
largest = TNL::max( largest, element.value() );
for( auto element : row )
element.value() /= largest;
} );
std::cout << "Divide each matrix row by its largest element... " << std::endl;
std::cout << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Output
Getting matrix rows on host:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1
Getting matrix rows on CUDA device:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1

◆ forRows() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Function >
void TNL::Matrices::DenseMatrix< 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 DenseMatrix::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::DenseMatrix::RowView.

Example
#include <iostream>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void forRowsExample()
{
using RowView = typename MatrixType::RowView;
const int size = 5;
MatrixType matrix( size, size );
/***
* Set the matrix elements.
*/
auto f = [=] __cuda_callable__ ( RowView& row ) mutable {
const int& rowIdx = row.getRowIndex();
if( rowIdx > 0 )
row.setValue( rowIdx - 1, -1.0 );
row.setValue( rowIdx, rowIdx + 1.0 );
if( rowIdx < size - 1 )
row.setValue( rowIdx + 1, -1.0 );
};
matrix.forAllRows( f );
std::cout << matrix << std::endl;
/***
* Now divide each matrix row by its largest element with use of iterators.
*/
matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
for( auto element : row )
largest = TNL::max( largest, element.value() );
for( auto element : row )
element.value() /= largest;
} );
std::cout << "Divide each matrix row by its largest element... " << std::endl;
std::cout << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
forRowsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
forRowsExample< TNL::Devices::Cuda >();
#endif
}
Output
Getting matrix rows on host:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1
Getting matrix rows on CUDA device:
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-1 1:2 2:-1 3:0 4:0
Row: 2 -> 0:0 1:-1 2:3 3:-1 4:0
Row: 3 -> 0:0 1:0 2:-1 3:4 4:-1
Row: 4 -> 0:0 1:0 2:0 3:-1 4:5
Divide each matrix row by its largest element...
Row: 0 -> 0:1 1:-1 2:0 3:0 4:0
Row: 1 -> 0:-0.5 1:1 2:-0.5 3:0 4:0
Row: 2 -> 0:0 1:-0.333333 2:1 3:-0.333333 4:0
Row: 3 -> 0:0 1:0 2:-0.25 3:1 4:-0.25
Row: 4 -> 0:0 1:0 2:0 3:-0.2 4:1

◆ getCompressedRowLengths()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename RowLengthsVector >
void TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::getCompressedRowLengths ( RowLengthsVector &  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/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void getCompressedRowLengthsExample()
{
{ 1 },
{ 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 }
};
std::cout << triangularMatrix << std::endl;
triangularMatrix.getCompressedRowLengths( rowLengths );
std::cout << "Compressed row lengths are: " << rowLengths << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting compressed row lengths on host: " << std::endl;
getCompressedRowLengthsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Getting compressed row lengths on CUDA device: " << std::endl;
getCompressedRowLengthsExample< TNL::Devices::Cuda >();
#endif
}
Vector extends Array with algebraic operations.
Definition: Vector.h:40
Output
Getting compressed row lengths on host:
Row: 0 -> 0:1 1:0 2:0 3:0 4:0
Row: 1 -> 0:2 1:3 2:0 3:0 4:0
Row: 2 -> 0:4 1:5 2:6 3:0 4:0
Row: 3 -> 0:7 1:8 2:9 3:10 4:0
Row: 4 -> 0:11 1:12 2:13 3:14 4:15
Compressed row lengths are: [ 1, 2, 3, 4, 5 ]
Getting compressed row lengths on CUDA device:
Row: 0 -> 0:1 1:0 2:0 3:0 4:0
Row: 1 -> 0:2 1:3 2:0 3:0 4:0
Row: 2 -> 0:4 1:5 2:6 3:0 4:0
Row: 3 -> 0:7 1:8 2:9 3:10 4:0
Row: 4 -> 0:11 1:12 2:13 3:14 4:15
Compressed row lengths are: [ 1, 2, 3, 4, 5 ]

◆ getConstView()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
auto TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::getConstView

Returns a non-modifiable view of the dense matrix.

See DenseMatrixView.

Returns
dense matrix view.

◆ getElement()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
__cuda_callable__ Real TNL::Matrices::DenseMatrix< 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. DenseMatrix::getRow or DenseMatrix::forElements and DenseMatrix::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/DenseMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void getElements()
{
{ 1, 0, 0, 0, 0 },
{ -1, 2, -1, 0, 0 },
{ 0, -1, 2, -1, 0 },
{ 0, 0, -1, 2, -1 },
{ 0, 0, 0, 0, 1 } };
for( int i = 0; i < 5; i++ )
{
for( int j = 0; j < 5; 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 __CUDACC__
std::cout << "Get elements on CUDA device:" << std::endl;
getElements< TNL::Devices::Cuda >();
#endif
}
T setw(T... args)
Output
Get elements on host:
1 0 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 0 1
Get elements on CUDA device:
1 0 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 0 1

◆ getNonzeroElementsCount()

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

Returns number of non-zero matrix elements.

Returns
number of all non-zero matrix elements.
Example
#include <iostream>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void getElementsCountExample()
{
{ 1 },
{ 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 }
};
std::cout << "Matrix elements count is " << triangularMatrix.getAllocatedElementsCount() << "." << std::endl;
std::cout << "Non-zero matrix elements count is " << triangularMatrix.getNonzeroElementsCount() << "." << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Computing matrix elements on host: " << std::endl;
getElementsCountExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Computing matrix elements on CUDA device: " << std::endl;
getElementsCountExample< TNL::Devices::Cuda >();
#endif
}
Output
Computing matrix elements on host:
Matrix elements count is 25.
Non-zero matrix elements count is 15.
Computing matrix elements on CUDA device:
Matrix elements count is 25.
Non-zero matrix elements count is 15.

Reimplemented from TNL::Matrices::Matrix< Real, Device, Index, RealAllocator >.

◆ getOrganization()

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 ElementsOrganization TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::getOrganization ( )
inlinestaticconstexpr

Matrix elements organization getter.

Returns
matrix elements organization - RowMajorOrder of ColumnMajorOrder.

◆ getRow() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
__cuda_callable__ auto TNL::Matrices::DenseMatrix< 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/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
#include <TNL/Pointers/SharedPointer.h>
template< typename Device >
void getRowExample()
{
MatrixType* matrix_device = &matrix.template modifyData< Device >();
auto f = [=] __cuda_callable__ ( int rowIdx ) mutable {
auto row = matrix_device->getRow( rowIdx );
row.setValue( rowIdx, 10 * ( rowIdx + 1 ) );
};
/***
* For the case when Device is CUDA device we need to synchronize smart
* pointers. To avoid this you may use DenseMatrixView. See
* DenseMatrixView::getRow example for details.
*/
TNL::Pointers::synchronizeSmartPointersOnDevice< Device >();
/***
* Set the matrix elements.
*/
TNL::Algorithms::parallelFor< Device >( 0, matrix->getRows(), f );
std::cout << matrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Getting matrix rows on host: " << std::endl;
getRowExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
getRowExample< TNL::Devices::Cuda >();
#endif
}
Cross-device shared smart pointer.
Definition: SharedPointer.h:49
Output
Getting matrix rows on host:
1
Getting matrix rows on CUDA device:
1

See DenseMatrixRowView.

◆ getRow() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
__cuda_callable__ auto TNL::Matrices::DenseMatrix< 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/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
#include <TNL/Pointers/SharedPointer.h>
template< typename Device >
void getRowExample()
{
{ 1, 0, 0, 0, 0 },
{ 1, 2, 0, 0, 0 },
{ 1, 2, 3, 0, 0 },
{ 1, 2, 3, 4, 0 },
{ 1, 2, 3, 4, 5 }
};
/***
* Fetch lambda function returns diagonal element in each row.
*/
const MatrixType* matrix_device = &matrix.template getData< Device >();
auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double {
auto row = matrix_device->getRow( rowIdx );
return row.getValue( rowIdx );
};
/***
* For the case when Device is CUDA device we need to synchronize smart
* pointers. To avoid this you may use DenseMatrixView. See
* DenseMatrixView::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 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 __CUDACC__
std::cout << "Getting matrix rows on CUDA device: " << std::endl;
getRowExample< TNL::Devices::Cuda >();
#endif
}
Output
Getting matrix rows on host:
Matrix trace is 15.
Getting matrix rows on CUDA device:
Matrix trace is 15.

See DenseMatrixRowView.

◆ getRowCapacities()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Vector >
void TNL::Matrices::DenseMatrix< 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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::getSerializationType
static

Returns string with serialization type.

The string has a form `MatricesDenseMatrix< RealType, [any_device], IndexType, [any_allocator], true/false >`.

Returns
String with the serialization type.

◆ getSerializationTypeVirtual()

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

Returns string with serialization type.

See DenseMatrix::getSerializationType.

Returns
String with the serialization type.

Reimplemented from TNL::Object.

◆ getView()

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

Returns a modifiable view of the dense matrix.

See DenseMatrixView.

Returns
dense 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::DenseMatrix< 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::DenseMatrix< 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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::load ( File file)
overridevirtual

Method for loading the matrix from a file.

Parameters
fileis the file from which the matrix will be loaded.

Reimplemented from TNL::Object.

◆ operator!=() [1/3]

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

Comparison operator with another dense matrix.

Parameters
matrixis the right-hand side matrix.
Returns
false if the RHS matrix is equal, true otherwise.

◆ operator!=() [2/3]

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

Comparison operator with another dense matrix view.

Parameters
matrixis the right-hand side matrix view.
Returns
false if the RHS matrix view is equal, true otherwise.

◆ operator!=() [3/3]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Matrix >
bool TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::operator!= ( const Matrix matrix) const

Comparison operator with another arbitrary matrix type.

Parameters
matrixis the right-hand side matrix.
Returns
true if the RHS matrix is equal, false otherwise.

◆ operator()() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
__cuda_callable__ Real & TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::operator() ( IndexType  row,
IndexType  column 
)

Returns non-constant reference to element at row row and column column.

Since this method returns reference to the element, it cannot be called across different address spaces. It means that it can be called only form CPU if the matrix is allocated on CPU or only from GPU kernels if the matrix is allocated on GPU.

Parameters
rowis a row index of the element.
columnis a columns index of the element.
Returns
reference to given matrix element.

◆ operator()() [2/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
__cuda_callable__ const Real & TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::operator() ( IndexType  row,
IndexType  column 
) const

Returns constant reference to element at row row and column column.

Since this method returns reference to the element, it cannot be called across different address spaces. It means that it can be called only form CPU if the matrix is allocated on CPU or only from GPU kernels if the matrix is allocated on GPU.

Parameters
rowis a row index of the element.
columnis a columns index of the element.
Returns
reference to given matrix element.

◆ operator=() [1/6]

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 >>
DenseMatrix & TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::operator= ( const DenseMatrix< Real, Device, Index, Organization, RealAllocator > &  matrix)

Assignment operator with exactly the same type of the dense matrix.

Parameters
matrixis the right-hand side matrix.
Returns
reference to this matrix.

◆ operator=() [2/6]

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 RHSReal , typename RHSDevice , typename RHSIndex , typename RHSRealAllocator >
DenseMatrix & TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::operator= ( const DenseMatrix< RHSReal, RHSDevice, RHSIndex, Organization, RHSRealAllocator > &  matrix)

Assignment operator with the same organization.

Parameters
matrixis the right-hand side matrix.
Returns
reference to this matrix.

◆ operator=() [3/6]

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 RHSReal , typename RHSDevice , typename RHSIndex , ElementsOrganization RHSOrganization, typename RHSRealAllocator >
DenseMatrix & TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::operator= ( const DenseMatrix< RHSReal, RHSDevice, RHSIndex, RHSOrganization, RHSRealAllocator > &  matrix)

Assignment operator with other dense matrices.

Parameters
matrixis the right-hand side matrix.
Returns
reference to this matrix.

◆ operator=() [4/6]

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 RHSReal , typename RHSDevice , typename RHSIndex >
DenseMatrix & TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::operator= ( const DenseMatrixView< RHSReal, RHSDevice, RHSIndex, Organization > &  matrix)

Assignment operator with matrix view having the same elements organization.

Parameters
matrixis the right-hand side matrix.
Returns
reference to this matrix.

◆ operator=() [5/6]

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 RHSReal , typename RHSDevice , typename RHSIndex , ElementsOrganization RHSOrganization>
DenseMatrix & TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::operator= ( const DenseMatrixView< RHSReal, RHSDevice, RHSIndex, RHSOrganization > &  matrix)

Assignment operator with other dense matrices.

Parameters
matrixis the right-hand side matrix.
Returns
reference to this matrix.

◆ operator=() [6/6]

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 RHSMatrix >
DenseMatrix & TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::operator= ( const RHSMatrix &  matrix)

Assignment operator with other (sparse) types of matrices.

Parameters
matrixis the right-hand side matrix.
Returns
reference to this matrix.

◆ operator==() [1/3]

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

Comparison operator with another dense matrix.

Parameters
matrixis the right-hand side matrix.
Returns
true if the RHS matrix is equal, false otherwise.

◆ operator==() [2/3]

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

Comparison operator with another dense matrix view.

Parameters
matrixis the right-hand side matrix view.
Returns
true if the RHS matrix view is equal, false otherwise.

◆ operator==() [3/3]

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

Comparison operator with another arbitrary matrix type.

Parameters
matrixis the right-hand side matrix.
Returns
true if the RHS matrix is equal, false otherwise.

◆ print()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::DenseMatrix< 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< Real, Device, Index, RealAllocator >.

◆ 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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::reduceAllRows ( Fetch &  fetch,
const 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 { ... };
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 ) { ... };
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/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
{
{ 1, 0, 0, 0, 0 },
{ 1, 2, 0, 0, 0 },
{ 0, 1, 8, 0, 0 },
{ 0, 0, 1, 9, 0 },
{ 0, 0, 0, 0, 1 } };
/***
* Find largest element in each row.
*/
TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() );
/***
* Prepare vector view and matrix 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 << "Max. elements in rows are: " << rowMax << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "All rows reduction on host:" << std::endl;
reduceAllRows< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "All rows reduction on CUDA device:" << std::endl;
reduceAllRows< TNL::Devices::Cuda >();
#endif
}
void reduceAllRows(Fetch &fetch, const Reduce &reduce, Keep &keep, const FetchReal &identity)
Method for performing general reduction on ALL matrix rows.
Definition: DenseMatrix.hpp:261
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
__cuda_callable__ T abs(const T &n)
This function returns absolute value of given number n.
Definition: Math.h:91
Output
All rows reduction on host:
Max. elements in rows are: [ 1, 2, 8, 9, 1 ]
All rows reduction on CUDA device:
Max. elements in rows are: [ 1, 2, 8, 9, 1 ]

◆ 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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::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
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/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
{
{ 1, 0, 0, 0, 0 },
{ 1, 2, 0, 0, 0 },
{ 0, 1, 8, 0, 0 },
{ 0, 0, 1, 9, 0 },
{ 0, 0, 0, 0, 1 } };
/***
* Find largest element in each row.
*/
TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() );
/***
* Prepare vector view and matrix 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 << "Max. elements in rows are: " << rowMax << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "All rows reduction on host:" << std::endl;
reduceAllRows< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "All rows reduction on CUDA device:" << std::endl;
reduceAllRows< TNL::Devices::Cuda >();
#endif
}
Output
All rows reduction on host:
Max. elements in rows are: [ 1, 2, 8, 9, 1 ]
All rows reduction on CUDA device:
Max. elements in rows are: [ 1, 2, 8, 9, 1 ]

◆ reduceRows() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Fetch , typename Reduce , typename Keep , typename FetchValue >
void TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::reduceRows ( IndexType  begin,
IndexType  end,
Fetch &  fetch,
const Reduce &  reduce,
Keep &  keep,
const FetchValue &  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/DenseMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void reduceRows()
{
{ 1, 0, 0, 0, 0 },
{ 1, 2, 0, 0, 0 },
{ 0, 1, 8, 0, 0 },
{ 0, 0, 1, 9, 0 },
{ 0, 0, 0, 0, 1 } };
/***
* 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.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() );
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 __CUDACC__
std::cout << "Rows reduction on CUDA device:" << std::endl;
reduceRows< TNL::Devices::Cuda >();
#endif
}
void reduceRows(IndexType begin, IndexType end, Fetch &fetch, const Reduce &reduce, Keep &keep, const FetchValue &identity)
Method for performing general reduction on matrix rows.
Definition: DenseMatrix.hpp:235
Output
Rows reduction on host:
Max. elements in rows are: [ 1, 2, 8, 9, 1 ]
Rows reduction on CUDA device:
Max. elements in rows are: [ 1, 2, 8, 9, 1 ]

◆ reduceRows() [2/2]

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

Method for performing general reduction on matrix rows for constant 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/DenseMatrix.h>
#include <TNL/Devices/Host.h>
template< typename Device >
void reduceRows()
{
{ 1, 0, 0, 0, 0 },
{ 1, 2, 0, 0, 0 },
{ 0, 1, 8, 0, 0 },
{ 0, 0, 1, 9, 0 },
{ 0, 0, 0, 0, 1 } };
/***
* 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.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() );
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 __CUDACC__
std::cout << "Rows reduction on CUDA device:" << std::endl;
reduceRows< TNL::Devices::Cuda >();
#endif
}
Output
Rows reduction on host:
Max. elements in rows are: [ 1, 2, 8, 9, 1 ]
Rows reduction on CUDA device:
Max. elements in rows are: [ 1, 2, 8, 9, 1 ]

◆ save() [1/2]

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
void TNL::Matrices::DenseMatrix< 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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::save ( File file) const
overridevirtual

Method for saving the matrix to a file.

Parameters
fileis the file where the matrix will be saved.

Reimplemented from TNL::Object.

◆ sequentialForAllRows() [1/2]

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

This method calls sequentialForRows for all matrix rows.

See DenseMatrix::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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::sequentialForAllRows ( Function &&  function) const

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

See DenseMatrix::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::DenseMatrix< 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::DenseMatrix::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::DenseMatrix< 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::DenseMatrix::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::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::setDimensions ( IndexType  rows,
IndexType  columns 
)
overridevirtual

Set number of rows and columns of this matrix.

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

Reimplemented from TNL::Matrices::Matrix< Real, Device, Index, RealAllocator >.

◆ setElement()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
__cuda_callable__ void TNL::Matrices::DenseMatrix< 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. DenseMatrix::getRow or DenseMatrix::forElements and DenseMatrix::forAllElements.

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/Containers/StaticArray.h>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
#include <TNL/Pointers/SharedPointer.h>
#include <TNL/Pointers/SmartPointersRegister.h>
template< typename Device >
{
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;
MatrixType* matrix_device = &matrix.template modifyData< Device >();
auto f = [=] __cuda_callable__ ( const TNL::Containers::StaticArray< 2, int >& i ) mutable {
matrix_device->addElement( i[ 0 ], i[ 1 ], 5.0 );
};
/***
* For the case when Device is CUDA device we need to synchronize smart
* pointers. To avoid this you may use DenseMatrixView. See
* DenseMatrixView::getRow example for details.
*/
TNL::Pointers::synchronizeSmartPointersOnDevice< Device >();
TNL::Algorithms::parallelFor< Device >( begin, end, f );
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 __CUDACC__
std::cout << "Set elements on CUDA device:" << std::endl;
setElements< TNL::Devices::Cuda >();
#endif
}
Array with constant size.
Definition: StaticArray.h:24
void setElements(std::initializer_list< std::initializer_list< Value > > data)
This method recreates the dense matrix from 2D initializer list.
Definition: DenseMatrix.hpp:45
Output
Set elements on host:
Matrix set from the host:
Row: 0 -> 0:0 1:0 2:0 3:0 4:0
Row: 1 -> 0:0 1:1 2:0 3:0 4:0
Row: 2 -> 0:0 1:0 2:2 3:0 4:0
Row: 3 -> 0:0 1:0 2:0 3:3 4:0
Row: 4 -> 0:0 1:0 2:0 3:0 4:4
Matrix set from its native device:
Row: 0 -> 0:5 1:5 2:5 3:5 4:5
Row: 1 -> 0:5 1:6 2:5 3:5 4:5
Row: 2 -> 0:5 1:5 2:7 3:5 4:5
Row: 3 -> 0:5 1:5 2:5 3:8 4:5
Row: 4 -> 0:5 1:5 2:5 3:5 4:9
Set elements on CUDA device:
Matrix set from the host:
Row: 0 -> 0:0 1:0 2:0 3:0 4:0
Row: 1 -> 0:0 1:1 2:0 3:0 4:0
Row: 2 -> 0:0 1:0 2:2 3:0 4:0
Row: 3 -> 0:0 1:0 2:0 3:3 4:0
Row: 4 -> 0:0 1:0 2:0 3:0 4:4
Matrix set from its native device:
Row: 0 -> 0:5 1:5 2:5 3:5 4:5
Row: 1 -> 0:5 1:6 2:5 3:5 4:5
Row: 2 -> 0:5 1:5 2:7 3:5 4:5
Row: 3 -> 0:5 1:5 2:5 3:8 4:5
Row: 4 -> 0:5 1:5 2:5 3:5 4:9

◆ setElements()

template<typename Real , typename Device , typename Index , ElementsOrganization Organization, typename RealAllocator >
template<typename Value >
void TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::setElements ( std::initializer_list< std::initializer_list< Value > >  data)

This method recreates the dense matrix from 2D initializer list.

The number of matrix rows is set to the outer list size and the number of matrix columns is set to maximum size of inner lists. Missing elements are filled in with zeros.

Parameters
datais a initializer list of initializer lists representing list of matrix rows.
Example
#include <iostream>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void setElementsExample()
{
matrix.setElements( {
{ 1, 2, 3, 4, 5, 6 },
{ 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18 }
} );
std::cout << matrix << std::endl;
triangularMatrix.setElements( {
{ 1 },
{ 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 }
} );
std::cout << triangularMatrix << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Setting matrix elements on host: " << std::endl;
setElementsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Setting matrix elements on CUDA device: " << std::endl;
setElementsExample< TNL::Devices::Cuda >();
#endif
}
Output
Setting matrix elements on host:
Row: 0 -> 0:1 1:2 2:3 3:4 4:5 5:6
Row: 1 -> 0:7 1:8 2:9 3:10 4:11 5:12
Row: 2 -> 0:13 1:14 2:15 3:16 4:17 5:18
Row: 0 -> 0:1 1:0 2:0 3:0 4:0
Row: 1 -> 0:2 1:3 2:0 3:0 4:0
Row: 2 -> 0:4 1:5 2:6 3:0 4:0
Row: 3 -> 0:7 1:8 2:9 3:10 4:0
Row: 4 -> 0:11 1:12 2:13 3:14 4:15
Setting matrix elements on CUDA device:
Row: 0 -> 0:1 1:2 2:3 3:4 4:5 5:6
Row: 1 -> 0:7 1:8 2:9 3:10 4:11 5:12
Row: 2 -> 0:13 1:14 2:15 3:16 4:17 5:18
Row: 0 -> 0:1 1:0 2:0 3:0 4:0
Row: 1 -> 0:2 1:3 2:0 3:0 4:0
Row: 2 -> 0:4 1:5 2:6 3:0 4:0
Row: 3 -> 0:7 1:8 2:9 3:10 4:0
Row: 4 -> 0:11 1:12 2:13 3:14 4:15

◆ setLike()

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 Matrix >
void TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::setLike ( const Matrix matrix)

Set the number of matrix rows and columns by the given matrix.

Template Parameters
Matrixis matrix type. This can be any matrix having methods getRows and getColumns.
Parameters
matrixin the input matrix dimensions of which are to be adopted.

◆ setRowCapacities()

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

This method is only for the compatibility with the sparse matrices.

This method does nothing. In debug mode it contains assertions checking that given rowCapacities are compatible with the current matrix dimensions.

◆ setValue()

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 >>
void TNL::Matrices::DenseMatrix< Real, Device, Index, Organization, RealAllocator >::setValue ( const RealType v)

Sets all matrix elements to value v.

Parameters
vis value all matrix elements will be set to.

◆ vectorProduct()

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

Computes product of matrix and vector.

More precisely, it computes:

outVector = matrixMultiplicator * ( *this ) * inVector + outVectorMultiplicator * 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.
matrixMultiplicatoris a factor by which the matrix is multiplied. It is one by default.
outVectorMultiplicatoris 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.

Note that the ouput vector dimension must be the same as the number of matrix rows no matter how we set begin and end parameters. These parameters just say that some matrix rows and the output vector elements are omitted.


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