From e0eb72002bd92638672abe2211b12ded7a3c6724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= <oberhuber.tomas@gmail.com> Date: Wed, 22 Apr 2020 21:36:29 +0200 Subject: [PATCH] Writting documentation and refactoring DenseMatrix. --- .../Examples/Matrices/CMakeLists.txt | 62 ++++++---- ...enseMatrixExample_Constructor_init_list.cu | 1 - ...seMatrixExample_getCompressedRowLengths.cu | 1 - .../DenseMatrixExample_getConstRow.cpp | 32 +++++ .../DenseMatrixExample_getElementsCount.cpp | 17 +++ .../Matrices/DenseMatrixExample_getRow.cpp | 21 ++++ .../DenseMatrixExample_setElements.cu | 1 - src/TNL/Containers/Segments/BiEllpack.hpp | 3 +- .../Containers/Segments/ChunkedEllpack.hpp | 3 +- src/TNL/File.hpp | 2 +- src/TNL/Matrices/DenseMatrix.h | 114 +++++++++++++++--- src/TNL/Matrices/DenseMatrix.hpp | 16 +-- src/TNL/Matrices/DenseMatrixRowView.h | 4 +- src/TNL/Matrices/DenseMatrixRowView.hpp | 4 +- src/TNL/Matrices/DenseMatrixView.h | 4 +- src/TNL/Matrices/DenseMatrixView.hpp | 10 +- src/TNL/Matrices/MatrixView.h | 2 - src/TNL/Matrices/MatrixView.hpp | 11 -- src/TNL/Matrices/SparseMatrix.h | 2 +- src/UnitTests/Matrices/DenseMatrixTest.h | 72 ++--------- 20 files changed, 236 insertions(+), 146 deletions(-) delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_Constructor_init_list.cu delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_getConstRow.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_getRow.cpp delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_setElements.cu diff --git a/Documentation/Examples/Matrices/CMakeLists.txt b/Documentation/Examples/Matrices/CMakeLists.txt index 8cb519f7a2..f91ae36e06 100644 --- a/Documentation/Examples/Matrices/CMakeLists.txt +++ b/Documentation/Examples/Matrices/CMakeLists.txt @@ -1,17 +1,17 @@ -IF( BUILD_CUDA ) - CUDA_ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list_cuda DenseMatrixExample_Constructor_init_list.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_Constructor_init_list_cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_Constructor_init_list.out - OUTPUT DenseMatrixExample_Constructor_init_list.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_setElements_cuda DenseMatrixExample_setElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_setElements_cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_setElements.out - OUTPUT DenseMatrixExample_setElements.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_getCompressedRowLengths_cuda DenseMatrixExample_getCompressedRowLengths.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getCompressedRowLengths_cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getCompressedRowLengths.out - OUTPUT DenseMatrixExample_getCompressedRowLengths.out ) - -ELSE() +#IF( BUILD_CUDA ) +# CUDA_ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list_cuda DenseMatrixExample_Constructor_init_list.cu ) +# ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_Constructor_init_list_cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_Constructor_init_list.out +# OUTPUT DenseMatrixExample_Constructor_init_list.out ) +# +# CUDA_ADD_EXECUTABLE( DenseMatrixExample_setElements_cuda DenseMatrixExample_setElements.cu ) +# ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_setElements_cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_setElements.out +# OUTPUT DenseMatrixExample_setElements.out ) +# +# CUDA_ADD_EXECUTABLE( DenseMatrixExample_getCompressedRowLengths_cuda DenseMatrixExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getCompressedRowLengths_cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getCompressedRowLengths.out +# OUTPUT DenseMatrixExample_getCompressedRowLengths.out ) +# +#ELSE() ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list DenseMatrixExample_Constructor_init_list.cpp ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_Constructor_init_list > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_Constructor_init_list.out OUTPUT DenseMatrixExample_Constructor_init_list.out ) @@ -23,18 +23,34 @@ ELSE() ADD_EXECUTABLE( DenseMatrixExample_getCompressedRowLengths DenseMatrixExample_getCompressedRowLengths.cpp ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getCompressedRowLengths > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getCompressedRowLengths.out OUTPUT DenseMatrixExample_getCompressedRowLengths.out ) -ENDIF() -IF( BUILD_CUDA ) -ADD_CUSTOM_TARGET( RunMatricesExamples-cuda ALL DEPENDS - DenseMatrixExample_Constructor_init_list.out - DenseMatrixExample_setElements.out - DenseMatrixExample_getCompressedRowLengths.out - ) -ELSE() + ADD_EXECUTABLE( DenseMatrixExample_getElementsCount DenseMatrixExample_getElementsCount.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getElementsCount > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getElementsCount.out + OUTPUT DenseMatrixExample_getElementsCount.out ) + + ADD_EXECUTABLE( DenseMatrixExample_getConstRow DenseMatrixExample_getConstRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getConstRow > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getConstRow.out + OUTPUT DenseMatrixExample_getConstRow.out ) + + ADD_EXECUTABLE( DenseMatrixExample_getRow DenseMatrixExample_getRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getRow > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getRow.out + OUTPUT DenseMatrixExample_getRow.out ) + +#ENDIF() + +#IF( BUILD_CUDA ) +#ADD_CUSTOM_TARGET( RunMatricesExamples-cuda ALL DEPENDS +# DenseMatrixExample_Constructor_init_list.out +# DenseMatrixExample_setElements.out +# DenseMatrixExample_getCompressedRowLengths.out +# ) +#ELSE() ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS DenseMatrixExample_Constructor_init_list.out DenseMatrixExample_setElements.out DenseMatrixExample_getCompressedRowLengths.out + DenseMatrixExample_getElementsCount.out + DenseMatrixExample_getConstRow.out + DenseMatrixExample_getRow.out ) -ENDIF() +#ENDIF() diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_Constructor_init_list.cu b/Documentation/Examples/Matrices/DenseMatrixExample_Constructor_init_list.cu deleted file mode 120000 index 91fa4f073f..0000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_Constructor_init_list.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_Constructor_init_list.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cu deleted file mode 120000 index 2b3cd6c137..0000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_getCompressedRowLengths.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getConstRow.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_getConstRow.cpp new file mode 100644 index 0000000000..8e5da1d4b7 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixExample_getConstRow.cpp @@ -0,0 +1,32 @@ +#include <iostream> +#include <functional> +#include <TNL/Algorithms/ParallelFor.h> +#include <TNL/Matrices/DenseMatrix.h> +#include <TNL/Devices/Host.h> + +int main( int argc, char* argv[] ) +{ + TNL::Matrices::DenseMatrix< double, TNL::Devices::Host > matrix { + { 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 } + }; + + /*** + * We need a matrix view to pass the matrix to lambda function even on CUDA device. + */ + const auto matrixView = matrix.getConstView(); + + /*** + * Fetch lambda function returns diagonal element in each row. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { + auto row = matrixView.getRow( rowIdx ); + return row.getElement( rowIdx ); + }; + + int trace = TNL::Algorithms::Reduction< TNL::Devices::Host >::reduce( matrix.getRows(), std::plus<>{}, fetch, 0 ); + std::cout << "Matrix trace is " << trace << "." << std::endl; +} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp new file mode 100644 index 0000000000..997dadb7fc --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp @@ -0,0 +1,17 @@ +#include <iostream> +#include <TNL/Matrices/DenseMatrix.h> +#include <TNL/Devices/Host.h> + +int main( int argc, char* argv[] ) +{ + TNL::Matrices::DenseMatrix< double, TNL::Devices::Host > triangularMatrix { + { 1 }, + { 2, 3 }, + { 4, 5, 6 }, + { 7, 8, 9, 10 }, + { 11, 12, 13, 14, 15 } + }; + + std::cout << "Matrix elements count is " << triangularMatrix.getElementsCount() << "." << std::endl; + std::cout << "Non-zero matrix elements count is " << triangularMatrix.getNonzeroElementsCount() << "." << std::endl; +} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_getRow.cpp new file mode 100644 index 0000000000..120c934a3b --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixExample_getRow.cpp @@ -0,0 +1,21 @@ +#include <iostream> +#include <TNL/Algorithms/ParallelFor.h> +#include <TNL/Matrices/DenseMatrix.h> +#include <TNL/Devices/Host.h> + +int main( int argc, char* argv[] ) +{ + TNL::Matrices::DenseMatrix< double, TNL::Devices::Host > matrix( 5, 5 ); + + /*** + * We need a matrix view to pass the matrix to lambda function even on CUDA device. + */ + auto matrixView = matrix.getView(); + auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { + auto row = matrixView.getRow( rowIdx ); + row.setElement( rowIdx, 10* ( rowIdx + 1 ) ); + }; + + TNL::Algorithms::ParallelFor< TNL::Devices::Host >::exec( 0, matrix.getRows(), f ); + std::cout << matrix << std::endl; +} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_setElements.cu b/Documentation/Examples/Matrices/DenseMatrixExample_setElements.cu deleted file mode 120000 index fa2487e278..0000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_setElements.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_setElements.cpp \ No newline at end of file diff --git a/src/TNL/Containers/Segments/BiEllpack.hpp b/src/TNL/Containers/Segments/BiEllpack.hpp index 91ebea2073..5203b3b3fa 100644 --- a/src/TNL/Containers/Segments/BiEllpack.hpp +++ b/src/TNL/Containers/Segments/BiEllpack.hpp @@ -341,7 +341,8 @@ setSegmentsSizes( const SizesHolder& segmentsSizes ) else { BiEllpack< Devices::Host, Index, typename Allocators::Default< Devices::Host >::template Allocator< IndexType >, RowMajorOrder > hostSegments; - Containers::Vector< IndexType, Devices::Host, IndexType > hostSegmentsSizes( segmentsSizes ); + Containers::Vector< IndexType, Devices::Host, IndexType > hostSegmentsSizes; + hostSegmentsSizes = segmentsSizes; hostSegments.setSegmentsSizes( hostSegmentsSizes ); *this = hostSegments; } diff --git a/src/TNL/Containers/Segments/ChunkedEllpack.hpp b/src/TNL/Containers/Segments/ChunkedEllpack.hpp index 444360d66d..83150c7668 100644 --- a/src/TNL/Containers/Segments/ChunkedEllpack.hpp +++ b/src/TNL/Containers/Segments/ChunkedEllpack.hpp @@ -294,7 +294,8 @@ setSegmentsSizes( const SizesHolder& segmentsSizes ) else { ChunkedEllpack< Devices::Host, Index, typename Allocators::Default< Devices::Host >::template Allocator< Index >, RowMajorOrder > hostSegments; - Containers::Vector< IndexType, Devices::Host, IndexType > hostSegmentsSizes( segmentsSizes ); + Containers::Vector< IndexType, Devices::Host, IndexType > hostSegmentsSizes; + hostSegmentsSizes = segmentsSizes; hostSegments.setSegmentsSizes( hostSegmentsSizes ); *this = hostSegments; } diff --git a/src/TNL/File.hpp b/src/TNL/File.hpp index af112e992a..05e9d9ad70 100644 --- a/src/TNL/File.hpp +++ b/src/TNL/File.hpp @@ -176,7 +176,7 @@ template< typename Type, typename Allocator > void File::save( const Type* buffer, std::streamsize elements ) { - static_assert( std::is_same< Type, typename Allocator::value_type >::value, + static_assert( std::is_same< std::remove_cv_t< Type >, typename Allocator::value_type >::value, "Allocator::value_type must be the same as Type." ); TNL_ASSERT_GE( elements, 0, "Number of elements to save must be non-negative." ); diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 40fc1302be..8b22930e90 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -87,23 +87,17 @@ class DenseMatrix : public Matrix< Real, Device, Index > using RowView = DenseMatrixRowView< SegmentViewType, ValuesViewType >; /** - * \brief Helper type for getting self type or its variations. + * \brief Helper type for getting self type or its modifications. */ template< typename _Real = Real, typename _Device = Device, typename _Index = Index, - bool RowMajorOrder_ = RowMajorOrder, - typename RealAllocator_ = RealAllocator > - using Self = DenseMatrix< _Real, _Device, _Index, RowMajorOrder_, RealAllocator_ >; - - // TODO: remove this - using CompressedRowLengthsVector = typename Matrix< Real, Device, Index >::CompressedRowLengthsVector; - using ConstCompressedRowLengthsVectorView = typename Matrix< Real, Device, Index >::ConstCompressedRowLengthsVectorView; - - + bool _RowMajorOrder = RowMajorOrder, + typename _RealAllocator = RealAllocator > + using Self = DenseMatrix< _Real, _Device, _Index, _RowMajorOrder, _RealAllocator >; /** - * \brief Constrictor without parameters. + * \brief Constructor without parameters. */ DenseMatrix(); @@ -228,31 +222,119 @@ class DenseMatrix : public Matrix< Real, Device, Index > template< typename RowLengthsVector > void getCompressedRowLengths( RowLengthsVector& rowLengths ) const; - IndexType getMaxRowLength() const; - - IndexType getNumberOfMatrixElements() const; + /** + * \brief Returns number of all matrix elements. + * + * This method is here mainly for compatibility with sparse matrices since + * the number of all matrix elements is just number of rows times number of + * columns. + * + * \return number of all matrix elements. + * + * \par Example + * \include Matrices/DenseMatrixExample_getElementsCount.cpp + * \par Output + * \include DenseMatrixExample_getElementsCount.out + */ + IndexType getElementsCount() const; - IndexType getNumberOfNonzeroMatrixElements() const; + /** + * \brief Returns number of non-zero matrix elements. + * + * \return number of all non-zero matrix elements. + * + * \par Example + * \include Matrices/DenseMatrixExample_getElementsCount.cpp + * \par Output + * \include DenseMatrixExample_getElementsCount.out + */ + IndexType getNonzeroElementsCount() const; + /** + * \brief Resets the matrix to zero dimensions. + */ void reset(); + /** + * \brief Constant getter of simple structure for accessing given matrix row. + * + * \param rowIdx is matrix row index. + * + * \return RowView for accessing given matrix row. + * + * \par Example + * \include Matrices/DenseMatrixExample_getConstRow.cpp + * \par Output + * \include DenseMatrixExample_getConstRow.out + */ __cuda_callable__ const RowView getRow( const IndexType& rowIdx ) const; + /** + * \brief Non-constant getter of simple structure for accessing given matrix row. + * + * \param rowIdx is matrix row index. + * + * \return RowView for accessing given matrix row. + * + * \par Example + * \include Matrices/DenseMatrixExample_getRow.cpp + * \par Output + * \include DenseMatrixExample_getRow.out + */ __cuda_callable__ RowView getRow( const IndexType& rowIdx ); - + /** + * \brief Sets all matrix elements to value \e v. + * + * \param v is value all matrix elements will be set to. + */ void setValue( const RealType& v ); + /** + * \brief Returns non-constant reference to element at row \e 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. + * + * \param row is a row index of the element. + * \param column is a columns index of the element. + * \return reference to given matrix element. + */ __cuda_callable__ Real& operator()( const IndexType row, const IndexType column ); + /** + * \brief Returns constant reference to element at row \e 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. + * + * \param row is a row index of the element. + * \param column is a columns index of the element. + * \return reference to given matrix element. + */ __cuda_callable__ const Real& operator()( const IndexType row, const IndexType column ) const; + /** + * \brief Sets element at given \e row and \e column to given \e value. + * + * This method can be called only from the host system (CPU) no matter + * where the matrix is allocated. If the matrix is allocated in GPU device + * this methods transfer values of each matrix element separately and so the + * performance is very low. For higher performance see. \ref DenseMatrix::getRow + * or \ref DenseMatrix::forRows and \ref DenseMatrix::forAllRows. + * + * \param row is row index of the element. + * \param column is columns index of the element. + * \param value is the value the element will be set to. + */ void setElement( const IndexType row, const IndexType column, const RealType& value ); diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index 64a14afc8d..4d8166f640 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -202,17 +202,7 @@ template< typename Real, typename Index, bool RowMajorOrder, typename RealAllocator > -Index DenseMatrix< Real, Device, Index, RowMajorOrder, RealAllocator >::getMaxRowLength() const -{ - return this->getColumns(); -} - -template< typename Real, - typename Device, - typename Index, - bool RowMajorOrder, - typename RealAllocator > -Index DenseMatrix< Real, Device, Index, RowMajorOrder, RealAllocator >::getNumberOfMatrixElements() const +Index DenseMatrix< Real, Device, Index, RowMajorOrder, RealAllocator >::getElementsCount() const { return this->getRows() * this->getColumns(); } @@ -222,9 +212,9 @@ template< typename Real, typename Index, bool RowMajorOrder, typename RealAllocator > -Index DenseMatrix< Real, Device, Index, RowMajorOrder, RealAllocator >::getNumberOfNonzeroMatrixElements() const +Index DenseMatrix< Real, Device, Index, RowMajorOrder, RealAllocator >::getNonzeroElementsCount() const { - return this->view.getNumberOfNonzeroMatrixElements(); + return this->view.getNonzeroElementsCount(); } template< typename Real, diff --git a/src/TNL/Matrices/DenseMatrixRowView.h b/src/TNL/Matrices/DenseMatrixRowView.h index 84c6b141cd..01fdd9408d 100644 --- a/src/TNL/Matrices/DenseMatrixRowView.h +++ b/src/TNL/Matrices/DenseMatrixRowView.h @@ -32,10 +32,10 @@ class DenseMatrixRowView IndexType getSize() const; __cuda_callable__ - const RealType& getValue( const IndexType column ) const; + const RealType& getElement( const IndexType column ) const; __cuda_callable__ - RealType& getValue( const IndexType column ); + RealType& getElement( const IndexType column ); __cuda_callable__ void setElement( const IndexType column, diff --git a/src/TNL/Matrices/DenseMatrixRowView.hpp b/src/TNL/Matrices/DenseMatrixRowView.hpp index 1962a4d9a8..9ca725396d 100644 --- a/src/TNL/Matrices/DenseMatrixRowView.hpp +++ b/src/TNL/Matrices/DenseMatrixRowView.hpp @@ -38,7 +38,7 @@ template< typename SegmentView, typename ValuesView > __cuda_callable__ auto DenseMatrixRowView< SegmentView, ValuesView >:: -getValue( const IndexType column ) const -> const RealType& +getElement( const IndexType column ) const -> const RealType& { TNL_ASSERT_LT( column, this->getSize(), "Column index exceeds matrix row size." ); return values[ segmentView.getGlobalIndex( column ) ]; @@ -48,7 +48,7 @@ template< typename SegmentView, typename ValuesView > __cuda_callable__ auto DenseMatrixRowView< SegmentView, ValuesView >:: -getValue( const IndexType column ) -> RealType& +getElement( const IndexType column ) -> RealType& { TNL_ASSERT_LT( column, this->getSize(), "Column index exceeds matrix row size." ); return values[ segmentView.getGlobalIndex( column ) ]; diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index a7e1a09a78..f0d0b388e5 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -87,9 +87,9 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > IndexType getMaxRowLength() const; - IndexType getNumberOfMatrixElements() const; + IndexType getElementsCount() const; - IndexType getNumberOfNonzeroMatrixElements() const; + IndexType getNonzeroElementsCount() const; void reset(); diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index ec2e3f8925..bf2ebd4f27 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -144,7 +144,7 @@ template< typename Real, typename Device, typename Index, bool RowMajorOrder > -Index DenseMatrixView< Real, Device, Index, RowMajorOrder >::getNumberOfMatrixElements() const +Index DenseMatrixView< Real, Device, Index, RowMajorOrder >::getElementsCount() const { return this->getRows() * this->getColumns(); } @@ -153,7 +153,7 @@ template< typename Real, typename Device, typename Index, bool RowMajorOrder > -Index DenseMatrixView< Real, Device, Index, RowMajorOrder >::getNumberOfNonzeroMatrixElements() const +Index DenseMatrixView< Real, Device, Index, RowMajorOrder >::getNonzeroElementsCount() const { const auto values_view = this->values.getConstView(); auto fetch = [=] __cuda_callable__ ( const IndexType i ) -> IndexType { @@ -189,7 +189,7 @@ DenseMatrixView< Real, Device, Index, RowMajorOrder >:: getRow( const IndexType& rowIdx ) const -> const RowView { TNL_ASSERT_LT( rowIdx, this->getRows(), "Row index is larger than number of matrix rows." ); - return RowView( this->segments.getSegmentView( rowIdx ), this->values.getView() ); + return RowView( this->segments.getSegmentView( rowIdx ), this->values.getConstView() ); } template< typename Real, @@ -685,8 +685,8 @@ void DenseMatrixView< Real, Device, Index, RowMajorOrder >::print( std::ostream& for( IndexType column = 0; column < this->getColumns(); column++ ) { std::stringstream str_; - str_ << column << ":" << this->getElement( row, column ); - str << std::setw( 6 ) << str_.str(); + str_ << std::setw( 4 ) << std::right << column << ":" << std::setw( 4 ) << std::left << this->getElement( row, column ); + str << std::setw( 10 ) << str_.str(); } str << std::endl; } diff --git a/src/TNL/Matrices/MatrixView.h b/src/TNL/Matrices/MatrixView.h index 8955101818..76a3948a98 100644 --- a/src/TNL/Matrices/MatrixView.h +++ b/src/TNL/Matrices/MatrixView.h @@ -83,8 +83,6 @@ public: virtual void save( File& file ) const; - virtual void load( File& file ); - virtual void print( std::ostream& str ) const; diff --git a/src/TNL/Matrices/MatrixView.hpp b/src/TNL/Matrices/MatrixView.hpp index dfac8f3afc..b2b181e4c4 100644 --- a/src/TNL/Matrices/MatrixView.hpp +++ b/src/TNL/Matrices/MatrixView.hpp @@ -155,17 +155,6 @@ void MatrixView< Real, Device, Index >::save( File& file ) const file << this->values; } -template< typename Real, - typename Device, - typename Index > -void MatrixView< Real, Device, Index >::load( File& file ) -{ - Object::load( file ); - file.load( &this->rows ); - file.load( &this->columns ); - file >> this->values; -} - template< typename Real, typename Device, typename Index > diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 046b0a6ae1..0348a3945b 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -115,7 +115,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > // TODO: Remove this when possible void setCompressedRowLengths( ConstCompressedRowLengthsVectorView rowLengths ) { - this->setCompressedRowLengths( rowLengths ); + this->setRowCapacities( rowLengths ); }; void setElements( const std::initializer_list< std::tuple< IndexType, IndexType, RealType > >& data ); diff --git a/src/UnitTests/Matrices/DenseMatrixTest.h b/src/UnitTests/Matrices/DenseMatrixTest.h index 81e2ae6823..391043f0f3 100644 --- a/src/UnitTests/Matrices/DenseMatrixTest.h +++ b/src/UnitTests/Matrices/DenseMatrixTest.h @@ -163,7 +163,7 @@ void test_GetCompressedRowLengths() } template< typename Matrix > -void test_GetNumberOfMatrixElements() +void test_GetElementsCount() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -176,11 +176,11 @@ void test_GetNumberOfMatrixElements() m.reset(); m.setDimensions( rows, cols ); - EXPECT_EQ( m.getNumberOfMatrixElements(), 42 ); + EXPECT_EQ( m.getElementsCount(), 42 ); } template< typename Matrix > -void test_GetNumberOfNonzeroMatrixElements() +void test_GetNonzeroElementsCount() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -212,7 +212,7 @@ void test_GetNumberOfNonzeroMatrixElements() m.setElement( 0, 0, 0); // Set the first element of the diagonal to 0. m.setElement( 6, 5, 0); // Set the last element of the diagonal to 0. - EXPECT_EQ( m.getNumberOfNonzeroMatrixElements(), 40 ); + EXPECT_EQ( m.getNonzeroElementsCount(), 40 ); } template< typename Matrix > @@ -706,7 +706,7 @@ void test_AddRow() auto row = matrix_view.getRow( rowIdx ); for( IndexType i = 0; i < 5; i++ ) { - RealType& val = row.getValue( i ); + RealType& val = row.getElement( i ); val = rowIdx * val + values[ rowIdx ][ i ]; } }; @@ -1281,53 +1281,6 @@ void test_SaveAndLoad() EXPECT_EQ( savedMatrix.getElement( 3, 3 ), 16 ); } -template< typename Matrix > -void test_Print() -{ - using RealType = typename Matrix::RealType; - using DeviceType = typename Matrix::DeviceType; - using IndexType = typename Matrix::IndexType; -/* - * Sets up the following 5x4 sparse matrix: - * - * / 1 2 3 4 \ - * | 5 6 7 8 | - * | 9 10 11 12 | - * | 13 14 15 16 | - * \ 17 18 19 20 / - */ - const IndexType rows = 5; - const IndexType cols = 4; - - Matrix m; - m.reset(); - m.setDimensions( rows, cols ); - - RealType value = 1; - for( IndexType i = 0; i < rows; i++) - for( IndexType j = 0; j < cols; j++) - m.setElement( i, j, value++ ); - - #include <sstream> - std::stringstream printed; - std::stringstream couted; - - //change the underlying buffer and save the old buffer - auto old_buf = std::cout.rdbuf(printed.rdbuf()); - - m.print( std::cout ); //all the std::cout goes to ss - - std::cout.rdbuf(old_buf); //reset - - couted << "Row: 0 -> Col:0->1 Col:1->2 Col:2->3 Col:3->4\t\n" - "Row: 1 -> Col:0->5 Col:1->6 Col:2->7 Col:3->8\t\n" - "Row: 2 -> Col:0->9 Col:1->10 Col:2->11 Col:3->12\t\n" - "Row: 3 -> Col:0->13 Col:1->14 Col:2->15 Col:3->16\t\n" - "Row: 4 -> Col:0->17 Col:1->18 Col:2->19 Col:3->20\t\n"; - - EXPECT_EQ( printed.str(), couted.str() ); -} - // test fixture for typed tests template< typename Matrix > class MatrixTest : public ::testing::Test @@ -1395,18 +1348,18 @@ TYPED_TEST( MatrixTest, setElementsTest ) test_SetElements< MatrixType >(); } -TYPED_TEST( MatrixTest, getNumberOfMatrixElementsTest ) +TYPED_TEST( MatrixTest, getElementsCountTest ) { using MatrixType = typename TestFixture::MatrixType; - test_GetNumberOfMatrixElements< MatrixType >(); + test_GetElementsCount< MatrixType >(); } -TYPED_TEST( MatrixTest, getNumberOfNonzeroMatrixElementsTest ) +TYPED_TEST( MatrixTest, getNonzeroElementsCountTest ) { using MatrixType = typename TestFixture::MatrixType; - test_GetNumberOfNonzeroMatrixElements< MatrixType >(); + test_GetNonzeroElementsCount< MatrixType >(); } TYPED_TEST( MatrixTest, resetTest ) @@ -1479,13 +1432,6 @@ TYPED_TEST( MatrixTest, saveAndLoadTest ) test_SaveAndLoad< MatrixType >(); } -TYPED_TEST( MatrixTest, printTest ) -{ - using MatrixType = typename TestFixture::MatrixType; - - test_Print< MatrixType >(); -} - //// test_getType is not general enough yet. DO NOT TEST IT YET. //TEST( DenseMatrixTest, Dense_GetTypeTest_Host ) -- GitLab