Loading src/TNL/Matrices/MultidiagonalMatrix.h +76 −27 Original line number Diff line number Diff line Loading @@ -47,14 +47,15 @@ namespace Matrices { * are \f$\{-3,-1,0,1,3\}\f$. Advantage is that we do not store the column indexes * explicitly as it is in \ref SparseMatrix. This can reduce significantly the * memory requirements which also means better performance. See the following table * for the storage requirements comparison between \ref MultidiagonalMatrix and \ref SparseMatrix. * for the storage requirements comparison between \ref TNL::Matrices::MultidiagonalMatrix * and \ref TNL::Matrices::SparseMatrix. * * Data types | SparseMatrix | MultidiagonalMatrix | Ratio * --------------------|----------------------|---------------------|-------- * float + 32-bit int | 8 bytes per element | 4 bytes per element | 50% * double + 32-bit int| 12 bytes per element | 8 bytes per element | 75% * float + 64-bit int | 12 bytes per element | 4 bytes per element | 30% * double + 64-bit int| 16 bytes per element | 8 bytes per element | 50% * Real | Index | SparseMatrix | MultidiagonalMatrix | Ratio * --------|-----------|----------------------|---------------------|------- * float | 32-bit int| 8 bytes per element | 4 bytes per element | 50% * double | 32-bit int| 12 bytes per element | 8 bytes per element | 75% * float | 64-bit int| 12 bytes per element | 4 bytes per element | 30% * double | 64-bit int| 16 bytes per element | 8 bytes per element | 50% * * \tparam Real is a type of matrix elements. * \tparam Device is a device where the matrix is allocated. Loading Loading @@ -296,6 +297,28 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > const IndexType columns, const Vector& diagonalsOffsets ); /** * @brief Set the diagonals offsets by means of vector-like container. * * This method deletes current matrix elements. * * @tparam Vector is a type of vector-like container holding the subdiagonals offsets. * @param diagonalsOffsets is a vector-like container holding the subdiagonals offsets. */ template< typename Vector > void setDiagonalsOffsets( const Vector& diagonalsOffsets ); /** * @brief Set the diagonals offsets by means of initializer list. * * This method deletes current matrix elements. * * @tparam ListIndex is type of indexes used for the subdiagonals offsets definition. * @param diagonalsOffsets is a initializer list with subdiagonals offsets. */ template< typename ListIndex > void setDiagonalsOffsets( const std::initializer_list< ListIndex > diagonalsOffsets ); /** * \brief This method is for compatibility with \ref SparseMatrix. * Loading @@ -311,6 +334,22 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename RowCapacitiesVector > void setRowCapacities( const RowCapacitiesVector& rowCapacities ); /** * \brief Returns number of diagonals. * * \return Number of diagonals. */ const IndexType& getDiagonalsCount() const; /** * \brief Returns vector with diagonals offsets. * * \return vector with diagonals offsets. */ const DiagonalsOffsetsType& getDiagonalsOffsets() const; template< typename RowCapacitiesVector > void setRowCapacities( const RowCapacitiesVector& rowCapacities ); /** * \brief Set matrix elements from an initializer list. * Loading @@ -329,20 +368,6 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename ListReal > void setElements( const std::initializer_list< std::initializer_list< ListReal > >& data ); /** * \brief Returns number of diagonals. * * \return Number of diagonals. */ const IndexType& getDiagonalsCount() const; /** * \brief Returns vector with diagonals offsets. * * \return vector with diagonals offsets. */ const DiagonalsOffsetsType& getDiagonalsOffsets() const; /** * \brief Computes number of non-zeros in each row. * Loading Loading @@ -666,9 +691,21 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`. * The \e localIdx parameter is a rank of the non-zero element in given row. * If the 'compute' variable is set to false the iteration over the row can * * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`, * * where * * \e rowIdx is an index of the matrix row. * * \e localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, * index of the matrix subdiagonal. * * \e columnIdx is a column index of the matrx element. * * \e value is the matrix element value. * * \e compute is a reference to a boolen variable. If it is set to false the iteration over the row can * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. Loading @@ -688,9 +725,21 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`. * The \e localIdx parameter is a rank of the non-zero element in given row. * If the 'compute' variable is set to false the iteration over the row can * * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`, * * where * * \e rowIdx is an index of the matrix row. * * \e localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, * index of the matrix subdiagonal. * * \e columnIdx is a column index of the matrx element. * * \e value is a reference to the matrix element value. It can be used even for changing the matrix element value. * * \e compute is a reference to a boolen variable. If it is set to false the iteration over the row can * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. Loading src/TNL/Matrices/MultidiagonalMatrix.hpp +34 −3 Original line number Diff line number Diff line Loading @@ -74,9 +74,9 @@ MultidiagonalMatrix( const IndexType columns, const std::initializer_list< ListIndex > diagonalsOffsets, const std::initializer_list< std::initializer_list< ListReal > >& data ) { Containers::Vector< IndexType, DeviceType, IndexType > shifts( diagonalsOffsets ); TNL_ASSERT_GT( shifts.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals shifts." ); this->setDimensions( data.size(), columns, shifts ); Containers::Vector< IndexType, DeviceType, IndexType > offsets( diagonalsOffsets ); TNL_ASSERT_GT( offsets.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals offsets." ); this->setDimensions( data.size(), columns, offsets ); this->setElements( data ); } Loading Loading @@ -149,6 +149,37 @@ setDimensions( const IndexType rows, this->view = this->getView(); } template< typename Real, typename Device, typename Index, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > template< typename Vector > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: setDiagonalsOffsets( const Vector& diagonalsOffsets ) { TNL_ASSERT_GT( diagonalsOffsets.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals offsets." ); this->setDimensions( this->getRows(), this->getColumns(), diagonalsOffsets ); } template< typename Real, typename Device, typename Index, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > template< typename ListIndex > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: setDiagonalsOffsets( const std::initializer_list< ListIndex > diagonalsOffsets ) { Containers::Vector< IndexType, DeviceType, IndexType > offsets( diagonalsOffsets ); TNL_ASSERT_GT( offsets.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals offsets." ); this->setDimensions( this->getRows(), this->getColumns(), offsets ); } template< typename Real, typename Device, typename Index, Loading src/TNL/Matrices/MultidiagonalMatrixView.h +37 −13 Original line number Diff line number Diff line Loading @@ -455,9 +455,21 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`. * The \e localIdx parameter is a rank of the non-zero element in given row. * If the 'compute' variable is set to false the iteration over the row can * * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`, * * where * * \e rowIdx is an index of the matrix row. * * \e localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, * index of the matrix subdiagonal. * * \e columnIdx is a column index of the matrx element. * * \e value is the matrix element value. * * \e compute is a reference to a boolen variable. If it is set to false the iteration over the row can * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. Loading @@ -477,9 +489,21 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`. * The \e localIdx parameter is a rank of the non-zero element in given row. * If the 'compute' variable is set to false the iteration over the row can * * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`, * * where * * \e rowIdx is an index of the matrix row. * * \e localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, * index of the matrix subdiagonal. * * \e columnIdx is a column index of the matrx element. * * \e value is a reference to the matrix element value. It can be used even for changing the matrix element value. * * \e compute is a reference to a boolen variable. If it is set to false the iteration over the row can * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. Loading src/UnitTests/Matrices/MultidiagonalMatrixTest.h +38 −0 Original line number Diff line number Diff line Loading @@ -64,6 +64,44 @@ void test_SetDimensions() EXPECT_EQ( m.getColumns(), 8 ); } template< typename Matrix > void test_SetDiagonalsOffsets() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; const IndexType rows = 9; const IndexType cols = 8; const DiagonalsOffsetsType diagonalsOffsets{ -3, -1, 0, 2, 4 }; Matrix m; m.setDimensions( rows, cols ); m.setDiagonalsOffsets( diagonalsOffsets ); EXPECT_EQ( m.getRows(), 9 ); EXPECT_EQ( m.getColumns(), 8 ); } template< typename Matrix > void test_SetDiagonalsOffsets_initalizer_list() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; const IndexType rows = 9; const IndexType cols = 8; Matrix m; m.setDimensions( rows, cols ); m.setDiagonalsOffsets( { -3, -1, 0, 2, 4 } ); EXPECT_EQ( m.getRows(), 9 ); EXPECT_EQ( m.getColumns(), 8 ); } template< typename Matrix1, typename Matrix2 > void test_SetLike() Loading Loading
src/TNL/Matrices/MultidiagonalMatrix.h +76 −27 Original line number Diff line number Diff line Loading @@ -47,14 +47,15 @@ namespace Matrices { * are \f$\{-3,-1,0,1,3\}\f$. Advantage is that we do not store the column indexes * explicitly as it is in \ref SparseMatrix. This can reduce significantly the * memory requirements which also means better performance. See the following table * for the storage requirements comparison between \ref MultidiagonalMatrix and \ref SparseMatrix. * for the storage requirements comparison between \ref TNL::Matrices::MultidiagonalMatrix * and \ref TNL::Matrices::SparseMatrix. * * Data types | SparseMatrix | MultidiagonalMatrix | Ratio * --------------------|----------------------|---------------------|-------- * float + 32-bit int | 8 bytes per element | 4 bytes per element | 50% * double + 32-bit int| 12 bytes per element | 8 bytes per element | 75% * float + 64-bit int | 12 bytes per element | 4 bytes per element | 30% * double + 64-bit int| 16 bytes per element | 8 bytes per element | 50% * Real | Index | SparseMatrix | MultidiagonalMatrix | Ratio * --------|-----------|----------------------|---------------------|------- * float | 32-bit int| 8 bytes per element | 4 bytes per element | 50% * double | 32-bit int| 12 bytes per element | 8 bytes per element | 75% * float | 64-bit int| 12 bytes per element | 4 bytes per element | 30% * double | 64-bit int| 16 bytes per element | 8 bytes per element | 50% * * \tparam Real is a type of matrix elements. * \tparam Device is a device where the matrix is allocated. Loading Loading @@ -296,6 +297,28 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > const IndexType columns, const Vector& diagonalsOffsets ); /** * @brief Set the diagonals offsets by means of vector-like container. * * This method deletes current matrix elements. * * @tparam Vector is a type of vector-like container holding the subdiagonals offsets. * @param diagonalsOffsets is a vector-like container holding the subdiagonals offsets. */ template< typename Vector > void setDiagonalsOffsets( const Vector& diagonalsOffsets ); /** * @brief Set the diagonals offsets by means of initializer list. * * This method deletes current matrix elements. * * @tparam ListIndex is type of indexes used for the subdiagonals offsets definition. * @param diagonalsOffsets is a initializer list with subdiagonals offsets. */ template< typename ListIndex > void setDiagonalsOffsets( const std::initializer_list< ListIndex > diagonalsOffsets ); /** * \brief This method is for compatibility with \ref SparseMatrix. * Loading @@ -311,6 +334,22 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename RowCapacitiesVector > void setRowCapacities( const RowCapacitiesVector& rowCapacities ); /** * \brief Returns number of diagonals. * * \return Number of diagonals. */ const IndexType& getDiagonalsCount() const; /** * \brief Returns vector with diagonals offsets. * * \return vector with diagonals offsets. */ const DiagonalsOffsetsType& getDiagonalsOffsets() const; template< typename RowCapacitiesVector > void setRowCapacities( const RowCapacitiesVector& rowCapacities ); /** * \brief Set matrix elements from an initializer list. * Loading @@ -329,20 +368,6 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename ListReal > void setElements( const std::initializer_list< std::initializer_list< ListReal > >& data ); /** * \brief Returns number of diagonals. * * \return Number of diagonals. */ const IndexType& getDiagonalsCount() const; /** * \brief Returns vector with diagonals offsets. * * \return vector with diagonals offsets. */ const DiagonalsOffsetsType& getDiagonalsOffsets() const; /** * \brief Computes number of non-zeros in each row. * Loading Loading @@ -666,9 +691,21 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`. * The \e localIdx parameter is a rank of the non-zero element in given row. * If the 'compute' variable is set to false the iteration over the row can * * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`, * * where * * \e rowIdx is an index of the matrix row. * * \e localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, * index of the matrix subdiagonal. * * \e columnIdx is a column index of the matrx element. * * \e value is the matrix element value. * * \e compute is a reference to a boolen variable. If it is set to false the iteration over the row can * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. Loading @@ -688,9 +725,21 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`. * The \e localIdx parameter is a rank of the non-zero element in given row. * If the 'compute' variable is set to false the iteration over the row can * * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`, * * where * * \e rowIdx is an index of the matrix row. * * \e localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, * index of the matrix subdiagonal. * * \e columnIdx is a column index of the matrx element. * * \e value is a reference to the matrix element value. It can be used even for changing the matrix element value. * * \e compute is a reference to a boolen variable. If it is set to false the iteration over the row can * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. Loading
src/TNL/Matrices/MultidiagonalMatrix.hpp +34 −3 Original line number Diff line number Diff line Loading @@ -74,9 +74,9 @@ MultidiagonalMatrix( const IndexType columns, const std::initializer_list< ListIndex > diagonalsOffsets, const std::initializer_list< std::initializer_list< ListReal > >& data ) { Containers::Vector< IndexType, DeviceType, IndexType > shifts( diagonalsOffsets ); TNL_ASSERT_GT( shifts.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals shifts." ); this->setDimensions( data.size(), columns, shifts ); Containers::Vector< IndexType, DeviceType, IndexType > offsets( diagonalsOffsets ); TNL_ASSERT_GT( offsets.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals offsets." ); this->setDimensions( data.size(), columns, offsets ); this->setElements( data ); } Loading Loading @@ -149,6 +149,37 @@ setDimensions( const IndexType rows, this->view = this->getView(); } template< typename Real, typename Device, typename Index, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > template< typename Vector > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: setDiagonalsOffsets( const Vector& diagonalsOffsets ) { TNL_ASSERT_GT( diagonalsOffsets.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals offsets." ); this->setDimensions( this->getRows(), this->getColumns(), diagonalsOffsets ); } template< typename Real, typename Device, typename Index, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > template< typename ListIndex > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: setDiagonalsOffsets( const std::initializer_list< ListIndex > diagonalsOffsets ) { Containers::Vector< IndexType, DeviceType, IndexType > offsets( diagonalsOffsets ); TNL_ASSERT_GT( offsets.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals offsets." ); this->setDimensions( this->getRows(), this->getColumns(), offsets ); } template< typename Real, typename Device, typename Index, Loading
src/TNL/Matrices/MultidiagonalMatrixView.h +37 −13 Original line number Diff line number Diff line Loading @@ -455,9 +455,21 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`. * The \e localIdx parameter is a rank of the non-zero element in given row. * If the 'compute' variable is set to false the iteration over the row can * * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`, * * where * * \e rowIdx is an index of the matrix row. * * \e localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, * index of the matrix subdiagonal. * * \e columnIdx is a column index of the matrx element. * * \e value is the matrix element value. * * \e compute is a reference to a boolen variable. If it is set to false the iteration over the row can * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. Loading @@ -477,9 +489,21 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`. * The \e localIdx parameter is a rank of the non-zero element in given row. * If the 'compute' variable is set to false the iteration over the row can * * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`, * * where * * \e rowIdx is an index of the matrix row. * * \e localIdx parameter is a rank of the non-zero element in given row. It is also, in fact, * index of the matrix subdiagonal. * * \e columnIdx is a column index of the matrx element. * * \e value is a reference to the matrix element value. It can be used even for changing the matrix element value. * * \e compute is a reference to a boolen variable. If it is set to false the iteration over the row can * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. Loading
src/UnitTests/Matrices/MultidiagonalMatrixTest.h +38 −0 Original line number Diff line number Diff line Loading @@ -64,6 +64,44 @@ void test_SetDimensions() EXPECT_EQ( m.getColumns(), 8 ); } template< typename Matrix > void test_SetDiagonalsOffsets() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; const IndexType rows = 9; const IndexType cols = 8; const DiagonalsOffsetsType diagonalsOffsets{ -3, -1, 0, 2, 4 }; Matrix m; m.setDimensions( rows, cols ); m.setDiagonalsOffsets( diagonalsOffsets ); EXPECT_EQ( m.getRows(), 9 ); EXPECT_EQ( m.getColumns(), 8 ); } template< typename Matrix > void test_SetDiagonalsOffsets_initalizer_list() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; const IndexType rows = 9; const IndexType cols = 8; Matrix m; m.setDimensions( rows, cols ); m.setDiagonalsOffsets( { -3, -1, 0, 2, 4 } ); EXPECT_EQ( m.getRows(), 9 ); EXPECT_EQ( m.getColumns(), 8 ); } template< typename Matrix1, typename Matrix2 > void test_SetLike() Loading