From ee5a506f8f70a46043aee996b91cc6f9c3d0d459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sun, 10 May 2020 17:18:26 +0200 Subject: [PATCH 01/38] Writing documentation on DenseMatrixView. --- .../Examples/Matrices/CMakeLists.txt | 23 +++++++ .../DenseMatrixExample_getElementsCount.cpp | 2 +- ...seMatrixExample_getNonzeroElementsCount.cu | 1 + .../DenseMatrixViewExample_constructor.cpp | 20 +++--- ...rixViewExample_getCompressedRowLengths.cpp | 35 +++++++++++ ...trixViewExample_getCompressedRowLengths.cu | 1 + ...enseMatrixViewExample_getElementsCount.cpp | 31 +++++++++ ...DenseMatrixViewExample_getElementsCount.cu | 1 + .../DenseMatrixViewExample_getRow.cpp | 24 ++++--- .../Matrices/DenseMatrixViewExample_getRow.cu | 1 + .../Segments/ElementsOrganization.h | 10 ++- src/TNL/Matrices/DenseMatrix.h | 2 +- src/TNL/Matrices/DenseMatrix.hpp | 2 +- src/TNL/Matrices/DenseMatrixView.h | 63 +++++++++++++++++-- src/TNL/Matrices/DenseMatrixView.hpp | 30 ++------- src/UnitTests/Matrices/DenseMatrixTest.h | 26 ++++---- 16 files changed, 208 insertions(+), 64 deletions(-) create mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_getNonzeroElementsCount.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cpp create mode 120000 Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cpp create mode 120000 Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cu create mode 120000 Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cu diff --git a/Documentation/Examples/Matrices/CMakeLists.txt b/Documentation/Examples/Matrices/CMakeLists.txt index 710524aaa..25b7ef3c1 100644 --- a/Documentation/Examples/Matrices/CMakeLists.txt +++ b/Documentation/Examples/Matrices/CMakeLists.txt @@ -69,6 +69,17 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_constructor.out OUTPUT DenseMatrixViewExample_constructor.out ) + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_getCompressedRowLengths_cuda DenseMatrixViewExample_getCompressedRowLengths.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getCompressedRowLengths_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getCompressedRowLengths.out + OUTPUT DenseMatrixViewExample_getCompressedRowLengths.out ) + + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_getElementsCount_cuda DenseMatrixViewExample_getElementsCount.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getElementsCount_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getElementsCount.out + OUTPUT DenseMatrixViewExample_getElementsCount.out ) + + ELSE() ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list DenseMatrixExample_Constructor_init_list.cpp ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_Constructor_init_list > @@ -140,6 +151,16 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_constructor.out OUTPUT DenseMatrixViewExample_constructor.out ) + ADD_EXECUTABLE( DenseMatrixViewExample_getCompressedRowLengths DenseMatrixViewExample_getCompressedRowLengths.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getCompressedRowLengths > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getCompressedRowLengths.out + OUTPUT DenseMatrixViewExample_getCompressedRowLengths.out ) + + ADD_EXECUTABLE( DenseMatrixViewExample_getElementsCount DenseMatrixViewExample_getElementsCount.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getElementsCount > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getElementsCount.out + OUTPUT DenseMatrixViewExample_getElementsCount.out ) + ENDIF() ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS @@ -157,5 +178,7 @@ ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS DenseMatrixExample_forRows.out DenseMatrixExample_forAllRows.out DenseMatrixViewExample_constructor.out + DenseMatrixViewExample_getCompressedRowLengths.out + DenseMatrixViewExample_getElementsCount.out ) diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp index a95fa00e7..0a4a7bb7b 100644 --- a/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp +++ b/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp @@ -14,7 +14,7 @@ void getElementsCountExample() { 11, 12, 13, 14, 15 } }; - std::cout << "Matrix elements count is " << triangularMatrix.getElementsCount() << "." << std::endl; + std::cout << "Matrix elements count is " << triangularMatrix.getAllocatedElementsCount() << "." << std::endl; std::cout << "Non-zero matrix elements count is " << triangularMatrix.getNonzeroElementsCount() << "." << std::endl; } diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getNonzeroElementsCount.cu b/Documentation/Examples/Matrices/DenseMatrixExample_getNonzeroElementsCount.cu new file mode 120000 index 000000000..045fa3c1b --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixExample_getNonzeroElementsCount.cu @@ -0,0 +1 @@ +DenseMatrixExample_getNonzeroElementsCount.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_constructor.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_constructor.cpp index e8c036fa4..d180caa51 100644 --- a/Documentation/Examples/Matrices/DenseMatrixViewExample_constructor.cpp +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_constructor.cpp @@ -12,19 +12,19 @@ void createMatrixView() 5, 6, 7, 8, 9, 10, 11, 12 }; - TNL::Matrices::DenseMatrixView< double, Device, int, TNL::Containers::Segments::RowMajorOrder > matrix( 5, 5, values.getView() ); - /*** - * We need a matrix view to pass the matrix to lambda function even on CUDA device. + * Create dense matrix view with row major order */ - /*auto matrixView = matrix.getView(); - auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { - auto row = matrixView.getRow( rowIdx ); - row.setElement( rowIdx, 10* ( rowIdx + 1 ) ); - }; + TNL::Matrices::DenseMatrixView< double, Device, int, TNL::Containers::Segments::RowMajorOrder > rowMajorMatrix( 3, 4, values.getView() ); + std::cout << "Row major order matrix:" << std::endl; + std::cout << rowMajorMatrix << std::endl; - TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix.getRows(), f ); - std::cout << matrix << std::endl;*/ + /*** + * Create dense matrix view with column major order + */ + TNL::Matrices::DenseMatrixView< double, Device, int, TNL::Containers::Segments::RowMajorOrder > columnMajorMatrix( 4, 3, values.getView() ); + std::cout << "Column major order matrix:" << std::endl; + std::cout << columnMajorMatrix << std::endl; } int main( int argc, char* argv[] ) diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cpp new file mode 100644 index 000000000..4572f41a3 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +template< typename Device > +void getCompressedRowLengthsExample() +{ + TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { + { 1 }, + { 2, 3 }, + { 4, 5, 6 }, + { 7, 8, 9, 10 }, + { 11, 12, 13, 14, 15 } + }; + auto triangularMatrixView = triangularMatrix.getConstView(); + + std::cout << triangularMatrixView << std::endl; + + TNL::Containers::Vector< int, Device > rowLengths; + triangularMatrixView.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 HAVE_CUDA + std::cout << "Getting compressed row lengths on CUDA device: " << std::endl; + getCompressedRowLengthsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cu new file mode 120000 index 000000000..d9f9713a8 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_getCompressedRowLengths.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cpp new file mode 100644 index 000000000..04566eb27 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +template< typename Device > +void getElementsCountExample() +{ + TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { + { 1 }, + { 2, 3 }, + { 4, 5, 6 }, + { 7, 8, 9, 10 }, + { 11, 12, 13, 14, 15 } + }; + auto triangularMatrixView = triangularMatrix.getConstView(); + + std::cout << "Matrix elements count is " << triangularMatrixView.getAllocatedElementsCount() << "." << std::endl; + std::cout << "Non-zero matrix elements count is " << triangularMatrixView.getNonzeroElementsCount() << "." << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Computing matrix elements on host: " << std::endl; + getElementsCountExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Computing matrix elements on CUDA device: " << std::endl; + getElementsCountExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cu b/Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cu new file mode 120000 index 000000000..b5d2bcebd --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_getElementsCount.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cpp index 30d893bc1..00a6b1119 100644 --- a/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cpp +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cpp @@ -3,22 +3,30 @@ #include #include #include +#include template< typename Device > void getRowExample() { - TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + using MatrixType = TNL::Matrices::DenseMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > 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 ); + auto row = matrix->getRow( rowIdx ); row.setElement( rowIdx, 10* ( rowIdx + 1 ) ); }; - TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix.getRows(), f ); + /*** + * 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 >::exec( 0, matrix->getRows(), f ); std::cout << matrix << std::endl; } @@ -31,4 +39,4 @@ int main( int argc, char* argv[] ) std::cout << "Getting matrix rows on CUDA device: " << std::endl; getRowExample< TNL::Devices::Cuda >(); #endif -} \ No newline at end of file +} diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cu b/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cu new file mode 120000 index 000000000..5af514505 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_getRow.cpp \ No newline at end of file diff --git a/src/TNL/Containers/Segments/ElementsOrganization.h b/src/TNL/Containers/Segments/ElementsOrganization.h index 3c4086cd2..d930f57f9 100644 --- a/src/TNL/Containers/Segments/ElementsOrganization.h +++ b/src/TNL/Containers/Segments/ElementsOrganization.h @@ -26,7 +26,15 @@ struct DefaultElementsOrganization return ColumnMajorOrder; }; }; - } // namespace Segments } // namespace Containers + +String getSerializationType( Containers::Segments::ElementsOrganization Organization ) +{ + if( Organization == Containers::Segments::RowMajorOrder ) + return String( "RowMajorOrder" ); + else + return String( "ColumnMajorOrder" ); +} + } // namespace TNL diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 392bdc617..71637f984 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -244,7 +244,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \par Output * \include DenseMatrixExample_getElementsCount.out */ - IndexType getElementsCount() const; + IndexType getAllocatedElementsCount() const; /** * \brief Returns number of non-zero matrix elements. diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index 2b2fcc996..cbe283b2a 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -206,7 +206,7 @@ template< typename Real, typename RealAllocator > Index DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -getElementsCount() const +getAllocatedElementsCount() const { return this->getRows() * this->getColumns(); } diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index e09253cb2..f9556d21f 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -119,6 +119,12 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param rows number of matrix rows. * \param columns number of matrix columns. * \param values is vector view with matrix elements values. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_constructor.cpp + * \par Output + * \include DenseMatrixViewExample_constructor.out + */ __cuda_callable__ DenseMatrixView( const IndexType rows, @@ -149,20 +155,69 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > __cuda_callable__ ConstViewType getConstView() const; + /** + * \brief Returns string with serialization type. + * + * The string has a form \e `Matrices::DenseMatrix< RealType, [any_device], IndexType, [any_allocator], true/false >`. + * + * \return \e String with the serialization type. + */ static String getSerializationType(); + /** + * \brief Returns string with serialization type. + * + * See \ref DenseMatrixView::getSerializationType. + * + * \return \e String with the serialization type. + */ virtual String getSerializationTypeVirtual() const; + /** + * \brief Computes number of non-zeros in each row. + * + * \param rowLengths is a vector into which the number of non-zeros in each row + * will be stored. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_getCompressedRowLengths.cpp + * \par Output + * \include DenseMatrixViewExample_getCompressedRowLengths.out + */ template< typename Vector > void getCompressedRowLengths( Vector& rowLengths ) const; - [[deprecated]] - IndexType getRowLength( const IndexType row ) const; + //[[deprecated]] + //IndexType getRowLength( const IndexType row ) const; - IndexType getMaxRowLength() const; + //IndexType getMaxRowLength() const; - IndexType getElementsCount() 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 getAllocatedElementsCount() const; + /** + * \brief Returns number of non-zero matrix elements. + * + * \return number of all non-zero matrix elements. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_getElementsCount.cpp + * \par Output + * \include DenseMatrixViewExample_getElementsCount.out + */ IndexType getNonzeroElementsCount() const; __cuda_callable__ diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index 2ba34e549..b593be656 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -82,9 +82,9 @@ DenseMatrixView< Real, Device, Index, Organization >:: getSerializationType() { return String( "Matrices::DenseMatrix< " ) + - TNL::getSerializationType< RealType >() + ", [any_device], " + - TNL::getSerializationType< IndexType >() + ", " + - ( Organization ? "true" : "false" ) + ", [any_allocator] >"; + TNL::getSerializationType< RealType >() + ", [any_device], " + + TNL::getSerializationType< IndexType >() + ", " + + TNL::getSerializationType( Organization ) + " >"; } template< typename Real, @@ -125,29 +125,7 @@ template< typename Real, ElementsOrganization Organization > Index DenseMatrixView< Real, Device, Index, Organization >:: -getRowLength( const IndexType row ) const -{ - return this->getColumns(); -} - -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization > -Index -DenseMatrixView< Real, Device, Index, Organization >:: -getMaxRowLength() const -{ - return this->getColumns(); -} - -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization > -Index -DenseMatrixView< Real, Device, Index, Organization >:: -getElementsCount() const +getAllocatedElementsCount() const { return this->getRows() * this->getColumns(); } diff --git a/src/UnitTests/Matrices/DenseMatrixTest.h b/src/UnitTests/Matrices/DenseMatrixTest.h index ceed58546..627bcdf0f 100644 --- a/src/UnitTests/Matrices/DenseMatrixTest.h +++ b/src/UnitTests/Matrices/DenseMatrixTest.h @@ -36,14 +36,16 @@ static const char* TEST_FILE_NAME = "test_DenseMatrixTest.tnl"; void test_GetSerializationType() { using namespace TNL::Containers::Segments; - EXPECT_EQ( ( TNL::Matrices::DenseMatrix< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< float, [any_device], int, true, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::DenseMatrix< int, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< int, [any_device], int, true, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::DenseMatrix< float, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< float, [any_device], int, true, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::DenseMatrix< int, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< int, [any_device], int, true, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::DenseMatrix< float, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< float, [any_device], int, false, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::DenseMatrix< int, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< int, [any_device], int, false, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::DenseMatrix< float, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< float, [any_device], int, false, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::DenseMatrix< int, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< int, [any_device], int, false, [any_allocator] >" ) ); + std::cerr << TNL::Matrices::DenseMatrix< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() << std::endl; + std::cerr << TNL::Matrices::DenseMatrix< float, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() << std::endl; + EXPECT_EQ( ( TNL::Matrices::DenseMatrix< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< float, [any_device], int, RowMajorOrder >" ) ); + EXPECT_EQ( ( TNL::Matrices::DenseMatrix< int, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< int, [any_device], int, RowMajorOrder >" ) ); + EXPECT_EQ( ( TNL::Matrices::DenseMatrix< float, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< float, [any_device], int, RowMajorOrder >" ) ); + EXPECT_EQ( ( TNL::Matrices::DenseMatrix< int, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< int, [any_device], int, RowMajorOrder >" ) ); + EXPECT_EQ( ( TNL::Matrices::DenseMatrix< float, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< float, [any_device], int, ColumnMajorOrder >" ) ); + EXPECT_EQ( ( TNL::Matrices::DenseMatrix< int, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< int, [any_device], int, ColumnMajorOrder >" ) ); + EXPECT_EQ( ( TNL::Matrices::DenseMatrix< float, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< float, [any_device], int, ColumnMajorOrder >" ) ); + EXPECT_EQ( ( TNL::Matrices::DenseMatrix< int, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< int, [any_device], int, ColumnMajorOrder >" ) ); } template< typename Matrix > @@ -166,7 +168,7 @@ void test_GetCompressedRowLengths() } template< typename Matrix > -void test_GetElementsCount() +void test_GetAllocatedElementsCount() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -179,7 +181,7 @@ void test_GetElementsCount() m.reset(); m.setDimensions( rows, cols ); - EXPECT_EQ( m.getElementsCount(), 42 ); + EXPECT_EQ( m.getAllocatedElementsCount(), 42 ); } template< typename Matrix > @@ -1365,11 +1367,11 @@ TYPED_TEST( MatrixTest, setElementsTest ) test_SetElements< MatrixType >(); } -TYPED_TEST( MatrixTest, getElementsCountTest ) +TYPED_TEST( MatrixTest, getAllocatedElementsCountTest ) { using MatrixType = typename TestFixture::MatrixType; - test_GetElementsCount< MatrixType >(); + test_GetAllocatedElementsCount< MatrixType >(); } TYPED_TEST( MatrixTest, getNonzeroElementsCountTest ) -- GitLab From 4f169b0cd6fc571f9d103c21b2306ac87b9895b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sun, 10 May 2020 21:15:19 +0200 Subject: [PATCH 02/38] Finished DenseMatrix documentation. --- .../Matrices/{ => DenseMatrix}/CMakeLists.txt | 99 ++++++ ...nseMatrixExample_Constructor_init_list.cpp | 37 +++ ...enseMatrixExample_Constructor_init_list.cu | 37 +++ .../DenseMatrixExample_addElement.cpp | 30 ++ .../DenseMatrixExample_addElement.cu | 30 ++ .../DenseMatrixExample_allRowsReduction.cpp | 66 ++++ .../DenseMatrixExample_allRowsReduction.cu | 66 ++++ .../DenseMatrixExample_forAllRows.cpp | 31 ++ .../DenseMatrixExample_forAllRows.cu | 31 ++ .../DenseMatrixExample_forRows.cpp | 31 ++ .../DenseMatrix/DenseMatrixExample_forRows.cu | 31 ++ ...eMatrixExample_getCompressedRowLengths.cpp | 34 +++ ...seMatrixExample_getCompressedRowLengths.cu | 34 +++ .../DenseMatrixExample_getConstRow.cpp | 52 ++++ .../DenseMatrixExample_getConstRow.cu | 52 ++++ .../DenseMatrixExample_getElement.cpp | 34 +++ .../DenseMatrixExample_getElement.cu | 34 +++ .../DenseMatrixExample_getElementsCount.cpp | 30 ++ .../DenseMatrixExample_getElementsCount.cu | 30 ++ .../DenseMatrixExample_addElement.cpp | 4 +- .../Matrices/DenseMatrixExample_getRow.cpp | 2 +- .../DenseMatrixViewExample_addElement.cpp | 32 ++ .../DenseMatrixViewExample_addElement.cu | 1 + .../DenseMatrixViewExample_forAllRows.cpp | 32 ++ .../DenseMatrixViewExample_forAllRows.cu | 1 + .../DenseMatrixViewExample_forRows.cpp | 32 ++ .../DenseMatrixViewExample_forRows.cu | 1 + .../DenseMatrixViewExample_getConstRow.cu | 1 + .../DenseMatrixViewExample_getElement.cpp | 35 +++ .../DenseMatrixViewExample_getElement.cu | 1 + .../DenseMatrixViewExample_getRow.cpp | 23 +- .../DenseMatrixViewExample_rowsReduction.cpp | 66 ++++ .../DenseMatrixViewExample_rowsReduction.cu | 1 + .../DenseMatrixViewExample_setElement.cpp | 35 +++ .../DenseMatrixViewExample_setElement.cu | 1 + src/TNL/Matrices/DenseMatrix.h | 22 +- src/TNL/Matrices/DenseMatrixView.h | 282 +++++++++++++++++- 37 files changed, 1334 insertions(+), 27 deletions(-) rename Documentation/Examples/Matrices/{ => DenseMatrix}/CMakeLists.txt (64%) create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cpp create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cpp create mode 120000 Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cpp create mode 120000 Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cu create mode 120000 Documentation/Examples/Matrices/DenseMatrixViewExample_getConstRow.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cpp create mode 120000 Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cpp create mode 120000 Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cu diff --git a/Documentation/Examples/Matrices/CMakeLists.txt b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt similarity index 64% rename from Documentation/Examples/Matrices/CMakeLists.txt rename to Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt index 25b7ef3c1..a0db86c4d 100644 --- a/Documentation/Examples/Matrices/CMakeLists.txt +++ b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt @@ -79,6 +79,50 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getElementsCount.out OUTPUT DenseMatrixViewExample_getElementsCount.out ) + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_getConstRow_cuda DenseMatrixViewExample_getConstRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getConstRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getConstRow.out + OUTPUT DenseMatrixViewExample_getConstRow.out ) + + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_getRow_cuda DenseMatrixViewExample_getRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getRow.out + OUTPUT DenseMatrixViewExample_getRow.out ) + + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_setElement_cuda DenseMatrixViewExample_setElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_setElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_setElement.out + OUTPUT DenseMatrixViewExample_setElement.out ) + + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_addElement_cuda DenseMatrixViewExample_addElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_addElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_addElement.out + OUTPUT DenseMatrixViewExample_addElement.out ) + + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_getElement_cuda DenseMatrixViewExample_getElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getElement.out + OUTPUT DenseMatrixViewExample_getElement.out ) + + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_rowsReduction_cuda DenseMatrixViewExample_rowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_rowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_rowsReduction.out + OUTPUT DenseMatrixViewExample_rowsReduction.out ) + + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_allRowsReduction_cuda DenseMatrixViewExample_allRowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_allRowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_allRowsReduction.out + OUTPUT DenseMatrixViewExample_allRowsReduction.out ) + + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_forRows_cuda DenseMatrixViewExample_forRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forRows.out + OUTPUT DenseMatrixViewExample_forRows.out ) + + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_forAllRows_cuda DenseMatrixViewExample_forAllRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forAllRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forAllRows.out + OUTPUT DenseMatrixViewExample_forAllRows.out ) ELSE() ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list DenseMatrixExample_Constructor_init_list.cpp ) @@ -161,6 +205,51 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getElementsCount.out OUTPUT DenseMatrixViewExample_getElementsCount.out ) + ADD_EXECUTABLE( DenseMatrixViewExample_getConstRow DenseMatrixViewExample_getConstRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getConstRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getConstRow.out + OUTPUT DenseMatrixViewExample_getConstRow.out ) + + ADD_EXECUTABLE( DenseMatrixViewExample_getRow DenseMatrixViewExample_getRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getRow.out + OUTPUT DenseMatrixViewExample_getRow.out ) + + ADD_EXECUTABLE( DenseMatrixViewExample_setElement DenseMatrixViewExample_setElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_setElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_setElement.out + OUTPUT DenseMatrixViewExample_setElement.out ) + + ADD_EXECUTABLE( DenseMatrixViewExample_addElement DenseMatrixViewExample_addElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_addElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_addElement.out + OUTPUT DenseMatrixViewExample_addElement.out ) + + ADD_EXECUTABLE( DenseMatrixViewExample_getElement DenseMatrixViewExample_getElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getElement.out + OUTPUT DenseMatrixViewExample_getElement.out ) + + ADD_EXECUTABLE( DenseMatrixViewExample_rowsReduction DenseMatrixViewExample_rowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_rowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_rowsReduction.out + OUTPUT DenseMatrixViewExample_rowsReduction.out ) + + ADD_EXECUTABLE( DenseMatrixViewExample_allRowsReduction DenseMatrixViewExample_allRowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_allRowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_allRowsReduction.out + OUTPUT DenseMatrixViewExample_allRowsReduction.out ) + + ADD_EXECUTABLE( DenseMatrixViewExample_forRows DenseMatrixViewExample_forRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forRows.out + OUTPUT DenseMatrixViewExample_forRows.out ) + + ADD_EXECUTABLE( DenseMatrixViewExample_forAllRows DenseMatrixViewExample_forAllRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forAllRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forAllRows.out + OUTPUT DenseMatrixViewExample_forAllRows.out ) + ENDIF() ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS @@ -180,5 +269,15 @@ ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS DenseMatrixViewExample_constructor.out DenseMatrixViewExample_getCompressedRowLengths.out DenseMatrixViewExample_getElementsCount.out + DenseMatrixViewExample_getConstRow.out + DenseMatrixViewExample_getRow.out + DenseMatrixViewExample_setElement.out + DenseMatrixViewExample_addElement.out + DenseMatrixViewExample_getElement.out + DenseMatrixViewExample_rowsReduction.out + DenseMatrixViewExample_allRowsReduction.out + DenseMatrixViewExample_forRows.out + DenseMatrixViewExample_forAllRows.out + ) diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cpp new file mode 100644 index 000000000..91426a6f1 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + + +template< typename Device > +void initializerListExample() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix { + { 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; + + TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { + { 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 HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + initializerListExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu new file mode 100644 index 000000000..91426a6f1 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu @@ -0,0 +1,37 @@ +#include +#include +#include + + +template< typename Device > +void initializerListExample() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix { + { 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; + + TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { + { 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 HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + initializerListExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cpp new file mode 100644 index 000000000..4a36b007e --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +template< typename Device > +void addElements() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + 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 HAVE_CUDA + std::cout << "Add elements on CUDA device:" << std::endl; + addElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu new file mode 100644 index 000000000..4a36b007e --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu @@ -0,0 +1,30 @@ +#include +#include +#include + +template< typename Device > +void addElements() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + 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 HAVE_CUDA + std::cout << "Add elements on CUDA device:" << std::endl; + addElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp new file mode 100644 index 000000000..ce323671f --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void allRowsReduction() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix { + { 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.allRowsReduction( 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; + allRowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "All rows reduction on CUDA device:" << std::endl; + allRowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu new file mode 100644 index 000000000..ce323671f --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void allRowsReduction() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix { + { 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.allRowsReduction( 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; + allRowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "All rows reduction on CUDA device:" << std::endl; + allRowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp new file mode 100644 index 000000000..5fddf0f34 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +template< typename Device > +void forAllRowsExample() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + + auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) + compute = false; + else + value = rowIdx + columnIdx; + }; + + matrix.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forAllRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forAllRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu new file mode 100644 index 000000000..5fddf0f34 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +template< typename Device > +void forAllRowsExample() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + + auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) + compute = false; + else + value = rowIdx + columnIdx; + }; + + matrix.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forAllRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forAllRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp new file mode 100644 index 000000000..f3e45a006 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + + auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) + compute = false; + else + value = rowIdx + columnIdx; + }; + + matrix.forRows( 0, matrix.getRows(), f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu new file mode 100644 index 000000000..f3e45a006 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + + auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) + compute = false; + else + value = rowIdx + columnIdx; + }; + + matrix.forRows( 0, matrix.getRows(), f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cpp new file mode 100644 index 000000000..e89992d9f --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +template< typename Device > +void getCompressedRowLengthsExample() +{ + TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { + { 1 }, + { 2, 3 }, + { 4, 5, 6 }, + { 7, 8, 9, 10 }, + { 11, 12, 13, 14, 15 } + }; + + std::cout << triangularMatrix << std::endl; + + TNL::Containers::Vector< int, Device > rowLengths; + 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 HAVE_CUDA + std::cout << "Getting compressed row lengths on CUDA device: " << std::endl; + getCompressedRowLengthsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu new file mode 100644 index 000000000..e89992d9f --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +template< typename Device > +void getCompressedRowLengthsExample() +{ + TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { + { 1 }, + { 2, 3 }, + { 4, 5, 6 }, + { 7, 8, 9, 10 }, + { 11, 12, 13, 14, 15 } + }; + + std::cout << triangularMatrix << std::endl; + + TNL::Containers::Vector< int, Device > rowLengths; + 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 HAVE_CUDA + std::cout << "Getting compressed row lengths on CUDA device: " << std::endl; + getCompressedRowLengthsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cpp new file mode 100644 index 000000000..08b655e55 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + using MatrixType = TNL::Matrices::DenseMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > 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 } + }; + + /*** + * Fetch lambda function returns diagonal element in each row. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { + auto row = matrix->getRow( rowIdx ); + return row.getElement( 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::Reduction< Device >::reduce( matrix->getRows(), std::plus<>{}, fetch, 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 HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu new file mode 100644 index 000000000..08b655e55 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + using MatrixType = TNL::Matrices::DenseMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > 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 } + }; + + /*** + * Fetch lambda function returns diagonal element in each row. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { + auto row = matrix->getRow( rowIdx ); + return row.getElement( 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::Reduction< Device >::reduce( matrix->getRows(), std::plus<>{}, fetch, 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 HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cpp new file mode 100644 index 000000000..72a5d0af4 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +template< typename Device > +void getElements() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix { + { 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 ) << std::ios::right << matrix.getElement( i, i ); + std::cout << std::endl; + } +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get elements on host:" << std::endl; + getElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get elements on CUDA device:" << std::endl; + getElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu new file mode 100644 index 000000000..72a5d0af4 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +template< typename Device > +void getElements() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix { + { 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 ) << std::ios::right << matrix.getElement( i, i ); + std::cout << std::endl; + } +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get elements on host:" << std::endl; + getElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get elements on CUDA device:" << std::endl; + getElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cpp new file mode 100644 index 000000000..0a4a7bb7b --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +template< typename Device > +void getElementsCountExample() +{ + TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { + { 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 HAVE_CUDA + std::cout << "Computing matrix elements on CUDA device: " << std::endl; + getElementsCountExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu new file mode 100644 index 000000000..0a4a7bb7b --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +template< typename Device > +void getElementsCountExample() +{ + TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { + { 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 HAVE_CUDA + std::cout << "Computing matrix elements on CUDA device: " << std::endl; + getElementsCountExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_addElement.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_addElement.cpp index 32e39e6a3..4a36b007e 100644 --- a/Documentation/Examples/Matrices/DenseMatrixExample_addElement.cpp +++ b/Documentation/Examples/Matrices/DenseMatrixExample_addElement.cpp @@ -9,13 +9,13 @@ void addElements() for( int i = 0; i < 5; i++ ) matrix.setElement( i, i, i ); - std::cout << "Initial matrix is: " << matrix << std::endl; + 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: " << matrix << std::endl; + std::cout << "Matrix after addition is: " << std::endl << matrix << std::endl; } int main( int argc, char* argv[] ) diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_getRow.cpp index 00a6b1119..0cf1e1d76 100644 --- a/Documentation/Examples/Matrices/DenseMatrixExample_getRow.cpp +++ b/Documentation/Examples/Matrices/DenseMatrixExample_getRow.cpp @@ -13,7 +13,7 @@ void getRowExample() auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { auto row = matrix->getRow( rowIdx ); - row.setElement( rowIdx, 10* ( rowIdx + 1 ) ); + row.setElement( rowIdx, 10 * ( rowIdx + 1 ) ); }; /*** diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cpp new file mode 100644 index 000000000..bc72a6241 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +template< typename Device > +void addElements() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + auto matrixView = matrix.getView(); + + for( int i = 0; i < 5; i++ ) + matrixView.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++ ) + matrixView.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 HAVE_CUDA + std::cout << "Add elements on CUDA device:" << std::endl; + addElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cu b/Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cu new file mode 120000 index 000000000..9266b5e73 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cpp new file mode 100644 index 000000000..3c51e8ee5 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +template< typename Device > +void forAllRowsExample() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + auto matrixView = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) + compute = false; + else + value = rowIdx + columnIdx; + }; + + matrixView.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forAllRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forAllRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cu b/Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cu new file mode 120000 index 000000000..6b0114a09 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_forAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cpp new file mode 100644 index 000000000..810bf1118 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + auto matrixView = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) + compute = false; + else + value = rowIdx + columnIdx; + }; + + matrixView.forRows( 0, matrix.getRows(), f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cu b/Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cu new file mode 120000 index 000000000..8111505a3 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getConstRow.cu b/Documentation/Examples/Matrices/DenseMatrixViewExample_getConstRow.cu new file mode 120000 index 000000000..facd73fa1 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_getConstRow.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_getConstRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cpp new file mode 100644 index 000000000..6f4be000b --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +template< typename Device > +void getElements() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix { + { 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 } }; + auto matrixView = matrix.getConstView(); + + + for( int i = 0; i < 5; i++ ) + { + for( int j = 0; j < 5; j++ ) + std::cout << std::setw( 5 ) << std::ios::right << matrixView.getElement( i, i ); + std::cout << std::endl; + } +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get elements on host:" << std::endl; + getElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get elements on CUDA device:" << std::endl; + getElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cu b/Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cu new file mode 120000 index 000000000..e2a308ee5 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_getElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cpp index 00a6b1119..8142599c1 100644 --- a/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cpp +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cpp @@ -3,30 +3,27 @@ #include #include #include -#include template< typename Device > void getRowExample() { - using MatrixType = TNL::Matrices::DenseMatrix< double, Device >; - TNL::Pointers::SharedPointer< MatrixType > matrix( 5, 5 ); - - auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { - auto row = matrix->getRow( rowIdx ); - row.setElement( rowIdx, 10* ( rowIdx + 1 ) ); - }; + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); /*** - * 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. + * Create dense matrix view which can be captured by the following lambda + * function. */ - TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); + auto matrixView = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { + auto row = matrixView.getRow( rowIdx ); + row.setElement( rowIdx, 10 * ( rowIdx + 1 ) ); + }; /*** * Set the matrix elements. */ - TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix->getRows(), f ); + TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix.getRows(), f ); std::cout << matrix << std::endl; } diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cpp new file mode 100644 index 000000000..dbc44f854 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void rowsReduction() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix { + { 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. + */ + const auto matrixView = matrix.getConstView(); + 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.rowsReduction( 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; + rowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + rowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cu new file mode 120000 index 000000000..4170aaa62 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cpp b/Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cpp new file mode 100644 index 000000000..92985bc5a --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +template< typename Device > +void setElements() +{ + TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + auto matrixView = matrix.getView(); + for( int i = 0; i < 5; i++ ) + matrixView.setElement( i, i, i ); + + std::cout << "Matrix set from the host:" << std::endl; + std::cout << matrix << std::endl; + + auto f = [=] __cuda_callable__ ( int i ) mutable { + matrixView.setElement( i, i, -i ); + }; + TNL::Algorithms::ParallelFor< Device >::exec( 0, 5, 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 HAVE_CUDA + std::cout << "Set elements on CUDA device:" << std::endl; + setElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cu b/Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cu new file mode 120000 index 000000000..902de9225 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_setElement.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 71637f984..50e0bd9dc 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -349,7 +349,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \param value is the value the element will be set to. * * \par Example - * \include Matrices/DenseMatrixExample_setElement.cpp + * \include Matrices/DenseMatrixViewExample_setElement.cpp * \par Output * \include DenseMatrixExample_setElement.out */ @@ -373,6 +373,12 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \param value is the value the element will be set to. * \param thisElementMultiplicator is multiplicator the original matrix element * value is multiplied by before addition of given \e value. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_addElement.cpp + * \par Output + * \include DenseMatrixExample_addElement.out + * */ __cuda_callable__ void addElement( const IndexType row, @@ -394,6 +400,12 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \param column i a column index of the matrix element. * * \return value of given matrix element. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_getElement.cpp + * \par Output + * \include DenseMatrixExample_getElement.out + * */ __cuda_callable__ Real getElement( const IndexType row, @@ -514,7 +526,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > /** * \brief This method calls \e forRows for all matrix rows. * - * See \ref DenseMatrix::forRows. + * See \ref DenseMatrix::forAllRows. * * \tparam Function is a type of lambda function that will operate on matrix elements. * \param function is an instance of the lambda function to be called in each row. @@ -579,7 +591,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > const RealType& omega = 1.0 ) const; /** - * \brief Assignment operator for exactly the same type of the dense matrix. + * \brief Assignment operator with exactly the same type of the dense matrix. * * \param matrix is the right-hand side matrix. * \return reference to this matrix. @@ -587,7 +599,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > DenseMatrix& operator=( const DenseMatrix& matrix ); /** - * \brief Assignment operator for other dense matrices. + * \brief Assignment operator with other dense matrices. * * \param matrix is the right-hand side matrix. * \return reference to this matrix. @@ -597,7 +609,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > DenseMatrix& operator=( const DenseMatrix< RHSReal, RHSDevice, RHSIndex, RHSOrganization, RHSRealAllocator >& matrix ); /** - * \brief Assignment operator for other (sparse) types of matrices. + * \brief Assignment operator with other (sparse) types of matrices. * * \param matrix is the right-hand side matrix. * \return reference to this matrix. diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index f9556d21f..bca7a5515 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -202,9 +202,9 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \return number of all matrix elements. * * \par Example - * \include Matrices/DenseMatrixExample_getElementsCount.cpp + * \include Matrices/DenseViewMatrixExample_getElementsCount.cpp * \par Output - * \include DenseMatrixExample_getElementsCount.out + * \include DenseMatrixViewExample_getElementsCount.out */ IndexType getAllocatedElementsCount() const; @@ -220,56 +220,304 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > */ IndexType getNonzeroElementsCount() const; + /** + * \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/DenseMatrixViewExample_getConstRow.cpp + * \par Output + * \include DenseMatrixViewExample_getConstRow.out + * + * See \ref DenseMatrixRowView. + */ __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/DenseMatrixViewExample_getRow.cpp + * \par Output + * \include DenseMatrixExample_getRow.out + * + * See \ref DenseMatrixRowView. + */ __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 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. \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. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_setElement.cpp + * \par Output + * \include DenseMatrixExample_setElement.out + */ __cuda_callable__ void setElement( const IndexType row, const IndexType column, const RealType& value ); + /** + * \brief Add element at given \e row and \e column to given \e 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. \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. + * \param thisElementMultiplicator is multiplicator the original matrix element + * value is multiplied by before addition of given \e value. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_addElement.cpp + * \par Output + * \include DenseMatrixExample_addElement.out + * + */ __cuda_callable__ void addElement( const IndexType row, const IndexType column, const RealType& value, const RealType& thisElementMultiplicator = 1.0 ); + /** + * \brief 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. \ref DenseMatrix::getRow + * or \ref DenseMatrix::forRows and \ref DenseMatrix::forAllRows. + * + * \param row is a row index of the matrix element. + * \param column i a column index of the matrix element. + * + * \return value of given matrix element. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_getElement.cpp + * \par Output + * \include DenseMatrixExample_getElement.out + * + */ __cuda_callable__ Real getElement( const IndexType row, const IndexType column ) const; + /** + * \brief Method for performing general reduction on matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param first is an index of the first row the reduction will be performed on. + * \param last is an index of the row after the last row the reduction will be performed on. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_rowsReduction.cpp + * \par Output + * \include DenseMatrixViewExample_rowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for performing general reduction on ALL matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_allRowsReduction.cpp + * \par Output + * \include DenseMatrixViewExample_allRowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for iteration over all matrix rows for constant instances. + * + * \tparam Function is type of lambda function that will operate on matrix elements. + * It is should have form like + * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx, const RealType& value, bool& compute )`. + * The column index repeats twice only for compatibility with sparse matrices. + * If the 'compute' variable is set to false the iteration over the row can + * be interrupted. + * + * \param first is index is the first row to be processed. + * \param last is index of the row after the last row to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_forRows.cpp + * \par Output + * \include DenseMatrixViewExample_forRows.out + */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ) const; + /** + * \brief Method for iteration over all matrix rows for non-constant instances. + * + * \tparam Function is type of lambda function that will operate on matrix elements. + * It is should have form like + * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx, RealType& value, bool& compute )`. + * The column index repeats twice only for compatibility with sparse matrices. + * If the 'compute' variable is set to false the iteration over the row can + * be interrupted. + * + * \param first is index is the first row to be processed. + * \param last is index of the row after the last row to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_forRows.cpp + * \par Output + * \include DenseMatrixViewExample_forRows.out + */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ); + /** + * \brief This method calls \e forRows for all matrix rows. + * + * See \ref DenseMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/DenseMatrixViewExample_forAllRows.cpp + * \par Output + * \include DenseMatrixViewExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ) const; + /** + * \brief This method calls \e forRows for all matrix rows. + * + * See \ref DenseMatrix::forAllRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/DenseMatrixExample_forAllRows.cpp + * \par Output + * \include DenseMatrixExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ); + /** + * \brief Computes product of matrix and vector. + * + * More precisely, it computes: + * + * outVector = matrixMultiplicator * ( *this ) * inVector + outVectorMultiplicator * outVector. + * + * \tparam InVector is type of input vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * \tparam OutVector is type of output vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * + * \param inVector is input vector. + * \param outVector is output vector. + * \param matrixMultiplicator is a factor by which the matrix is multiplied. It is one by default. + * \param outVectorMultiplicator is a factor by which the outVector is multiplied before added + * to the result of matrix-vector product. It is zero by default. + * \param begin is the beginning of the rows range for which the vector product + * is computed. It is zero by default. + * \param end is the end of the rows range for which the vector product + * is computed. It is number if the matrix rows by default. + */ template< typename InVector, typename OutVector > void vectorProduct( const InVector& inVector, OutVector& outVector, @@ -299,12 +547,37 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > Vector2& x, const RealType& omega = 1.0 ) const; + /** + * \brief Assignment operator with DenseMatrix. + * + * \param matrix is the right-hand side matrix. + * \return reference to this matrix. + */ DenseMatrixView& operator=( const DenseMatrixView& matrix ); + /** + * \brief Method for saving the matrix view to the file with given filename. + * + * The ouput file can be loaded by \ref DenseMatrix. + * + * \param fileName is name of the file. + */ void save( const String& fileName ) const; + /** + * \brief Method for saving the matrix view to a file. + * + * The ouput file can be loaded by \ref DenseMatrix. + * + * \param fileName is name of the file. + */ void save( File& file ) const; + /** + * \brief Method for printing the matrix to output stream. + * + * \param str is the output stream. + */ void print( std::ostream& str ) const; protected: @@ -313,9 +586,6 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > IndexType getElementIndex( const IndexType row, const IndexType column ) const; - //typedef DenseDeviceDependentCode< DeviceType > DeviceDependentCode; - //friend class DenseDeviceDependentCode< DeviceType >; - SegmentsViewType segments; }; -- GitLab From 3abcbe4772226d74acbaf1350aa27a13ddd056f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sun, 10 May 2020 21:42:40 +0200 Subject: [PATCH 03/38] Moving DenseMatrix examples to separate folder. --- .../Examples/Matrices/CMakeLists.txt | 1 + ...enseMatrixExample_Constructor_init_list.cu | 38 +---------- .../DenseMatrixExample_addElement.cu | 31 +-------- .../DenseMatrixExample_allRowsReduction.cu | 67 +------------------ .../DenseMatrixExample_forAllRows.cu | 32 +-------- .../DenseMatrix/DenseMatrixExample_forRows.cu | 32 +-------- ...seMatrixExample_getCompressedRowLengths.cu | 35 +--------- .../DenseMatrixExample_getConstRow.cu | 53 +-------------- .../DenseMatrixExample_getElement.cu | 35 +--------- .../DenseMatrixExample_getElementsCount.cu | 31 +-------- ...seMatrixExample_getNonzeroElementsCount.cu | 0 .../DenseMatrixExample_getRow.cpp | 0 .../DenseMatrixExample_getRow.cu | 0 .../DenseMatrixExample_rowsReduction.cpp | 0 .../DenseMatrixExample_rowsReduction.cu | 0 .../DenseMatrixExample_setElement.cpp | 0 .../DenseMatrixExample_setElement.cu | 0 .../DenseMatrixExample_setElements.cpp | 0 .../DenseMatrixExample_setElements.cu | 0 .../DenseMatrixViewExample_addElement.cpp | 0 .../DenseMatrixViewExample_addElement.cu | 0 ...enseMatrixViewExample_allRowsReduction.cpp | 0 ...DenseMatrixViewExample_allRowsReduction.cu | 0 .../DenseMatrixViewExample_constructor.cpp | 0 .../DenseMatrixViewExample_constructor.cu | 0 .../DenseMatrixViewExample_forAllRows.cpp | 0 .../DenseMatrixViewExample_forAllRows.cu | 0 .../DenseMatrixViewExample_forRows.cpp | 0 .../DenseMatrixViewExample_forRows.cu | 0 ...rixViewExample_getCompressedRowLengths.cpp | 0 ...trixViewExample_getCompressedRowLengths.cu | 0 .../DenseMatrixViewExample_getConstRow.cpp | 0 .../DenseMatrixViewExample_getConstRow.cu | 0 .../DenseMatrixViewExample_getElement.cpp | 0 .../DenseMatrixViewExample_getElement.cu | 0 ...enseMatrixViewExample_getElementsCount.cpp | 0 ...DenseMatrixViewExample_getElementsCount.cu | 0 .../DenseMatrixViewExample_getRow.cpp | 0 .../DenseMatrixViewExample_getRow.cu | 0 .../DenseMatrixViewExample_rowsReduction.cpp | 0 .../DenseMatrixViewExample_rowsReduction.cu | 0 .../DenseMatrixViewExample_setElement.cpp | 0 .../DenseMatrixViewExample_setElement.cu | 0 ...nseMatrixExample_Constructor_init_list.cpp | 37 ---------- ...enseMatrixExample_Constructor_init_list.cu | 1 - .../DenseMatrixExample_addElement.cpp | 30 --------- .../Matrices/DenseMatrixExample_addElement.cu | 1 - .../DenseMatrixExample_allRowsReduction.cpp | 66 ------------------ .../DenseMatrixExample_allRowsReduction.cu | 1 - .../DenseMatrixExample_forAllRows.cpp | 31 --------- .../Matrices/DenseMatrixExample_forAllRows.cu | 1 - .../Matrices/DenseMatrixExample_forRows.cpp | 31 --------- .../Matrices/DenseMatrixExample_forRows.cu | 1 - ...eMatrixExample_getCompressedRowLengths.cpp | 34 ---------- ...seMatrixExample_getCompressedRowLengths.cu | 1 - .../DenseMatrixExample_getConstRow.cpp | 52 -------------- .../DenseMatrixExample_getConstRow.cu | 1 - .../DenseMatrixExample_getElement.cpp | 34 ---------- .../Matrices/DenseMatrixExample_getElement.cu | 1 - .../DenseMatrixExample_getElementsCount.cpp | 30 --------- .../DenseMatrixExample_getElementsCount.cu | 1 - src/TNL/Matrices/DenseMatrix.h | 32 ++++----- src/TNL/Matrices/DenseMatrixView.h | 28 ++++---- 63 files changed, 40 insertions(+), 729 deletions(-) create mode 100644 Documentation/Examples/Matrices/CMakeLists.txt mode change 100644 => 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu mode change 100644 => 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu mode change 100644 => 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu mode change 100644 => 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu mode change 100644 => 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu mode change 100644 => 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu mode change 100644 => 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu mode change 100644 => 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu mode change 100644 => 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixExample_getNonzeroElementsCount.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixExample_getRow.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixExample_getRow.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixExample_rowsReduction.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixExample_rowsReduction.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixExample_setElement.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixExample_setElement.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixExample_setElements.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixExample_setElements.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_addElement.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_addElement.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_allRowsReduction.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_allRowsReduction.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_constructor.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_constructor.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_forAllRows.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_forAllRows.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_forRows.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_forRows.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_getCompressedRowLengths.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_getCompressedRowLengths.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_getConstRow.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_getConstRow.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_getElement.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_getElement.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_getElementsCount.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_getElementsCount.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_getRow.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_getRow.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_rowsReduction.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_rowsReduction.cu (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_setElement.cpp (100%) rename Documentation/Examples/Matrices/{ => DenseMatrix}/DenseMatrixViewExample_setElement.cu (100%) delete mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_Constructor_init_list.cpp delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_Constructor_init_list.cu delete mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_addElement.cpp delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_addElement.cu delete mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_allRowsReduction.cpp delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_allRowsReduction.cu delete mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_forAllRows.cpp delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_forAllRows.cu delete mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_forRows.cpp delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_forRows.cu delete mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cpp delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cu delete mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_getConstRow.cpp delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_getConstRow.cu delete mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_getElement.cpp delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_getElement.cu delete mode 100644 Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp delete mode 120000 Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cu diff --git a/Documentation/Examples/Matrices/CMakeLists.txt b/Documentation/Examples/Matrices/CMakeLists.txt new file mode 100644 index 000000000..916608570 --- /dev/null +++ b/Documentation/Examples/Matrices/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY( DenseMatrix ) diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu deleted file mode 100644 index 91426a6f1..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include - - -template< typename Device > -void initializerListExample() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix { - { 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; - - TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { - { 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 HAVE_CUDA - std::cout << "Creating matrices on CUDA GPU ... " << std::endl; - initializerListExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu new file mode 120000 index 000000000..91fa4f073 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu @@ -0,0 +1 @@ +DenseMatrixExample_Constructor_init_list.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu deleted file mode 100644 index 4a36b007e..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include - -template< typename Device > -void addElements() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); - 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 HAVE_CUDA - std::cout << "Add elements on CUDA device:" << std::endl; - addElements< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu new file mode 120000 index 000000000..dd83670e4 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu @@ -0,0 +1 @@ +DenseMatrixExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu deleted file mode 100644 index ce323671f..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include -#include -#include -#include - -template< typename Device > -void allRowsReduction() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix { - { 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.allRowsReduction( 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; - allRowsReduction< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "All rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu new file mode 120000 index 000000000..70f517f68 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu @@ -0,0 +1 @@ +DenseMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu deleted file mode 100644 index 5fddf0f34..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include -#include - -template< typename Device > -void forAllRowsExample() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); - - auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { - if( rowIdx < columnIdx ) - compute = false; - else - value = rowIdx + columnIdx; - }; - - matrix.forAllRows( f ); - std::cout << matrix << std::endl; -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Creating matrix on host: " << std::endl; - forAllRowsExample< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Creating matrix on CUDA device: " << std::endl; - forAllRowsExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu new file mode 120000 index 000000000..589520f79 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cu @@ -0,0 +1 @@ +DenseMatrixExample_forAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu deleted file mode 100644 index f3e45a006..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include -#include - -template< typename Device > -void forRowsExample() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); - - auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { - if( rowIdx < columnIdx ) - compute = false; - else - value = rowIdx + columnIdx; - }; - - matrix.forRows( 0, matrix.getRows(), f ); - std::cout << matrix << std::endl; -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Creating matrix on host: " << std::endl; - forRowsExample< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Creating matrix on CUDA device: " << std::endl; - forRowsExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu new file mode 120000 index 000000000..f97a66ee3 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu @@ -0,0 +1 @@ +DenseMatrixExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu deleted file mode 100644 index e89992d9f..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -#include - -template< typename Device > -void getCompressedRowLengthsExample() -{ - TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { - { 1 }, - { 2, 3 }, - { 4, 5, 6 }, - { 7, 8, 9, 10 }, - { 11, 12, 13, 14, 15 } - }; - - std::cout << triangularMatrix << std::endl; - - TNL::Containers::Vector< int, Device > rowLengths; - 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 HAVE_CUDA - std::cout << "Getting compressed row lengths on CUDA device: " << std::endl; - getCompressedRowLengthsExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu new file mode 120000 index 000000000..2b3cd6c13 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cu @@ -0,0 +1 @@ +DenseMatrixExample_getCompressedRowLengths.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu deleted file mode 100644 index 08b655e55..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -template< typename Device > -void getRowExample() -{ - using MatrixType = TNL::Matrices::DenseMatrix< double, Device >; - TNL::Pointers::SharedPointer< MatrixType > 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 } - }; - - /*** - * Fetch lambda function returns diagonal element in each row. - */ - auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { - auto row = matrix->getRow( rowIdx ); - return row.getElement( 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::Reduction< Device >::reduce( matrix->getRows(), std::plus<>{}, fetch, 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 HAVE_CUDA - std::cout << "Getting matrix rows on CUDA device: " << std::endl; - getRowExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu new file mode 120000 index 000000000..c78f9cfdc --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cu @@ -0,0 +1 @@ +DenseMatrixExample_getConstRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu deleted file mode 100644 index 72a5d0af4..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -#include - -template< typename Device > -void getElements() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix { - { 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 ) << std::ios::right << matrix.getElement( i, i ); - std::cout << std::endl; - } -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Get elements on host:" << std::endl; - getElements< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Get elements on CUDA device:" << std::endl; - getElements< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu new file mode 120000 index 000000000..bad6f2fab --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cu @@ -0,0 +1 @@ +DenseMatrixExample_getElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu deleted file mode 100644 index 0a4a7bb7b..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include -#include - -template< typename Device > -void getElementsCountExample() -{ - TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { - { 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 HAVE_CUDA - std::cout << "Computing matrix elements on CUDA device: " << std::endl; - getElementsCountExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu new file mode 120000 index 000000000..6e8348f73 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cu @@ -0,0 +1 @@ +DenseMatrixExample_getElementsCount.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getNonzeroElementsCount.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getNonzeroElementsCount.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixExample_getNonzeroElementsCount.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getNonzeroElementsCount.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getRow.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixExample_getRow.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getRow.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getRow.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getRow.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixExample_getRow.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getRow.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_rowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixExample_rowsReduction.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_setElement.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElement.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixExample_setElement.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElement.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_setElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElement.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixExample_setElement.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElement.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_setElements.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElements.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixExample_setElements.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElements.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_setElements.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElements.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixExample_setElements.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElements.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_addElement.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_addElement.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_addElement.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_addElement.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_addElement.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_allRowsReduction.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_allRowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_allRowsReduction.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_constructor.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_constructor.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_constructor.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_constructor.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_constructor.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_constructor.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_constructor.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_constructor.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllRows.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllRows.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllRows.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_forAllRows.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllRows.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_forRows.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getCompressedRowLengths.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getCompressedRowLengths.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getCompressedRowLengths.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_getCompressedRowLengths.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getCompressedRowLengths.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getConstRow.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getConstRow.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_getConstRow.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getConstRow.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getConstRow.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getConstRow.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_getConstRow.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getConstRow.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getElement.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getElement.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getElement.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_getElement.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getElement.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getElementsCount.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getElementsCount.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getElementsCount.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_getElementsCount.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getElementsCount.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_getRow.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_rowsReduction.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_setElement.cpp similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_setElement.cpp diff --git a/Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_setElement.cu similarity index 100% rename from Documentation/Examples/Matrices/DenseMatrixViewExample_setElement.cu rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_setElement.cu diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_Constructor_init_list.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_Constructor_init_list.cpp deleted file mode 100644 index 91426a6f1..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_Constructor_init_list.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include - - -template< typename Device > -void initializerListExample() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix { - { 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; - - TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { - { 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 HAVE_CUDA - std::cout << "Creating matrices on CUDA GPU ... " << std::endl; - initializerListExample< TNL::Devices::Cuda >(); -#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 91fa4f073..000000000 --- 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_addElement.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_addElement.cpp deleted file mode 100644 index 4a36b007e..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_addElement.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include - -template< typename Device > -void addElements() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); - 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 HAVE_CUDA - std::cout << "Add elements on CUDA device:" << std::endl; - addElements< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_addElement.cu b/Documentation/Examples/Matrices/DenseMatrixExample_addElement.cu deleted file mode 120000 index dd83670e4..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_addElement.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_allRowsReduction.cpp deleted file mode 100644 index ce323671f..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_allRowsReduction.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include -#include -#include -#include - -template< typename Device > -void allRowsReduction() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix { - { 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.allRowsReduction( 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; - allRowsReduction< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "All rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrixExample_allRowsReduction.cu deleted file mode 120000 index 70f517f68..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_allRowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_forAllRows.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_forAllRows.cpp deleted file mode 100644 index 5fddf0f34..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_forAllRows.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include -#include - -template< typename Device > -void forAllRowsExample() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); - - auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { - if( rowIdx < columnIdx ) - compute = false; - else - value = rowIdx + columnIdx; - }; - - matrix.forAllRows( f ); - std::cout << matrix << std::endl; -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Creating matrix on host: " << std::endl; - forAllRowsExample< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Creating matrix on CUDA device: " << std::endl; - forAllRowsExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_forAllRows.cu b/Documentation/Examples/Matrices/DenseMatrixExample_forAllRows.cu deleted file mode 120000 index 589520f79..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_forAllRows.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_forAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_forRows.cpp deleted file mode 100644 index f3e45a006..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_forRows.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include -#include - -template< typename Device > -void forRowsExample() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); - - auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { - if( rowIdx < columnIdx ) - compute = false; - else - value = rowIdx + columnIdx; - }; - - matrix.forRows( 0, matrix.getRows(), f ); - std::cout << matrix << std::endl; -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Creating matrix on host: " << std::endl; - forRowsExample< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Creating matrix on CUDA device: " << std::endl; - forRowsExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_forRows.cu b/Documentation/Examples/Matrices/DenseMatrixExample_forRows.cu deleted file mode 120000 index f97a66ee3..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_forRows.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cpp deleted file mode 100644 index e89992d9f..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -#include - -template< typename Device > -void getCompressedRowLengthsExample() -{ - TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { - { 1 }, - { 2, 3 }, - { 4, 5, 6 }, - { 7, 8, 9, 10 }, - { 11, 12, 13, 14, 15 } - }; - - std::cout << triangularMatrix << std::endl; - - TNL::Containers::Vector< int, Device > rowLengths; - 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 HAVE_CUDA - std::cout << "Getting compressed row lengths on CUDA device: " << std::endl; - getCompressedRowLengthsExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/DenseMatrixExample_getCompressedRowLengths.cu deleted file mode 120000 index 2b3cd6c13..000000000 --- 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 deleted file mode 100644 index 08b655e55..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_getConstRow.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -template< typename Device > -void getRowExample() -{ - using MatrixType = TNL::Matrices::DenseMatrix< double, Device >; - TNL::Pointers::SharedPointer< MatrixType > 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 } - }; - - /*** - * Fetch lambda function returns diagonal element in each row. - */ - auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { - auto row = matrix->getRow( rowIdx ); - return row.getElement( 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::Reduction< Device >::reduce( matrix->getRows(), std::plus<>{}, fetch, 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 HAVE_CUDA - std::cout << "Getting matrix rows on CUDA device: " << std::endl; - getRowExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getConstRow.cu b/Documentation/Examples/Matrices/DenseMatrixExample_getConstRow.cu deleted file mode 120000 index c78f9cfdc..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_getConstRow.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_getConstRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getElement.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_getElement.cpp deleted file mode 100644 index 72a5d0af4..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_getElement.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -#include - -template< typename Device > -void getElements() -{ - TNL::Matrices::DenseMatrix< double, Device > matrix { - { 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 ) << std::ios::right << matrix.getElement( i, i ); - std::cout << std::endl; - } -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Get elements on host:" << std::endl; - getElements< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Get elements on CUDA device:" << std::endl; - getElements< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getElement.cu b/Documentation/Examples/Matrices/DenseMatrixExample_getElement.cu deleted file mode 120000 index bad6f2fab..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_getElement.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_getElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp b/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp deleted file mode 100644 index 0a4a7bb7b..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include -#include - -template< typename Device > -void getElementsCountExample() -{ - TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { - { 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 HAVE_CUDA - std::cout << "Computing matrix elements on CUDA device: " << std::endl; - getElementsCountExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cu b/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cu deleted file mode 120000 index 6e8348f73..000000000 --- a/Documentation/Examples/Matrices/DenseMatrixExample_getElementsCount.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_getElementsCount.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 50e0bd9dc..215c2ec2e 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -126,7 +126,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * list of matrix rows. * * \par Example - * \include Matrices/DenseMatrixExample_Constructor_init_list.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cpp * \par Output * \include DenseMatrixExample_Constructor_init_list.out */ @@ -200,7 +200,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * list of matrix rows. * * \par Example - * \include Matrices/DenseMatrixExample_setElements.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_setElements.cpp * \par Output * \include DenseMatrixExample_setElements.out */ @@ -223,7 +223,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * will be stored. * * \par Example - * \include Matrices/DenseMatrixExample_getCompressedRowLengths.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_getCompressedRowLengths.cpp * \par Output * \include DenseMatrixExample_getCompressedRowLengths.out */ @@ -240,7 +240,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \return number of all matrix elements. * * \par Example - * \include Matrices/DenseMatrixExample_getElementsCount.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cpp * \par Output * \include DenseMatrixExample_getElementsCount.out */ @@ -252,7 +252,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \return number of all non-zero matrix elements. * * \par Example - * \include Matrices/DenseMatrixExample_getElementsCount.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_getElementsCount.cpp * \par Output * \include DenseMatrixExample_getElementsCount.out */ @@ -271,7 +271,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \return RowView for accessing given matrix row. * * \par Example - * \include Matrices/DenseMatrixExample_getConstRow.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cpp * \par Output * \include DenseMatrixExample_getConstRow.out * @@ -288,7 +288,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \return RowView for accessing given matrix row. * * \par Example - * \include Matrices/DenseMatrixExample_getRow.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_getRow.cpp * \par Output * \include DenseMatrixExample_getRow.out * @@ -349,7 +349,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \param value is the value the element will be set to. * * \par Example - * \include Matrices/DenseMatrixViewExample_setElement.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_setElement.cpp * \par Output * \include DenseMatrixExample_setElement.out */ @@ -375,7 +375,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * value is multiplied by before addition of given \e value. * * \par Example - * \include Matrices/DenseMatrixViewExample_addElement.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_addElement.cpp * \par Output * \include DenseMatrixExample_addElement.out * @@ -402,7 +402,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \return value of given matrix element. * * \par Example - * \include Matrices/DenseMatrixViewExample_getElement.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_getElement.cpp * \par Output * \include DenseMatrixExample_getElement.out * @@ -431,7 +431,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrixExample_rowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp * \par Output * \include DenseMatrixExample_rowsReduction.out */ @@ -456,7 +456,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrixExample_allRowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp * \par Output * \include DenseMatrixExample_allRowsReduction.out */ @@ -478,7 +478,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/DenseMatrixExample_forRows.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp * \par Output * \include DenseMatrixExample_forRows.out */ @@ -500,7 +500,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/DenseMatrixExample_forRows.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp * \par Output * \include DenseMatrixExample_forRows.out */ @@ -516,7 +516,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/DenseMatrixExample_forAllRows.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp * \par Output * \include DenseMatrixExample_forAllRows.out */ @@ -532,7 +532,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/DenseMatrixExample_forAllRows.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp * \par Output * \include DenseMatrixExample_forAllRows.out */ diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index bca7a5515..b9fd8b5fd 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -121,7 +121,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param values is vector view with matrix elements values. * * \par Example - * \include Matrices/DenseMatrixViewExample_constructor.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_constructor.cpp * \par Output * \include DenseMatrixViewExample_constructor.out @@ -180,7 +180,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * will be stored. * * \par Example - * \include Matrices/DenseMatrixViewExample_getCompressedRowLengths.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_getCompressedRowLengths.cpp * \par Output * \include DenseMatrixViewExample_getCompressedRowLengths.out */ @@ -214,7 +214,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \return number of all non-zero matrix elements. * * \par Example - * \include Matrices/DenseMatrixViewExample_getElementsCount.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_getElementsCount.cpp * \par Output * \include DenseMatrixViewExample_getElementsCount.out */ @@ -228,7 +228,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \return RowView for accessing given matrix row. * * \par Example - * \include Matrices/DenseMatrixViewExample_getConstRow.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_getConstRow.cpp * \par Output * \include DenseMatrixViewExample_getConstRow.out * @@ -245,7 +245,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \return RowView for accessing given matrix row. * * \par Example - * \include Matrices/DenseMatrixViewExample_getRow.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp * \par Output * \include DenseMatrixExample_getRow.out * @@ -306,7 +306,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param value is the value the element will be set to. * * \par Example - * \include Matrices/DenseMatrixViewExample_setElement.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_setElement.cpp * \par Output * \include DenseMatrixExample_setElement.out */ @@ -332,7 +332,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * value is multiplied by before addition of given \e value. * * \par Example - * \include Matrices/DenseMatrixViewExample_addElement.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_addElement.cpp * \par Output * \include DenseMatrixExample_addElement.out * @@ -359,7 +359,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \return value of given matrix element. * * \par Example - * \include Matrices/DenseMatrixViewExample_getElement.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_getElement.cpp * \par Output * \include DenseMatrixExample_getElement.out * @@ -388,7 +388,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrixViewExample_rowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cpp * \par Output * \include DenseMatrixViewExample_rowsReduction.out */ @@ -413,7 +413,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrixViewExample_allRowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cpp * \par Output * \include DenseMatrixViewExample_allRowsReduction.out */ @@ -435,7 +435,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/DenseMatrixViewExample_forRows.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp * \par Output * \include DenseMatrixViewExample_forRows.out */ @@ -457,7 +457,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/DenseMatrixViewExample_forRows.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp * \par Output * \include DenseMatrixViewExample_forRows.out */ @@ -473,7 +473,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/DenseMatrixViewExample_forAllRows.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_forAllRows.cpp * \par Output * \include DenseMatrixViewExample_forAllRows.out */ @@ -489,7 +489,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/DenseMatrixExample_forAllRows.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp * \par Output * \include DenseMatrixExample_forAllRows.out */ -- GitLab From bcad4d75fd3a5d03799f03c5c5fea42ce51ab80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sun, 10 May 2020 22:13:45 +0200 Subject: [PATCH 04/38] Deleting obsolete DenseRow. --- src/TNL/Matrices/DenseRow.h | 50 ----------------------- src/TNL/Matrices/DenseRow_impl.h | 68 -------------------------------- 2 files changed, 118 deletions(-) delete mode 100644 src/TNL/Matrices/DenseRow.h delete mode 100644 src/TNL/Matrices/DenseRow_impl.h diff --git a/src/TNL/Matrices/DenseRow.h b/src/TNL/Matrices/DenseRow.h deleted file mode 100644 index a1d6d2124..000000000 --- a/src/TNL/Matrices/DenseRow.h +++ /dev/null @@ -1,50 +0,0 @@ -/*************************************************************************** - DenseRow.h - description - ------------------- - begin : Dec 24, 2014 - copyright : (C) 2014 by oberhuber - email : tomas.oberhuber@fjfi.cvut.cz - ***************************************************************************/ - -/* See Copyright Notice in tnl/Copyright */ - -#pragma once - -namespace TNL { -namespace Matrices { - -template< typename Real, typename Index > -class DenseRow -{ - public: - - __cuda_callable__ - DenseRow(); - - __cuda_callable__ - DenseRow( Real* values, - const Index columns, - const Index step ); - - __cuda_callable__ - void bind( Real* values, - const Index columns, - const Index step ); - - __cuda_callable__ - void setElement( const Index& elementIndex, - const Index& column, - const Real& value ); - - protected: - - Real* values; - - Index columns, step; -}; - -} // namespace Matrices -} // namespace TNL - -#include - diff --git a/src/TNL/Matrices/DenseRow_impl.h b/src/TNL/Matrices/DenseRow_impl.h deleted file mode 100644 index 7b1bac1a5..000000000 --- a/src/TNL/Matrices/DenseRow_impl.h +++ /dev/null @@ -1,68 +0,0 @@ -/*************************************************************************** - DenseRow_impl.h - description - ------------------- - begin : Dec 24, 2014 - copyright : (C) 2014 by oberhuber - email : tomas.oberhuber@fjfi.cvut.cz - ***************************************************************************/ - -/* See Copyright Notice in tnl/Copyright */ - -#pragma once - -namespace TNL { -namespace Matrices { - -template< typename Real, typename Index > -__cuda_callable__ -DenseRow< Real, Index >:: -DenseRow() -: values( 0 ), - columns( 0 ), - step( 0 ) -{ -} - -template< typename Real, typename Index > -__cuda_callable__ -DenseRow< Real, Index >:: -DenseRow( Real* values, - const Index columns, - const Index step ) -: values( values ), - columns( columns ), - step( step ) -{ -} - -template< typename Real, typename Index > -__cuda_callable__ -void -DenseRow< Real, Index >:: -bind( Real* values, - const Index columns, - const Index step ) -{ - this->values = values; - this->columns = columns; - this->step = step; -} - -template< typename Real, typename Index > -__cuda_callable__ -void -DenseRow< Real, Index >:: -setElement( const Index& elementIndex, - const Index& column, - const Real& value ) -{ - TNL_ASSERT( this->values, ); - TNL_ASSERT( this->step > 0,); - TNL_ASSERT( column >= 0 && column < this->columns, - std::cerr << "column = " << column << " this->columns = " << this->columns ); - - this->values[ column * this->step ] = value; -} - -} // namespace Matrices -} // namespace TNL -- GitLab From 5daa811dbb3d91beec73f4e2be257ced63202ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Mon, 11 May 2020 14:04:23 +0200 Subject: [PATCH 05/38] Small fixes in DenseMatrix. --- src/TNL/Matrices/DenseMatrixView.h | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index b9fd8b5fd..ad4a0730e 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -187,11 +187,6 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > template< typename Vector > void getCompressedRowLengths( Vector& rowLengths ) const; - //[[deprecated]] - //IndexType getRowLength( const IndexType row ) const; - - //IndexType getMaxRowLength() const; - /** * \brief Returns number of all matrix elements. * @@ -202,7 +197,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \return number of all matrix elements. * * \par Example - * \include Matrices/DenseViewMatrixExample_getElementsCount.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_getElementsCount.cpp * \par Output * \include DenseMatrixViewExample_getElementsCount.out */ -- GitLab From 08eff7c9224051605caef55e96ed8411435546cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= Date: Wed, 13 May 2020 21:37:41 +0200 Subject: [PATCH 06/38] Fixed bug in BinarySymmetricMatrix and SymmetricBinaryMatrix Also simplified the implementation of all matrix types. --- src/TNL/Matrices/MatrixType.h | 43 +++++++---------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/src/TNL/Matrices/MatrixType.h b/src/TNL/Matrices/MatrixType.h index 8d4cfe7ba..afefd8d06 100644 --- a/src/TNL/Matrices/MatrixType.h +++ b/src/TNL/Matrices/MatrixType.h @@ -11,7 +11,7 @@ #pragma once namespace TNL { - namespace Matrices { +namespace Matrices { template< bool Symmetric, bool Binary > @@ -20,44 +20,17 @@ struct MatrixType static constexpr bool isSymmetric() { return Symmetric; } static constexpr bool isBinary() { return Binary; } - }; -struct GeneralMatrix -{ - static constexpr bool isSymmetric() { return false; } +struct GeneralMatrix : MatrixType< false, false > {}; - static constexpr bool isBinary() { return false; } -}; +struct SymmetricMatrix : MatrixType< true, false > {}; -struct SymmetricMatrix -{ - static constexpr bool isSymmetric() { return true; } +struct BinaryMatrix : MatrixType< false, true > {}; - static constexpr bool isBinary() { return false; } -}; - -struct BinaryMatrix -{ - static constexpr bool isSymmetric() { return false; } - - static constexpr bool isBinary() { return true; } -}; - -struct BinarySymmetricMatrix -{ - static constexpr bool isSymmetric() { return false; } - - static constexpr bool isBinary() { return true; } -}; - -struct SymmetricBinaryMatrix -{ - static constexpr bool isSymmetric() { return false; } - - static constexpr bool isBinary() { return true; } -}; +struct BinarySymmetricMatrix : MatrixType< true, true > {}; +struct SymmetricBinaryMatrix : MatrixType< true, true > {}; - } //namespace Matrices -} //namespace TNL \ No newline at end of file +} // namespace Matrices +} // namespace TNL -- GitLab From 9df02f9fc9961c85901da7342e877b63dd2b95ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= Date: Wed, 13 May 2020 21:42:52 +0200 Subject: [PATCH 07/38] Simplified template parameters of DenseMatrix in DistributedSpMV --- src/TNL/Matrices/DistributedSpMV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TNL/Matrices/DistributedSpMV.h b/src/TNL/Matrices/DistributedSpMV.h index 4785a6790..83c1bd682 100644 --- a/src/TNL/Matrices/DistributedSpMV.h +++ b/src/TNL/Matrices/DistributedSpMV.h @@ -218,7 +218,7 @@ public: protected: // communication pattern - Matrices::DenseMatrix< IndexType, Devices::Host, int, Containers::Segments::RowMajorOrder, Allocators::Host< IndexType > > commPatternStarts, commPatternEnds; + Matrices::DenseMatrix< IndexType, Devices::Host, int > commPatternStarts, commPatternEnds; // span of rows with only block-diagonal entries std::pair< IndexType, IndexType > localOnlySpan; -- GitLab From 9357944c564547a4758bbe1a39a2fe0751604c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= Date: Sat, 16 May 2020 16:47:50 +0200 Subject: [PATCH 08/38] Fixed asserts in tridiagonal and multidiagonal matrices --- src/TNL/Matrices/Multidiagonal.hpp | 2 +- src/TNL/Matrices/MultidiagonalMatrixView.hpp | 2 +- src/TNL/Matrices/Tridiagonal.hpp | 2 +- src/TNL/Matrices/TridiagonalMatrixView.hpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/TNL/Matrices/Multidiagonal.hpp b/src/TNL/Matrices/Multidiagonal.hpp index be6ec58c5..f20217dfa 100644 --- a/src/TNL/Matrices/Multidiagonal.hpp +++ b/src/TNL/Matrices/Multidiagonal.hpp @@ -294,7 +294,7 @@ operator == ( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAl return this->values == matrix.values; else { - TNL_ASSERT( false, "TODO" ); + TNL_ASSERT_TRUE( false, "TODO" ); } } diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index 8d772b6b9..634debab4 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -192,7 +192,7 @@ operator == ( const MultidiagonalMatrixView< Real_, Device_, Index_, Organizatio return this->values == matrix.values; else { - TNL_ASSERT( false, "TODO" ); + TNL_ASSERT_TRUE( false, "TODO" ); } } diff --git a/src/TNL/Matrices/Tridiagonal.hpp b/src/TNL/Matrices/Tridiagonal.hpp index 8a1804e9d..e4ec42563 100644 --- a/src/TNL/Matrices/Tridiagonal.hpp +++ b/src/TNL/Matrices/Tridiagonal.hpp @@ -224,7 +224,7 @@ operator == ( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllo return this->values == matrix.values; else { - TNL_ASSERT( false, "TODO" ); + TNL_ASSERT_TRUE( false, "TODO" ); } } diff --git a/src/TNL/Matrices/TridiagonalMatrixView.hpp b/src/TNL/Matrices/TridiagonalMatrixView.hpp index 092e63cbc..949e46317 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixView.hpp @@ -158,7 +158,7 @@ operator == ( const TridiagonalMatrixView< Real_, Device_, Index_, Organization_ return this->values == matrix.values; else { - TNL_ASSERT( false, "TODO" ); + TNL_ASSERT_TRUE( false, "TODO" ); } } -- GitLab From 789cf67b67642f59d93daaa99952a7eb59e824a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= Date: Tue, 19 May 2020 09:34:10 +0200 Subject: [PATCH 09/38] Fixed SparseMatrixRowView's operator== and operator<< for binary matrices --- src/TNL/Matrices/SparseMatrixRowView.hpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/TNL/Matrices/SparseMatrixRowView.hpp b/src/TNL/Matrices/SparseMatrixRowView.hpp index 545e395fc..a0a9c2604 100644 --- a/src/TNL/Matrices/SparseMatrixRowView.hpp +++ b/src/TNL/Matrices/SparseMatrixRowView.hpp @@ -140,7 +140,7 @@ operator==( const SparseMatrixRowView< _SegmentView, _ValuesView, _ColumnsIndexe while( i < getSize() && i < other.getSize() ) { if( getColumnIndex( i ) != other.getColumnIndex( i ) ) return false; - if( getValue( i ) != other.getValue( i ) ) + if( ! _isBinary && getValue( i ) != other.getValue( i ) ) return false; ++i; } @@ -163,7 +163,11 @@ std::ostream& operator<<( std::ostream& str, const SparseMatrixRowView< SegmentV { using NonConstIndex = std::remove_const_t< typename SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView, isBinary_ >::IndexType >; for( NonConstIndex i = 0; i < row.getSize(); i++ ) - str << " [ " << row.getColumnIndex( i ) << " ] = " << row.getValue( i ) << ", "; + if( isBinary_ ) + // TODO: check getPaddingIndex(), print only the column indices of non-zeros but not the values + str << " [ " << row.getColumnIndex( i ) << " ] = " << (row.getColumnIndex( i ) >= 0) << ", "; + else + str << " [ " << row.getColumnIndex( i ) << " ] = " << row.getValue( i ) << ", "; return str; } -- GitLab From 44bc1565241ff43d102bd5798d807b565eb7f2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= Date: Wed, 20 May 2020 09:47:48 +0200 Subject: [PATCH 10/38] Removed unused method from segments --- src/TNL/Containers/Segments/CSR.h | 3 --- src/TNL/Containers/Segments/CSR.hpp | 10 ---------- src/TNL/Containers/Segments/CSRView.h | 3 --- src/TNL/Containers/Segments/CSRView.hpp | 9 --------- src/TNL/Containers/Segments/Ellpack.h | 3 --- src/TNL/Containers/Segments/Ellpack.hpp | 11 ----------- src/TNL/Containers/Segments/EllpackView.h | 3 --- src/TNL/Containers/Segments/EllpackView.hpp | 9 --------- src/TNL/Containers/Segments/SlicedEllpack.h | 3 --- src/TNL/Containers/Segments/SlicedEllpack.hpp | 12 ------------ src/TNL/Containers/Segments/SlicedEllpackView.h | 3 --- src/TNL/Containers/Segments/SlicedEllpackView.hpp | 11 ----------- 12 files changed, 80 deletions(-) diff --git a/src/TNL/Containers/Segments/CSR.h b/src/TNL/Containers/Segments/CSR.h index 9bcd45d08..034c4560b 100644 --- a/src/TNL/Containers/Segments/CSR.h +++ b/src/TNL/Containers/Segments/CSR.h @@ -88,9 +88,6 @@ class CSR __cuda_callable__ IndexType getGlobalIndex( const Index segmentIdx, const Index localIdx ) const; - __cuda_callable__ - void getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const; - __cuda_callable__ SegmentViewType getSegmentView( const IndexType segmentIdx ) const; diff --git a/src/TNL/Containers/Segments/CSR.hpp b/src/TNL/Containers/Segments/CSR.hpp index 685f6ef54..0ba034d5b 100644 --- a/src/TNL/Containers/Segments/CSR.hpp +++ b/src/TNL/Containers/Segments/CSR.hpp @@ -170,16 +170,6 @@ getGlobalIndex( const Index segmentIdx, const Index localIdx ) const -> IndexTyp return offsets[ segmentIdx ] + localIdx; } -template< typename Device, - typename Index, - typename IndexAllocator > -__cuda_callable__ -void -CSR< Device, Index, IndexAllocator >:: -getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const -{ -} - template< typename Device, typename Index, typename IndexAllocator > diff --git a/src/TNL/Containers/Segments/CSRView.h b/src/TNL/Containers/Segments/CSRView.h index b01e6c66d..f915a2e38 100644 --- a/src/TNL/Containers/Segments/CSRView.h +++ b/src/TNL/Containers/Segments/CSRView.h @@ -87,9 +87,6 @@ class CSRView __cuda_callable__ IndexType getGlobalIndex( const Index segmentIdx, const Index localIdx ) const; - __cuda_callable__ - void getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const; - __cuda_callable__ SegmentViewType getSegmentView( const IndexType segmentIdx ) const; diff --git a/src/TNL/Containers/Segments/CSRView.hpp b/src/TNL/Containers/Segments/CSRView.hpp index 7599327d1..e6f840d6b 100644 --- a/src/TNL/Containers/Segments/CSRView.hpp +++ b/src/TNL/Containers/Segments/CSRView.hpp @@ -151,15 +151,6 @@ getGlobalIndex( const Index segmentIdx, const Index localIdx ) const -> IndexTyp return offsets[ segmentIdx ] + localIdx; } -template< typename Device, - typename Index > -__cuda_callable__ -void -CSRView< Device, Index >:: -getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const -{ -} - template< typename Device, typename Index > __cuda_callable__ diff --git a/src/TNL/Containers/Segments/Ellpack.h b/src/TNL/Containers/Segments/Ellpack.h index f5c8490de..8ef79f6d0 100644 --- a/src/TNL/Containers/Segments/Ellpack.h +++ b/src/TNL/Containers/Segments/Ellpack.h @@ -85,9 +85,6 @@ class Ellpack __cuda_callable__ IndexType getGlobalIndex( const Index segmentIdx, const Index localIdx ) const; - __cuda_callable__ - void getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const; - __cuda_callable__ SegmentViewType getSegmentView( const IndexType segmentIdx ) const; diff --git a/src/TNL/Containers/Segments/Ellpack.hpp b/src/TNL/Containers/Segments/Ellpack.hpp index 922a75765..91e06e110 100644 --- a/src/TNL/Containers/Segments/Ellpack.hpp +++ b/src/TNL/Containers/Segments/Ellpack.hpp @@ -233,17 +233,6 @@ getGlobalIndex( const Index segmentIdx, const Index localIdx ) const -> IndexTyp return segmentIdx + this->alignedSize * localIdx; } -template< typename Device, - typename Index, - typename IndexAllocator, - ElementsOrganization Organization, - int Alignment > -__cuda_callable__ -void Ellpack< Device, Index, IndexAllocator, Organization, Alignment >:: -getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const -{ -} - template< typename Device, typename Index, typename IndexAllocator, diff --git a/src/TNL/Containers/Segments/EllpackView.h b/src/TNL/Containers/Segments/EllpackView.h index 3559949d8..1d77c7445 100644 --- a/src/TNL/Containers/Segments/EllpackView.h +++ b/src/TNL/Containers/Segments/EllpackView.h @@ -81,9 +81,6 @@ class EllpackView __cuda_callable__ IndexType getGlobalIndex( const Index segmentIdx, const Index localIdx ) const; - __cuda_callable__ - void getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const; - __cuda_callable__ SegmentViewType getSegmentView( const IndexType segmentIdx ) const; diff --git a/src/TNL/Containers/Segments/EllpackView.hpp b/src/TNL/Containers/Segments/EllpackView.hpp index 0195424e9..2d1d8090f 100644 --- a/src/TNL/Containers/Segments/EllpackView.hpp +++ b/src/TNL/Containers/Segments/EllpackView.hpp @@ -164,15 +164,6 @@ getGlobalIndex( const Index segmentIdx, const Index localIdx ) const -> IndexTyp return segmentIdx + this->alignedSize * localIdx; } -template< typename Device, - typename Index, - ElementsOrganization Organization, - int Alignment > -__cuda_callable__ void EllpackView< Device, Index, Organization, Alignment >:: -getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const -{ -} - template< typename Device, typename Index, ElementsOrganization Organization, diff --git a/src/TNL/Containers/Segments/SlicedEllpack.h b/src/TNL/Containers/Segments/SlicedEllpack.h index c85e45742..a0e5c81b0 100644 --- a/src/TNL/Containers/Segments/SlicedEllpack.h +++ b/src/TNL/Containers/Segments/SlicedEllpack.h @@ -82,9 +82,6 @@ class SlicedEllpack __cuda_callable__ IndexType getGlobalIndex( const Index segmentIdx, const Index localIdx ) const; - __cuda_callable__ - void getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const; - __cuda_callable__ SegmentViewType getSegmentView( const IndexType segmentIdx ) const; diff --git a/src/TNL/Containers/Segments/SlicedEllpack.hpp b/src/TNL/Containers/Segments/SlicedEllpack.hpp index 6a9bc4fe2..a288bf7df 100644 --- a/src/TNL/Containers/Segments/SlicedEllpack.hpp +++ b/src/TNL/Containers/Segments/SlicedEllpack.hpp @@ -259,18 +259,6 @@ getGlobalIndex( const Index segmentIdx, const Index localIdx ) const -> IndexTyp return sliceOffset + segmentInSliceIdx + SliceSize * localIdx; } -template< typename Device, - typename Index, - typename IndexAllocator, - ElementsOrganization Organization, - int SliceSize > -__cuda_callable__ -void -SlicedEllpack< Device, Index, IndexAllocator, Organization, SliceSize >:: -getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const -{ -} - template< typename Device, typename Index, typename IndexAllocator, diff --git a/src/TNL/Containers/Segments/SlicedEllpackView.h b/src/TNL/Containers/Segments/SlicedEllpackView.h index 2a0fcb189..5c6e0eeb2 100644 --- a/src/TNL/Containers/Segments/SlicedEllpackView.h +++ b/src/TNL/Containers/Segments/SlicedEllpackView.h @@ -83,9 +83,6 @@ class SlicedEllpackView __cuda_callable__ IndexType getGlobalIndex( const Index segmentIdx, const Index localIdx ) const; - __cuda_callable__ - void getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const; - __cuda_callable__ SegmentViewType getSegmentView( const IndexType segmentIdx ) const; diff --git a/src/TNL/Containers/Segments/SlicedEllpackView.hpp b/src/TNL/Containers/Segments/SlicedEllpackView.hpp index 7331181ef..34f24def6 100644 --- a/src/TNL/Containers/Segments/SlicedEllpackView.hpp +++ b/src/TNL/Containers/Segments/SlicedEllpackView.hpp @@ -202,17 +202,6 @@ getGlobalIndex( const Index segmentIdx, const Index localIdx ) const -> IndexTyp return sliceOffset + segmentInSliceIdx + SliceSize * localIdx; } -template< typename Device, - typename Index, - ElementsOrganization Organization, - int SliceSize > -__cuda_callable__ -void -SlicedEllpackView< Device, Index, Organization, SliceSize >:: -getSegmentAndLocalIndex( const Index globalIdx, Index& segmentIdx, Index& localIdx ) const -{ -} - template< typename Device, typename Index, ElementsOrganization Organization, -- GitLab From 4e9330d8bf2e1db3a77900a457df122acff23133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 22 May 2020 16:44:08 +0200 Subject: [PATCH 11/38] Writting documentation on matrices. --- .../Examples/Matrices/CMakeLists.txt | 4 + .../Matrices/DenseMatrix/CMakeLists.txt | 2 +- .../Matrices/LambdaMatrix/CMakeLists.txt | 282 +++++++++++++++++ .../MultidiagonalMatrix/CMakeLists.txt | 282 +++++++++++++++++ .../Matrices/SparseMatrix/CMakeLists.txt | 283 ++++++++++++++++++ ...eMatrixExample_Constructor_init_list_1.cpp | 27 ++ .../Matrices/TridiagonalMatrix/CMakeLists.txt | 282 +++++++++++++++++ src/TNL/Matrices/DenseMatrix.h | 32 +- src/TNL/Matrices/DenseMatrix.hpp | 12 +- src/TNL/Matrices/DenseMatrixView.hpp | 3 + src/TNL/Matrices/MatrixType.h | 42 +++ src/TNL/Matrices/SparseMatrix.h | 199 +++++++++--- src/TNL/Matrices/SparseMatrix.hpp | 27 +- 13 files changed, 1410 insertions(+), 67 deletions(-) create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt create mode 100644 Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt diff --git a/Documentation/Examples/Matrices/CMakeLists.txt b/Documentation/Examples/Matrices/CMakeLists.txt index 916608570..8e4f5b37d 100644 --- a/Documentation/Examples/Matrices/CMakeLists.txt +++ b/Documentation/Examples/Matrices/CMakeLists.txt @@ -1 +1,5 @@ ADD_SUBDIRECTORY( DenseMatrix ) +ADD_SUBDIRECTORY( LambdaMatrix ) +ADD_SUBDIRECTORY( MultidiagonalMatrix ) +ADD_SUBDIRECTORY( SparseMatrix ) +ADD_SUBDIRECTORY( TridiagonalMatrix ) diff --git a/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt index a0db86c4d..156b19dba 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt @@ -252,7 +252,7 @@ ELSE() ENDIF() -ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS +ADD_CUSTOM_TARGET( RunDenseMatricesExamples ALL DEPENDS DenseMatrixExample_Constructor_init_list.out DenseMatrixExample_setElements.out DenseMatrixExample_getCompressedRowLengths.out diff --git a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt new file mode 100644 index 000000000..64655b0a7 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt @@ -0,0 +1,282 @@ +IF( BUILD_CUDA ) +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_cuda SparseMatrixExample_Constructor_init_list.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out +# OUTPUT SparseMatrixExample_Constructor_init_list.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out +# OUTPUT SparseMatrixExample_setElements.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths_cuda SparseMatrixExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElementsCount_cuda SparseMatrixExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out +# OUTPUT SparseMatrixExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getConstRow_cuda SparseMatrixExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out +# OUTPUT SparseMatrixExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getRow_cuda SparseMatrixExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out +# OUTPUT SparseMatrixExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement_cuda SparseMatrixExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out +# OUTPUT SparseMatrixExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement_cuda SparseMatrixExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out +# OUTPUT SparseMatrixExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElement_cuda SparseMatrixExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out +# OUTPUT SparseMatrixExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out +# OUTPUT SparseMatrixExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction_cuda SparseMatrixExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out +# OUTPUT SparseMatrixExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forRows_cuda SparseMatrixExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out +# OUTPUT SparseMatrixExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forAllRows_cuda SparseMatrixExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out +# OUTPUT SparseMatrixExample_forAllRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_constructor_cuda SparseMatrixViewExample_constructor.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out +# OUTPUT SparseMatrixViewExample_constructor.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths_cuda SparseMatrixViewExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount_cuda SparseMatrixViewExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out +# OUTPUT SparseMatrixViewExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow_cuda SparseMatrixViewExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out +# OUTPUT SparseMatrixViewExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getRow_cuda SparseMatrixViewExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out +# OUTPUT SparseMatrixViewExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement_cuda SparseMatrixViewExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out +# OUTPUT SparseMatrixViewExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_addElement_cuda SparseMatrixViewExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out +# OUTPUT SparseMatrixViewExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElement_cuda SparseMatrixViewExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out +# OUTPUT SparseMatrixViewExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction_cuda SparseMatrixViewExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out +# OUTPUT SparseMatrixViewExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction_cuda SparseMatrixViewExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out +# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forRows_cuda SparseMatrixViewExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out +# OUTPUT SparseMatrixViewExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows_cuda SparseMatrixViewExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out +# OUTPUT SparseMatrixViewExample_forAllRows.out ) + +ELSE() +# ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list SparseMatrixExample_Constructor_init_list.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out +# OUTPUT SparseMatrixExample_Constructor_init_list.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out +# OUTPUT SparseMatrixExample_setElements.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths SparseMatrixExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getElementsCount SparseMatrixExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out +# OUTPUT SparseMatrixExample_getElementsCount.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getConstRow SparseMatrixExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out +# OUTPUT SparseMatrixExample_getConstRow.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getRow SparseMatrixExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out +# OUTPUT SparseMatrixExample_getRow.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out +# OUTPUT SparseMatrixExample_setElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out +# OUTPUT SparseMatrixExample_addElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getElement SparseMatrixExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out +# OUTPUT SparseMatrixExample_getElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out +# OUTPUT SparseMatrixExample_rowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction SparseMatrixExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out +# OUTPUT SparseMatrixExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_forRows SparseMatrixExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out +# OUTPUT SparseMatrixExample_forRows.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_forAllRows SparseMatrixExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out +# OUTPUT SparseMatrixExample_forAllRows.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_constructor SparseMatrixViewExample_constructor.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out +# OUTPUT SparseMatrixViewExample_constructor.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount SparseMatrixViewExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out +# OUTPUT SparseMatrixViewExample_getElementsCount.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow SparseMatrixViewExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out +# OUTPUT SparseMatrixViewExample_getConstRow.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getRow SparseMatrixViewExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out +# OUTPUT SparseMatrixViewExample_getRow.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out +# OUTPUT SparseMatrixViewExample_setElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_addElement SparseMatrixViewExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out +# OUTPUT SparseMatrixViewExample_addElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getElement SparseMatrixViewExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out +# OUTPUT SparseMatrixViewExample_getElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out +# OUTPUT SparseMatrixViewExample_rowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out +# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_forRows SparseMatrixViewExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out +# OUTPUT SparseMatrixViewExample_forRows.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows SparseMatrixViewExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out +# OUTPUT SparseMatrixViewExample_forAllRows.out ) + +ENDIF() + +#ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS +# SparseMatrixExample_Constructor_init_list.out +# SparseMatrixExample_setElements.out +# SparseMatrixExample_getCompressedRowLengths.out +# SparseMatrixExample_getElementsCount.out +# SparseMatrixExample_getConstRow.out +# SparseMatrixExample_getRow.out +# SparseMatrixExample_setElement.out +# SparseMatrixExample_addElement.out +# SparseMatrixExample_getElement.out +# SparseMatrixExample_rowsReduction.out +# SparseMatrixExample_allRowsReduction.out +# SparseMatrixExample_forRows.out +# SparseMatrixExample_forAllRows.out +# SparseMatrixViewExample_constructor.out +# SparseMatrixViewExample_getCompressedRowLengths.out +# SparseMatrixViewExample_getElementsCount.out +# SparseMatrixViewExample_getConstRow.out +# SparseMatrixViewExample_getRow.out +# SparseMatrixViewExample_setElement.out +# SparseMatrixViewExample_addElement.out +# SparseMatrixViewExample_getElement.out +# SparseMatrixViewExample_rowsReduction.out +# SparseMatrixViewExample_allRowsReduction.out +# SparseMatrixViewExample_forRows.out +# SparseMatrixViewExample_forAllRows.out +#) + diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt new file mode 100644 index 000000000..64655b0a7 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -0,0 +1,282 @@ +IF( BUILD_CUDA ) +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_cuda SparseMatrixExample_Constructor_init_list.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out +# OUTPUT SparseMatrixExample_Constructor_init_list.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out +# OUTPUT SparseMatrixExample_setElements.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths_cuda SparseMatrixExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElementsCount_cuda SparseMatrixExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out +# OUTPUT SparseMatrixExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getConstRow_cuda SparseMatrixExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out +# OUTPUT SparseMatrixExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getRow_cuda SparseMatrixExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out +# OUTPUT SparseMatrixExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement_cuda SparseMatrixExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out +# OUTPUT SparseMatrixExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement_cuda SparseMatrixExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out +# OUTPUT SparseMatrixExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElement_cuda SparseMatrixExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out +# OUTPUT SparseMatrixExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out +# OUTPUT SparseMatrixExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction_cuda SparseMatrixExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out +# OUTPUT SparseMatrixExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forRows_cuda SparseMatrixExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out +# OUTPUT SparseMatrixExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forAllRows_cuda SparseMatrixExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out +# OUTPUT SparseMatrixExample_forAllRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_constructor_cuda SparseMatrixViewExample_constructor.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out +# OUTPUT SparseMatrixViewExample_constructor.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths_cuda SparseMatrixViewExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount_cuda SparseMatrixViewExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out +# OUTPUT SparseMatrixViewExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow_cuda SparseMatrixViewExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out +# OUTPUT SparseMatrixViewExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getRow_cuda SparseMatrixViewExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out +# OUTPUT SparseMatrixViewExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement_cuda SparseMatrixViewExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out +# OUTPUT SparseMatrixViewExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_addElement_cuda SparseMatrixViewExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out +# OUTPUT SparseMatrixViewExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElement_cuda SparseMatrixViewExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out +# OUTPUT SparseMatrixViewExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction_cuda SparseMatrixViewExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out +# OUTPUT SparseMatrixViewExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction_cuda SparseMatrixViewExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out +# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forRows_cuda SparseMatrixViewExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out +# OUTPUT SparseMatrixViewExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows_cuda SparseMatrixViewExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out +# OUTPUT SparseMatrixViewExample_forAllRows.out ) + +ELSE() +# ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list SparseMatrixExample_Constructor_init_list.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out +# OUTPUT SparseMatrixExample_Constructor_init_list.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out +# OUTPUT SparseMatrixExample_setElements.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths SparseMatrixExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getElementsCount SparseMatrixExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out +# OUTPUT SparseMatrixExample_getElementsCount.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getConstRow SparseMatrixExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out +# OUTPUT SparseMatrixExample_getConstRow.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getRow SparseMatrixExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out +# OUTPUT SparseMatrixExample_getRow.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out +# OUTPUT SparseMatrixExample_setElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out +# OUTPUT SparseMatrixExample_addElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getElement SparseMatrixExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out +# OUTPUT SparseMatrixExample_getElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out +# OUTPUT SparseMatrixExample_rowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction SparseMatrixExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out +# OUTPUT SparseMatrixExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_forRows SparseMatrixExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out +# OUTPUT SparseMatrixExample_forRows.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_forAllRows SparseMatrixExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out +# OUTPUT SparseMatrixExample_forAllRows.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_constructor SparseMatrixViewExample_constructor.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out +# OUTPUT SparseMatrixViewExample_constructor.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount SparseMatrixViewExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out +# OUTPUT SparseMatrixViewExample_getElementsCount.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow SparseMatrixViewExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out +# OUTPUT SparseMatrixViewExample_getConstRow.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getRow SparseMatrixViewExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out +# OUTPUT SparseMatrixViewExample_getRow.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out +# OUTPUT SparseMatrixViewExample_setElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_addElement SparseMatrixViewExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out +# OUTPUT SparseMatrixViewExample_addElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getElement SparseMatrixViewExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out +# OUTPUT SparseMatrixViewExample_getElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out +# OUTPUT SparseMatrixViewExample_rowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out +# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_forRows SparseMatrixViewExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out +# OUTPUT SparseMatrixViewExample_forRows.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows SparseMatrixViewExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out +# OUTPUT SparseMatrixViewExample_forAllRows.out ) + +ENDIF() + +#ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS +# SparseMatrixExample_Constructor_init_list.out +# SparseMatrixExample_setElements.out +# SparseMatrixExample_getCompressedRowLengths.out +# SparseMatrixExample_getElementsCount.out +# SparseMatrixExample_getConstRow.out +# SparseMatrixExample_getRow.out +# SparseMatrixExample_setElement.out +# SparseMatrixExample_addElement.out +# SparseMatrixExample_getElement.out +# SparseMatrixExample_rowsReduction.out +# SparseMatrixExample_allRowsReduction.out +# SparseMatrixExample_forRows.out +# SparseMatrixExample_forAllRows.out +# SparseMatrixViewExample_constructor.out +# SparseMatrixViewExample_getCompressedRowLengths.out +# SparseMatrixViewExample_getElementsCount.out +# SparseMatrixViewExample_getConstRow.out +# SparseMatrixViewExample_getRow.out +# SparseMatrixViewExample_setElement.out +# SparseMatrixViewExample_addElement.out +# SparseMatrixViewExample_getElement.out +# SparseMatrixViewExample_rowsReduction.out +# SparseMatrixViewExample_allRowsReduction.out +# SparseMatrixViewExample_forRows.out +# SparseMatrixViewExample_forAllRows.out +#) + diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt new file mode 100644 index 000000000..a22729e95 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -0,0 +1,283 @@ +IF( BUILD_CUDA ) + CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_1_cuda SparseMatrixExample_Constructor_init_list_1.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_1_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_1.out + OUTPUT SparseMatrixExample_Constructor_init_list_1.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out +# OUTPUT SparseMatrixExample_setElements.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths_cuda SparseMatrixExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElementsCount_cuda SparseMatrixExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out +# OUTPUT SparseMatrixExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getConstRow_cuda SparseMatrixExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out +# OUTPUT SparseMatrixExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getRow_cuda SparseMatrixExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out +# OUTPUT SparseMatrixExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement_cuda SparseMatrixExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out +# OUTPUT SparseMatrixExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement_cuda SparseMatrixExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out +# OUTPUT SparseMatrixExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElement_cuda SparseMatrixExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out +# OUTPUT SparseMatrixExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out +# OUTPUT SparseMatrixExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction_cuda SparseMatrixExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out +# OUTPUT SparseMatrixExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forRows_cuda SparseMatrixExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out +# OUTPUT SparseMatrixExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forAllRows_cuda SparseMatrixExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out +# OUTPUT SparseMatrixExample_forAllRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_constructor_cuda SparseMatrixViewExample_constructor.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out +# OUTPUT SparseMatrixViewExample_constructor.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths_cuda SparseMatrixViewExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount_cuda SparseMatrixViewExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out +# OUTPUT SparseMatrixViewExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow_cuda SparseMatrixViewExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out +# OUTPUT SparseMatrixViewExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getRow_cuda SparseMatrixViewExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out +# OUTPUT SparseMatrixViewExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement_cuda SparseMatrixViewExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out +# OUTPUT SparseMatrixViewExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_addElement_cuda SparseMatrixViewExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out +# OUTPUT SparseMatrixViewExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElement_cuda SparseMatrixViewExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out +# OUTPUT SparseMatrixViewExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction_cuda SparseMatrixViewExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out +# OUTPUT SparseMatrixViewExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction_cuda SparseMatrixViewExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out +# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forRows_cuda SparseMatrixViewExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out +# OUTPUT SparseMatrixViewExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows_cuda SparseMatrixViewExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out +# OUTPUT SparseMatrixViewExample_forAllRows.out ) + +ELSE() + ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_1 SparseMatrixExample_Constructor_init_list_1.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_1 > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_1.out + OUTPUT SparseMatrixExample_Constructor_init_list_1.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out +# OUTPUT SparseMatrixExample_setElements.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths SparseMatrixExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getElementsCount SparseMatrixExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out +# OUTPUT SparseMatrixExample_getElementsCount.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getConstRow SparseMatrixExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out +# OUTPUT SparseMatrixExample_getConstRow.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getRow SparseMatrixExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out +# OUTPUT SparseMatrixExample_getRow.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out +# OUTPUT SparseMatrixExample_setElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out +# OUTPUT SparseMatrixExample_addElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getElement SparseMatrixExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out +# OUTPUT SparseMatrixExample_getElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out +# OUTPUT SparseMatrixExample_rowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction SparseMatrixExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out +# OUTPUT SparseMatrixExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_forRows SparseMatrixExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out +# OUTPUT SparseMatrixExample_forRows.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_forAllRows SparseMatrixExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out +# OUTPUT SparseMatrixExample_forAllRows.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_constructor SparseMatrixViewExample_constructor.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out +# OUTPUT SparseMatrixViewExample_constructor.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount SparseMatrixViewExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out +# OUTPUT SparseMatrixViewExample_getElementsCount.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow SparseMatrixViewExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out +# OUTPUT SparseMatrixViewExample_getConstRow.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getRow SparseMatrixViewExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out +# OUTPUT SparseMatrixViewExample_getRow.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out +# OUTPUT SparseMatrixViewExample_setElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_addElement SparseMatrixViewExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out +# OUTPUT SparseMatrixViewExample_addElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getElement SparseMatrixViewExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out +# OUTPUT SparseMatrixViewExample_getElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out +# OUTPUT SparseMatrixViewExample_rowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out +# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_forRows SparseMatrixViewExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out +# OUTPUT SparseMatrixViewExample_forRows.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows SparseMatrixViewExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out +# OUTPUT SparseMatrixViewExample_forAllRows.out ) + +ENDIF() + +ADD_CUSTOM_TARGET( RunSparseMatricesExamples ALL DEPENDS + SparseMatrixExample_Constructor_init_list_1.out +# SparseMatrixExample_setElements.out +# SparseMatrixExample_getCompressedRowLengths.out +# SparseMatrixExample_getElementsCount.out +# SparseMatrixExample_getConstRow.out +# SparseMatrixExample_getRow.out +# SparseMatrixExample_setElement.out +# SparseMatrixExample_addElement.out +# SparseMatrixExample_getElement.out +# SparseMatrixExample_rowsReduction.out +# SparseMatrixExample_allRowsReduction.out +# SparseMatrixExample_forRows.out +# SparseMatrixExample_forAllRows.out +# SparseMatrixViewExample_constructor.out +# SparseMatrixViewExample_getCompressedRowLengths.out +# SparseMatrixViewExample_getElementsCount.out +# SparseMatrixViewExample_getConstRow.out +# SparseMatrixViewExample_getRow.out +# SparseMatrixViewExample_setElement.out +# SparseMatrixViewExample_addElement.out +# SparseMatrixViewExample_getElement.out +# SparseMatrixViewExample_rowsReduction.out +# SparseMatrixViewExample_allRowsReduction.out +# SparseMatrixViewExample_forRows.out +# SparseMatrixViewExample_forAllRows.out + +) + diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp new file mode 100644 index 000000000..e00a13418 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + + +template< typename Device > +void initializerListExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix { + { 1, 2, 3, 4, 5 } }; + + for( int row = 0; row < matrix.getRows(); i++ ) + for( int column = 0; column <= row; column++ ) + matrix.setElement( row, column, row - column + 1 ); + std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrices on CPU ... " << std::endl; + initializerListExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + initializerListExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt new file mode 100644 index 000000000..64655b0a7 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt @@ -0,0 +1,282 @@ +IF( BUILD_CUDA ) +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_cuda SparseMatrixExample_Constructor_init_list.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out +# OUTPUT SparseMatrixExample_Constructor_init_list.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out +# OUTPUT SparseMatrixExample_setElements.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths_cuda SparseMatrixExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElementsCount_cuda SparseMatrixExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out +# OUTPUT SparseMatrixExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getConstRow_cuda SparseMatrixExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out +# OUTPUT SparseMatrixExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getRow_cuda SparseMatrixExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out +# OUTPUT SparseMatrixExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement_cuda SparseMatrixExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out +# OUTPUT SparseMatrixExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement_cuda SparseMatrixExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out +# OUTPUT SparseMatrixExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElement_cuda SparseMatrixExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out +# OUTPUT SparseMatrixExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out +# OUTPUT SparseMatrixExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction_cuda SparseMatrixExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out +# OUTPUT SparseMatrixExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forRows_cuda SparseMatrixExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out +# OUTPUT SparseMatrixExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forAllRows_cuda SparseMatrixExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out +# OUTPUT SparseMatrixExample_forAllRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_constructor_cuda SparseMatrixViewExample_constructor.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out +# OUTPUT SparseMatrixViewExample_constructor.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths_cuda SparseMatrixViewExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount_cuda SparseMatrixViewExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out +# OUTPUT SparseMatrixViewExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow_cuda SparseMatrixViewExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out +# OUTPUT SparseMatrixViewExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getRow_cuda SparseMatrixViewExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out +# OUTPUT SparseMatrixViewExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement_cuda SparseMatrixViewExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out +# OUTPUT SparseMatrixViewExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_addElement_cuda SparseMatrixViewExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out +# OUTPUT SparseMatrixViewExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElement_cuda SparseMatrixViewExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out +# OUTPUT SparseMatrixViewExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction_cuda SparseMatrixViewExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out +# OUTPUT SparseMatrixViewExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction_cuda SparseMatrixViewExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out +# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forRows_cuda SparseMatrixViewExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out +# OUTPUT SparseMatrixViewExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows_cuda SparseMatrixViewExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out +# OUTPUT SparseMatrixViewExample_forAllRows.out ) + +ELSE() +# ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list SparseMatrixExample_Constructor_init_list.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out +# OUTPUT SparseMatrixExample_Constructor_init_list.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out +# OUTPUT SparseMatrixExample_setElements.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths SparseMatrixExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getElementsCount SparseMatrixExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out +# OUTPUT SparseMatrixExample_getElementsCount.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getConstRow SparseMatrixExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out +# OUTPUT SparseMatrixExample_getConstRow.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getRow SparseMatrixExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out +# OUTPUT SparseMatrixExample_getRow.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out +# OUTPUT SparseMatrixExample_setElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out +# OUTPUT SparseMatrixExample_addElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_getElement SparseMatrixExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out +# OUTPUT SparseMatrixExample_getElement.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out +# OUTPUT SparseMatrixExample_rowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction SparseMatrixExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out +# OUTPUT SparseMatrixExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_forRows SparseMatrixExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out +# OUTPUT SparseMatrixExample_forRows.out ) + +# ADD_EXECUTABLE( SparseMatrixExample_forAllRows SparseMatrixExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out +# OUTPUT SparseMatrixExample_forAllRows.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_constructor SparseMatrixViewExample_constructor.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out +# OUTPUT SparseMatrixViewExample_constructor.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out +# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount SparseMatrixViewExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out +# OUTPUT SparseMatrixViewExample_getElementsCount.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow SparseMatrixViewExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out +# OUTPUT SparseMatrixViewExample_getConstRow.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getRow SparseMatrixViewExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out +# OUTPUT SparseMatrixViewExample_getRow.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out +# OUTPUT SparseMatrixViewExample_setElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_addElement SparseMatrixViewExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out +# OUTPUT SparseMatrixViewExample_addElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_getElement SparseMatrixViewExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out +# OUTPUT SparseMatrixViewExample_getElement.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out +# OUTPUT SparseMatrixViewExample_rowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out +# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_forRows SparseMatrixViewExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out +# OUTPUT SparseMatrixViewExample_forRows.out ) + +# ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows SparseMatrixViewExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out +# OUTPUT SparseMatrixViewExample_forAllRows.out ) + +ENDIF() + +#ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS +# SparseMatrixExample_Constructor_init_list.out +# SparseMatrixExample_setElements.out +# SparseMatrixExample_getCompressedRowLengths.out +# SparseMatrixExample_getElementsCount.out +# SparseMatrixExample_getConstRow.out +# SparseMatrixExample_getRow.out +# SparseMatrixExample_setElement.out +# SparseMatrixExample_addElement.out +# SparseMatrixExample_getElement.out +# SparseMatrixExample_rowsReduction.out +# SparseMatrixExample_allRowsReduction.out +# SparseMatrixExample_forRows.out +# SparseMatrixExample_forAllRows.out +# SparseMatrixViewExample_constructor.out +# SparseMatrixViewExample_getCompressedRowLengths.out +# SparseMatrixViewExample_getElementsCount.out +# SparseMatrixViewExample_getConstRow.out +# SparseMatrixViewExample_getRow.out +# SparseMatrixViewExample_setElement.out +# SparseMatrixViewExample_addElement.out +# SparseMatrixViewExample_getElement.out +# SparseMatrixViewExample_rowsReduction.out +# SparseMatrixViewExample_allRowsReduction.out +# SparseMatrixViewExample_forRows.out +# SparseMatrixViewExample_forAllRows.out +#) + diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 215c2ec2e..5e8892abe 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -35,7 +35,7 @@ template< typename Real = double, typename Index = int, ElementsOrganization Organization = Containers::Segments::DefaultElementsOrganization< Device >::getOrganization(), typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real > > -class DenseMatrix : public Matrix< Real, Device, Index > +class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > { protected: using BaseType = Matrix< Real, Device, Index, RealAllocator >; @@ -88,7 +88,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > using ConstViewType = DenseMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; /** - * \brief Type for accessing matrix row. + * \brief Type for accessing matrix rows. */ using RowView = DenseMatrixRowView< SegmentViewType, ValuesViewType >; @@ -103,17 +103,35 @@ class DenseMatrix : public Matrix< Real, Device, Index > using Self = DenseMatrix< _Real, _Device, _Index, _Organization, _RealAllocator >; /** - * \brief Constructor without parameters. + * \brief Constructor only with values allocator. + * + * \param allocator is used for allocation of matrix elements values. + */ + DenseMatrix( const RealAllocatorType& allocator = RealAllocatorType() ); + + /** + * \brief Copy constructor. + * + * \param matrix is the source matrix + */ + DenseMatrix( const DenseMatrix& matrix ) = default; + + /** + * \brief Move constructor. + * + * \param matrix is the source matrix */ - DenseMatrix(); + DenseMatrix( DenseMatrix&& matrix ) = default; /** * \brief Constructor with matrix dimensions. * * \param rows is number of matrix rows. * \param columns is number of matrix columns. + * \param allocator is used for allocation of matrix elements values. */ - DenseMatrix( const IndexType rows, const IndexType columns ); + DenseMatrix( const IndexType rows, const IndexType columns, + const RealAllocatorType& allocator = RealAllocatorType() ); /** * \brief Constructor with 2D initializer list. @@ -124,6 +142,7 @@ class DenseMatrix : public Matrix< Real, Device, Index > * * \param data is a initializer list of initializer lists representing * list of matrix rows. + * \param allocator is used for allocation of matrix elements values. * * \par Example * \include Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cpp @@ -131,7 +150,8 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \include DenseMatrixExample_Constructor_init_list.out */ template< typename Value > - DenseMatrix( std::initializer_list< std::initializer_list< Value > > data ); + DenseMatrix( std::initializer_list< std::initializer_list< Value > > data, + const RealAllocatorType& allocator = RealAllocatorType() ); /** * \brief Returns a modifiable view of the dense matrix. diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index cbe283b2a..995a369f6 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -22,7 +22,9 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > -DenseMatrix< Real, Device, Index, Organization, RealAllocator >::DenseMatrix() +DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: +DenseMatrix( const RealAllocatorType& allocator ) +: Matrix< Real, Device, Index, RealAllocator >( allocator ) { } @@ -32,7 +34,9 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -DenseMatrix( const IndexType rows, const IndexType columns ) +DenseMatrix( const IndexType rows, const IndexType columns, + const RealAllocatorType& allocator ) +: Matrix< Real, Device, Index, RealAllocator >( allocator ) { this->setDimensions( rows, columns ); } @@ -44,7 +48,9 @@ template< typename Real, typename RealAllocator > template< typename Value > DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -DenseMatrix( std::initializer_list< std::initializer_list< Value > > data ) +DenseMatrix( std::initializer_list< std::initializer_list< Value > > data, + const RealAllocatorType& allocator ) +: Matrix< Real, Device, Index, RealAllocator >( allocator ) { this->setElements( data ); } diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index b593be656..33c746f01 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -40,6 +40,9 @@ DenseMatrixView( const IndexType rows, const ValuesViewType& values ) : MatrixView< Real, Device, Index >( rows, columns, values ) { + if( values.getSize() != this->getAllocatedElementsCount() ) + throw( std::logic_error( "Number of matrix elements does not agree with matrix dimensions." ) ); + SegmentsType a( rows, columns ); segments = a.getView(); } diff --git a/src/TNL/Matrices/MatrixType.h b/src/TNL/Matrices/MatrixType.h index afefd8d06..5842759ea 100644 --- a/src/TNL/Matrices/MatrixType.h +++ b/src/TNL/Matrices/MatrixType.h @@ -13,6 +13,11 @@ namespace TNL { namespace Matrices { +/** + * \brief Structure for specifying type of sparse matrix. + * + * It is used for specification of \ref SparseMatrix type. + */ template< bool Symmetric, bool Binary > struct MatrixType @@ -22,14 +27,51 @@ struct MatrixType static constexpr bool isBinary() { return Binary; } }; +/** + * \brief General non-symmetric matrix type. + * + * It is used for specification of \ref SparseMatrix type. + */ struct GeneralMatrix : MatrixType< false, false > {}; +/** + * \brief Symmetric matrix type. + * + * Symmetric matrix stores only lower part of the matrix and its diagonal. The + * upper part is reconstructed on the fly. + * It is used for specification of \ref SparseMatrix type. + */ struct SymmetricMatrix : MatrixType< true, false > {}; +/** + * \brief Binary matrix type. + * + * Binary matrix does not store explictly values of matrix elements and thus + * it reduces memory consumption. + * It is used for specification of \ref SparseMatrix type. + */ struct BinaryMatrix : MatrixType< false, true > {}; +/** + * \brief Symmetric and binary matrix type. + * + * Symmetric matrix stores only lower part of the matrix and its diagonal. The + * upper part is reconstructed on the fly. + * Binary matrix does not store explictly values of matrix elements and thus + * it reduces memory consumption. + * It is used for specification of \ref SparseMatrix type. + */ struct BinarySymmetricMatrix : MatrixType< true, true > {}; +/** + * \brief Symmetric and binary matrix type. + * + * Symmetric matrix stores only lower part of the matrix and its diagonal. The + * upper part is reconstructed on the fly. + * Binary matrix does not store explictly values of matrix elements and thus + * it reduces memory consumption. + * It is used for specification of \ref SparseMatrix type. + */ struct SymmetricBinaryMatrix : MatrixType< true, true > {}; } // namespace Matrices diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 598643137..91e409164 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -22,6 +22,23 @@ namespace TNL { namespace Matrices { +/** + * \brief Implementation of sparse matrix, i.e. matrix storing only non-zero elements. + * + * \tparam Real is a type of matrix elements. + * \tparam Device is a device where the matrix is allocated. + * \tparam Index is a type for indexing of the matrix elements. + * \tparam MatrixType specifies the type of matrix - its symmetry or binarity. See \ref MatrixType. + * Both symmetric and binary matrix types reduces memory consumption. Binary matrix does not store + * the matrix values explicitly since the non-zero elements can have only value equal to one. Symmetric + * matrices store only lower part of the matrix and its diagonal. The upper part is reconstructed on the fly. + * GeneralMatrix with no symmetry is used by default. + * \tparam Segments is a structure representing the sparse matrix format. Depending on the pattern of the non-zero elements + * different matrix formats can perform differently especially on GPUs. By default \ref CSR format is used. See also + * \ref Ellpack, \ref SlicedEllpack, \ref ChunkedEllpack or \ref BiEllpack. + * \tparam RealAllocator is allocator for the matrix elements values. + * \tparam IndexAllocator is allocator for the matrix elements column indexes. + */ template< typename Real, typename Device = Devices::Host, typename Index = int, @@ -31,43 +48,102 @@ template< typename Real, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index > > class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > { + static_assert( + ! MatrixType::isSymmetric() || + ! std::is_same< Device, Devices::Cuda >::value || + ( std::is_same< Real, float >::value || std::is_same< Real, double >::value || std::is_same< Real, int >::value || std::is_same< Real, long long int >::value ), + "Given Real type is not supported by atomic operations on GPU which are necessary for symmetric operations." ); + public: - static constexpr bool isSymmetric() { return MatrixType::isSymmetric(); }; - static constexpr bool isBinary() { return MatrixType::isBinary(); }; - static_assert( - ! isSymmetric() || - ! std::is_same< Device, Devices::Cuda >::value || - ( std::is_same< Real, float >::value || std::is_same< Real, double >::value || std::is_same< Real, int >::value || std::is_same< Real, long long int >::value ), - "Given Real type is not supported by atomic operations on GPU which are necessary for symmetric operations." ); + // Supporting types - they are not important for the user + using BaseType = Matrix< Real, Device, Index, RealAllocator >; + using ValuesVectorType = typename Matrix< Real, Device, Index, RealAllocator >::ValuesVectorType; + using ValuesViewType = typename ValuesVectorType::ViewType; + using ConstValuesViewType = typename ValuesViewType::ConstViewType; + using ColumnsIndexesVectorType = Containers::Vector< Index, Device, Index, IndexAllocator >; + using ColumnsIndexesViewType = typename ColumnsIndexesVectorType::ViewType; + using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType; + using RowsCapacitiesType = Containers::Vector< Index, Device, Index, IndexAllocator >; + using RowsCapacitiesView = Containers::VectorView< Index, Device, Index >; + using ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType; + /** + * \brief The type of matrix elements. + */ using RealType = Real; + + /** + * \brief The device where the matrix is allocated. + */ + using DeviceType = Device; + + /** + * \brief The type used for matrix elements indexing. + */ + using IndexType = Index; + + /** + * \brief Templated type of segments, i.e. sparse matrix format. + */ template< typename Device_, typename Index_, typename IndexAllocator_ > using SegmentsTemplate = Segments< Device_, Index_, IndexAllocator_ >; + + /** + * \brief Type of segments used by this matrix. It represents the sparse matrix format. + */ using SegmentsType = Segments< Device, Index, IndexAllocator >; + + /** + * \brief Templated view type of segments, i.e. sparse matrix format. + */ template< typename Device_, typename Index_ > using SegmentsViewTemplate = typename SegmentsType::template ViewTemplate< Device_, Index >; + + /** + * \brief Type of segments view used by the related matrix view. It represents the sparse matrix format. + */ using SegmentsViewType = typename SegmentsType::ViewType; - using SegmentViewType = typename SegmentsType::SegmentViewType; - using DeviceType = Device; - using IndexType = Index; + + /** + * \brief The allocator for matrix elements values. + */ using RealAllocatorType = RealAllocator; + + /** + * \brief The allocator for matrix elements column indexes. + */ using IndexAllocatorType = IndexAllocator; - using BaseType = Matrix< Real, Device, Index, RealAllocator >; - using RowsCapacitiesType = Containers::Vector< IndexType, DeviceType, IndexType, IndexAllocatorType >; - using RowsCapacitiesView = Containers::VectorView< IndexType, DeviceType, IndexType >; - using ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType; - using ValuesVectorType = typename Matrix< Real, Device, Index, RealAllocator >::ValuesVectorType; - using ValuesViewType = typename ValuesVectorType::ViewType; - using ConstValuesViewType = typename ValuesViewType::ConstViewType; - using ColumnsIndexesVectorType = Containers::Vector< IndexType, DeviceType, IndexType, IndexAllocatorType >; - using ColumnsIndexesViewType = typename ColumnsIndexesVectorType::ViewType; - using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType; + + /** + * \brief Type of related matrix view. + * + * See \ref SparseMatrixView. + */ using ViewType = SparseMatrixView< Real, Device, Index, MatrixType, SegmentsViewTemplate >; + + /** + * \brief Matrix view type for constant instances. + * + * See \ref SparseMatrixView. + */ using ConstViewType = SparseMatrixView< typename std::add_const< Real >::type, Device, Index, MatrixType, SegmentsViewTemplate >; - using RowView = SparseMatrixRowView< SegmentViewType, ValuesViewType, ColumnsIndexesViewType, isBinary() >; + + //using SegmentViewType = typename SegmentsType::SegmentViewType; + + /** + * \brief Type for accessing matrix rows. + */ + using RowView = SparseMatrixRowView< typename SegmentsType::SegmentViewType, ValuesViewType, ColumnsIndexesViewType, MatrixType::isBinary() >; + + /** + * \brief Type for accessing constant matrix rows. + */ using ConstRowView = typename RowView::ConstViewType; + /** + * \brief Helper type for getting self type or its modifications. + */ template< typename _Real = Real, typename _Device = Device, typename _Index = Index, @@ -77,39 +153,87 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > typename _IndexAllocator = typename Allocators::Default< _Device >::template Allocator< _Index > > using Self = SparseMatrix< _Real, _Device, _Index, _MatrixType, _Segments, _RealAllocator, _IndexAllocator >; - // TODO: remove this - it is here only for compatibility with original matrix implementation - typedef Containers::Vector< IndexType, DeviceType, IndexType > CompressedRowLengthsVector; - typedef Containers::VectorView< IndexType, DeviceType, IndexType > CompressedRowLengthsVectorView; - typedef typename CompressedRowLengthsVectorView::ConstViewType ConstCompressedRowLengthsVectorView; + /** + * \brief Test of symmetric matrix type. + * + * \return \e true if the matrix is stored as symmetric and \e false otherwise. + */ + static constexpr bool isSymmetric() { return MatrixType::isSymmetric(); }; + /** + * \brief Test of binary matrix type. + * + * \return \e true if the matrix is stored as binary and \e false otherwise. + */ + static constexpr bool isBinary() { return MatrixType::isBinary(); }; + + /** + * \brief Constructor only with values and column indexes allocators. + * + * \param realAllocator is used for allocation of matrix elements values. + * \param indexAllocator is used for allocation of matrix elements column indexes. + */ SparseMatrix( const RealAllocatorType& realAllocator = RealAllocatorType(), const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); - SparseMatrix( const SparseMatrix& m ) = default; + /** + * \brief Copy constructor. + * + * \param matrix is the source matrix + */ + SparseMatrix( const SparseMatrix& matrix1 ) = default; - SparseMatrix( SparseMatrix&& m ) = default; + /** + * \brief Move constructor. + * + * \param matrix is the source matrix + */ + SparseMatrix( SparseMatrix&& matrix ) = default; + /** + * \brief Constructor with matrix dimensions. + * + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + * \param realAllocator is used for allocation of matrix elements values. + * \param indexAllocator is used for allocation of matrix elements column indexes. + */ SparseMatrix( const IndexType rows, const IndexType columns, const RealAllocatorType& realAllocator = RealAllocatorType(), const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); - SparseMatrix( const std::initializer_list< IndexType >& rowCapacities, - const IndexType columns, - const RealAllocatorType& realAllocator = RealAllocatorType(), - const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); + /** + * \brief Constructor with matrix rows capacities and number of columns. + * + * The number of matrix rows is given by the size of \e rowCapacities list. + * + * \tparam ListIndex is the initializer list values type. + * \param rowCapacities is a list telling how many matrix elements must be + * allocated in each row. + * \param columns is the number of matrix columns. + * \param realAllocator is used for allocation of matrix elements values. + * \param indexAllocator is used for allocation of matrix elements column indexes. + */ + template< typename ListIndex > + explicit SparseMatrix( const std::initializer_list< ListIndex >& rowCapacities, + const IndexType columns, + const RealAllocatorType& realAllocator = RealAllocatorType(), + const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); - SparseMatrix( const IndexType rows, - const IndexType columns, - const std::initializer_list< std::tuple< IndexType, IndexType, RealType > >& data, - const RealAllocatorType& realAllocator = RealAllocatorType(), - const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); + explicit SparseMatrix( const IndexType rows, + const IndexType columns, + const std::initializer_list< std::tuple< IndexType, IndexType, RealType > >& data, + const RealAllocatorType& realAllocator = RealAllocatorType(), + const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); template< typename MapIndex, typename MapValue > explicit SparseMatrix( const IndexType rows, const IndexType columns, - const std::map< std::pair< MapIndex, MapIndex > , MapValue >& map ); + const std::map< std::pair< MapIndex, MapIndex >, MapValue >& map, + const RealAllocatorType& realAllocator = RealAllocatorType(), + const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); virtual void setDimensions( const IndexType rows, const IndexType columns ) override; @@ -267,6 +391,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > const SegmentsType& getSegments() const; + // TODO: restore it and also in Matrix // protected: diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index d13537cef..43a4801a4 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -57,14 +57,15 @@ template< typename Real, template< typename, typename, typename > class Segments, typename RealAllocator, typename IndexAllocator > + template< typename ListIndex > SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: -SparseMatrix( const std::initializer_list< IndexType >& rowCapacities, +SparseMatrix( const std::initializer_list< ListIndex >& rowCapacities, const IndexType columns, const RealAllocatorType& realAllocator, const IndexAllocatorType& indexAllocator ) : BaseType( rowCapacities.size(), columns, realAllocator ), columnIndexes( indexAllocator ) { - this->setCompressedRowLengths( RowsCapacitiesType( rowCapacities ) ); + this->setRowCapacities( RowsCapacitiesType( rowCapacities ) ); } template< typename Real, @@ -97,7 +98,10 @@ template< typename Real, SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: SparseMatrix( const IndexType rows, const IndexType columns, - const std::map< std::pair< MapIndex, MapIndex > , MapValue >& map ) + const std::map< std::pair< MapIndex, MapIndex > , MapValue >& map, + const RealAllocatorType& realAllocator, + const IndexAllocatorType& indexAllocator ) +: BaseType( rows, columns, realAllocator ), columnIndexes( indexAllocator ) { this->setDimensions( rows, columns ); this->setElements( map ); @@ -446,23 +450,6 @@ getElement( const IndexType row, return this->view.getElement( row, column ); } -/*template< typename Real, - typename Device, - typename Index, - typename MatrixType, - template< typename, typename, typename > class Segments, - typename RealAllocator, - typename IndexAllocator > - template< typename Vector > -__cuda_callable__ -typename Vector::RealType -SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: -rowVectorProduct( const IndexType row, - const Vector& vector ) const -{ - return this->view.rowVectorProduct( row, vector ); -}*/ - template< typename Real, typename Device, typename Index, -- GitLab From 96484f79f88a28b82ef0caaa067f0aac34eda0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Mon, 25 May 2020 14:50:50 +0200 Subject: [PATCH 12/38] Writting documentation on sparse matrix. --- .../Matrices/SparseMatrix/CMakeLists.txt | 22 ++++++++ ...eMatrixExample_Constructor_init_list_1.cpp | 4 +- ...seMatrixExample_Constructor_init_list_1.cu | 1 + ...eMatrixExample_Constructor_init_list_2.cpp | 28 ++++++++++ ...seMatrixExample_Constructor_init_list_2.cu | 1 + ...parseMatrixExample_Constructor_std_map.cpp | 38 ++++++++++++++ ...SparseMatrixExample_Constructor_std_map.cu | 1 + src/TNL/Matrices/DenseMatrixRowView.h | 5 ++ src/TNL/Matrices/SparseMatrix.h | 51 +++++++++++++++++-- src/TNL/Matrices/SparseMatrix.hpp | 2 +- src/TNL/Matrices/SparseMatrixView.h | 4 +- .../Linear/Preconditioners/Diagonal_impl.h | 2 +- 12 files changed, 149 insertions(+), 10 deletions(-) create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cu diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt index a22729e95..8f17c5799 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -4,6 +4,16 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_1.out OUTPUT SparseMatrixExample_Constructor_init_list_1.out ) + CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_2_cuda SparseMatrixExample_Constructor_init_list_2.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_2_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_2.out + OUTPUT SparseMatrixExample_Constructor_init_list_2.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_std_map_cuda SparseMatrixExample_Constructor_std_map.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_std_map_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_std_map.out + OUTPUT SparseMatrixExample_Constructor_std_map.out ) + # CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) # ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > # ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out @@ -130,6 +140,16 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_1.out OUTPUT SparseMatrixExample_Constructor_init_list_1.out ) + ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_2 SparseMatrixExample_Constructor_init_list_2.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_2 > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_2.out + OUTPUT SparseMatrixExample_Constructor_init_list_2.out ) + + ADD_EXECUTABLE( SparseMatrixExample_Constructor_std_map SparseMatrixExample_Constructor_std_map.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_std_map > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_std_map.out + OUTPUT SparseMatrixExample_Constructor_std_map.out ) + # ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) # ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > # ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out @@ -254,6 +274,8 @@ ENDIF() ADD_CUSTOM_TARGET( RunSparseMatricesExamples ALL DEPENDS SparseMatrixExample_Constructor_init_list_1.out + SparseMatrixExample_Constructor_init_list_2.out + SparseMatrixExample_Constructor_std_map.out # SparseMatrixExample_setElements.out # SparseMatrixExample_getCompressedRowLengths.out # SparseMatrixExample_getElementsCount.out diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp index e00a13418..7b45c0a18 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp @@ -7,9 +7,9 @@ template< typename Device > void initializerListExample() { TNL::Matrices::SparseMatrix< double, Device > matrix { - { 1, 2, 3, 4, 5 } }; + { 1, 2, 3, 4, 5 }, 6 }; - for( int row = 0; row < matrix.getRows(); i++ ) + for( int row = 0; row < matrix.getRows(); row++ ) for( int column = 0; column <= row; column++ ) matrix.setElement( row, column, row - column + 1 ); std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cu new file mode 120000 index 000000000..4afbecd7f --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cu @@ -0,0 +1 @@ +SparseMatrixExample_Constructor_init_list_1.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cpp new file mode 100644 index 000000000..d1e341a5c --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + + +template< typename Device > +void initializerListExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { + { 0, 0, 2.0 }, + { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, + { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, + { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, + { 4, 4, 2.0 } } ); + + std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrices on CPU ... " << std::endl; + initializerListExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + initializerListExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cu new file mode 120000 index 000000000..112624ab3 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cu @@ -0,0 +1 @@ +SparseMatrixExample_Constructor_init_list_2.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cpp new file mode 100644 index 000000000..8f64d0e9b --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void initializerListExample() +{ + std::map< std::pair< int, int >, double > map; + map.insert( std::make_pair( std::make_pair( 0, 0 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 0 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 1 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 2 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 1 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 2 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 3 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 2 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 3 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 4 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 4, 4 ), 2.0 ) ); + + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, map ); + + std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrices on CPU ... " << std::endl; + initializerListExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + initializerListExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cu new file mode 120000 index 000000000..3f08e48c7 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cu @@ -0,0 +1 @@ +SparseMatrixExample_Constructor_std_map.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/DenseMatrixRowView.h b/src/TNL/Matrices/DenseMatrixRowView.h index 78fecd0f7..996c5a6e7 100644 --- a/src/TNL/Matrices/DenseMatrixRowView.h +++ b/src/TNL/Matrices/DenseMatrixRowView.h @@ -25,6 +25,11 @@ namespace TNL { * \include Matrices/DenseMatrixExample_getRow.cpp * \par Output * \include DenseMatrixExample_getRow.out + * + * \par Example + * \include Matrices/DenseMatrixViewExample_getRow.cpp + * \par Output + * \include DenseMatrixViewExample_getRow.out */ template< typename SegmentView, typename ValuesView > diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 91e409164..0378d96bf 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -127,7 +127,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * * See \ref SparseMatrixView. */ - using ConstViewType = SparseMatrixView< typename std::add_const< Real >::type, Device, Index, MatrixType, SegmentsViewTemplate >; + using ConstViewType = SparseMatrixView< std::add_const_t< Real >, Device, Index, MatrixType, SegmentsViewTemplate >; //using SegmentViewType = typename SegmentsType::SegmentViewType; @@ -214,6 +214,11 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param columns is the number of matrix columns. * \param realAllocator is used for allocation of matrix elements values. * \param indexAllocator is used for allocation of matrix elements column indexes. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp + * \par Output + * \include SparseMatrixExample_Constructor_init_list_1.out */ template< typename ListIndex > explicit SparseMatrix( const std::initializer_list< ListIndex >& rowCapacities, @@ -221,12 +226,49 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > const RealAllocatorType& realAllocator = RealAllocatorType(), const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); + /** + * \brief Constructor with matrix dimensions and data in initializer list. + * + * The matrix elements values are given as a list \e data of triples: + * { { row1, column1, value1 }, + * { row2, column2, value2 }, + * ... }. + * + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + * \param data is a list of matrix elements values. + * \param realAllocator is used for allocation of matrix elements values. + * \param indexAllocator is used for allocation of matrix elements column indexes. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cpp + * \par Output + * \include SparseMatrixExample_Constructor_init_list_2.out + */ explicit SparseMatrix( const IndexType rows, const IndexType columns, const std::initializer_list< std::tuple< IndexType, IndexType, RealType > >& data, const RealAllocatorType& realAllocator = RealAllocatorType(), const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); + /** + * \brief Constructor with matrix dimensions and data in std::map. + * + * The matrix elements values are given as a map \e data where keys are + * std::pair of matrix coordinates ( {row, column} ) and value is the + * matrix element value. + * + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + * \param data is std::map containing matrix elements values. + * \param realAllocator is used for allocation of matrix elements values. + * \param indexAllocator is used for allocation of matrix elements column indexes. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cpp + * \par Output + * \include SparseMatrixExample_Constructor_std_map.out + */ template< typename MapIndex, typename MapValue > explicit SparseMatrix( const IndexType rows, @@ -235,10 +277,8 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > const RealAllocatorType& realAllocator = RealAllocatorType(), const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); - virtual void setDimensions( const IndexType rows, - const IndexType columns ) override; - ViewType getView() const; // TODO: remove const + ViewType getView(); // TODO: remove const ConstViewType getConstView() const; @@ -246,6 +286,9 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > virtual String getSerializationTypeVirtual() const; + virtual void setDimensions( const IndexType rows, + const IndexType columns ) override; + template< typename RowsCapacitiesVector > void setRowCapacities( const RowsCapacitiesVector& rowCapacities ); diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index 43a4801a4..d16c12f95 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -133,7 +133,7 @@ template< typename Real, typename IndexAllocator > auto SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: -getView() const -> ViewType +getView() -> ViewType { return ViewType( this->getRows(), this->getColumns(), diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index 6bba7d414..606d20659 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -44,8 +44,8 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > using ConstValuesViewType = typename ValuesViewType::ConstViewType; using ColumnsIndexesViewType = Containers::VectorView< IndexType, DeviceType, IndexType >; using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType; - using ViewType = SparseMatrixView< typename std::remove_const< Real >::type, Device, Index, MatrixType, SegmentsViewTemplate >; - using ConstViewType = SparseMatrixView< typename std::add_const< Real >::type, Device, Index, MatrixType, SegmentsViewTemplate >; + using ViewType = SparseMatrixView< std::remove_const_t< Real >, Device, Index, MatrixType, SegmentsViewTemplate >; + using ConstViewType = SparseMatrixView< std::add_const_t< Real >, Device, std::add_const_t< Index >, MatrixType, SegmentsViewTemplate >; using RowView = SparseMatrixRowView< SegmentViewType, ValuesViewType, ColumnsIndexesViewType, isBinary() >; using ConstRowView = typename RowView::ConstViewType; diff --git a/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h b/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h index 104768ef7..bbf332ef7 100644 --- a/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h +++ b/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h @@ -33,7 +33,7 @@ update( const MatrixPointer& matrixPointer ) VectorViewType diag_view( diagonal ); - const auto kernel_matrix = matrixPointer->getView(); + const auto kernel_matrix = matrixPointer->getConstView(); // TODO: Rewrite this with SparseMatrix::forAllRows auto kernel = [=] __cuda_callable__ ( IndexType i ) mutable -- GitLab From fca6741978df5891e35a8bbcee2280f2e217e1a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Mon, 25 May 2020 20:08:01 +0200 Subject: [PATCH 13/38] Writting sparse matrix documentation. --- .../Matrices/SparseMatrix/CMakeLists.txt | 71 ++++++--- ...eMatrixExample_Constructor_init_list_1.cpp | 3 +- ...eMatrixExample_Constructor_init_list_2.cpp | 15 +- ...eMatrixExample_getCompressedRowLengths.cpp | 34 ++++ ...seMatrixExample_getCompressedRowLengths.cu | 1 + ...arseMatrixExample_getSerializationType.cpp | 23 +++ ...parseMatrixExample_getSerializationType.cu | 1 + .../SparseMatrixExample_setElements.cpp | 29 ++++ .../SparseMatrixExample_setElements.cu | 1 + .../SparseMatrixExample_setElements_map.cpp | 39 +++++ .../SparseMatrixExample_setElements_map.cu | 1 + .../SparseMatrixExample_setRowCapacities.cpp | 29 ++++ .../SparseMatrixExample_setRowCapacities.cu | 1 + src/Benchmarks/SpMV/spmv-legacy.h | 4 +- src/TNL/Matrices/DenseMatrix.h | 18 +-- src/TNL/Matrices/Matrix.h | 2 +- src/TNL/Matrices/Matrix.hpp | 2 +- src/TNL/Matrices/MatrixType.h | 12 ++ src/TNL/Matrices/SparseMatrix.h | 147 +++++++++++++++++- src/TNL/Matrices/SparseMatrix.hpp | 79 +++++----- src/TNL/Matrices/SparseMatrixView.hpp | 7 +- .../Linear/Preconditioners/Diagonal_impl.h | 2 +- .../Matrices/BinarySparseMatrixTest.hpp | 58 ------- .../Matrices/BinarySparseMatrixTest_CSR.h | 8 - .../Matrices/BinarySparseMatrixTest_Ellpack.h | 8 - .../BinarySparseMatrixTest_SlicedEllpack.h | 7 - src/UnitTests/Matrices/SparseMatrixTest.h | 7 - src/UnitTests/Matrices/SparseMatrixTest.hpp | 62 +------- .../Matrices/SymmetricSparseMatrixTest.h | 4 +- .../Matrices/SymmetricSparseMatrixTest.hpp | 4 +- 30 files changed, 437 insertions(+), 242 deletions(-) create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getCompressedRowLengths.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getCompressedRowLengths.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cu diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt index 8f17c5799..573437912 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -14,15 +14,30 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_std_map.out OUTPUT SparseMatrixExample_Constructor_std_map.out ) -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out -# OUTPUT SparseMatrixExample_setElements.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths_cuda SparseMatrixExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) + CUDA_ADD_EXECUTABLE( SparseMatrixExample_getSerializationType_cuda SparseMatrixExample_getSerializationType.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getSerializationType_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getSerializationType.out + OUTPUT SparseMatrixExample_getSerializationType.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_setRowCapacities_cuda SparseMatrixExample_setRowCapacities.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setRowCapacities_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setRowCapacities.out + OUTPUT SparseMatrixExample_setRowCapacities.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out + OUTPUT SparseMatrixExample_setElements.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_map_cuda SparseMatrixExample_setElements_map.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_map_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements_map.out + OUTPUT SparseMatrixExample_setElements_map.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths_cuda SparseMatrixExample_getCompressedRowLengths.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out + OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) # CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElementsCount_cuda SparseMatrixExample_getElementsCount.cu ) # ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount_cuda > @@ -150,15 +165,30 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_std_map.out OUTPUT SparseMatrixExample_Constructor_std_map.out ) -# ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out -# OUTPUT SparseMatrixExample_setElements.out ) + ADD_EXECUTABLE( SparseMatrixExample_getSerializationType SparseMatrixExample_getSerializationType.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getSerializationType > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getSerializationType.out + OUTPUT SparseMatrixExample_getSerializationType.out ) + + ADD_EXECUTABLE( SparseMatrixExample_setRowCapacities SparseMatrixExample_setRowCapacities.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setRowCapacities > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setRowCapacities.out + OUTPUT SparseMatrixExample_setRowCapacities.out ) + + ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out + OUTPUT SparseMatrixExample_setElements.out ) + + ADD_EXECUTABLE( SparseMatrixExample_setElements_map SparseMatrixExample_setElements_map.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_map > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements_map.out + OUTPUT SparseMatrixExample_setElements_map.out ) -# ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths SparseMatrixExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) + ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths SparseMatrixExample_getCompressedRowLengths.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out + OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) # ADD_EXECUTABLE( SparseMatrixExample_getElementsCount SparseMatrixExample_getElementsCount.cpp ) # ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount > @@ -276,8 +306,11 @@ ADD_CUSTOM_TARGET( RunSparseMatricesExamples ALL DEPENDS SparseMatrixExample_Constructor_init_list_1.out SparseMatrixExample_Constructor_init_list_2.out SparseMatrixExample_Constructor_std_map.out -# SparseMatrixExample_setElements.out -# SparseMatrixExample_getCompressedRowLengths.out + SparseMatrixExample_getSerializationType.out + SparseMatrixExample_setRowCapacities.out + SparseMatrixExample_setElements.out + SparseMatrixExample_setElements_map.out + SparseMatrixExample_getCompressedRowLengths.out # SparseMatrixExample_getElementsCount.out # SparseMatrixExample_getConstRow.out # SparseMatrixExample_getRow.out diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp index 7b45c0a18..92524b173 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_1.cpp @@ -7,7 +7,8 @@ template< typename Device > void initializerListExample() { TNL::Matrices::SparseMatrix< double, Device > matrix { - { 1, 2, 3, 4, 5 }, 6 }; + { 1, 2, 3, 4, 5 }, // row capacities + 6 }; // number of matrix columns for( int row = 0; row < matrix.getRows(); row++ ) for( int column = 0; column <= row; column++ ) diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cpp index d1e341a5c..e14f06188 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cpp @@ -6,12 +6,15 @@ template< typename Device > void initializerListExample() { - TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { - { 0, 0, 2.0 }, - { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, - { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, - { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, - { 4, 4, 2.0 } } ); + TNL::Matrices::SparseMatrix< double, Device > matrix ( + 5, // number of matrix rows + 5, // number of matrix columns + { // matrix elements definition + { 0, 0, 2.0 }, + { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, + { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, + { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, + { 4, 4, 2.0 } } ); std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; } diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getCompressedRowLengths.cpp new file mode 100644 index 000000000..e56abe484 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getCompressedRowLengths.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +template< typename Device > +void getCompressedRowLengthsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > triangularMatrix( 5, 5 ); + triangularMatrix.setElements( { + { 0, 0, 1 }, + { 1, 0, 2 }, { 1, 1, 3 }, + { 2, 0, 4 }, { 2, 1, 5 }, { 2, 2, 6 }, + { 3, 0, 7 }, { 3, 1, 8 }, { 3, 2, 9 }, { 3, 3, 10 }, + { 4, 0, 11 }, { 4, 1, 12 }, { 4, 2, 13 }, { 4, 3, 14 }, { 4, 4, 15 } } ); + + std::cout << triangularMatrix << std::endl; + + TNL::Containers::Vector< int, Device > rowLengths; + 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 HAVE_CUDA + std::cout << "Getting compressed row lengths on CUDA device: " << std::endl; + getCompressedRowLengthsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getCompressedRowLengths.cu new file mode 120000 index 000000000..8fc20b77f --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getCompressedRowLengths.cu @@ -0,0 +1 @@ +SparseMatrixExample_getCompressedRowLengths.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp new file mode 100644 index 000000000..35dbf0406 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + + +template< typename Device > +void getSerializationTypeExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix; + + std::cout << "Matrix type is: " << matrix.getSerializationType(); +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get serialization type on CPU ... " << std::endl; + getSerializationTypeExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get serialization type on CUDA GPU ... " << std::endl; + getSerializationTypeExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cu new file mode 120000 index 000000000..a042183a0 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cu @@ -0,0 +1 @@ +SparseMatrixExample_getSerializationType.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements.cpp new file mode 100644 index 000000000..ff0c0bde8 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +template< typename Device > +void setElementsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5 ); // matrix dimensions + matrix.setElements( { // matrix elements definition + { 0, 0, 2.0 }, + { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, + { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, + { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, + { 4, 4, 2.0 } } ); + + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Setting matrix elements on host: " << std::endl; + setElementsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Setting matrix elements on CUDA device: " << std::endl; + setElementsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements.cu new file mode 120000 index 000000000..dfbad6b27 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements.cu @@ -0,0 +1 @@ +SparseMatrixExample_setElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cpp new file mode 100644 index 000000000..c7bdbbc17 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void setElementsExample() +{ + std::map< std::pair< int, int >, double > map; + map.insert( std::make_pair( std::make_pair( 0, 0 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 0 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 1 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 2 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 1 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 2 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 3 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 2 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 3 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 4 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 4, 4 ), 2.0 ) ); + + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5 ); + matrix.setElements( map ); + + std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrices on CPU ... " << std::endl; + setElementsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + setElementsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cu new file mode 120000 index 000000000..aaf831b5d --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cu @@ -0,0 +1 @@ +SparseMatrixExample_setElements_map.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cpp new file mode 100644 index 000000000..f282aee6d --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void setRowCapacitiesExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( 5, 5 ); + TNL::Containers::Vector< int, Device > rowCapacities{ 1, 2, 3, 4, 5 }; + matrix.setRowCapacities( rowCapacities ); + for( int row = 0; row < 5; row++ ) + for( int column = 0; column <= row; column++ ) + matrix.setElement( row, column, row - column + 1 ); + + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrices on CPU ... " << std::endl; + setRowCapacitiesExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + setRowCapacitiesExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cu new file mode 120000 index 000000000..77bb1a91f --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cu @@ -0,0 +1 @@ +SparseMatrixExample_setRowCapacities.cpp \ No newline at end of file diff --git a/src/Benchmarks/SpMV/spmv-legacy.h b/src/Benchmarks/SpMV/spmv-legacy.h index a066b461e..91db24d01 100644 --- a/src/Benchmarks/SpMV/spmv-legacy.h +++ b/src/Benchmarks/SpMV/spmv-legacy.h @@ -158,12 +158,12 @@ benchmarkSpMV( Benchmark& benchmark, benchmark.setMetadataColumns( Benchmark::MetadataColumns({ { "matrix name", convertToString( inputFileName ) }, - { "non-zeros", convertToString( hostMatrix.getNumberOfNonzeroMatrixElements() ) }, + { "non-zeros", convertToString( hostMatrix.getNonzeroElementsCount() ) }, { "rows", convertToString( hostMatrix.getRows() ) }, { "columns", convertToString( hostMatrix.getColumns() ) }, { "matrix format", MatrixInfo< HostMatrix >::getFormat() } } )); - const int elements = hostMatrix.getNumberOfNonzeroMatrixElements(); + const int elements = hostMatrix.getNonzeroElementsCount(); const double datasetSize = (double) elements * ( 2 * sizeof( Real ) + sizeof( int ) ) / oneGB; benchmark.setOperation( datasetSize ); diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 5e8892abe..84237a7ba 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -209,6 +209,15 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename Matrix > void setLike( const Matrix& matrix ); + /** + * \brief 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. + */ + template< typename RowCapacitiesVector > + void setRowCapacities( const RowCapacitiesVector& rowCapacities ); + /** * \brief This method recreates the dense matrix from 2D initializer list. * @@ -227,15 +236,6 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename Value > void setElements( std::initializer_list< std::initializer_list< Value > > data ); - /** - * \brief 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. - */ - template< typename RowCapacitiesVector > - void setRowCapacities( const RowCapacitiesVector& rowCapacities ); - /** * \brief Computes number of non-zeros in each row. * diff --git a/src/TNL/Matrices/Matrix.h b/src/TNL/Matrices/Matrix.h index 4b954d314..210332bcd 100644 --- a/src/TNL/Matrices/Matrix.h +++ b/src/TNL/Matrices/Matrix.h @@ -58,7 +58,7 @@ public: IndexType getAllocatedElementsCount() const; - IndexType getNumberOfNonzeroMatrixElements() const; + IndexType getNonzeroElementsCount() const; void reset(); diff --git a/src/TNL/Matrices/Matrix.hpp b/src/TNL/Matrices/Matrix.hpp index ce5f52274..adacaee57 100644 --- a/src/TNL/Matrices/Matrix.hpp +++ b/src/TNL/Matrices/Matrix.hpp @@ -79,7 +79,7 @@ template< typename Real, typename Device, typename Index, typename RealAllocator > -Index Matrix< Real, Device, Index, RealAllocator >::getNumberOfNonzeroMatrixElements() const +Index Matrix< Real, Device, Index, RealAllocator >::getNonzeroElementsCount() const { const auto values_view = this->values.getConstView(); auto fetch = [=] __cuda_callable__ ( const IndexType i ) -> IndexType { diff --git a/src/TNL/Matrices/MatrixType.h b/src/TNL/Matrices/MatrixType.h index 5842759ea..ad1faaa8b 100644 --- a/src/TNL/Matrices/MatrixType.h +++ b/src/TNL/Matrices/MatrixType.h @@ -25,6 +25,18 @@ struct MatrixType static constexpr bool isSymmetric() { return Symmetric; } static constexpr bool isBinary() { return Binary; } + + static String getSerializationType() { + String type; + if( ! isBinary() && ! isSymmetric() ) + type = "General"; + else + { + if( isSymmetric ) type = "Symmetric"; + if( isBinary ) type += "Binary"; + } + return type; + } }; /** diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 0378d96bf..b40307cb0 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -258,9 +258,12 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * std::pair of matrix coordinates ( {row, column} ) and value is the * matrix element value. * + * \tparam MapIndex is a type for indexing rows and columns. + * \tparam MapValue is a type for matrix elements values in the map. + * * \param rows is number of matrix rows. * \param columns is number of matrix columns. - * \param data is std::map containing matrix elements values. + * \param map is std::map containing matrix elements. * \param realAllocator is used for allocation of matrix elements values. * \param indexAllocator is used for allocation of matrix elements column indexes. * @@ -277,43 +280,173 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > const RealAllocatorType& realAllocator = RealAllocatorType(), const IndexAllocatorType& indexAllocator = IndexAllocatorType() ); + /** + * \brief Returns a modifiable view of the sparse matrix. + * + * See \ref SparseMatrixView. + * + * \return sparse matrix view. + */ + ViewType getView() const; // TODO: remove const - ViewType getView(); // TODO: remove const - + /** + * \brief Returns a non-modifiable view of the sparse matrix. + * + * See \ref SparseMatrixView. + * + * \return sparse matrix view. + */ ConstViewType getConstView() const; + /** + * \brief Returns string with serialization type. + * + * The string has a form \e `Matrices::SparseMatrix< RealType, [any_device], IndexType, General/Symmetric, Format, [any_allocator] >`. + * + * \return \e String with the serialization type. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp + * \par Output + * \include SparseMatrixExample_getSerializationType.out + */ static String getSerializationType(); + /** + * \brief Returns string with serialization type. + * + * See \ref SparseMatrix::getSerializationType. + * + * \return \e String with the serialization type. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp + * \par Output + * \include SparseMatrixExample_getSerializationType.out + */ virtual String getSerializationTypeVirtual() const; + /** + * \brief Set number of rows and columns of this matrix. + * + * \param rows is the number of matrix rows. + * \param columns is the number of matrix columns. + */ virtual void setDimensions( const IndexType rows, const IndexType columns ) override; + /** + * \brief Set the number of matrix rows and columns by the given matrix. + * + * \tparam Matrix is matrix type. This can be any matrix having methods + * \ref getRows and \ref getColumns. + * + * \param matrix in the input matrix dimensions of which are to be adopted. + */ + template< typename Matrix > + void setLike( const Matrix& matrix ); + + /** + * \brief Allocates memory for non-zero matrix elements. + * + * The size of the input vector must be equal to the number of matrix rows. + * The number of allocated matrix elements for each matrix row depends on + * the sparse matrix format. Some formats may allocate more elements than + * required. + * + * \tparam RowsCapacitiesVector is a type of vector/array used for row + * capacities setting. + * + * \param rowCapacities is a vector telling the number of required non-zero + * matrix elements in each row. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cpp + * \par Output + * \include SparseMatrixExample_setRowCapacities.out + */ template< typename RowsCapacitiesVector > void setRowCapacities( const RowsCapacitiesVector& rowCapacities ); // TODO: Remove this when possible template< typename RowsCapacitiesVector > + [[deprecated]] void setCompressedRowLengths( const RowsCapacitiesVector& rowLengths ) { this->setRowCapacities( rowLengths ); }; + /** + * \brief This method sets the sparse matrix elements from initializer list. + * + * The number of matrix rows and columns must be set already. + * The matrix elements values are given as a list \e data of triples: + * { { row1, column1, value1 }, + * { row2, column2, value2 }, + * ... }. + * + * \param data is a initializer list of initializer lists representing + * list of matrix rows. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_setElements.cpp + * \par Output + * \include SparseMatrixExample_setElements.out + */ void setElements( const std::initializer_list< std::tuple< IndexType, IndexType, RealType > >& data ); + /** + * \brief This method sets the sparse matrix elements from std::map. + * + * The matrix elements values are given as a map \e data where keys are + * std::pair of matrix coordinates ( {row, column} ) and value is the + * matrix element value. + * + * \tparam MapIndex is a type for indexing rows and columns. + * \tparam MapValue is a type for matrix elements values in the map. + * + * \param map is std::map containing matrix elements. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cpp + * \par Output + * \include SparseMatrixExample_setElements_map.out + */ template< typename MapIndex, typename MapValue > void setElements( const std::map< std::pair< MapIndex, MapIndex > , MapValue >& map ); + /** + * \brief Computes number of non-zeros in each row. + * + * \param rowLengths is a vector into which the number of non-zeros in each row + * will be stored. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_getCompressedRowLengths.cpp + * \par Output + * \include SparseMatrixExample_getCompressedRowLengths.out + */ template< typename Vector > void getCompressedRowLengths( Vector& rowLengths ) const; + /** + * \brief Returns capacity of given matrix row. + * + * \param row index of matrix row. + * \return number of matrix elements allocated for the row. + */ __cuda_callable__ IndexType getRowCapacity( const IndexType row ) const; - template< typename Matrix > - void setLike( const Matrix& matrix ); - - IndexType getNumberOfNonzeroMatrixElements() const; + /** + * \brief Returns number of non-zero matrix elements. + * + * This method really counts the non-zero matrix elements and so + * it returns zero for matrix having all allocated elements set to zero. + * + * \return number of non-zero matrix elements. + */ + IndexType getNonzeroElementsCount() const; void reset(); diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index d16c12f95..23baf11f0 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -107,23 +107,6 @@ SparseMatrix( const IndexType rows, this->setElements( map ); } -template< typename Real, - typename Device, - typename Index, - typename MatrixType, - template< typename, typename, typename > class Segments, - typename RealAllocator, - typename IndexAllocator > -void -SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: -setDimensions( const IndexType rows, - const IndexType columns ) -{ - BaseType::setDimensions( rows, columns ); - segments.setSegmentsSizes( Containers::Vector< IndexType, DeviceType, IndexType >( rows, 0 ) ); - this->view = this->getView(); -} - template< typename Real, typename Device, typename Index, @@ -133,7 +116,7 @@ template< typename Real, typename IndexAllocator > auto SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: -getView() -> ViewType +getView() const -> ViewType { return ViewType( this->getRows(), this->getColumns(), @@ -171,10 +154,7 @@ String SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: getSerializationType() { - return String( "Matrices::SparseMatrix< " ) + - TNL::getSerializationType< RealType >() + ", " + - TNL::getSerializationType< SegmentsType >() + ", [any_device], " + - TNL::getSerializationType< IndexType >() + ", [any_allocator] >"; + return ViewType::getSerializationType(); } template< typename Real, @@ -191,6 +171,41 @@ getSerializationTypeVirtual() const return this->getSerializationType(); } +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename, typename > class Segments, + typename RealAllocator, + typename IndexAllocator > +void +SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: +setDimensions( const IndexType rows, + const IndexType columns ) +{ + BaseType::setDimensions( rows, columns ); + segments.setSegmentsSizes( Containers::Vector< IndexType, DeviceType, IndexType >( rows, 0 ) ); + this->view = this->getView(); +} + +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename, typename > class Segments, + typename RealAllocator, + typename IndexAllocator > + template< typename Matrix_ > +void +SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: +setLike( const Matrix_& matrix ) +{ + BaseType::setLike( matrix ); + this->segments.setSegmentsSizes( Containers::Vector< IndexType, DeviceType, IndexType >( matrix.getRows(), 0 ) ), + this->view = this->getView(); + TNL_ASSERT_EQ( this->getRows(), segments.getSegmentsCount(), "mismatched segments count" ); +} + template< typename Real, typename Device, typename Index, @@ -324,24 +339,6 @@ getRowCapacity( const IndexType row ) const return this->view.getRowCapacity( row ); } -template< typename Real, - typename Device, - typename Index, - typename MatrixType, - template< typename, typename, typename > class Segments, - typename RealAllocator, - typename IndexAllocator > - template< typename Matrix_ > -void -SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: -setLike( const Matrix_& matrix ) -{ - BaseType::setLike( matrix ); - this->segments.setSegmentsSizes( Containers::Vector< IndexType, DeviceType, IndexType >( matrix.getRows(), 0 ) ), - this->view = this->getView(); - TNL_ASSERT_EQ( this->getRows(), segments.getSegmentsCount(), "mismatched segments count" ); -} - template< typename Real, typename Device, typename Index, @@ -351,7 +348,7 @@ template< typename Real, typename IndexAllocator > Index SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: -getNumberOfNonzeroMatrixElements() const +getNonzeroElementsCount() const { return this->view.getNumberOfNonzeroMatrixElements(); } diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index 1aa4289dc..6d564afe9 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -92,7 +92,8 @@ getSerializationType() return String( "Matrices::SparseMatrix< " ) + TNL::getSerializationType< RealType >() + ", " + TNL::getSerializationType< SegmentsViewType >() + ", [any_device], " + - TNL::getSerializationType< IndexType >() + ", [any_allocator] >"; + TNL::getSerializationType< IndexType >() + ", " + + MatrixType::getSerializationType() + ", [any_allocator], [any_allocator] >"; } template< typename Real, @@ -709,7 +710,9 @@ print( std::ostream& str ) const value = ( RealType ) 1.0; else value = this->values.getElement( globalIdx ); - str << " Col:" << column << "->" << value << "\t"; + std::stringstream str_; + str_ << std::setw( 4 ) << std::right << column << ":" << std::setw( 4 ) << std::left << value; + str << std::setw( 10 ) << str_.str(); } str << std::endl; } diff --git a/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h b/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h index bbf332ef7..104768ef7 100644 --- a/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h +++ b/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h @@ -33,7 +33,7 @@ update( const MatrixPointer& matrixPointer ) VectorViewType diag_view( diagonal ); - const auto kernel_matrix = matrixPointer->getConstView(); + const auto kernel_matrix = matrixPointer->getView(); // TODO: Rewrite this with SparseMatrix::forAllRows auto kernel = [=] __cuda_callable__ ( IndexType i ) mutable diff --git a/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp b/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp index 87d5e139b..6f5fce26b 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp @@ -1136,62 +1136,4 @@ void test_SaveAndLoad( const char* filename ) EXPECT_EQ( std::remove( filename ), 0 ); } -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 1 1 0 \ - * | 0 0 0 1 | - * | 1 1 1 0 | - * | 0 1 1 1 | - * \ 0 0 1 1 / - */ - - const IndexType m_rows = 5; - const IndexType m_cols = 4; - - Matrix m( m_rows, m_cols ); - typename Matrix::CompressedRowLengthsVector rowLengths( m_rows, 3 ); - m.setCompressedRowLengths( rowLengths ); - - RealType value = 1; - for( IndexType i = 0; i < m_cols - 1; i++ ) // 0th row - m.setElement( 0, i, 1 ); - - m.setElement( 1, 3, 1 ); // 1st row - - for( IndexType i = 0; i < m_cols - 1; i++ ) // 2nd row - m.setElement( 2, i, 1 ); - - for( IndexType i = 1; i < m_cols; i++ ) // 3rd row - m.setElement( 3, i, 1 ); - - for( IndexType i = 2; i < m_cols; i++ ) // 4th row - m.setElement( 4, i, 1 ); - - 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->1 Col:2->1\t\n" - "Row: 1 -> Col:3->1\t\n" - "Row: 2 -> Col:0->1 Col:1->1 Col:2->1\t\n" - "Row: 3 -> Col:1->1 Col:2->1 Col:3->1\t\n" - "Row: 4 -> Col:2->1 Col:3->1\t\n"; - - EXPECT_EQ( printed.str(), couted.str() ); -} - #endif diff --git a/src/UnitTests/Matrices/BinarySparseMatrixTest_CSR.h b/src/UnitTests/Matrices/BinarySparseMatrixTest_CSR.h index a853281be..ab072ab8a 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixTest_CSR.h +++ b/src/UnitTests/Matrices/BinarySparseMatrixTest_CSR.h @@ -114,14 +114,6 @@ TYPED_TEST( BinaryMatrixTest_CSR, saveAndLoadTest ) test_SaveAndLoad< CSRMatrixType >( "test_BinarySparseMatrixTest_CSR" ); } - -TYPED_TEST( BinaryMatrixTest_CSR, printTest ) -{ - using CSRMatrixType = typename TestFixture::CSRMatrixType; - - test_Print< CSRMatrixType >(); -} - #endif #include "../main.h" diff --git a/src/UnitTests/Matrices/BinarySparseMatrixTest_Ellpack.h b/src/UnitTests/Matrices/BinarySparseMatrixTest_Ellpack.h index e532c9ff1..f8cd5f415 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixTest_Ellpack.h +++ b/src/UnitTests/Matrices/BinarySparseMatrixTest_Ellpack.h @@ -125,14 +125,6 @@ TYPED_TEST( BinaryMatrixTest_Ellpack, saveAndLoadTest ) test_SaveAndLoad< EllpackMatrixType >( "test_BinarySparseMatrixTest_Ellpack" ); } - -TYPED_TEST( BinaryMatrixTest_Ellpack, printTest ) -{ - using EllpackMatrixType = typename TestFixture::EllpackMatrixType; - - test_Print< EllpackMatrixType >(); -} - #endif #include "../main.h" diff --git a/src/UnitTests/Matrices/BinarySparseMatrixTest_SlicedEllpack.h b/src/UnitTests/Matrices/BinarySparseMatrixTest_SlicedEllpack.h index e332bc010..f58a018ac 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixTest_SlicedEllpack.h +++ b/src/UnitTests/Matrices/BinarySparseMatrixTest_SlicedEllpack.h @@ -126,13 +126,6 @@ TYPED_TEST( BinaryMatrixTest_SlicedEllpack, saveAndLoadTest ) test_SaveAndLoad< SlicedEllpackMatrixType >( "test_BinarySparseMatrixTest" ); } -TYPED_TEST( BinaryMatrixTest_SlicedEllpack, printTest ) -{ - using SlicedEllpackMatrixType = typename TestFixture::SlicedEllpackMatrixType; - - test_Print< SlicedEllpackMatrixType >(); -} - #endif #include "../main.h" diff --git a/src/UnitTests/Matrices/SparseMatrixTest.h b/src/UnitTests/Matrices/SparseMatrixTest.h index a00e69687..98e779dae 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.h +++ b/src/UnitTests/Matrices/SparseMatrixTest.h @@ -108,11 +108,4 @@ TYPED_TEST( MatrixTest, saveAndLoadTest ) test_SaveAndLoad< MatrixType >( saveAndLoadFileName ); } - -TYPED_TEST( MatrixTest, printTest ) -{ - using MatrixType = typename TestFixture::MatrixType; - - test_Print< MatrixType >(); -} #endif \ No newline at end of file diff --git a/src/UnitTests/Matrices/SparseMatrixTest.hpp b/src/UnitTests/Matrices/SparseMatrixTest.hpp index 6d7c64360..61142cb21 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SparseMatrixTest.hpp @@ -320,7 +320,7 @@ void test_SetLike() } template< typename Matrix > -void test_GetNumberOfNonzeroMatrixElements() +void test_GetNonzeroElementsCount() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -369,7 +369,7 @@ void test_GetNumberOfNonzeroMatrixElements() for( IndexType i = 0; i < cols; i++ ) m.setElement( j, i, value++ ); - EXPECT_EQ( m.getNumberOfNonzeroMatrixElements(), 41 ); + EXPECT_EQ( m.getNonzeroElementsCount(), 41 ); } template< typename Matrix > @@ -1557,62 +1557,4 @@ void test_SaveAndLoad( const char* filename ) EXPECT_EQ( std::remove( filename ), 0 ); } -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 0 \ - * | 0 0 0 4 | - * | 5 6 7 0 | - * | 0 8 9 10 | - * \ 0 0 11 12 / - */ - - const IndexType m_rows = 5; - const IndexType m_cols = 4; - - Matrix m( m_rows, m_cols ); - typename Matrix::CompressedRowLengthsVector rowLengths( m_rows, 3 ); - m.setCompressedRowLengths( rowLengths ); - - RealType value = 1; - for( IndexType i = 0; i < m_cols - 1; i++ ) // 0th row - m.setElement( 0, i, value++ ); - - m.setElement( 1, 3, value++ ); // 1st row - - for( IndexType i = 0; i < m_cols - 1; i++ ) // 2nd row - m.setElement( 2, i, value++ ); - - for( IndexType i = 1; i < m_cols; i++ ) // 3rd row - m.setElement( 3, i, value++ ); - - for( IndexType i = 2; i < m_cols; i++ ) // 4th row - m.setElement( 4, i, value++ ); - - 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\t\n" - "Row: 1 -> Col:3->4\t\n" - "Row: 2 -> Col:0->5 Col:1->6 Col:2->7\t\n" - "Row: 3 -> Col:1->8 Col:2->9 Col:3->10\t\n" - "Row: 4 -> Col:2->11 Col:3->12\t\n"; - - EXPECT_EQ( printed.str(), couted.str() ); -} - #endif diff --git a/src/UnitTests/Matrices/SymmetricSparseMatrixTest.h b/src/UnitTests/Matrices/SymmetricSparseMatrixTest.h index 02fd8c585..3c4093785 100644 --- a/src/UnitTests/Matrices/SymmetricSparseMatrixTest.h +++ b/src/UnitTests/Matrices/SymmetricSparseMatrixTest.h @@ -45,11 +45,11 @@ TYPED_TEST( MatrixTest, setLikeTest ) test_SetLike< MatrixType, MatrixType >(); } -TYPED_TEST( MatrixTest, getNumberOfNonzeroMatrixElements ) +TYPED_TEST( MatrixTest, getNonzeroElementsCount ) { using MatrixType = typename TestFixture::MatrixType; - test_GetNumberOfNonzeroMatrixElements< MatrixType >(); + test_GetNonzeroElementsCount< MatrixType >(); } TYPED_TEST( MatrixTest, resetTest ) diff --git a/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp b/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp index 4e28842ba..da8a632c0 100644 --- a/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp @@ -163,7 +163,7 @@ void test_SetLike() } template< typename Matrix > -void test_GetNumberOfNonzeroMatrixElements() +void test_GetNonzeroElementsCount() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -204,7 +204,7 @@ void test_GetNumberOfNonzeroMatrixElements() { 10, 2, 28 }, { 10, 4, 29 }, { 10, 10, 30 } } ); - EXPECT_EQ( m.getNumberOfNonzeroMatrixElements(), 49 ); + EXPECT_EQ( m.getNonzeroElementsCount(), 49 ); } template< typename Matrix > -- GitLab From f374b4741339b69292efd5ef057fb29263aca569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 26 May 2020 15:51:19 +0200 Subject: [PATCH 14/38] Writting documentation on sparse matrix. --- .../DenseMatrixExample_getElement.cpp | 2 +- .../DenseMatrixExample_setElement.cpp | 8 +- .../Matrices/SparseMatrix/CMakeLists.txt | 109 +++++------ .../SparseMatrixExample_addElement.cpp | 30 +++ .../SparseMatrixExample_addElement.cu | 1 + .../SparseMatrixExample_getConstRow.cpp | 52 +++++ .../SparseMatrixExample_getConstRow.cu | 1 + .../SparseMatrixExample_getElement.cpp | 37 ++++ .../SparseMatrixExample_getElement.cu | 1 + .../SparseMatrixExample_getRow.cpp | 45 +++++ .../SparseMatrixExample_getRow.cu | 1 + .../SparseMatrixExample_setElement.cpp | 45 +++++ .../SparseMatrixExample_setElement.cu | 1 + src/TNL/Matrices/DenseMatrix.h | 40 ++-- src/TNL/Matrices/DenseMatrix.hpp | 20 +- src/TNL/Matrices/DenseMatrixView.h | 18 +- src/TNL/Matrices/DenseMatrixView.hpp | 14 +- src/TNL/Matrices/SparseMatrix.h | 182 +++++++++++++++--- src/TNL/Matrices/SparseMatrix.hpp | 12 +- src/TNL/Matrices/SparseMatrixView.h | 6 +- src/TNL/Matrices/SparseMatrixView.hpp | 12 +- 21 files changed, 477 insertions(+), 160 deletions(-) create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_addElement.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_addElement.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getConstRow.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getConstRow.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getElement.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getElement.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElement.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElement.cu diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cpp index 72a5d0af4..7bd089ed8 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getElement.cpp @@ -17,7 +17,7 @@ void getElements() for( int i = 0; i < 5; i++ ) { for( int j = 0; j < 5; j++ ) - std::cout << std::setw( 5 ) << std::ios::right << matrix.getElement( i, i ); + std::cout << std::setw( 5 ) << matrix.getElement( i, j ); std::cout << std::endl; } } diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElement.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElement.cpp index 4f92496f8..9441cc60d 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElement.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_setElement.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -18,12 +19,17 @@ void setElements() auto f = [=] __cuda_callable__ ( int i ) mutable { matrix->setElement( i, i, -i ); }; + + /*** + * 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 >::exec( 0, 5, f ); std::cout << "Matrix set from its native device:" << std::endl; std::cout << *matrix << std::endl; - } int main( int argc, char* argv[] ) diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt index 573437912..8821cb3c8 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -39,35 +39,30 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElementsCount_cuda SparseMatrixExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out -# OUTPUT SparseMatrixExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getConstRow_cuda SparseMatrixExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out -# OUTPUT SparseMatrixExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getRow_cuda SparseMatrixExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out -# OUTPUT SparseMatrixExample_getRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement_cuda SparseMatrixExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out -# OUTPUT SparseMatrixExample_setElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement_cuda SparseMatrixExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out -# OUTPUT SparseMatrixExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElement_cuda SparseMatrixExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out -# OUTPUT SparseMatrixExample_getElement.out ) + CUDA_ADD_EXECUTABLE( SparseMatrixExample_getConstRow_cuda SparseMatrixExample_getConstRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out + OUTPUT SparseMatrixExample_getConstRow.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_getRow_cuda SparseMatrixExample_getRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out + OUTPUT SparseMatrixExample_getRow.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement_cuda SparseMatrixExample_setElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out + OUTPUT SparseMatrixExample_setElement.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement_cuda SparseMatrixExample_addElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out + OUTPUT SparseMatrixExample_addElement.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElement_cuda SparseMatrixExample_getElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out + OUTPUT SparseMatrixExample_getElement.out ) # CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) # ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > @@ -190,35 +185,30 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) -# ADD_EXECUTABLE( SparseMatrixExample_getElementsCount SparseMatrixExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out -# OUTPUT SparseMatrixExample_getElementsCount.out ) + ADD_EXECUTABLE( SparseMatrixExample_getConstRow SparseMatrixExample_getConstRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out + OUTPUT SparseMatrixExample_getConstRow.out ) -# ADD_EXECUTABLE( SparseMatrixExample_getConstRow SparseMatrixExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out -# OUTPUT SparseMatrixExample_getConstRow.out ) + ADD_EXECUTABLE( SparseMatrixExample_getRow SparseMatrixExample_getRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out + OUTPUT SparseMatrixExample_getRow.out ) -# ADD_EXECUTABLE( SparseMatrixExample_getRow SparseMatrixExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out -# OUTPUT SparseMatrixExample_getRow.out ) + ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out + OUTPUT SparseMatrixExample_setElement.out ) -# ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out -# OUTPUT SparseMatrixExample_setElement.out ) + ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out + OUTPUT SparseMatrixExample_addElement.out ) -# ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out -# OUTPUT SparseMatrixExample_addElement.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getElement SparseMatrixExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out -# OUTPUT SparseMatrixExample_getElement.out ) + ADD_EXECUTABLE( SparseMatrixExample_getElement SparseMatrixExample_getElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out + OUTPUT SparseMatrixExample_getElement.out ) # ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) # ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > @@ -311,12 +301,11 @@ ADD_CUSTOM_TARGET( RunSparseMatricesExamples ALL DEPENDS SparseMatrixExample_setElements.out SparseMatrixExample_setElements_map.out SparseMatrixExample_getCompressedRowLengths.out -# SparseMatrixExample_getElementsCount.out -# SparseMatrixExample_getConstRow.out -# SparseMatrixExample_getRow.out -# SparseMatrixExample_setElement.out -# SparseMatrixExample_addElement.out -# SparseMatrixExample_getElement.out + SparseMatrixExample_getConstRow.out + SparseMatrixExample_getRow.out + SparseMatrixExample_setElement.out + SparseMatrixExample_addElement.out + SparseMatrixExample_getElement.out # SparseMatrixExample_rowsReduction.out # SparseMatrixExample_allRowsReduction.out # SparseMatrixExample_forRows.out diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_addElement.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_addElement.cpp new file mode 100644 index 000000000..1b51f494f --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_addElement.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +template< typename Device > +void addElements() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 5, 5, 5, 5, 5 }, 5 ); + 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 HAVE_CUDA + std::cout << "Add elements on CUDA device:" << std::endl; + addElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_addElement.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_addElement.cu new file mode 120000 index 000000000..2ec36ad74 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_addElement.cu @@ -0,0 +1 @@ +SparseMatrixExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getConstRow.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getConstRow.cpp new file mode 100644 index 000000000..747a3c825 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getConstRow.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > matrix ( 5, 5 ); + matrix->setElements( + { { 0, 0, 1 }, + { 1, 0, 1 }, { 1, 1, 2 }, + { 2, 0, 1 }, { 2, 1, 2 }, { 2, 2, 3 }, + { 3, 0, 1 }, { 3, 1, 2 }, { 3, 2, 3 }, { 3, 3, 4 }, + { 4, 0, 1 }, { 4, 1, 2 }, { 4, 2, 3 }, { 4, 3, 4 }, { 4, 4, 5 } } ); + + /*** + * Fetch lambda function returns diagonal element in each row. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { + auto row = matrix->getRow( rowIdx ); + return row.getValue( rowIdx ); + }; + + /*** + * For the case when Device is CUDA device we need to synchronize smart + * pointers. To avoid this you may use SparseMatrixView. See + * SparseMatrixView::getConstRow example for details. + */ + TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); + + /*** + * Compute the matrix trace. + */ + int trace = TNL::Algorithms::Reduction< Device >::reduce( matrix->getRows(), std::plus<>{}, fetch, 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 HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getConstRow.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getConstRow.cu new file mode 120000 index 000000000..03a7d1e7a --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getConstRow.cu @@ -0,0 +1 @@ +SparseMatrixExample_getConstRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getElement.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getElement.cpp new file mode 100644 index 000000000..4db765979 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getElement.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +template< typename Device > +void getElements() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( + 5, // number of matrix rows + 5, // number of matrix columns + { // matrix elements definition + { 0, 0, 2.0 }, + { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, + { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, + { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, + { 4, 4, 2.0 } } ); + + + for( int i = 0; i < 5; i++ ) + { + for( int j = 0; j < 5; j++ ) + std::cout << std::setw( 5 ) << matrix.getElement( i, j ); + std::cout << std::endl; + } +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get elements on host:" << std::endl; + getElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get elements on CUDA device:" << std::endl; + getElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getElement.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getElement.cu new file mode 120000 index 000000000..bb3bc6636 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getElement.cu @@ -0,0 +1 @@ +SparseMatrixExample_getElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cpp new file mode 100644 index 000000000..d07577ff9 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + auto rowCapacities = { 1, 1, 1, 1, 1 }; // Variadic templates in SharedPointer + // constructor do not recognize initializer + // list so we give it a hint. + using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > matrix( rowCapacities, 5 ); + + auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { + auto row = matrix->getRow( rowIdx ); + row.setElement( 0, rowIdx, 10 * ( rowIdx + 1 ) ); + }; + + /*** + * For the case when Device is CUDA device we need to synchronize smart + * pointers. To avoid this you may use SparseMatrixView. See + * SparseMatrixView::getRow example for details. + */ + TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); + + /*** + * Set the matrix elements. + */ + TNL::Algorithms::ParallelFor< Device >::exec( 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 HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cu new file mode 120000 index 000000000..776f8a9d3 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cu @@ -0,0 +1 @@ +SparseMatrixExample_getRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElement.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElement.cpp new file mode 100644 index 000000000..178e502dc --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElement.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void setElements() +{ + auto rowCapacities = { 1, 1, 1, 1, 1 }; + TNL::Pointers::SharedPointer< TNL::Matrices::SparseMatrix< double, Device > > matrix( rowCapacities, 5 ); + for( int i = 0; i < 5; i++ ) + matrix->setElement( i, i, i ); + + std::cout << "Matrix set from the host:" << std::endl; + std::cout << *matrix << std::endl; + + auto f = [=] __cuda_callable__ ( int i ) mutable { + matrix->setElement( i, i, -i ); + }; + + /*** + * For the case when Device is CUDA device we need to synchronize smart + * pointers. To avoid this you may use SparseMatrixView. See + * SparseMatrixView::getRow example for details. + */ + TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); + TNL::Algorithms::ParallelFor< Device >::exec( 0, 5, 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 HAVE_CUDA + std::cout << "Set elements on CUDA device:" << std::endl; + setElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElement.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElement.cu new file mode 120000 index 000000000..97b115c3f --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_setElement.cu @@ -0,0 +1 @@ +SparseMatrixExample_setElement.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 84237a7ba..6f68514bf 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -250,22 +250,6 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename RowLengthsVector > void getCompressedRowLengths( RowLengthsVector& rowLengths ) 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/DenseMatrix/DenseMatrixExample_getElementsCount.cpp - * \par Output - * \include DenseMatrixExample_getElementsCount.out - */ - IndexType getAllocatedElementsCount() const; - /** * \brief Returns number of non-zero matrix elements. * @@ -369,7 +353,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param value is the value the element will be set to. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixViewExample_setElement.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_setElement.cpp * \par Output * \include DenseMatrixExample_setElement.out */ @@ -395,7 +379,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * value is multiplied by before addition of given \e value. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixViewExample_addElement.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_addElement.cpp * \par Output * \include DenseMatrixExample_addElement.out * @@ -422,7 +406,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \return value of given matrix element. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixViewExample_getElement.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_getElement.cpp * \par Output * \include DenseMatrixExample_getElement.out * @@ -443,8 +427,8 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * It is declared as `keep( const IndexType rowIdx, const double& value )`. * \tparam FetchValue is type returned by the Fetch lambda function. * - * \param first is an index of the first row the reduction will be performed on. - * \param last is an index of the row after the last row the reduction will be performed on. + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. * \param fetch is an instance of lambda function for data fetch. * \param reduce is an instance of lambda function for reduction. * \param keep in an instance of lambda function for storing results. @@ -456,7 +440,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_rowsReduction.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const; + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const; /** * \brief Method for performing general reduction on ALL matrix rows. @@ -493,8 +477,8 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * If the 'compute' variable is set to false the iteration over the row can * be interrupted. * - * \param first is index is the first row to be processed. - * \param last is index of the row after the last row to be processed. + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. * \param function is an instance of the lambda function to be called in each row. * * \par Example @@ -503,7 +487,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_forRows.out */ template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ) const; + void forRows( IndexType begin, IndexType end, Function& function ) const; /** * \brief Method for iteration over all matrix rows for non-constant instances. @@ -515,8 +499,8 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * If the 'compute' variable is set to false the iteration over the row can * be interrupted. * - * \param first is index is the first row to be processed. - * \param last is index of the row after the last row to be processed. + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. * \param function is an instance of the lambda function to be called in each row. * * \par Example @@ -525,7 +509,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_forRows.out */ template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ); + void forRows( IndexType begin, IndexType end, Function& function ); /** * \brief This method calls \e forRows for all matrix rows. diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index 995a369f6..ec7f2fb1c 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -205,18 +205,6 @@ getCompressedRowLengths( RowLengthsVector& rowLengths ) const this->view.getCompressedRowLengths( rowLengths ); } -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization, - typename RealAllocator > -Index -DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -getAllocatedElementsCount() const -{ - return this->getRows() * this->getColumns(); -} - template< typename Real, typename Device, typename Index, @@ -352,9 +340,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const +rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const { - this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); + this->view.rowsReduction( begin, end, fetch, reduce, keep, zero ); } template< typename Real, @@ -378,9 +366,9 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -forRows( IndexType first, IndexType last, Function& function ) const +forRows( IndexType begin, IndexType end, Function& function ) const { - this->view.forRows( first, last, function ); + this->view.forRows( begin, end, function ); } template< typename Real, diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index ad4a0730e..1706e4603 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -375,8 +375,8 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * It is declared as `keep( const IndexType rowIdx, const double& value )`. * \tparam FetchValue is type returned by the Fetch lambda function. * - * \param first is an index of the first row the reduction will be performed on. - * \param last is an index of the row after the last row the reduction will be performed on. + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. * \param fetch is an instance of lambda function for data fetch. * \param reduce is an instance of lambda function for reduction. * \param keep in an instance of lambda function for storing results. @@ -388,7 +388,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixViewExample_rowsReduction.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on ALL matrix rows. @@ -425,8 +425,8 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * If the 'compute' variable is set to false the iteration over the row can * be interrupted. * - * \param first is index is the first row to be processed. - * \param last is index of the row after the last row to be processed. + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. * \param function is an instance of the lambda function to be called in each row. * * \par Example @@ -435,7 +435,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixViewExample_forRows.out */ template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ) const; + void forRows( IndexType begin, IndexType end, Function& function ) const; /** * \brief Method for iteration over all matrix rows for non-constant instances. @@ -447,8 +447,8 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * If the 'compute' variable is set to false the iteration over the row can * be interrupted. * - * \param first is index is the first row to be processed. - * \param last is index of the row after the last row to be processed. + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. * \param function is an instance of the lambda function to be called in each row. * * \par Example @@ -457,7 +457,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixViewExample_forRows.out */ template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ); + void forRows( IndexType begin, IndexType end, Function& function ); /** * \brief This method calls \e forRows for all matrix rows. diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index 33c746f01..eae7dd9a6 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -267,14 +267,14 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void DenseMatrixView< Real, Device, Index, Organization >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const +rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const { const auto values_view = this->values.getConstView(); auto fetch_ = [=] __cuda_callable__ ( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, bool& compute ) mutable -> decltype( fetch( IndexType(), IndexType(), RealType() ) ) { return fetch( rowIdx, columnIdx, values_view[ globalIdx ] ); return zero; }; - this->segments.segmentsReduction( first, last, fetch_, reduce, keep, zero ); + this->segments.segmentsReduction( begin, end, fetch_, reduce, keep, zero ); } template< typename Real, @@ -296,14 +296,13 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -forRows( IndexType first, IndexType last, Function& function ) const +forRows( IndexType begin, IndexType end, Function& function ) const { const auto values_view = this->values.getConstView(); auto f = [=] __cuda_callable__ ( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, bool& compute ) mutable { function( rowIdx, columnIdx, columnIdx, values_view[ globalIdx ], compute ); }; - this->segments.forSegments( first, last, f ); - + this->segments.forSegments( begin, end, f ); } template< typename Real, @@ -313,14 +312,13 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -forRows( IndexType first, IndexType last, Function& function ) +forRows( IndexType begin, IndexType end, Function& function ) { auto values_view = this->values.getView(); auto f = [=] __cuda_callable__ ( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, bool& compute ) mutable { function( rowIdx, columnIdx, globalIdx, values_view[ globalIdx ], compute ); }; - this->segments.forSegments( first, last, f ); - + this->segments.forSegments( begin, end, f ); } template< typename Real, diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index b40307cb0..16dc35468 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -448,33 +448,189 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > */ 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/SparseMatrix/SparseMatrixExample_getConstRow.cpp + * \par Output + * \include SparseMatrixExample_getConstRow.out + * + * See \ref SparseMatrixRowView. + */ __cuda_callable__ const ConstRowView 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/SparseMatrix/SparseMatrixExample_getRow.cpp + * \par Output + * \include SparseMatrixExample_getRow.out + * + * See \ref SparseMatrixRowView. + */ __cuda_callable__ RowView getRow( const IndexType& rowIdx ); + /** + * \brief Sets element at given \e row and \e column to given \e 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. \ref SparseMatrix::getRow + * or \ref SparseMatrix::forRows and \ref SparseMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_setElement.cpp + * \par Output + * \include SparseMatrixExample_setElement.out + */ __cuda_callable__ void setElement( const IndexType row, const IndexType column, const RealType& value ); + /** + * \brief Add element at given \e row and \e column to given \e 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. \ref SparseMatrix::getRow + * or \ref SparseMatrix::forRows and \ref SparseMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * \param thisElementMultiplicator is multiplicator the original matrix element + * value is multiplied by before addition of given \e value. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_addElement.cpp + * \par Output + * \include SparseMatrixExample_addElement.out + * + */ __cuda_callable__ void addElement( const IndexType row, const IndexType column, const RealType& value, const RealType& thisElementMultiplicator ); + /** + * \brief 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. \ref SparseMatrix::getRow + * or \ref SparseMatrix::forRows and \ref SparseMatrix::forAllRows. + * + * \param row is a row index of the matrix element. + * \param column i a column index of the matrix element. + * + * \return value of given matrix element. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_getElement.cpp + * \par Output + * \include SparseMatrixExample_getElement.out + * + */ __cuda_callable__ RealType getElement( const IndexType row, const IndexType column ) const; - /*template< typename Vector > - __cuda_callable__ - typename Vector::RealType rowVectorProduct( const IndexType row, - const Vector& vector ) const;*/ + /** + * \brief Method for performing general reduction on matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, RealType elementValue ) -> FetchValue`. + * Parameter \e globalIdx is position of the matrix element in arrays \e values and \e columnIdexes + * of this matrix. The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp + * \par Output + * \include SparseMatrixExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + + /** + * \brief Method for performing general reduction on all matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, RealType elementValue ) -> FetchValue`. + * Parameter \e globalIdx is position of the matrix element in arrays \e values and \e columnIdexes + * of this matrix. The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp + * \par Output + * \include SparseMatrixExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + + template< typename Function > + void forRows( IndexType begin, IndexType end, Function& function ) const; + + template< typename Function > + void forRows( IndexType begin, IndexType end, Function& function ); + + template< typename Function > + void forAllRows( Function& function ) const; + + template< typename Function > + void forAllRows( Function& function ); /*** * \brief This method computes outVector = matrixMultiplicator * ( *this ) * inVector + inVectorAddition * inVector @@ -498,24 +654,6 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > const RealType& matrixMultiplicator = 1.0 ); */ - template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; - - template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; - - template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ) const; - - template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ); - - template< typename Function > - void forAllRows( Function& function ) const; - - template< typename Function > - void forAllRows( Function& function ); - template< typename Vector1, typename Vector2 > bool performSORIteration( const Vector1& b, const IndexType row, diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index 23baf11f0..6a7ed38c0 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -478,9 +478,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const +rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const { - this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); + this->view.rowsReduction( begin, end, fetch, reduce, keep, zero ); } template< typename Real, @@ -508,9 +508,9 @@ template< typename Real, template< typename Function > void SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: -forRows( IndexType first, IndexType last, Function& function ) const +forRows( IndexType begin, IndexType end, Function& function ) const { - this->view.forRows( first, last, function ); + this->view.forRows( begin, end, function ); } template< typename Real, @@ -523,9 +523,9 @@ template< typename Real, template< typename Function > void SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: -forRows( IndexType first, IndexType last, Function& function ) +forRows( IndexType begin, IndexType end, Function& function ) { - this->view.forRows( first, last, function ); + this->view.forRows( begin, end, function ); } template< typename Real, diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index 606d20659..6facd7378 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -129,16 +129,16 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > IndexType lastRow = 0 ) const; template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ) const; + void forRows( IndexType begin, IndexType end, Function& function ) const; template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ); + void forRows( IndexType begin, IndexType end, Function& function ); template< typename Function > void forAllRows( Function& function ) const; diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index 6d564afe9..26a376b02 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -443,7 +443,7 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const +rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const { const auto columns_view = this->columnIndexes.getConstView(); const auto values_view = this->values.getConstView(); @@ -459,7 +459,7 @@ rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& redu } return zero; }; - this->segments.segmentsReduction( first, last, fetch_, reduce, keep, zero ); + this->segments.segmentsReduction( begin, end, fetch_, reduce, keep, zero ); } template< typename Real, @@ -483,7 +483,7 @@ template< typename Real, template< typename Function > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >:: -forRows( IndexType first, IndexType last, Function& function ) const +forRows( IndexType begin, IndexType end, Function& function ) const { const auto columns_view = this->columnIndexes.getConstView(); const auto values_view = this->values.getConstView(); @@ -495,7 +495,7 @@ forRows( IndexType first, IndexType last, Function& function ) const function( rowIdx, localIdx, columns_view[ globalIdx ], values_view[ globalIdx ], compute ); return true; }; - this->segments.forSegments( first, last, f ); + this->segments.forSegments( begin, end, f ); } template< typename Real, @@ -506,7 +506,7 @@ template< typename Real, template< typename Function > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >:: -forRows( IndexType first, IndexType last, Function& function ) +forRows( IndexType begin, IndexType end, Function& function ) { auto columns_view = this->columnIndexes.getView(); auto values_view = this->values.getView(); @@ -520,7 +520,7 @@ forRows( IndexType first, IndexType last, Function& function ) else function( rowIdx, localIdx, columns_view[ globalIdx ], values_view[ globalIdx ], compute ); }; - this->segments.forSegments( first, last, f ); + this->segments.forSegments( begin, end, f ); } template< typename Real, -- GitLab From 9578471a4252eff87c72b3ea8bc5771a85a49142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 26 May 2020 22:04:23 +0200 Subject: [PATCH 15/38] Removing globalIdx from Fetch lambda in SparseMatrixView::rowsReduction. --- src/TNL/Matrices/DenseMatrixView.hpp | 4 ++++ src/TNL/Matrices/SparseMatrixView.hpp | 8 ++++---- src/UnitTests/Matrices/BinarySparseMatrixTest.hpp | 4 ++-- src/UnitTests/Matrices/SparseMatrixTest.hpp | 4 ++-- src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index eae7dd9a6..95674e00f 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -40,8 +40,12 @@ DenseMatrixView( const IndexType rows, const ValuesViewType& values ) : MatrixView< Real, Device, Index >( rows, columns, values ) { +#ifdef __CUDA_ARCH__ + TNL_ASSERT_EQ( values.getSize(), this->getAllocatedElementsCount(), "Number of matrix elements does not agree with matrix dimensions." ); +#else if( values.getSize() != this->getAllocatedElementsCount() ) throw( std::logic_error( "Number of matrix elements does not agree with matrix dimensions." ) ); +#endif SegmentsType a( rows, columns ); segments = a.getView(); diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index 26a376b02..427590e64 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -121,7 +121,7 @@ getCompressedRowLengths( Vector& rowLengths ) const details::set_size_if_resizable( rowLengths, this->getRows() ); rowLengths = 0; auto rowLengths_view = rowLengths.getView(); - auto fetch = [] __cuda_callable__ ( IndexType row, IndexType column, IndexType globalIdx, const RealType& value ) -> IndexType { + auto fetch = [] __cuda_callable__ ( IndexType row, IndexType column, const RealType& value ) -> IndexType { return ( value != 0.0 ); }; auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { @@ -448,14 +448,14 @@ rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduc const auto columns_view = this->columnIndexes.getConstView(); const auto values_view = this->values.getConstView(); const IndexType paddingIndex_ = this->getPaddingIndex(); - auto fetch_ = [=] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType globalIdx, bool& compute ) mutable -> decltype( fetch( IndexType(), IndexType(), IndexType(), RealType() ) ) { + auto fetch_ = [=] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType globalIdx, bool& compute ) mutable -> decltype( fetch( IndexType(), IndexType(), RealType() ) ) { IndexType columnIdx = columns_view[ globalIdx ]; if( columnIdx != paddingIndex_ ) { if( isBinary() ) - return fetch( rowIdx, columnIdx, globalIdx, 1 ); + return fetch( rowIdx, columnIdx, 1 ); else - return fetch( rowIdx, columnIdx, globalIdx, values_view[ globalIdx ] ); + return fetch( rowIdx, columnIdx, values_view[ globalIdx ] ); } return zero; }; diff --git a/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp b/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp index 6f5fce26b..8d532ae71 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp @@ -948,7 +948,7 @@ void test_RowsReduction() // Compute number of non-zero elements in rows. typename Matrix::RowsCapacitiesType rowLengths( rows ); auto rowLengths_view = rowLengths.getView(); - auto fetch = [] __cuda_callable__ ( IndexType row, IndexType column, IndexType globalIdx, const RealType& value ) -> IndexType { + auto fetch = [] __cuda_callable__ ( IndexType row, IndexType column, const RealType& value ) -> IndexType { return ( value != 0.0 ); }; auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { @@ -963,7 +963,7 @@ void test_RowsReduction() // Compute max norm TNL::Containers::Vector< RealType, DeviceType, IndexType > rowSums( rows ); auto rowSums_view = rowSums.getView(); - auto max_fetch = [] __cuda_callable__ ( IndexType row, IndexType column, IndexType globalIdx, const RealType& value ) -> IndexType { + auto max_fetch = [] __cuda_callable__ ( IndexType row, IndexType column, const RealType& value ) -> IndexType { return abs( value ); }; auto max_keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { diff --git a/src/UnitTests/Matrices/SparseMatrixTest.hpp b/src/UnitTests/Matrices/SparseMatrixTest.hpp index 61142cb21..38abdf07e 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SparseMatrixTest.hpp @@ -1374,7 +1374,7 @@ void test_RowsReduction() // Compute number of non-zero elements in rows. typename Matrix::RowsCapacitiesType rowLengths( rows ); auto rowLengths_view = rowLengths.getView(); - auto fetch = [] __cuda_callable__ ( IndexType row, IndexType column, IndexType globalIdx, const RealType& value ) -> IndexType { + auto fetch = [] __cuda_callable__ ( IndexType row, IndexType column, const RealType& value ) -> IndexType { return ( value != 0.0 ); }; auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { @@ -1389,7 +1389,7 @@ void test_RowsReduction() // Compute max norm TNL::Containers::Vector< RealType, DeviceType, IndexType > rowSums( rows ); auto rowSums_view = rowSums.getView(); - auto max_fetch = [] __cuda_callable__ ( IndexType row, IndexType column, IndexType globalIdx, const RealType& value ) -> IndexType { + auto max_fetch = [] __cuda_callable__ ( IndexType row, IndexType column, const RealType& value ) -> IndexType { return abs( value ); }; auto max_keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { diff --git a/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp b/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp index da8a632c0..c316440d1 100644 --- a/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp @@ -898,7 +898,7 @@ void test_RowsReduction() typename Matrix::RowsCapacitiesType rowLengths_true( { 1, 1, 4, 4, 4, 4, 1, 1 } ); auto rowLengths_view = rowLengths.getView(); rowLengths_view = 0; - auto fetch = [=] __cuda_callable__ ( IndexType row, IndexType column, IndexType globalIdx, const RealType& value ) mutable -> IndexType { + auto fetch = [=] __cuda_callable__ ( IndexType row, IndexType column, const RealType& value ) mutable -> IndexType { if( value != 0.0 && row != column) TNL::Algorithms::AtomicOperations< DeviceType >::add( rowLengths_view[ column ], ( IndexType ) 1 ); return ( value != 0.0 ); -- GitLab From 44dacdb086f431e2e5d5106e81d686fa4cfa39a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 27 May 2020 12:36:11 +0200 Subject: [PATCH 16/38] Finished documentation of sparse matrix. --- .../DenseMatrixExample_forAllRows.cpp | 2 +- .../DenseMatrixExample_forRows.cpp | 2 +- .../DenseMatrixExample_rowsReduction.cpp | 3 +- .../Matrices/SparseMatrix/CMakeLists.txt | 78 +++---- .../SparseMatrixExample_allRowsReduction.cpp | 67 ++++++ .../SparseMatrixExample_allRowsReduction.cu | 1 + .../SparseMatrixExample_forAllRows.cpp | 35 ++++ .../SparseMatrixExample_forAllRows.cu | 1 + .../SparseMatrixExample_forRows.cpp | 35 ++++ .../SparseMatrixExample_forRows.cu | 1 + .../SparseMatrixExample_rowsReduction.cpp | 66 ++++++ .../SparseMatrixExample_rowsReduction.cu | 1 + src/TNL/Matrices/DenseMatrix.h | 6 +- src/TNL/Matrices/SparseMatrix.h | 190 ++++++++++++++++-- 14 files changed, 420 insertions(+), 68 deletions(-) create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cu diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp index 5fddf0f34..e218db690 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllRows.cpp @@ -8,7 +8,7 @@ void forAllRowsExample() { TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); - auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { + auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int columnIdx_, double& value, bool& compute ) { if( rowIdx < columnIdx ) compute = false; else diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp index f3e45a006..f98c580fd 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp @@ -8,7 +8,7 @@ void forRowsExample() { TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); - auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { + auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int columnIdx_, double& value, bool& compute ) { if( rowIdx < columnIdx ) compute = false; else diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp index dbc44f854..b521d15d2 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp @@ -20,9 +20,8 @@ void rowsReduction() TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() ); /*** - * Prepare vector view and matrix view for lambdas. + * Prepare vector view for lambdas. */ - const auto matrixView = matrix.getConstView(); auto rowMaxView = rowMax.getView(); /*** diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt index 8821cb3c8..09ec21530 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -64,25 +64,25 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out OUTPUT SparseMatrixExample_getElement.out ) -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out -# OUTPUT SparseMatrixExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction_cuda SparseMatrixExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out -# OUTPUT SparseMatrixExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forRows_cuda SparseMatrixExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out -# OUTPUT SparseMatrixExample_forRows.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forAllRows_cuda SparseMatrixExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out -# OUTPUT SparseMatrixExample_forAllRows.out ) + CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out + OUTPUT SparseMatrixExample_rowsReduction.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction_cuda SparseMatrixExample_allRowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out + OUTPUT SparseMatrixExample_allRowsReduction.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_forRows_cuda SparseMatrixExample_forRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out + OUTPUT SparseMatrixExample_forRows.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixExample_forAllRows_cuda SparseMatrixExample_forAllRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out + OUTPUT SparseMatrixExample_forAllRows.out ) # CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_constructor_cuda SparseMatrixViewExample_constructor.cu ) # ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor_cuda > @@ -210,25 +210,25 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out OUTPUT SparseMatrixExample_getElement.out ) -# ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out -# OUTPUT SparseMatrixExample_rowsReduction.out ) + ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out + OUTPUT SparseMatrixExample_rowsReduction.out ) -# ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction SparseMatrixExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out -# OUTPUT SparseMatrixExample_allRowsReduction.out ) + ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction SparseMatrixExample_allRowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out + OUTPUT SparseMatrixExample_allRowsReduction.out ) -# ADD_EXECUTABLE( SparseMatrixExample_forRows SparseMatrixExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out -# OUTPUT SparseMatrixExample_forRows.out ) + ADD_EXECUTABLE( SparseMatrixExample_forRows SparseMatrixExample_forRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out + OUTPUT SparseMatrixExample_forRows.out ) -# ADD_EXECUTABLE( SparseMatrixExample_forAllRows SparseMatrixExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out -# OUTPUT SparseMatrixExample_forAllRows.out ) + ADD_EXECUTABLE( SparseMatrixExample_forAllRows SparseMatrixExample_forAllRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out + OUTPUT SparseMatrixExample_forAllRows.out ) # ADD_EXECUTABLE( SparseMatrixViewExample_constructor SparseMatrixViewExample_constructor.cpp ) # ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor > @@ -306,10 +306,10 @@ ADD_CUSTOM_TARGET( RunSparseMatricesExamples ALL DEPENDS SparseMatrixExample_setElement.out SparseMatrixExample_addElement.out SparseMatrixExample_getElement.out -# SparseMatrixExample_rowsReduction.out -# SparseMatrixExample_allRowsReduction.out -# SparseMatrixExample_forRows.out -# SparseMatrixExample_forAllRows.out + SparseMatrixExample_rowsReduction.out + SparseMatrixExample_allRowsReduction.out + SparseMatrixExample_forRows.out + SparseMatrixExample_forAllRows.out # SparseMatrixViewExample_constructor.out # SparseMatrixViewExample_getCompressedRowLengths.out # SparseMatrixViewExample_getElementsCount.out diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp new file mode 100644 index 000000000..9ce31f09e --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void allRowsReduction() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { + { 0, 0, 1 }, + { 1, 1, 1 }, { 1, 2, 8 }, + { 2, 2, 1 }, { 2, 3, 9 }, + { 3, 3, 1 }, { 3, 4, 9 }, + { 4, 4, 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.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "All rows reduction on host:" << std::endl; + allRowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "All rows reduction on CUDA device:" << std::endl; + allRowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cu new file mode 120000 index 000000000..f087b816f --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cu @@ -0,0 +1 @@ +SparseMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cpp new file mode 100644 index 000000000..739600539 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +template< typename Device > +void forAllRowsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 2, 3, 4, 5 }, 5 ); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int& columnIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) // This is important, some matrix formats may allocate more matrix elements + // than we requested. These padding elements are processed here as well. + compute = false; + else + { + columnIdx = localIdx; + value = rowIdx + localIdx; + } + }; + + matrix.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forAllRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forAllRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cu new file mode 120000 index 000000000..51cc7bd49 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cu @@ -0,0 +1 @@ +SparseMatrixExample_forAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp new file mode 100644 index 000000000..2330c2ca5 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 2, 3, 4, 5 }, 5 ); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int& columnIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) // This is important, some matrix formats may allocate more matrix elements + // than we requested. These padding elements are processed here as well. + compute = false; + else + { + columnIdx = localIdx; + value = rowIdx + localIdx; + } + }; + + matrix.forRows( 0, matrix.getRows(), f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cu new file mode 120000 index 000000000..87c20fbe0 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cu @@ -0,0 +1 @@ +SparseMatrixExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp new file mode 100644 index 000000000..20279888a --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void rowsReduction() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { + { 0, 0, 1 }, + { 1, 1, 1 }, { 1, 2, 8 }, + { 2, 2, 1 }, { 2, 3, 9 }, + { 3, 3, 1 }, { 3, 4, 9 }, + { 4, 4, 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.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Rows reduction on host:" << std::endl; + rowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + rowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cu new file mode 120000 index 000000000..212f16288 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cu @@ -0,0 +1 @@ +SparseMatrixExample_rowsReduction.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 6f68514bf..b19e8a8f9 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -472,7 +472,7 @@ class DenseMatrix : 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 columnIdx, IndexType columnIdx, const RealType& value, bool& compute )`. + * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx_, const RealType& value, bool& compute )`. * The column index repeats twice only for compatibility with sparse matrices. * If the 'compute' variable is set to false the iteration over the row can * be interrupted. @@ -494,7 +494,7 @@ class DenseMatrix : 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 columnIdx, IndexType columnIdx, RealType& value, bool& compute )`. + * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx_, RealType& value, bool& compute )`. * The column index repeats twice only for compatibility with sparse matrices. * If the 'compute' variable is set to false the iteration over the row can * be interrupted. @@ -512,7 +512,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > void forRows( IndexType begin, IndexType end, Function& function ); /** - * \brief This method calls \e forRows for all matrix rows. + * \brief This method calls \e forRows for all matrix rows (for constant instances). * * See \ref DenseMatrix::forRows. * diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 16dc35468..d45ec315f 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -570,9 +570,8 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \brief Method for performing general reduction on matrix rows. * * \tparam Fetch is a type of lambda function for data fetch declared as - * `fetch( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, RealType elementValue ) -> FetchValue`. - * Parameter \e globalIdx is position of the matrix element in arrays \e values and \e columnIdexes - * of this matrix. The return type of this lambda can be any non void. + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. * \tparam Reduce is a type of lambda function for reduction declared as * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. * \tparam Keep is a type of lambda function for storing results of reduction in each row. @@ -598,9 +597,8 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \brief Method for performing general reduction on all matrix rows. * * \tparam Fetch is a type of lambda function for data fetch declared as - * `fetch( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, RealType elementValue ) -> FetchValue`. - * Parameter \e globalIdx is position of the matrix element in arrays \e values and \e columnIdexes - * of this matrix. The return type of this lambda can be any non void. + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. * \tparam Reduce is a type of lambda function for reduction declared as * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. * \tparam Keep is a type of lambda function for storing results of reduction in each row. @@ -613,27 +611,110 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp + * \include Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp * \par Output - * \include SparseMatrixExample_rowsReduction.out + * \include SparseMatrixExample_allRowsReduction.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for iteration over all matrix rows for constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp + * \par Output + * \include SparseMatrixExample_forRows.out + */ template< typename Function > void forRows( IndexType begin, IndexType end, Function& function ) const; + /** + * \brief Method for iteration over all matrix rows for non-constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp + * \par Output + * \include SparseMatrixExample_forRows.out + */ template< typename Function > void forRows( IndexType begin, IndexType end, Function& function ); + /** + * \brief This method calls \e forRows for all matrix rows (for constant instances). + * + * See \ref SparseMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cpp + * \par Output + * \include SparseMatrixExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ) const; + /** + * \brief This method calls \e forRows for all matrix rows. + * + * See \ref SparseMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cpp + * \par Output + * \include SparseMatrixExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ); - /*** - * \brief This method computes outVector = matrixMultiplicator * ( *this ) * inVector + inVectorAddition * inVector + /** + * \brief Computes product of matrix and vector. + * + * More precisely, it computes: + * + * \e outVector = \e matrixMultiplicator * ( * \e this ) * \e inVector + \e outVectorMultiplicator * \e outVector. + * + * \tparam InVector is type of input vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * \tparam OutVector is type of output vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * + * \param inVector is input vector. + * \param outVector is output vector. + * \param matrixMultiplicator is a factor by which the matrix is multiplied. It is one by default. + * \param outVectorMultiplicator is a factor by which the outVector is multiplied before added + * to the result of matrix-vector product. It is zero by default. + * \param begin is the beginning of the rows range for which the vector product + * is computed. It is zero by default. + * \param end is the end of the rows range for which the vector product + * is computed. It is number if the matrix rows by default. */ template< typename InVector, typename OutVector > @@ -662,52 +743,117 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Assignment of exactly the same matrix type. - * @param matrix - * @return + * + * \param matrix is input matrix for the assignment. + * \return reference to this matrix. */ SparseMatrix& operator=( const SparseMatrix& matrix ); /** * \brief Assignment of dense matrix + * + * \param matrix is input matrix for the assignment. + * \return reference to this matrix. */ template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization, typename RealAllocator_ > SparseMatrix& operator=( const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocator_ >& matrix ); /** - * \brief Assignment of any other matrix type. - * @param matrix - * @return + * \brief Assignment of any matrix type other then this and dense. + * . + * \param matrix is input matrix for the assignment. + * \return reference to this matrix. */ template< typename RHSMatrix > SparseMatrix& operator=( const RHSMatrix& matrix ); + /** + * \brief Comparison operator with another arbitrary matrix type. + * + * \param matrix is the right-hand side matrix. + * \return \e true if the RHS matrix is equal, \e false otherwise. + */ template< typename Matrix > bool operator==( const Matrix& m ) const; + /** + * \brief Comparison operator with another arbitrary matrix type. + * + * \param matrix is the right-hand side matrix. + * \return \e true if the RHS matrix is equal, \e false otherwise. + */ template< typename Matrix > bool operator!=( const Matrix& m ) const; - void save( File& file ) const; - - void load( File& file ); - + /** + * \brief Method for saving the matrix to the file with given filename. + * + * \param fileName is name of the file. + */ void save( const String& fileName ) const; + + /** + * \brief Method for loading the matrix from the file with given filename. + * + * \param fileName is name of the file. + */ void load( const String& fileName ); + /** + * \brief Method for saving the matrix to a file. + * + * \param fileName is name of the file. + */ + void save( File& file ) const; + + /** + * \brief Method for loading the matrix from a file. + * + * \param fileName is name of the file. + */ + void load( File& file ); + + /** + * \brief Method for printing the matrix to output stream. + * + * \param str is the output stream. + */ void print( std::ostream& str ) const; + /** + * \brief Returns a padding index value. + * + * Padding index is used for column indexes of padding zeros. Padding zeros + * are used in some sparse matrix formats for better data alignment in memory. + * + * \return value of the padding index. + */ __cuda_callable__ IndexType getPaddingIndex() const; + /** + * \brief Getter of segments for non-constant instances. + * + * \e Segments are a structure for addressing the matrix elements columns and values. + * In fact, \e Segments represent the sparse matrix format. + * + * \return Non-constant reference to segments. + */ SegmentsType& getSegments(); + /** + * \brief Getter of segments for constant instances. + * + * \e Segments are a structure for addressing the matrix elements columns and values. + * In fact, \e Segments represent the sparse matrix format. + * + * \return Constant reference to segments. + */ const SegmentsType& getSegments() const; - -// TODO: restore it and also in Matrix -// protected: + protected: ColumnsIndexesVectorType columnIndexes; -- GitLab From 4220d2ef8eb023e57b386ac9624106f529c174d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 27 May 2020 12:55:16 +0200 Subject: [PATCH 17/38] Added non-const rowsReduction to sparse and dense matrix. --- src/TNL/Matrices/DenseMatrix.h | 52 ++++++++++++++++++++++++++ src/TNL/Matrices/DenseMatrix.hpp | 26 +++++++++++++ src/TNL/Matrices/DenseMatrixView.h | 52 ++++++++++++++++++++++++++ src/TNL/Matrices/DenseMatrixView.hpp | 29 ++++++++++++++ src/TNL/Matrices/SparseMatrix.h | 54 ++++++++++++++++++++++++++- src/TNL/Matrices/SparseMatrix.hpp | 30 +++++++++++++++ src/TNL/Matrices/SparseMatrixView.h | 6 +++ src/TNL/Matrices/SparseMatrixView.hpp | 40 ++++++++++++++++++++ 8 files changed, 288 insertions(+), 1 deletion(-) diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index b19e8a8f9..d60ca25af 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -440,6 +440,33 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_rowsReduction.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ); + + /** + * \brief Method for performing general reduction on matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp + * \par Output + * \include DenseMatrixExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const; /** @@ -465,6 +492,31 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_allRowsReduction.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + + /** + * \brief Method for performing general reduction on ALL matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp + * \par Output + * \include DenseMatrixExample_allRowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index ec7f2fb1c..844fe576b 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -332,6 +332,19 @@ getElement( const IndexType row, return this->view.getElement( row, column ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > +void +DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: +rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) +{ + this->view.rowsReduction( begin, end, fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, @@ -345,6 +358,19 @@ rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduc this->view.rowsReduction( begin, end, fetch, reduce, keep, zero ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: +allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) +{ + this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index 1706e4603..44acc5eed 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -388,6 +388,33 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixViewExample_rowsReduction.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + + /** + * \brief Method for performing general reduction on matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cpp + * \par Output + * \include DenseMatrixViewExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** @@ -413,6 +440,31 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixViewExample_allRowsReduction.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + + /** + * \brief Method for performing general reduction on ALL matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cpp + * \par Output + * \include DenseMatrixViewExample_allRowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index 95674e00f..b96a8475b 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -264,6 +264,23 @@ getElement( const IndexType row, return this->values.getElement( this->getElementIndex( row, column ) ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > +void +DenseMatrixView< Real, Device, Index, Organization >:: +rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) +{ + auto values_view = this->values.getView(); + auto fetch_ = [=] __cuda_callable__ ( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, bool& compute ) mutable -> decltype( fetch( IndexType(), IndexType(), RealType() ) ) { + return fetch( rowIdx, columnIdx, values_view[ globalIdx ] ); + return zero; + }; + this->segments.segmentsReduction( begin, end, fetch_, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, @@ -281,6 +298,18 @@ rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduc this->segments.segmentsReduction( begin, end, fetch_, reduce, keep, zero ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +DenseMatrixView< Real, Device, Index, Organization >:: +allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) +{ + this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index d45ec315f..635894416 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -570,7 +570,34 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \brief Method for performing general reduction on matrix rows. * * \tparam Fetch is a type of lambda function for data fetch declared as - * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp + * \par Output + * \include SparseMatrixExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + + /** + * \brief Method for performing general reduction on matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. * The return type of this lambda can be any non void. * \tparam Reduce is a type of lambda function for reduction declared as * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. @@ -616,6 +643,31 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include SparseMatrixExample_allRowsReduction.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + + /** + * \brief Method for performing general reduction on all matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp + * \par Output + * \include SparseMatrixExample_allRowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index 6a7ed38c0..52a0e74e7 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -468,6 +468,21 @@ vectorProduct( const InVector& inVector, this->view.vectorProduct( inVector, outVector, matrixMultiplicator, outVectorMultiplicator, firstRow, lastRow ); } +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename, typename > class Segments, + typename RealAllocator, + typename IndexAllocator > + template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > +void +SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: +rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) +{ + this->view.rowsReduction( begin, end, fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, @@ -483,6 +498,21 @@ rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduc this->view.rowsReduction( begin, end, fetch, reduce, keep, zero ); } +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename, typename > class Segments, + typename RealAllocator, + typename IndexAllocator > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: +allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) +{ + this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index 6facd7378..a2e9cef89 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -128,9 +128,15 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > const IndexType firstRow = 0, IndexType lastRow = 0 ) const; + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index 427590e64..0efa72ac4 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -435,6 +435,33 @@ vectorProduct( const InVector& inVector, this->segments.segmentsReduction( firstRow, lastRow, fetch, std::plus<>{}, keeper, ( RealType ) 0.0 ); } +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename > class SegmentsView > + template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > +void +SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >:: +rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) +{ + auto columns_view = this->columnIndexes.getView(); + auto values_view = this->values.getView(); + const IndexType paddingIndex_ = this->getPaddingIndex(); + auto fetch_ = [=] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType globalIdx, bool& compute ) mutable -> decltype( fetch( IndexType(), IndexType(), RealType() ) ) { + IndexType& columnIdx = columns_view[ globalIdx ]; + if( columnIdx != paddingIndex_ ) + { + if( isBinary() ) + return fetch( rowIdx, columnIdx, 1 ); + else + return fetch( rowIdx, columnIdx, values_view[ globalIdx ] ); + } + return zero; + }; + this->segments.segmentsReduction( begin, end, fetch_, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, @@ -462,6 +489,19 @@ rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduc this->segments.segmentsReduction( begin, end, fetch_, reduce, keep, zero ); } +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename > class SegmentsView > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >:: +allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) +{ + this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, -- GitLab From aa336ffb8e36799a93398d43491be2cb61d7698e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 27 May 2020 14:59:27 +0200 Subject: [PATCH 18/38] Writting documentation on SparseMatrixView. --- .../Matrices/SparseMatrix/CMakeLists.txt | 250 +++++---- .../SparseMatrixViewExample_addElement.cpp | 31 ++ .../SparseMatrixViewExample_addElement.cu | 1 + ...arseMatrixViewExample_allRowsReduction.cpp | 68 +++ ...parseMatrixViewExample_allRowsReduction.cu | 1 + .../SparseMatrixViewExample_forAllRows.cpp | 36 ++ .../SparseMatrixViewExample_forAllRows.cu | 1 + .../SparseMatrixViewExample_forRows.cpp | 36 ++ .../SparseMatrixViewExample_forRows.cu | 1 + ...rixViewExample_getCompressedRowLengths.cpp | 35 ++ ...trixViewExample_getCompressedRowLengths.cu | 1 + .../SparseMatrixViewExample_getConstRow.cpp | 44 ++ .../SparseMatrixViewExample_getConstRow.cu | 1 + .../SparseMatrixViewExample_getElement.cpp | 37 ++ .../SparseMatrixViewExample_getElement.cu | 1 + .../SparseMatrixViewExample_getRow.cpp | 35 ++ .../SparseMatrixViewExample_getRow.cu | 1 + ...MatrixViewExample_getSerializationType.cpp | 24 + ...eMatrixViewExample_getSerializationType.cu | 1 + .../SparseMatrixViewExample_rowsReduction.cpp | 67 +++ .../SparseMatrixViewExample_rowsReduction.cu | 1 + .../SparseMatrixViewExample_setElement.cpp | 39 ++ .../SparseMatrixViewExample_setElement.cu | 1 + src/TNL/Matrices/MatrixView.h | 5 +- src/TNL/Matrices/MatrixView.hpp | 2 +- src/TNL/Matrices/SparseMatrix.h | 32 +- src/TNL/Matrices/SparseMatrix.hpp | 2 +- src/TNL/Matrices/SparseMatrixView.h | 494 ++++++++++++++++-- src/TNL/Matrices/SparseMatrixView.hpp | 5 +- 29 files changed, 1060 insertions(+), 193 deletions(-) create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_addElement.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_addElement.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllRows.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllRows.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getCompressedRowLengths.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getCompressedRowLengths.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getConstRow.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getConstRow.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getElement.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getElement.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getSerializationType.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getSerializationType.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cu diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt index 09ec21530..3f0410315 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -84,65 +84,60 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out OUTPUT SparseMatrixExample_forAllRows.out ) -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_constructor_cuda SparseMatrixViewExample_constructor.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out -# OUTPUT SparseMatrixViewExample_constructor.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths_cuda SparseMatrixViewExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount_cuda SparseMatrixViewExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out -# OUTPUT SparseMatrixViewExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow_cuda SparseMatrixViewExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out -# OUTPUT SparseMatrixViewExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getRow_cuda SparseMatrixViewExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out -# OUTPUT SparseMatrixViewExample_getRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement_cuda SparseMatrixViewExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out -# OUTPUT SparseMatrixViewExample_setElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_addElement_cuda SparseMatrixViewExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out -# OUTPUT SparseMatrixViewExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElement_cuda SparseMatrixViewExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out -# OUTPUT SparseMatrixViewExample_getElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction_cuda SparseMatrixViewExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out -# OUTPUT SparseMatrixViewExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction_cuda SparseMatrixViewExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out -# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forRows_cuda SparseMatrixViewExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out -# OUTPUT SparseMatrixViewExample_forRows.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows_cuda SparseMatrixViewExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out -# OUTPUT SparseMatrixViewExample_forAllRows.out ) + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getSerializationType_cuda SparseMatrixViewExample_getSerializationType.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getSerializationType_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getSerializationType.out + OUTPUT SparseMatrixViewExample_getSerializationType.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths_cuda SparseMatrixViewExample_getCompressedRowLengths.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out + OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow_cuda SparseMatrixViewExample_getConstRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out + OUTPUT SparseMatrixViewExample_getConstRow.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getRow_cuda SparseMatrixViewExample_getRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out + OUTPUT SparseMatrixViewExample_getRow.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement_cuda SparseMatrixViewExample_setElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out + OUTPUT SparseMatrixViewExample_setElement.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_addElement_cuda SparseMatrixViewExample_addElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out + OUTPUT SparseMatrixViewExample_addElement.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElement_cuda SparseMatrixViewExample_getElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out + OUTPUT SparseMatrixViewExample_getElement.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction_cuda SparseMatrixViewExample_rowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out + OUTPUT SparseMatrixViewExample_rowsReduction.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction_cuda SparseMatrixViewExample_allRowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out + OUTPUT SparseMatrixViewExample_allRowsReduction.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forRows_cuda SparseMatrixViewExample_forRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out + OUTPUT SparseMatrixViewExample_forRows.out ) + + CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows_cuda SparseMatrixViewExample_forAllRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out + OUTPUT SparseMatrixViewExample_forAllRows.out ) ELSE() ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_1 SparseMatrixExample_Constructor_init_list_1.cpp ) @@ -230,65 +225,60 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out OUTPUT SparseMatrixExample_forAllRows.out ) -# ADD_EXECUTABLE( SparseMatrixViewExample_constructor SparseMatrixViewExample_constructor.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out -# OUTPUT SparseMatrixViewExample_constructor.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount SparseMatrixViewExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out -# OUTPUT SparseMatrixViewExample_getElementsCount.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow SparseMatrixViewExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out -# OUTPUT SparseMatrixViewExample_getConstRow.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getRow SparseMatrixViewExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out -# OUTPUT SparseMatrixViewExample_getRow.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out -# OUTPUT SparseMatrixViewExample_setElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_addElement SparseMatrixViewExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out -# OUTPUT SparseMatrixViewExample_addElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getElement SparseMatrixViewExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out -# OUTPUT SparseMatrixViewExample_getElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out -# OUTPUT SparseMatrixViewExample_rowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out -# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_forRows SparseMatrixViewExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out -# OUTPUT SparseMatrixViewExample_forRows.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows SparseMatrixViewExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out -# OUTPUT SparseMatrixViewExample_forAllRows.out ) + ADD_EXECUTABLE( SparseMatrixViewExample_getSerializationType SparseMatrixViewExample_getSerializationType.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getSerializationType > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getSerializationType.out + OUTPUT SparseMatrixViewExample_getSerializationType.out ) + + ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getCompressedRowLengths.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out + OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) + + ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow SparseMatrixViewExample_getConstRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out + OUTPUT SparseMatrixViewExample_getConstRow.out ) + + ADD_EXECUTABLE( SparseMatrixViewExample_getRow SparseMatrixViewExample_getRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out + OUTPUT SparseMatrixViewExample_getRow.out ) + + ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out + OUTPUT SparseMatrixViewExample_setElement.out ) + + ADD_EXECUTABLE( SparseMatrixViewExample_addElement SparseMatrixViewExample_addElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out + OUTPUT SparseMatrixViewExample_addElement.out ) + + ADD_EXECUTABLE( SparseMatrixViewExample_getElement SparseMatrixViewExample_getElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out + OUTPUT SparseMatrixViewExample_getElement.out ) + + ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_rowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out + OUTPUT SparseMatrixViewExample_rowsReduction.out ) + + ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_allRowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out + OUTPUT SparseMatrixViewExample_allRowsReduction.out ) + + ADD_EXECUTABLE( SparseMatrixViewExample_forRows SparseMatrixViewExample_forRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out + OUTPUT SparseMatrixViewExample_forRows.out ) + + ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows SparseMatrixViewExample_forAllRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out + OUTPUT SparseMatrixViewExample_forAllRows.out ) ENDIF() @@ -310,18 +300,16 @@ ADD_CUSTOM_TARGET( RunSparseMatricesExamples ALL DEPENDS SparseMatrixExample_allRowsReduction.out SparseMatrixExample_forRows.out SparseMatrixExample_forAllRows.out -# SparseMatrixViewExample_constructor.out -# SparseMatrixViewExample_getCompressedRowLengths.out -# SparseMatrixViewExample_getElementsCount.out -# SparseMatrixViewExample_getConstRow.out -# SparseMatrixViewExample_getRow.out -# SparseMatrixViewExample_setElement.out -# SparseMatrixViewExample_addElement.out -# SparseMatrixViewExample_getElement.out -# SparseMatrixViewExample_rowsReduction.out -# SparseMatrixViewExample_allRowsReduction.out -# SparseMatrixViewExample_forRows.out -# SparseMatrixViewExample_forAllRows.out - + SparseMatrixViewExample_getSerializationType.out + SparseMatrixViewExample_getCompressedRowLengths.out + SparseMatrixViewExample_getConstRow.out + SparseMatrixViewExample_getRow.out + SparseMatrixViewExample_setElement.out + SparseMatrixViewExample_addElement.out + SparseMatrixViewExample_getElement.out + SparseMatrixViewExample_rowsReduction.out + SparseMatrixViewExample_allRowsReduction.out + SparseMatrixViewExample_forRows.out + SparseMatrixViewExample_forAllRows.out ) diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_addElement.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_addElement.cpp new file mode 100644 index 000000000..987188519 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_addElement.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +template< typename Device > +void addElements() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 5, 5, 5, 5, 5 }, 5 ); + auto view = matrix.getView(); + for( int i = 0; i < 5; i++ ) + view.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++ ) + view.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 HAVE_CUDA + std::cout << "Add elements on CUDA device:" << std::endl; + addElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_addElement.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_addElement.cu new file mode 120000 index 000000000..505aa14b1 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_addElement.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp new file mode 100644 index 000000000..98a03b941 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void allRowsReduction() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { + { 0, 0, 1 }, + { 1, 1, 1 }, { 1, 2, 8 }, + { 2, 2, 1 }, { 2, 3, 9 }, + { 3, 3, 1 }, { 3, 4, 9 }, + { 4, 4, 1 } } ); + auto matrixView = matrix.getView(); + + /*** + * 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. + */ + matrixView.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "All rows reduction on host:" << std::endl; + allRowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "All rows reduction on CUDA device:" << std::endl; + allRowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cu new file mode 120000 index 000000000..d63cf05c5 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllRows.cpp new file mode 100644 index 000000000..fda71a42f --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllRows.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +template< typename Device > +void forAllRowsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 2, 3, 4, 5 }, 5 ); + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int& columnIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) // This is important, some matrix formats may allocate more matrix elements + // than we requested. These padding elements are processed here as well. + compute = false; + else + { + columnIdx = localIdx; + value = rowIdx + localIdx; + } + }; + + view.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forAllRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forAllRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllRows.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllRows.cu new file mode 120000 index 000000000..dd77d11f9 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllRows.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_forAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp new file mode 100644 index 000000000..987c3dec4 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 2, 3, 4, 5 }, 5 ); + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int& columnIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) // This is important, some matrix formats may allocate more matrix elements + // than we requested. These padding elements are processed here as well. + compute = false; + else + { + columnIdx = localIdx; + value = rowIdx + localIdx; + } + }; + + view.forRows( 0, matrix.getRows(), f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cu new file mode 120000 index 000000000..5058dc6cf --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getCompressedRowLengths.cpp new file mode 100644 index 000000000..0cc12da83 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getCompressedRowLengths.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +template< typename Device > +void getCompressedRowLengthsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > triangularMatrix( 5, 5 ); + triangularMatrix.setElements( { + { 0, 0, 1 }, + { 1, 0, 2 }, { 1, 1, 3 }, + { 2, 0, 4 }, { 2, 1, 5 }, { 2, 2, 6 }, + { 3, 0, 7 }, { 3, 1, 8 }, { 3, 2, 9 }, { 3, 3, 10 }, + { 4, 0, 11 }, { 4, 1, 12 }, { 4, 2, 13 }, { 4, 3, 14 }, { 4, 4, 15 } } ); + + std::cout << triangularMatrix << std::endl; + + auto view = triangularMatrix.getView(); + TNL::Containers::Vector< int, Device > rowLengths; + view.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 HAVE_CUDA + std::cout << "Getting compressed row lengths on CUDA device: " << std::endl; + getCompressedRowLengthsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getCompressedRowLengths.cu new file mode 120000 index 000000000..6fa51b25b --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getCompressedRowLengths.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_getCompressedRowLengths.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getConstRow.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getConstRow.cpp new file mode 100644 index 000000000..8c10b6a80 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getConstRow.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { + { 0, 0, 1 }, + { 1, 0, 1 }, { 1, 1, 2 }, + { 2, 0, 1 }, { 2, 1, 2 }, { 2, 2, 3 }, + { 3, 0, 1 }, { 3, 1, 2 }, { 3, 2, 3 }, { 3, 3, 4 }, + { 4, 0, 1 }, { 4, 1, 2 }, { 4, 2, 3 }, { 4, 3, 4 }, { 4, 4, 5 } } ); + auto matrixView = matrix.getView(); + + /*** + * Fetch lambda function returns diagonal element in each row. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { + auto row = matrixView.getRow( rowIdx ); + return row.getValue( rowIdx ); + }; + + /*** + * Compute the matrix trace. + */ + int trace = TNL::Algorithms::Reduction< Device >::reduce( matrix.getRows(), std::plus<>{}, fetch, 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 HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getConstRow.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getConstRow.cu new file mode 120000 index 000000000..a1501e378 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getConstRow.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_getConstRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getElement.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getElement.cpp new file mode 100644 index 000000000..e14134f32 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getElement.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +template< typename Device > +void getElements() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( + 5, // number of matrix rows + 5, // number of matrix columns + { // matrix elements definition + { 0, 0, 2.0 }, + { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, + { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, + { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, + { 4, 4, 2.0 } } ); + auto view = matrix.getView(); + + for( int i = 0; i < 5; i++ ) + { + for( int j = 0; j < 5; j++ ) + std::cout << std::setw( 5 ) << view.getElement( i, j ); + std::cout << std::endl; + } +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get elements on host:" << std::endl; + getElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get elements on CUDA device:" << std::endl; + getElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getElement.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getElement.cu new file mode 120000 index 000000000..03d5d32f2 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getElement.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_getElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp new file mode 100644 index 000000000..569fabb6a --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 1, 1, 1, 1 }, 5 ); + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { + auto row = view.getRow( rowIdx ); + row.setElement( 0, rowIdx, 10 * ( rowIdx + 1 ) ); + }; + + /*** + * Set the matrix elements. + */ + TNL::Algorithms::ParallelFor< Device >::exec( 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 HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cu new file mode 120000 index 000000000..56b0f7e62 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_getRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getSerializationType.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getSerializationType.cpp new file mode 100644 index 000000000..3147bd291 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getSerializationType.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + + +template< typename Device > +void getSerializationTypeExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix; + auto view = matrix.getView(); + + std::cout << "Matrix type is: " << view.getSerializationType(); +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get serialization type on CPU ... " << std::endl; + getSerializationTypeExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get serialization type on CUDA GPU ... " << std::endl; + getSerializationTypeExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getSerializationType.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getSerializationType.cu new file mode 120000 index 000000000..9ddc5c6f2 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getSerializationType.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_getSerializationType.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp new file mode 100644 index 000000000..69e2ff6fd --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void rowsReduction() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { + { 0, 0, 1 }, + { 1, 1, 1 }, { 1, 2, 8 }, + { 2, 2, 1 }, { 2, 3, 9 }, + { 3, 3, 1 }, { 3, 4, 9 }, + { 4, 4, 1 } } ); + auto matrixView = matrix.getView(); + + /*** + * Find largest element in each row. + */ + TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() ); + + /*** + * Prepare vector view for lambdas. + */ + auto rowMaxView = rowMax.getView(); + + /*** + * Fetch lambda just returns absolute value of matrix elements. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx, int columnIdx, const double& value ) -> double { + return TNL::abs( value ); + }; + + /*** + * Reduce lambda return maximum of given values. + */ + auto reduce = [=] __cuda_callable__ ( 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. + */ + matrixView.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Rows reduction on host:" << std::endl; + rowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + rowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cu new file mode 120000 index 000000000..f244c8372 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cpp new file mode 100644 index 000000000..3de6634a3 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void setElements() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 1, 1, 1, 1 }, 5 ); + auto view = matrix.getView(); + for( int i = 0; i < 5; i++ ) + view.setElement( i, i, i ); + + std::cout << "Matrix set from the host:" << std::endl; + std::cout << matrix << std::endl; + + auto f = [=] __cuda_callable__ ( int i ) mutable { + view.setElement( i, i, -i ); + }; + + TNL::Algorithms::ParallelFor< Device >::exec( 0, 5, 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 HAVE_CUDA + std::cout << "Set elements on CUDA device:" << std::endl; + setElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cu new file mode 120000 index 000000000..3dba0b9ec --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_setElement.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/MatrixView.h b/src/TNL/Matrices/MatrixView.h index 76a3948a9..9c23e539f 100644 --- a/src/TNL/Matrices/MatrixView.h +++ b/src/TNL/Matrices/MatrixView.h @@ -49,9 +49,12 @@ public: __cuda_callable__ MatrixView( const MatrixView& view ) = default; + __cuda_callable__ + MatrixView( MatrixView&& view ) = default; + IndexType getAllocatedElementsCount() const; - virtual IndexType getNumberOfNonzeroMatrixElements() const; + virtual IndexType getNonzeroElementsCount() const; __cuda_callable__ IndexType getRows() const; diff --git a/src/TNL/Matrices/MatrixView.hpp b/src/TNL/Matrices/MatrixView.hpp index b2b181e4c..e10874201 100644 --- a/src/TNL/Matrices/MatrixView.hpp +++ b/src/TNL/Matrices/MatrixView.hpp @@ -57,7 +57,7 @@ template< typename Real, typename Index > Index MatrixView< Real, Device, Index >:: -getNumberOfNonzeroMatrixElements() const +getNonzeroElementsCount() const { const auto values_view = this->values.getConstView(); auto fetch = [=] __cuda_callable__ ( const IndexType i ) -> IndexType { diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 635894416..ba649e5d0 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -68,6 +68,20 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > using RowsCapacitiesView = Containers::VectorView< Index, Device, Index >; using ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType; + /** + * \brief Test of symmetric matrix type. + * + * \return \e true if the matrix is stored as symmetric and \e false otherwise. + */ + static constexpr bool isSymmetric() { return MatrixType::isSymmetric(); }; + + /** + * \brief Test of binary matrix type. + * + * \return \e true if the matrix is stored as binary and \e false otherwise. + */ + static constexpr bool isBinary() { return MatrixType::isBinary(); }; + /** * \brief The type of matrix elements. */ @@ -129,12 +143,10 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > */ using ConstViewType = SparseMatrixView< std::add_const_t< Real >, Device, Index, MatrixType, SegmentsViewTemplate >; - //using SegmentViewType = typename SegmentsType::SegmentViewType; - /** * \brief Type for accessing matrix rows. */ - using RowView = SparseMatrixRowView< typename SegmentsType::SegmentViewType, ValuesViewType, ColumnsIndexesViewType, MatrixType::isBinary() >; + using RowView = SparseMatrixRowView< typename SegmentsType::SegmentViewType, ValuesViewType, ColumnsIndexesViewType, isBinary() >; /** * \brief Type for accessing constant matrix rows. @@ -153,20 +165,6 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > typename _IndexAllocator = typename Allocators::Default< _Device >::template Allocator< _Index > > using Self = SparseMatrix< _Real, _Device, _Index, _MatrixType, _Segments, _RealAllocator, _IndexAllocator >; - /** - * \brief Test of symmetric matrix type. - * - * \return \e true if the matrix is stored as symmetric and \e false otherwise. - */ - static constexpr bool isSymmetric() { return MatrixType::isSymmetric(); }; - - /** - * \brief Test of binary matrix type. - * - * \return \e true if the matrix is stored as binary and \e false otherwise. - */ - static constexpr bool isBinary() { return MatrixType::isBinary(); }; - /** * \brief Constructor only with values and column indexes allocators. * diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index 52a0e74e7..d60d49611 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -350,7 +350,7 @@ Index SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAllocator >:: getNonzeroElementsCount() const { - return this->view.getNumberOfNonzeroMatrixElements(); + return this->view.getNonzeroElementsCount(); } template< typename Real, diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index a2e9cef89..bc608696d 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -19,6 +19,24 @@ namespace TNL { namespace Matrices { +/** + * \brief Implementation of sparse matrix view. + * + * It serves as an accessor to \ref SparseMatrix for example when passing the + * matrix to lambda functions. SparseMatrix view can be also created in CUDA kernels. + * + * \tparam Real is a type of matrix elements. + * \tparam Device is a device where the matrix is allocated. + * \tparam Index is a type for indexing of the matrix elements. + * \tparam MatrixType specifies the type of matrix - its symmetry or binarity. See \ref MatrixType. + * Both symmetric and binary matrix types reduces memory consumption. Binary matrix does not store + * the matrix values explicitly since the non-zero elements can have only value equal to one. Symmetric + * matrices store only lower part of the matrix and its diagonal. The upper part is reconstructed on the fly. + * GeneralMatrix with no symmetry is used by default. + * \tparam Segments is a structure representing the sparse matrix format. Depending on the pattern of the non-zero elements + * different matrix formats can perform differently especially on GPUs. By default \ref CSR format is used. See also + * \ref Ellpack, \ref SlicedEllpack, \ref ChunkedEllpack or \ref BiEllpack. + */ template< typename Real, typename Device = Devices::Host, typename Index = int, @@ -26,37 +44,108 @@ template< typename Real, template< typename Device_, typename Index_ > class SegmentsView = Containers::Segments::CSRView > class SparseMatrixView : public MatrixView< Real, Device, Index > { + static_assert( + ! MatrixType::isSymmetric() || + ! std::is_same< Device, Devices::Cuda >::value || + ( std::is_same< Real, float >::value || std::is_same< Real, double >::value || std::is_same< Real, int >::value || std::is_same< Real, long long int >::value ), + "Given Real type is not supported by atomic operations on GPU which are necessary for symmetric operations." ); + public: + + // Supporting types - they are not important for the user + using BaseType = MatrixView< Real, Device, Index >; + using ValuesViewType = typename BaseType::ValuesView; + using ConstValuesViewType = typename ValuesViewType::ConstViewType; + using ColumnsIndexesViewType = Containers::VectorView< Index, Device, Index >; + using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType; + using RowsCapacitiesView = Containers::VectorView< Index, Device, Index >; + using ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType; + + /** + * \brief Test of symmetric matrix type. + * + * \return \e true if the matrix is stored as symmetric and \e false otherwise. + */ static constexpr bool isSymmetric() { return MatrixType::isSymmetric(); }; + + /** + * \brief Test of binary matrix type. + * + * \return \e true if the matrix is stored as binary and \e false otherwise. + */ static constexpr bool isBinary() { return MatrixType::isBinary(); }; + /** + * \brief The type of matrix elements. + */ using RealType = Real; + + /** + * \brief The device where the matrix is allocated. + */ + using DeviceType = Device; + + /** + * \brief The type used for matrix elements indexing. + */ + using IndexType = Index; + + /** + * \brief Templated type of segments view, i.e. sparse matrix format. + */ template< typename Device_, typename Index_ > using SegmentsViewTemplate = SegmentsView< Device_, Index_ >; + + /** + * \brief Type of segments view used by this matrix. It represents the sparse matrix format. + */ using SegmentsViewType = SegmentsView< Device, Index >; - using SegmentViewType = typename SegmentsViewType::SegmentViewType; - using DeviceType = Device; - using IndexType = Index; - using BaseType = MatrixView< Real, Device, Index >; - using RowsCapacitiesView = Containers::VectorView< IndexType, DeviceType, IndexType >; - using ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType; - using ValuesViewType = typename BaseType::ValuesView; - using ConstValuesViewType = typename ValuesViewType::ConstViewType; - using ColumnsIndexesViewType = Containers::VectorView< IndexType, DeviceType, IndexType >; - using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType; + + /** + * \brief Type of related matrix view. + */ using ViewType = SparseMatrixView< std::remove_const_t< Real >, Device, Index, MatrixType, SegmentsViewTemplate >; + + /** + * \brief Matrix view type for constant instances. + */ using ConstViewType = SparseMatrixView< std::add_const_t< Real >, Device, std::add_const_t< Index >, MatrixType, SegmentsViewTemplate >; - using RowView = SparseMatrixRowView< SegmentViewType, ValuesViewType, ColumnsIndexesViewType, isBinary() >; - using ConstRowView = typename RowView::ConstViewType; - // TODO: remove this - it is here only for compatibility with original matrix implementation - typedef Containers::Vector< IndexType, DeviceType, IndexType > CompressedRowLengthsVector; - typedef Containers::VectorView< IndexType, DeviceType, IndexType > CompressedRowLengthsVectorView; - typedef typename CompressedRowLengthsVectorView::ConstViewType ConstCompressedRowLengthsVectorView; + /** + * \brief Type for accessing matrix rows. + */ + using RowView = SparseMatrixRowView< typename SegmentsViewType::SegmentViewType, ValuesViewType, ColumnsIndexesViewType, isBinary() >; + + /** + * \brief Type for accessing constant matrix rows. + */ + using ConstRowView = typename RowView::ConstViewType; + /** + * \brief Helper type for getting self type or its modifications. + */ + template< typename _Real = Real, + typename _Device = Device, + typename _Index = Index, + typename _MatrixType = MatrixType, + template< typename, typename > class _SegmentsView = SegmentsView > + using Self = SparseMatrixView< _Real, _Device, _Index, _MatrixType, _SegmentsView >; + + /** + * \brief Constructor with no parameters. + */ __cuda_callable__ SparseMatrixView(); + /** + * \brief Constructor with all necessary data and views. + * + * \param rows is a number of matrix rows. + * \param columns is a number of matrix columns. + * \param values is a vector view with matrix elements values. + * \param columnIndexes is a vector view with matrix elements column indexes. + * \param segments is a segments view representing the sparse matrix format. + */ __cuda_callable__ SparseMatrixView( const IndexType rows, const IndexType columns, @@ -64,94 +153,423 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > const ColumnsIndexesViewType& columnIndexes, const SegmentsViewType& segments ); + /** + * \brief Copy constructor. + * + * \param m is an input sparse matrix view. + */ __cuda_callable__ SparseMatrixView( const SparseMatrixView& m ) = default; - //__cuda_callable__ - //SparseMatrixView( const SparseMatrixView&& m ) = default; + /** + * \brief Move constructor. + * + * \param m is an input sparse matrix view. + */ + __cuda_callable__ + SparseMatrixView( SparseMatrixView&& m ) = default; + /** + * \brief Returns a modifiable view of the sparse matrix. + * + * \return sparse matrix view. + */ __cuda_callable__ ViewType getView(); + /** + * \brief Returns a non-modifiable view of the sparse matrix. + * + * \return sparse matrix view. + */ __cuda_callable__ ConstViewType getConstView() const; + /** + * \brief Returns string with serialization type. + * + * The string has a form \e `Matrices::SparseMatrix< RealType, [any_device], IndexType, General/Symmetric, Format, [any_allocator] >`. + * + * \return \e String with the serialization type. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_getSerializationType.cpp + * \par Output + * \include SparseMatrixViewExample_getSerializationType.out + */ static String getSerializationType(); + /** + * \brief Returns string with serialization type. + * + * See \ref SparseMatrix::getSerializationType. + * + * \return \e String with the serialization type. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp + * \par Output + * \include SparseMatrixExample_getSerializationType.out + */ virtual String getSerializationTypeVirtual() const; + /** + * \brief Computes number of non-zeros in each row. + * + * \param rowLengths is a vector into which the number of non-zeros in each row + * will be stored. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_getCompressedRowLengths.cpp + * \par Output + * \include SparseMatrixViewExample_getCompressedRowLengths.out + */ template< typename Vector > void getCompressedRowLengths( Vector& rowLengths ) const; + /** + * \brief Returns capacity of given matrix row. + * + * \param row index of matrix row. + * \return number of matrix elements allocated for the row. + */ __cuda_callable__ IndexType getRowCapacity( const IndexType row ) const; - IndexType getNumberOfNonzeroMatrixElements() const; - - void reset(); - + /** + * \brief Returns number of non-zero matrix elements. + * + * This method really counts the non-zero matrix elements and so + * it returns zero for matrix having all allocated elements set to zero. + * + * \return number of non-zero matrix elements. + */ + IndexType getNonzeroElementsCount() const; + + /** + * \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/SparseMatrix/SparseMatrixViewExample_getConstRow.cpp + * \par Output + * \include SparseMatrixViewExample_getConstRow.out + * + * See \ref SparseMatrixRowView. + */ __cuda_callable__ ConstRowView 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/SparseMatrix/SparseMatrixViewExample_getRow.cpp + * \par Output + * \include SparseMatrixViewExample_getRow.out + * + * See \ref SparseMatrixRowView. + */ __cuda_callable__ RowView getRow( const IndexType& rowIdx ); + /** + * \brief Sets element at given \e row and \e column to given \e 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. \ref SparseMatrix::getRow + * or \ref SparseMatrix::forRows and \ref SparseMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cpp + * \par Output + * \include SparseMatrixViewExample_setElement.out + */ __cuda_callable__ void setElement( const IndexType row, const IndexType column, const RealType& value ); + /** + * \brief Add element at given \e row and \e column to given \e 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. \ref SparseMatrix::getRow + * or \ref SparseMatrix::forRows and \ref SparseMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * \param thisElementMultiplicator is multiplicator the original matrix element + * value is multiplied by before addition of given \e value. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_addElement.cpp + * \par Output + * \include SparseMatrixViewExample_addElement.out + * + */ __cuda_callable__ void addElement( IndexType row, IndexType column, const RealType& value, const RealType& thisElementMultiplicator = 1.0 ); + /** + * \brief 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. \ref SparseMatrix::getRow + * or \ref SparseMatrix::forRows and \ref SparseMatrix::forAllRows. + * + * \param row is a row index of the matrix element. + * \param column i a column index of the matrix element. + * + * \return value of given matrix element. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_getElement.cpp + * \par Output + * \include SparseMatrixViewExample_getElement.out + * + */ __cuda_callable__ RealType getElement( IndexType row, IndexType column ) const; - /*template< typename Vector > - __cuda_callable__ - typename Vector::RealType rowVectorProduct( const IndexType row, - const Vector& vector ) const;*/ - - /*** - * \brief This method computes outVector = matrixMultiplicator * ( *this ) * inVector + inVectorAddition * inVector + /** + * \brief Method for performing general reduction on matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp + * \par Output + * \include SparseMatrixViewExample_rowsReduction.out */ - template< typename InVector, - typename OutVector > - void vectorProduct( const InVector& inVector, - OutVector& outVector, - const RealType matrixMultiplicator = 1.0, - const RealType outVectorMultiplicator = 0.0, - const IndexType firstRow = 0, - IndexType lastRow = 0 ) const; - template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + /** + * \brief Method for performing general reduction on matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp + * \par Output + * \include SparseMatrixViewExample_rowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for performing general reduction on all matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp + * \par Output + * \include SparseMatrixViewExample_allRowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + /** + * \brief Method for performing general reduction on all matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp + * \par Output + * \include SparseMatrixViewExample_allRowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for iteration over all matrix rows for constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp + * \par Output + * \include SparseMatrixViewExample_forRows.out + */ template< typename Function > void forRows( IndexType begin, IndexType end, Function& function ) const; + /** + * \brief Method for iteration over all matrix rows for non-constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp + * \par Output + * \include SparseMatrixViewExample_forRows.out + */ template< typename Function > void forRows( IndexType begin, IndexType end, Function& function ); + /** + * \brief This method calls \e forRows for all matrix rows (for constant instances). + * + * See \ref SparseMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_forAllRows.cpp + * \par Output + * \include SparseMatrixViewExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ) const; + /** + * \brief This method calls \e forRows for all matrix rows. + * + * See \ref SparseMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_forAllRows.cpp + * \par Output + * \include SparseMatrixViewExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ); + /** + * \brief Computes product of matrix and vector. + * + * More precisely, it computes: + * + * \e outVector = \e matrixMultiplicator * ( * \e this ) * \e inVector + \e outVectorMultiplicator * \e outVector. + * + * \tparam InVector is type of input vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * \tparam OutVector is type of output vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * + * \param inVector is input vector. + * \param outVector is output vector. + * \param matrixMultiplicator is a factor by which the matrix is multiplied. It is one by default. + * \param outVectorMultiplicator is a factor by which the outVector is multiplied before added + * to the result of matrix-vector product. It is zero by default. + * \param begin is the beginning of the rows range for which the vector product + * is computed. It is zero by default. + * \param end is the end of the rows range for which the vector product + * is computed. It is number if the matrix rows by default. + */ + template< typename InVector, + typename OutVector > + void vectorProduct( const InVector& inVector, + OutVector& outVector, + const RealType matrixMultiplicator = 1.0, + const RealType outVectorMultiplicator = 0.0, + const IndexType firstRow = 0, + IndexType lastRow = 0 ) const; + template< typename Vector1, typename Vector2 > bool performSORIteration( const Vector1& b, const IndexType row, diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index 0efa72ac4..c95128f7a 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -150,7 +150,7 @@ template< typename Real, template< typename, typename > class SegmentsView > Index SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >:: -getNumberOfNonzeroMatrixElements() const +getNonzeroElementsCount() const { const auto columns_view = this->columnIndexes.getConstView(); const IndexType paddingIndex = this->getPaddingIndex(); @@ -175,9 +175,6 @@ getNumberOfNonzeroMatrixElements() const return 0.0; return 1 + ( column != row && column < rows && row < columns ); // the addition is for non-diagonal elements }; - //auto reduction = [] __cuda_callable__ ( IndexType& sum, const IndexType& value ) { - // sum += value; - //}; auto keeper = [=] __cuda_callable__ ( IndexType row, const IndexType& value ) mutable { row_sums_view[ row ] = value; }; -- GitLab From eb12d705bd830e26b5068a462ab9ca5535cfa73e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 27 May 2020 17:09:04 +0200 Subject: [PATCH 19/38] Writting documentation on SparseMatrixRowView. --- src/TNL/Matrices/SparseMatrixRowView.h | 137 ++++++++++++++++++++++++- 1 file changed, 135 insertions(+), 2 deletions(-) diff --git a/src/TNL/Matrices/SparseMatrixRowView.h b/src/TNL/Matrices/SparseMatrixRowView.h index c859655ef..71555ab39 100644 --- a/src/TNL/Matrices/SparseMatrixRowView.h +++ b/src/TNL/Matrices/SparseMatrixRowView.h @@ -17,6 +17,26 @@ namespace TNL { namespace Matrices { +/** + * \brief RowView is a simple structure for accessing rows of sparse matrix. + * + * \tparam SegmentView is a segment view of segments representing the matrix format. + * \tparam ValuesView is a vector view storing the matrix elements values. + * \tparam ColumnsIndexesView is a vector view storing the column indexes of the matrix element. + * \tparam isBinary tells if the the parent matrix is a binary matrix. + * + * See \ref SparseMatrix and \ref SparseMatrixView. + * + * \par Example + * \include Matrices/SparseMatrixExample_getRow.cpp + * \par Output + * \include SparseMatrixExample_getRow.out + * + * \par Example + * \include Matrices/SparseMatrixViewExample_getRow.cpp + * \par Output + * \include SparseMatrixViewExample_getRow.out + */ template< typename SegmentView, typename ValuesView, typename ColumnsIndexesView, @@ -25,46 +45,152 @@ class SparseMatrixRowView { public: + /** + * \brief The type of matrix elements. + */ using RealType = typename ValuesView::RealType; + + /** + * \brief The type used for matrix elements indexing. + */ + using IndexType = typename ColumnsIndexesView::IndexType; + + /** + * \brief Type representing matrix row format. + */ using SegmentViewType = SegmentView; - using IndexType = typename SegmentViewType::IndexType; + + /** + * \brief Type of container view used for storing the matrix elements values. + */ using ValuesViewType = ValuesView; + + /** + * \brief Type of container view used for storing the column indexes of the matrix elements. + */ using ColumnsIndexesViewType = ColumnsIndexesView; + + /** + * \brief Type of constant container view used for storing the matrix elements values. + */ using ConstValuesViewType = typename ValuesViewType::ConstViewType; + + /** + * \brief Type of constant container view used for storing the column indexes of the matrix elements. + */ using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType; + + /** + * \brief Type of constant sparse matrix row view. + */ using ConstViewType = SparseMatrixRowView< SegmentView, ConstValuesViewType, ConstColumnsIndexesViewType, isBinary_ >; + /** + * \brief Tells whether the parent matrix is a binary matrix. + * @return + */ static constexpr bool isBinary() { return isBinary_; }; + /** + * \brief Constructor with \e segmentView, \e values and \e columnIndexes. + * + * \param segmentView instance of SegmentViewType representing matrix row. + * \param values is a container view for storing the matrix elements values. + * \param columnIndexes is a container view for storing the column indexes of the matrix elements. + */ __cuda_callable__ SparseMatrixRowView( const SegmentViewType& segmentView, const ValuesViewType& values, const ColumnsIndexesViewType& columnIndexes ); + /** + * \brief Returns size of the matrix row, i.e. number of matrix elements in this row. + * + * \return Size of the matrix row. + */ __cuda_callable__ IndexType getSize() const; + /** + * \brief Returns constants reference to a column index of an element with given rank in the row. + * + * \param localIdx is the rank of the non-zero element in given row. + * + * \return constant reference to the matrix element column index. + */ __cuda_callable__ const IndexType& getColumnIndex( const IndexType localIdx ) const; + /** + * \brief Returns non-constants reference to a column index of an element with given rank in the row. + * + * \param localIdx is the rank of the non-zero element in given row. + * + * \return non-constant reference to the matrix element column index. + */ __cuda_callable__ IndexType& getColumnIndex( const IndexType localIdx ); + /** + * \brief Returns constants reference to value of an element with given rank in the row. + * + * \param localIdx is the rank of the non-zero element in given row. + * + * \return constant reference to the matrix element value. + */ __cuda_callable__ const RealType& getValue( const IndexType localIdx ) const; + /** + * \brief Returns non-constants reference to value of an element with given rank in the row. + * + * \param localIdx is the rank of the non-zero element in given row. + * + * \return non-constant reference to the matrix element value. + */ __cuda_callable__ RealType& getValue( const IndexType localIdx ); + /** + * \brief Sets a value of matrix element with given rank in the matrix row. + * + * \param localIdx is the rank of the matrix element in the row. + * \param value is the new value of the matrix element. + */ __cuda_callable__ void setValue( const IndexType localIdx, const RealType& value ); + /** + * \brief Sets a column index of matrix element with given rank in the matrix row. + * + * \param localIdx is the rank of the matrix element in the row. + * \param columnIndex is the new column index of the matrix element. + */ + __cuda_callable__ + void setColumnIndex( const IndexType localIdx, + const RealType& columnIndex ); + + /** + * \brief Sets both a value and a column index of matrix element with given rank in the matrix row. + * + * \param localIdx is the rank of the matrix element in the row. + * \param columnIndex is the new column index of the matrix element. + * \param value is the new value of the matrix element. + */ __cuda_callable__ void setElement( const IndexType localIdx, - const IndexType column, + const IndexType columnIndex, const RealType& value ); + /** + * \brief Comparison of two matrix rows. + * + * The other matrix row can be from any other matrix. + * + * \param other is another matrix row. + * \return \e true if both rows are the same, \e false otherwise. + */ template< typename _SegmentView, typename _ValuesView, typename _ColumnsIndexesView, @@ -81,6 +207,13 @@ class SparseMatrixRowView ColumnsIndexesViewType columnIndexes; }; +/** + * \brief Insertion operator for a sparse matrix row. + * + * \param str is an output stream. + * \param row is an input sparse matrix row. + * \return reference to the output stream. + */ template< typename SegmentView, typename ValuesView, typename ColumnsIndexesView, -- GitLab From 60958e9a868b771928bf7a0e6cfe53ce1db71ba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 28 May 2020 21:00:13 +0200 Subject: [PATCH 20/38] Writting documentation on LambdaMatrix. --- .../Matrices/LambdaMatrix/CMakeLists.txt | 333 ++--------- .../LambdaMatrixExample_Constructor.cpp | 35 ++ .../LambdaMatrixExample_allRowsReduction.cpp | 72 +++ .../LambdaMatrixExample_allRowsReduction.cu | 1 + .../LambdaMatrixExample_forAllRows.cpp | 43 ++ .../LambdaMatrixExample_forAllRows.cu | 1 + .../LambdaMatrixExample_forRows.cpp | 43 ++ .../LambdaMatrixExample_forRows.cu | 1 + ...aMatrixExample_getCompressedRowLengths.cpp | 23 + ...daMatrixExample_getCompressedRowLengths.cu | 1 + ...aMatrixExample_getNonzeroElementsCount.cpp | 20 + .../LambdaMatrixExample_rowsReduction.cpp | 72 +++ .../LambdaMatrixExample_rowsReduction.cu | 1 + .../MultidiagonalMatrix/CMakeLists.txt | 546 +++++++++--------- .../Matrices/TridiagonalMatrix/CMakeLists.txt | 546 +++++++++--------- src/TNL/Matrices/DenseMatrix.h | 2 +- src/TNL/Matrices/DenseMatrixView.h | 2 +- src/TNL/Matrices/LambdaMatrix.h | 333 +++++++++-- src/TNL/Matrices/LambdaMatrix.hpp | 132 +++-- src/TNL/Matrices/SparseMatrix.h | 2 +- src/TNL/Matrices/SparseMatrixView.h | 2 +- src/TNL/Matrices/SparseMatrixView.hpp | 25 +- src/UnitTests/Matrices/LambdaMatrixTest.h | 8 - src/UnitTests/Matrices/LambdaMatrixTest.hpp | 58 +- 24 files changed, 1300 insertions(+), 1002 deletions(-) create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cu create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cpp create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cu create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cpp create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cu create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cpp create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cu diff --git a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt index 64655b0a7..6315309b2 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt @@ -1,282 +1,69 @@ -IF( BUILD_CUDA ) -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_cuda SparseMatrixExample_Constructor_init_list.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out -# OUTPUT SparseMatrixExample_Constructor_init_list.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out -# OUTPUT SparseMatrixExample_setElements.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths_cuda SparseMatrixExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElementsCount_cuda SparseMatrixExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out -# OUTPUT SparseMatrixExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getConstRow_cuda SparseMatrixExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out -# OUTPUT SparseMatrixExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getRow_cuda SparseMatrixExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out -# OUTPUT SparseMatrixExample_getRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement_cuda SparseMatrixExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out -# OUTPUT SparseMatrixExample_setElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement_cuda SparseMatrixExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out -# OUTPUT SparseMatrixExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElement_cuda SparseMatrixExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out -# OUTPUT SparseMatrixExample_getElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out -# OUTPUT SparseMatrixExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction_cuda SparseMatrixExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out -# OUTPUT SparseMatrixExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forRows_cuda SparseMatrixExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out -# OUTPUT SparseMatrixExample_forRows.out ) +ADD_EXECUTABLE( LambdaMatrixExample_Constructor LambdaMatrixExample_Constructor.cpp ) +ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_Constructor > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_Constructor.out + OUTPUT LambdaMatrixExample_Constructor.out ) -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forAllRows_cuda SparseMatrixExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out -# OUTPUT SparseMatrixExample_forAllRows.out ) +ADD_EXECUTABLE( LambdaMatrixExample_getCompressedRowLengths LambdaMatrixExample_getCompressedRowLengths.cpp ) +ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_getCompressedRowLengths > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_getCompressedRowLengths.out + OUTPUT LambdaMatrixExample_getCompressedRowLengths.out ) -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_constructor_cuda SparseMatrixViewExample_constructor.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out -# OUTPUT SparseMatrixViewExample_constructor.out ) +ADD_EXECUTABLE( LambdaMatrixExample_getNonzeroElementsCount LambdaMatrixExample_getNonzeroElementsCount.cpp ) +ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_getNonzeroElementsCount > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_getNonzeroElementsCount.out + OUTPUT LambdaMatrixExample_getNonzeroElementsCount.out ) -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths_cuda SparseMatrixViewExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount_cuda SparseMatrixViewExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out -# OUTPUT SparseMatrixViewExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow_cuda SparseMatrixViewExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out -# OUTPUT SparseMatrixViewExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getRow_cuda SparseMatrixViewExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out -# OUTPUT SparseMatrixViewExample_getRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement_cuda SparseMatrixViewExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out -# OUTPUT SparseMatrixViewExample_setElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_addElement_cuda SparseMatrixViewExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out -# OUTPUT SparseMatrixViewExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElement_cuda SparseMatrixViewExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out -# OUTPUT SparseMatrixViewExample_getElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction_cuda SparseMatrixViewExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out -# OUTPUT SparseMatrixViewExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction_cuda SparseMatrixViewExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out -# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forRows_cuda SparseMatrixViewExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out -# OUTPUT SparseMatrixViewExample_forRows.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows_cuda SparseMatrixViewExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out -# OUTPUT SparseMatrixViewExample_forAllRows.out ) +IF( BUILD_CUDA ) + CUDA_ADD_EXECUTABLE( LambdaMatrixExample_rowsReduction_cuda LambdaMatrixExample_rowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_rowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_rowsReduction.out + OUTPUT LambdaMatrixExample_rowsReduction.out ) + + CUDA_ADD_EXECUTABLE( LambdaMatrixExample_allRowsReduction_cuda LambdaMatrixExample_allRowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_allRowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_allRowsReduction.out + OUTPUT LambdaMatrixExample_allRowsReduction.out ) + + CUDA_ADD_EXECUTABLE( LambdaMatrixExample_forRows_cuda LambdaMatrixExample_forRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forRows.out + OUTPUT LambdaMatrixExample_forRows.out ) + + CUDA_ADD_EXECUTABLE( LambdaMatrixExample_forAllRows_cuda LambdaMatrixExample_forAllRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forAllRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forAllRows.out + OUTPUT LambdaMatrixExample_forAllRows.out ) ELSE() -# ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list SparseMatrixExample_Constructor_init_list.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out -# OUTPUT SparseMatrixExample_Constructor_init_list.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out -# OUTPUT SparseMatrixExample_setElements.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths SparseMatrixExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getElementsCount SparseMatrixExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out -# OUTPUT SparseMatrixExample_getElementsCount.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getConstRow SparseMatrixExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out -# OUTPUT SparseMatrixExample_getConstRow.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getRow SparseMatrixExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out -# OUTPUT SparseMatrixExample_getRow.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out -# OUTPUT SparseMatrixExample_setElement.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out -# OUTPUT SparseMatrixExample_addElement.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getElement SparseMatrixExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out -# OUTPUT SparseMatrixExample_getElement.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out -# OUTPUT SparseMatrixExample_rowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction SparseMatrixExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out -# OUTPUT SparseMatrixExample_allRowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_forRows SparseMatrixExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out -# OUTPUT SparseMatrixExample_forRows.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_forAllRows SparseMatrixExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out -# OUTPUT SparseMatrixExample_forAllRows.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_constructor SparseMatrixViewExample_constructor.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out -# OUTPUT SparseMatrixViewExample_constructor.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount SparseMatrixViewExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out -# OUTPUT SparseMatrixViewExample_getElementsCount.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow SparseMatrixViewExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out -# OUTPUT SparseMatrixViewExample_getConstRow.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getRow SparseMatrixViewExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out -# OUTPUT SparseMatrixViewExample_getRow.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out -# OUTPUT SparseMatrixViewExample_setElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_addElement SparseMatrixViewExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out -# OUTPUT SparseMatrixViewExample_addElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getElement SparseMatrixViewExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out -# OUTPUT SparseMatrixViewExample_getElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out -# OUTPUT SparseMatrixViewExample_rowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out -# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_forRows SparseMatrixViewExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out -# OUTPUT SparseMatrixViewExample_forRows.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows SparseMatrixViewExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out -# OUTPUT SparseMatrixViewExample_forAllRows.out ) - + ADD_EXECUTABLE( LambdaMatrixExample_rowsReduction LambdaMatrixExample_rowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_rowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_rowsReduction.out + OUTPUT LambdaMatrixExample_rowsReduction.out ) + + ADD_EXECUTABLE( LambdaMatrixExample_allRowsReduction LambdaMatrixExample_allRowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_allRowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_allRowsReduction.out + OUTPUT LambdaMatrixExample_allRowsReduction.out ) + + ADD_EXECUTABLE( LambdaMatrixExample_forRows LambdaMatrixExample_forRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forRows.out + OUTPUT LambdaMatrixExample_forRows.out ) + + ADD_EXECUTABLE( LambdaMatrixExample_forAllRows LambdaMatrixExample_forAllRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forAllRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forAllRows.out + OUTPUT LambdaMatrixExample_forAllRows.out ) ENDIF() -#ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS -# SparseMatrixExample_Constructor_init_list.out -# SparseMatrixExample_setElements.out -# SparseMatrixExample_getCompressedRowLengths.out -# SparseMatrixExample_getElementsCount.out -# SparseMatrixExample_getConstRow.out -# SparseMatrixExample_getRow.out -# SparseMatrixExample_setElement.out -# SparseMatrixExample_addElement.out -# SparseMatrixExample_getElement.out -# SparseMatrixExample_rowsReduction.out -# SparseMatrixExample_allRowsReduction.out -# SparseMatrixExample_forRows.out -# SparseMatrixExample_forAllRows.out -# SparseMatrixViewExample_constructor.out -# SparseMatrixViewExample_getCompressedRowLengths.out -# SparseMatrixViewExample_getElementsCount.out -# SparseMatrixViewExample_getConstRow.out -# SparseMatrixViewExample_getRow.out -# SparseMatrixViewExample_setElement.out -# SparseMatrixViewExample_addElement.out -# SparseMatrixViewExample_getElement.out -# SparseMatrixViewExample_rowsReduction.out -# SparseMatrixViewExample_allRowsReduction.out -# SparseMatrixViewExample_forRows.out -# SparseMatrixViewExample_forAllRows.out -#) +ADD_CUSTOM_TARGET( RunLambdaMatricesExamples ALL DEPENDS + LambdaMatrixExample_Constructor.out + LambdaMatrixExample_getCompressedRowLengths.out + LambdaMatrixExample_getNonzeroElementsCount.out + LambdaMatrixExample_rowsReduction.out + LambdaMatrixExample_allRowsReduction.out + LambdaMatrixExample_forRows.out + LambdaMatrixExample_forAllRows.out +) diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp new file mode 100644 index 000000000..e0b2e52a3 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp @@ -0,0 +1,35 @@ +#include +#include + +int main( int argc, char* argv[] ) +{ + /*** + * Lambda functions defining the matrix. + */ + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int { return 1; }; + auto matrixElements1 = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value ) { + columnIdx = rowIdx; + value = 1.0; + }; + auto matrixElements2 = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value ) { + columnIdx = rowIdx; + value = rowIdx; + }; + + const int size = 5; + + /*** + * Matrix construction with explicit type definition. + */ + using MatrixType = decltype( TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >::create( matrixElements1, rowLengths ) ); + MatrixType m1( size, size, matrixElements1, rowLengths ); + + /*** + * Matrix construction using 'auto'. + */ + auto m2 = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >::create( matrixElements2, rowLengths ); + m2.setDimensions( size, size ); + + std::cout << "The first lambda matrix: " << std::endl << m1 << std::endl; + std::cout << "The second lambda matrix: " << std::endl << m2 << std::endl; +} diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp new file mode 100644 index 000000000..cfffd0534 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void allRowsReduction() +{ + /*** + * Lambda functions defining the matrix. + */ + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int { return columns; }; + auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value ) { + columnIdx = localIdx; + value = TNL::max( rowIdx - columnIdx + 1, 0 ); + }; + + using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >; + auto matrix = MatrixFactory::create( 5, 5, matrixElements, rowLengths ); + + /*** + * 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.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "All rows reduction on host:" << std::endl; + allRowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "All rows reduction on CUDA device:" << std::endl; + allRowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cu new file mode 120000 index 000000000..f76fba15c --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cu @@ -0,0 +1 @@ +LambdaMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cpp new file mode 100644 index 000000000..3dae8fd06 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + /*** + * Lambda functions defining the matrix. + */ + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int { return columns; }; + auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value ) { + columnIdx = localIdx; + value = TNL::max( rowIdx - columnIdx + 1, 0 ); + }; + + using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >; + auto matrix = MatrixFactory::create( 5, 5, matrixElements, rowLengths ); + + TNL::Matrices::DenseMatrix< double, Device > denseMatrix( 5, 5 ); + auto denseView = denseMatrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double value, bool& compute ) mutable { + denseView.setElement( rowIdx, columnIdx, value ); + }; + + matrix.forAllRows( f ); + std::cout << "Original lambda matrix:" << std::endl << matrix << std::endl; + std::cout << "Dense matrix:" << std::endl << denseMatrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Copying matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Copying matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cu new file mode 120000 index 000000000..fef2d3777 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cu @@ -0,0 +1 @@ +LambdaMatrixExample_forAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp new file mode 100644 index 000000000..036543ac8 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + /*** + * Lambda functions defining the matrix. + */ + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int { return columns; }; + auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value ) { + columnIdx = localIdx; + value = TNL::max( rowIdx - columnIdx + 1, 0 ); + }; + + using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >; + auto matrix = MatrixFactory::create( 5, 5, matrixElements, rowLengths ); + + TNL::Matrices::DenseMatrix< double, Device > denseMatrix( 5, 5 ); + auto denseView = denseMatrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double value, bool& compute ) mutable { + denseView.setElement( rowIdx, columnIdx, value ); + }; + + matrix.forRows( 0, matrix.getRows(), f ); + std::cout << "Original lambda matrix:" << std::endl << matrix << std::endl; + std::cout << "Dense matrix:" << std::endl << denseMatrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Copying matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Copying matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cu new file mode 120000 index 000000000..6df275619 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cu @@ -0,0 +1 @@ +LambdaMatrixExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cpp new file mode 100644 index 000000000..ed1326dc1 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cpp @@ -0,0 +1,23 @@ +#include +#include + +int main( int argc, char* argv[] ) +{ + /*** + * Lambda functions defining the matrix. + */ + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int { return columns; }; + auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value ) { + columnIdx = localIdx; + value = TNL::max( rowIdx - columnIdx + 1, 0 ); + }; + + const int size = 5; + auto matrix = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >::create( size, size, matrixElements, rowLengths ); + + TNL::Containers::Vector< int > rowLengthsVector; + matrix.getCompressedRowLengths( rowLengthsVector ); + + std::cout << "Matrix looks as:" << std::endl << matrix << std::endl; + std::cout << "Compressed row lengths are: " << rowLengthsVector << std::endl; +} diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cu new file mode 120000 index 000000000..03568be58 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cu @@ -0,0 +1 @@ +LambdaMatrixExample_getCompressedRowLengths.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cpp new file mode 100644 index 000000000..4d0811959 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cpp @@ -0,0 +1,20 @@ +#include +#include + +int main( int argc, char* argv[] ) +{ + /*** + * Lambda functions defining the matrix. + */ + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int { return columns; }; + auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value ) { + columnIdx = localIdx; + value = TNL::max( rowIdx - columnIdx + 1, 0 ); + }; + + const int size = 5; + auto matrix = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >::create( size, size, matrixElements, rowLengths ); + + std::cout << "Matrix looks as:" << std::endl << matrix << std::endl; + std::cout << "Non-zero elements count is: " << matrix.getNonzeroElementsCount() << std::endl; +} diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp new file mode 100644 index 000000000..d7613735a --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void rowsReduction() +{ + /*** + * Lambda functions defining the matrix. + */ + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int { return columns; }; + auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value ) { + columnIdx = localIdx; + value = TNL::max( rowIdx - columnIdx + 1, 0 ); + }; + + using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >; + auto matrix = MatrixFactory::create( 5, 5, matrixElements, rowLengths ); + + /*** + * 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.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Rows reduction on host:" << std::endl; + rowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + rowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cu new file mode 120000 index 000000000..ecb2401ec --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cu @@ -0,0 +1 @@ +LambdaMatrixExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index 64655b0a7..027c92f4f 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -1,282 +1,282 @@ IF( BUILD_CUDA ) -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_cuda SparseMatrixExample_Constructor_init_list.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out -# OUTPUT SparseMatrixExample_Constructor_init_list.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out -# OUTPUT SparseMatrixExample_setElements.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths_cuda SparseMatrixExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElementsCount_cuda SparseMatrixExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out -# OUTPUT SparseMatrixExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getConstRow_cuda SparseMatrixExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out -# OUTPUT SparseMatrixExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getRow_cuda SparseMatrixExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out -# OUTPUT SparseMatrixExample_getRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement_cuda SparseMatrixExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out -# OUTPUT SparseMatrixExample_setElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement_cuda SparseMatrixExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out -# OUTPUT SparseMatrixExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElement_cuda SparseMatrixExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out -# OUTPUT SparseMatrixExample_getElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out -# OUTPUT SparseMatrixExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction_cuda SparseMatrixExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out -# OUTPUT SparseMatrixExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forRows_cuda SparseMatrixExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out -# OUTPUT SparseMatrixExample_forRows.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forAllRows_cuda SparseMatrixExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out -# OUTPUT SparseMatrixExample_forAllRows.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_constructor_cuda SparseMatrixViewExample_constructor.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out -# OUTPUT SparseMatrixViewExample_constructor.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths_cuda SparseMatrixViewExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount_cuda SparseMatrixViewExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out -# OUTPUT SparseMatrixViewExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow_cuda SparseMatrixViewExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out -# OUTPUT SparseMatrixViewExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getRow_cuda SparseMatrixViewExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out -# OUTPUT SparseMatrixViewExample_getRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement_cuda SparseMatrixViewExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out -# OUTPUT SparseMatrixViewExample_setElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_addElement_cuda SparseMatrixViewExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out -# OUTPUT SparseMatrixViewExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElement_cuda SparseMatrixViewExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out -# OUTPUT SparseMatrixViewExample_getElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction_cuda SparseMatrixViewExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out -# OUTPUT SparseMatrixViewExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction_cuda SparseMatrixViewExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out -# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forRows_cuda SparseMatrixViewExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out -# OUTPUT SparseMatrixViewExample_forRows.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows_cuda SparseMatrixViewExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out -# OUTPUT SparseMatrixViewExample_forAllRows.out ) +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list_cuda MultidiagonalMatrixExample_Constructor_init_list.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list.out +# OUTPUT MultidiagonalMatrixExample_Constructor_init_list.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElements_cuda MultidiagonalMatrixExample_setElements.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElements_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out +# OUTPUT MultidiagonalMatrixExample_setElements.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths_cuda MultidiagonalMatrixExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out +# OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getElementsCount_cuda MultidiagonalMatrixExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElementsCount.out +# OUTPUT MultidiagonalMatrixExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getConstRow_cuda MultidiagonalMatrixExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getConstRow.out +# OUTPUT MultidiagonalMatrixExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getRow_cuda MultidiagonalMatrixExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getRow.out +# OUTPUT MultidiagonalMatrixExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement_cuda MultidiagonalMatrixExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out +# OUTPUT MultidiagonalMatrixExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_addElement_cuda MultidiagonalMatrixExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_addElement.out +# OUTPUT MultidiagonalMatrixExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getElement_cuda MultidiagonalMatrixExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out +# OUTPUT MultidiagonalMatrixExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction_cuda MultidiagonalMatrixExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out +# OUTPUT MultidiagonalMatrixExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction_cuda MultidiagonalMatrixExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out +# OUTPUT MultidiagonalMatrixExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forRows_cuda MultidiagonalMatrixExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forRows.out +# OUTPUT MultidiagonalMatrixExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllRows_cuda MultidiagonalMatrixExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out +# OUTPUT MultidiagonalMatrixExample_forAllRows.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_constructor_cuda MultidiagonalMatrixViewExample_constructor.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_constructor_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_constructor.out +# OUTPUT MultidiagonalMatrixViewExample_constructor.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getCompressedRowLengths_cuda MultidiagonalMatrixViewExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getCompressedRowLengths.out +# OUTPUT MultidiagonalMatrixViewExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElementsCount_cuda MultidiagonalMatrixViewExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElementsCount.out +# OUTPUT MultidiagonalMatrixViewExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getConstRow_cuda MultidiagonalMatrixViewExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getConstRow.out +# OUTPUT MultidiagonalMatrixViewExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getRow_cuda MultidiagonalMatrixViewExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getRow.out +# OUTPUT MultidiagonalMatrixViewExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_setElement_cuda MultidiagonalMatrixViewExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_setElement.out +# OUTPUT MultidiagonalMatrixViewExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_addElement_cuda MultidiagonalMatrixViewExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_addElement.out +# OUTPUT MultidiagonalMatrixViewExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElement_cuda MultidiagonalMatrixViewExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElement.out +# OUTPUT MultidiagonalMatrixViewExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_rowsReduction_cuda MultidiagonalMatrixViewExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_rowsReduction.out +# OUTPUT MultidiagonalMatrixViewExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_allRowsReduction_cuda MultidiagonalMatrixViewExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_allRowsReduction.out +# OUTPUT MultidiagonalMatrixViewExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forRows_cuda MultidiagonalMatrixViewExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forRows.out +# OUTPUT MultidiagonalMatrixViewExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forAllRows_cuda MultidiagonalMatrixViewExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forAllRows.out +# OUTPUT MultidiagonalMatrixViewExample_forAllRows.out ) ELSE() -# ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list SparseMatrixExample_Constructor_init_list.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out -# OUTPUT SparseMatrixExample_Constructor_init_list.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out -# OUTPUT SparseMatrixExample_setElements.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths SparseMatrixExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getElementsCount SparseMatrixExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out -# OUTPUT SparseMatrixExample_getElementsCount.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getConstRow SparseMatrixExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out -# OUTPUT SparseMatrixExample_getConstRow.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getRow SparseMatrixExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out -# OUTPUT SparseMatrixExample_getRow.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out -# OUTPUT SparseMatrixExample_setElement.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out -# OUTPUT SparseMatrixExample_addElement.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getElement SparseMatrixExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out -# OUTPUT SparseMatrixExample_getElement.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out -# OUTPUT SparseMatrixExample_rowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction SparseMatrixExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out -# OUTPUT SparseMatrixExample_allRowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_forRows SparseMatrixExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out -# OUTPUT SparseMatrixExample_forRows.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_forAllRows SparseMatrixExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out -# OUTPUT SparseMatrixExample_forAllRows.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_constructor SparseMatrixViewExample_constructor.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out -# OUTPUT SparseMatrixViewExample_constructor.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount SparseMatrixViewExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out -# OUTPUT SparseMatrixViewExample_getElementsCount.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow SparseMatrixViewExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out -# OUTPUT SparseMatrixViewExample_getConstRow.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getRow SparseMatrixViewExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out -# OUTPUT SparseMatrixViewExample_getRow.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out -# OUTPUT SparseMatrixViewExample_setElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_addElement SparseMatrixViewExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out -# OUTPUT SparseMatrixViewExample_addElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getElement SparseMatrixViewExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out -# OUTPUT SparseMatrixViewExample_getElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out -# OUTPUT SparseMatrixViewExample_rowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out -# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_forRows SparseMatrixViewExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out -# OUTPUT SparseMatrixViewExample_forRows.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows SparseMatrixViewExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out -# OUTPUT SparseMatrixViewExample_forAllRows.out ) +# ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list MultidiagonalMatrixExample_Constructor_init_list.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list.out +# OUTPUT MultidiagonalMatrixExample_Constructor_init_list.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_setElements MultidiagonalMatrixExample_setElements.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElements > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out +# OUTPUT MultidiagonalMatrixExample_setElements.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths MultidiagonalMatrixExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out +# OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_getElementsCount MultidiagonalMatrixExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElementsCount.out +# OUTPUT MultidiagonalMatrixExample_getElementsCount.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_getConstRow MultidiagonalMatrixExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getConstRow.out +# OUTPUT MultidiagonalMatrixExample_getConstRow.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_getRow MultidiagonalMatrixExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getRow.out +# OUTPUT MultidiagonalMatrixExample_getRow.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement MultidiagonalMatrixExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out +# OUTPUT MultidiagonalMatrixExample_setElement.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_addElement MultidiagonalMatrixExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_addElement.out +# OUTPUT MultidiagonalMatrixExample_addElement.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_getElement MultidiagonalMatrixExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out +# OUTPUT MultidiagonalMatrixExample_getElement.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction MultidiagonalMatrixExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out +# OUTPUT MultidiagonalMatrixExample_rowsReduction.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction MultidiagonalMatrixExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out +# OUTPUT MultidiagonalMatrixExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_forRows MultidiagonalMatrixExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forRows.out +# OUTPUT MultidiagonalMatrixExample_forRows.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllRows MultidiagonalMatrixExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out +# OUTPUT MultidiagonalMatrixExample_forAllRows.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_constructor MultidiagonalMatrixViewExample_constructor.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_constructor > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_constructor.out +# OUTPUT MultidiagonalMatrixViewExample_constructor.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getCompressedRowLengths MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getCompressedRowLengths.out +# OUTPUT MultidiagonalMatrixViewExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElementsCount MultidiagonalMatrixViewExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElementsCount.out +# OUTPUT MultidiagonalMatrixViewExample_getElementsCount.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getConstRow MultidiagonalMatrixViewExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getConstRow.out +# OUTPUT MultidiagonalMatrixViewExample_getConstRow.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getRow MultidiagonalMatrixViewExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getRow.out +# OUTPUT MultidiagonalMatrixViewExample_getRow.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_setElement MultidiagonalMatrixViewExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_setElement.out +# OUTPUT MultidiagonalMatrixViewExample_setElement.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_addElement MultidiagonalMatrixViewExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_addElement.out +# OUTPUT MultidiagonalMatrixViewExample_addElement.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElement MultidiagonalMatrixViewExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElement.out +# OUTPUT MultidiagonalMatrixViewExample_getElement.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_rowsReduction MultidiagonalMatrixViewExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_rowsReduction.out +# OUTPUT MultidiagonalMatrixViewExample_rowsReduction.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_allRowsReduction MultidiagonalMatrixViewExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_allRowsReduction.out +# OUTPUT MultidiagonalMatrixViewExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forRows MultidiagonalMatrixViewExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forRows.out +# OUTPUT MultidiagonalMatrixViewExample_forRows.out ) + +# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forAllRows MultidiagonalMatrixViewExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forAllRows.out +# OUTPUT MultidiagonalMatrixViewExample_forAllRows.out ) ENDIF() #ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS -# SparseMatrixExample_Constructor_init_list.out -# SparseMatrixExample_setElements.out -# SparseMatrixExample_getCompressedRowLengths.out -# SparseMatrixExample_getElementsCount.out -# SparseMatrixExample_getConstRow.out -# SparseMatrixExample_getRow.out -# SparseMatrixExample_setElement.out -# SparseMatrixExample_addElement.out -# SparseMatrixExample_getElement.out -# SparseMatrixExample_rowsReduction.out -# SparseMatrixExample_allRowsReduction.out -# SparseMatrixExample_forRows.out -# SparseMatrixExample_forAllRows.out -# SparseMatrixViewExample_constructor.out -# SparseMatrixViewExample_getCompressedRowLengths.out -# SparseMatrixViewExample_getElementsCount.out -# SparseMatrixViewExample_getConstRow.out -# SparseMatrixViewExample_getRow.out -# SparseMatrixViewExample_setElement.out -# SparseMatrixViewExample_addElement.out -# SparseMatrixViewExample_getElement.out -# SparseMatrixViewExample_rowsReduction.out -# SparseMatrixViewExample_allRowsReduction.out -# SparseMatrixViewExample_forRows.out -# SparseMatrixViewExample_forAllRows.out +# MultidiagonalMatrixExample_Constructor_init_list.out +# MultidiagonalMatrixExample_setElements.out +# MultidiagonalMatrixExample_getCompressedRowLengths.out +# MultidiagonalMatrixExample_getElementsCount.out +# MultidiagonalMatrixExample_getConstRow.out +# MultidiagonalMatrixExample_getRow.out +# MultidiagonalMatrixExample_setElement.out +# MultidiagonalMatrixExample_addElement.out +# MultidiagonalMatrixExample_getElement.out +# MultidiagonalMatrixExample_rowsReduction.out +# MultidiagonalMatrixExample_allRowsReduction.out +# MultidiagonalMatrixExample_forRows.out +# MultidiagonalMatrixExample_forAllRows.out +# MultidiagonalMatrixViewExample_constructor.out +# MultidiagonalMatrixViewExample_getCompressedRowLengths.out +# MultidiagonalMatrixViewExample_getElementsCount.out +# MultidiagonalMatrixViewExample_getConstRow.out +# MultidiagonalMatrixViewExample_getRow.out +# MultidiagonalMatrixViewExample_setElement.out +# MultidiagonalMatrixViewExample_addElement.out +# MultidiagonalMatrixViewExample_getElement.out +# MultidiagonalMatrixViewExample_rowsReduction.out +# MultidiagonalMatrixViewExample_allRowsReduction.out +# MultidiagonalMatrixViewExample_forRows.out +# MultidiagonalMatrixViewExample_forAllRows.out #) diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt index 64655b0a7..6c7aef592 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt @@ -1,282 +1,282 @@ IF( BUILD_CUDA ) -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_cuda SparseMatrixExample_Constructor_init_list.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out -# OUTPUT SparseMatrixExample_Constructor_init_list.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out -# OUTPUT SparseMatrixExample_setElements.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths_cuda SparseMatrixExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElementsCount_cuda SparseMatrixExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out -# OUTPUT SparseMatrixExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getConstRow_cuda SparseMatrixExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out -# OUTPUT SparseMatrixExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getRow_cuda SparseMatrixExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out -# OUTPUT SparseMatrixExample_getRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement_cuda SparseMatrixExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out -# OUTPUT SparseMatrixExample_setElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement_cuda SparseMatrixExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out -# OUTPUT SparseMatrixExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElement_cuda SparseMatrixExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out -# OUTPUT SparseMatrixExample_getElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out -# OUTPUT SparseMatrixExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction_cuda SparseMatrixExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out -# OUTPUT SparseMatrixExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forRows_cuda SparseMatrixExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out -# OUTPUT SparseMatrixExample_forRows.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forAllRows_cuda SparseMatrixExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out -# OUTPUT SparseMatrixExample_forAllRows.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_constructor_cuda SparseMatrixViewExample_constructor.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out -# OUTPUT SparseMatrixViewExample_constructor.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths_cuda SparseMatrixViewExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount_cuda SparseMatrixViewExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out -# OUTPUT SparseMatrixViewExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow_cuda SparseMatrixViewExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out -# OUTPUT SparseMatrixViewExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getRow_cuda SparseMatrixViewExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out -# OUTPUT SparseMatrixViewExample_getRow.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement_cuda SparseMatrixViewExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out -# OUTPUT SparseMatrixViewExample_setElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_addElement_cuda SparseMatrixViewExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out -# OUTPUT SparseMatrixViewExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElement_cuda SparseMatrixViewExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out -# OUTPUT SparseMatrixViewExample_getElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction_cuda SparseMatrixViewExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out -# OUTPUT SparseMatrixViewExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction_cuda SparseMatrixViewExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out -# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forRows_cuda SparseMatrixViewExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out -# OUTPUT SparseMatrixViewExample_forRows.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows_cuda SparseMatrixViewExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out -# OUTPUT SparseMatrixViewExample_forAllRows.out ) +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_Constructor_init_list_cuda TridiagonalMatrixExample_Constructor_init_list.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_Constructor_init_list_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_Constructor_init_list.out +# OUTPUT TridiagonalMatrixExample_Constructor_init_list.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_setElements_cuda TridiagonalMatrixExample_setElements.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElements_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElements.out +# OUTPUT TridiagonalMatrixExample_setElements.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getCompressedRowLengths_cuda TridiagonalMatrixExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getCompressedRowLengths.out +# OUTPUT TridiagonalMatrixExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getElementsCount_cuda TridiagonalMatrixExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElementsCount.out +# OUTPUT TridiagonalMatrixExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getConstRow_cuda TridiagonalMatrixExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getConstRow.out +# OUTPUT TridiagonalMatrixExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getRow_cuda TridiagonalMatrixExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getRow.out +# OUTPUT TridiagonalMatrixExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_setElement_cuda TridiagonalMatrixExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElement.out +# OUTPUT TridiagonalMatrixExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_addElement_cuda TridiagonalMatrixExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_addElement.out +# OUTPUT TridiagonalMatrixExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getElement_cuda TridiagonalMatrixExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElement.out +# OUTPUT TridiagonalMatrixExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_rowsReduction_cuda TridiagonalMatrixExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_rowsReduction.out +# OUTPUT TridiagonalMatrixExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_allRowsReduction_cuda TridiagonalMatrixExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_allRowsReduction.out +# OUTPUT TridiagonalMatrixExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_forRows_cuda TridiagonalMatrixExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forRows.out +# OUTPUT TridiagonalMatrixExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_forAllRows_cuda TridiagonalMatrixExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forAllRows.out +# OUTPUT TridiagonalMatrixExample_forAllRows.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_constructor_cuda TridiagonalMatrixViewExample_constructor.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_constructor_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_constructor.out +# OUTPUT TridiagonalMatrixViewExample_constructor.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getCompressedRowLengths_cuda TridiagonalMatrixViewExample_getCompressedRowLengths.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getCompressedRowLengths_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getCompressedRowLengths.out +# OUTPUT TridiagonalMatrixViewExample_getCompressedRowLengths.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElementsCount_cuda TridiagonalMatrixViewExample_getElementsCount.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElementsCount_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElementsCount.out +# OUTPUT TridiagonalMatrixViewExample_getElementsCount.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getConstRow_cuda TridiagonalMatrixViewExample_getConstRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getConstRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getConstRow.out +# OUTPUT TridiagonalMatrixViewExample_getConstRow.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getRow_cuda TridiagonalMatrixViewExample_getRow.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getRow_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getRow.out +# OUTPUT TridiagonalMatrixViewExample_getRow.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_setElement_cuda TridiagonalMatrixViewExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_setElement.out +# OUTPUT TridiagonalMatrixViewExample_setElement.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_addElement_cuda TridiagonalMatrixViewExample_addElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_addElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_addElement.out +# OUTPUT TridiagonalMatrixViewExample_addElement.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElement_cuda TridiagonalMatrixViewExample_getElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElement.out +# OUTPUT TridiagonalMatrixViewExample_getElement.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_rowsReduction_cuda TridiagonalMatrixViewExample_rowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_rowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_rowsReduction.out +# OUTPUT TridiagonalMatrixViewExample_rowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_allRowsReduction_cuda TridiagonalMatrixViewExample_allRowsReduction.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_allRowsReduction_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_allRowsReduction.out +# OUTPUT TridiagonalMatrixViewExample_allRowsReduction.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_forRows_cuda TridiagonalMatrixViewExample_forRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forRows.out +# OUTPUT TridiagonalMatrixViewExample_forRows.out ) + +# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_forAllRows_cuda TridiagonalMatrixViewExample_forAllRows.cu ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forAllRows_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forAllRows.out +# OUTPUT TridiagonalMatrixViewExample_forAllRows.out ) ELSE() -# ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list SparseMatrixExample_Constructor_init_list.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list.out -# OUTPUT SparseMatrixExample_Constructor_init_list.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out -# OUTPUT SparseMatrixExample_setElements.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths SparseMatrixExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getElementsCount SparseMatrixExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElementsCount.out -# OUTPUT SparseMatrixExample_getElementsCount.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getConstRow SparseMatrixExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out -# OUTPUT SparseMatrixExample_getConstRow.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getRow SparseMatrixExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out -# OUTPUT SparseMatrixExample_getRow.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out -# OUTPUT SparseMatrixExample_setElement.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out -# OUTPUT SparseMatrixExample_addElement.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_getElement SparseMatrixExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out -# OUTPUT SparseMatrixExample_getElement.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out -# OUTPUT SparseMatrixExample_rowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction SparseMatrixExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out -# OUTPUT SparseMatrixExample_allRowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_forRows SparseMatrixExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out -# OUTPUT SparseMatrixExample_forRows.out ) - -# ADD_EXECUTABLE( SparseMatrixExample_forAllRows SparseMatrixExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forAllRows.out -# OUTPUT SparseMatrixExample_forAllRows.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_constructor SparseMatrixViewExample_constructor.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_constructor > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_constructor.out -# OUTPUT SparseMatrixViewExample_constructor.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out -# OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getElementsCount SparseMatrixViewExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElementsCount.out -# OUTPUT SparseMatrixViewExample_getElementsCount.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow SparseMatrixViewExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out -# OUTPUT SparseMatrixViewExample_getConstRow.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getRow SparseMatrixViewExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out -# OUTPUT SparseMatrixViewExample_getRow.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out -# OUTPUT SparseMatrixViewExample_setElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_addElement SparseMatrixViewExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out -# OUTPUT SparseMatrixViewExample_addElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_getElement SparseMatrixViewExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out -# OUTPUT SparseMatrixViewExample_getElement.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out -# OUTPUT SparseMatrixViewExample_rowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out -# OUTPUT SparseMatrixViewExample_allRowsReduction.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_forRows SparseMatrixViewExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forRows.out -# OUTPUT SparseMatrixViewExample_forRows.out ) - -# ADD_EXECUTABLE( SparseMatrixViewExample_forAllRows SparseMatrixViewExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forAllRows.out -# OUTPUT SparseMatrixViewExample_forAllRows.out ) +# ADD_EXECUTABLE( TridiagonalMatrixExample_Constructor_init_list TridiagonalMatrixExample_Constructor_init_list.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_Constructor_init_list > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_Constructor_init_list.out +# OUTPUT TridiagonalMatrixExample_Constructor_init_list.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_setElements TridiagonalMatrixExample_setElements.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElements > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElements.out +# OUTPUT TridiagonalMatrixExample_setElements.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_getCompressedRowLengths TridiagonalMatrixExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getCompressedRowLengths.out +# OUTPUT TridiagonalMatrixExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_getElementsCount TridiagonalMatrixExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElementsCount.out +# OUTPUT TridiagonalMatrixExample_getElementsCount.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_getConstRow TridiagonalMatrixExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getConstRow.out +# OUTPUT TridiagonalMatrixExample_getConstRow.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_getRow TridiagonalMatrixExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getRow.out +# OUTPUT TridiagonalMatrixExample_getRow.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_setElement TridiagonalMatrixExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElement.out +# OUTPUT TridiagonalMatrixExample_setElement.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_addElement TridiagonalMatrixExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_addElement.out +# OUTPUT TridiagonalMatrixExample_addElement.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_getElement TridiagonalMatrixExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElement.out +# OUTPUT TridiagonalMatrixExample_getElement.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_rowsReduction TridiagonalMatrixExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_rowsReduction.out +# OUTPUT TridiagonalMatrixExample_rowsReduction.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_allRowsReduction TridiagonalMatrixExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_allRowsReduction.out +# OUTPUT TridiagonalMatrixExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_forRows TridiagonalMatrixExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forRows.out +# OUTPUT TridiagonalMatrixExample_forRows.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixExample_forAllRows TridiagonalMatrixExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forAllRows.out +# OUTPUT TridiagonalMatrixExample_forAllRows.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_constructor TridiagonalMatrixViewExample_constructor.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_constructor > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_constructor.out +# OUTPUT TridiagonalMatrixViewExample_constructor.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_getCompressedRowLengths TridiagonalMatrixViewExample_getCompressedRowLengths.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getCompressedRowLengths > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getCompressedRowLengths.out +# OUTPUT TridiagonalMatrixViewExample_getCompressedRowLengths.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElementsCount TridiagonalMatrixViewExample_getElementsCount.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElementsCount > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElementsCount.out +# OUTPUT TridiagonalMatrixViewExample_getElementsCount.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_getConstRow TridiagonalMatrixViewExample_getConstRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getConstRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getConstRow.out +# OUTPUT TridiagonalMatrixViewExample_getConstRow.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_getRow TridiagonalMatrixViewExample_getRow.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getRow > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getRow.out +# OUTPUT TridiagonalMatrixViewExample_getRow.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_setElement TridiagonalMatrixViewExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_setElement.out +# OUTPUT TridiagonalMatrixViewExample_setElement.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_addElement TridiagonalMatrixViewExample_addElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_addElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_addElement.out +# OUTPUT TridiagonalMatrixViewExample_addElement.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElement TridiagonalMatrixViewExample_getElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElement.out +# OUTPUT TridiagonalMatrixViewExample_getElement.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_rowsReduction TridiagonalMatrixViewExample_rowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_rowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_rowsReduction.out +# OUTPUT TridiagonalMatrixViewExample_rowsReduction.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_allRowsReduction TridiagonalMatrixViewExample_allRowsReduction.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_allRowsReduction > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_allRowsReduction.out +# OUTPUT TridiagonalMatrixViewExample_allRowsReduction.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_forRows TridiagonalMatrixViewExample_forRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forRows.out +# OUTPUT TridiagonalMatrixViewExample_forRows.out ) + +# ADD_EXECUTABLE( TridiagonalMatrixViewExample_forAllRows TridiagonalMatrixViewExample_forAllRows.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forAllRows > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forAllRows.out +# OUTPUT TridiagonalMatrixViewExample_forAllRows.out ) ENDIF() #ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS -# SparseMatrixExample_Constructor_init_list.out -# SparseMatrixExample_setElements.out -# SparseMatrixExample_getCompressedRowLengths.out -# SparseMatrixExample_getElementsCount.out -# SparseMatrixExample_getConstRow.out -# SparseMatrixExample_getRow.out -# SparseMatrixExample_setElement.out -# SparseMatrixExample_addElement.out -# SparseMatrixExample_getElement.out -# SparseMatrixExample_rowsReduction.out -# SparseMatrixExample_allRowsReduction.out -# SparseMatrixExample_forRows.out -# SparseMatrixExample_forAllRows.out -# SparseMatrixViewExample_constructor.out -# SparseMatrixViewExample_getCompressedRowLengths.out -# SparseMatrixViewExample_getElementsCount.out -# SparseMatrixViewExample_getConstRow.out -# SparseMatrixViewExample_getRow.out -# SparseMatrixViewExample_setElement.out -# SparseMatrixViewExample_addElement.out -# SparseMatrixViewExample_getElement.out -# SparseMatrixViewExample_rowsReduction.out -# SparseMatrixViewExample_allRowsReduction.out -# SparseMatrixViewExample_forRows.out -# SparseMatrixViewExample_forAllRows.out +# TridiagonalMatrixExample_Constructor_init_list.out +# TridiagonalMatrixExample_setElements.out +# TridiagonalMatrixExample_getCompressedRowLengths.out +# TridiagonalMatrixExample_getElementsCount.out +# TridiagonalMatrixExample_getConstRow.out +# TridiagonalMatrixExample_getRow.out +# TridiagonalMatrixExample_setElement.out +# TridiagonalMatrixExample_addElement.out +# TridiagonalMatrixExample_getElement.out +# TridiagonalMatrixExample_rowsReduction.out +# TridiagonalMatrixExample_allRowsReduction.out +# TridiagonalMatrixExample_forRows.out +# TridiagonalMatrixExample_forAllRows.out +# TridiagonalMatrixViewExample_constructor.out +# TridiagonalMatrixViewExample_getCompressedRowLengths.out +# TridiagonalMatrixViewExample_getElementsCount.out +# TridiagonalMatrixViewExample_getConstRow.out +# TridiagonalMatrixViewExample_getRow.out +# TridiagonalMatrixViewExample_setElement.out +# TridiagonalMatrixViewExample_addElement.out +# TridiagonalMatrixViewExample_getElement.out +# TridiagonalMatrixViewExample_rowsReduction.out +# TridiagonalMatrixViewExample_allRowsReduction.out +# TridiagonalMatrixViewExample_forRows.out +# TridiagonalMatrixViewExample_forAllRows.out #) diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index d60ca25af..736cd3bcf 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -600,7 +600,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * * More precisely, it computes: * - * outVector = matrixMultiplicator * ( *this ) * inVector + outVectorMultiplicator * outVector. + * `outVector = matrixMultiplicator * ( *this ) * inVector + outVectorMultiplicator * outVector` * * \tparam InVector is type of input vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index 44acc5eed..b0c50fd56 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -548,7 +548,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * * More precisely, it computes: * - * outVector = matrixMultiplicator * ( *this ) * inVector + outVectorMultiplicator * outVector. + * `outVector = matrixMultiplicator * ( *this ) * inVector + outVectorMultiplicator * outVector` * * \tparam InVector is type of input vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. diff --git a/src/TNL/Matrices/LambdaMatrix.h b/src/TNL/Matrices/LambdaMatrix.h index 5f3ecdfb3..ed7c30e91 100644 --- a/src/TNL/Matrices/LambdaMatrix.h +++ b/src/TNL/Matrices/LambdaMatrix.h @@ -19,10 +19,26 @@ namespace Matrices { /** * \brief "Matrix-free" matrix based on lambda functions. * - * \tparam MatrixElementsLambda is a lambda function returning matrix elements - * values and positions. - * \tparam CompressedRowLengthsLambda is a lambda function returning a number - * of non-zero elements in each row. + * The elements of this matrix are not stored explicitly in memory but + * implicitly on a form of lambda functions. + * + * \tparam MatrixElementsLambda is a lambda function returning matrix elements values and positions. + * + * It has the following form: + * + * `matrixElements( IndexType rows, IndexType columns, IndexType row, IndexType localIdx, IndexType& elementColumn, RealType& elementValue )` + * + * where \e rows is the number of matrix rows, \e columns is the number of matrix columns, \e row is the index of matrix row being queried, + * \e localIdx is the rank of the non-zero element in given row, \e elementColumn is a column index of the matrix element computed by + * this lambda and \e elementValue is a value of the matrix element computed by this lambda. + * \tparam CompressedRowLengthsLambda is a lambda function returning a number of non-zero elements in each row. + * + * It has the following form: + * + * `rowLengths( IndexType rows, IndexType columns, IndexType row ) -> IndexType` + * + * where \e rows is the number of matrix rows, \e columns is the number of matrix columns and \e row is an index of the row being queried. + * * \tparam Real is a type of matrix elements values. * \tparam Device is a device on which the lambda functions can evaluated. * Devices::AnyDevice can be used for lambdas with no restriction. @@ -36,70 +52,260 @@ template< typename MatrixElementsLambda, class LambdaMatrix { public: - static constexpr bool isSymmetric() { return false; }; - static constexpr bool isBinary() { return false; }; + /** + * \brief The type of matrix elements. + */ using RealType = Real; + + /** + * \brief The device where the matrix is allocated. + */ using DeviceType = Device; + + /** + * \brief The type used for matrix elements indexing. + */ using IndexType = Index; + static constexpr bool isSymmetric() { return false; }; + static constexpr bool isBinary() { return false; }; + + /** + * \brief Constructor with lambda functions defining the matrix elements. + * + * Note: It might be difficult to express the types of the lambdas. For easier creation of + * \e LambdaMatrix you may use \ref LambdaMatrixFactory. + * + * \param matrixElements is a lambda function giving matrix elements position and value. + * \param compressedRowLentghs is a lambda function returning how many non-zero matrix elements are in given row. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp + * \par Output + * \include LambdaMatrixExample_Constructor.out + */ LambdaMatrix( MatrixElementsLambda& matrixElements, CompressedRowLengthsLambda& compressedRowLentghs ); + /** + * \brief Constructor with matrix dimensions and lambda functions defining the matrix elements. + * + * Note: It might be difficult to express the types of the lambdas. For easier creation of + * \e LambdaMatrix you may use \ref LambdaMatrixFactory. + * + * \param rows is a number of the matrix rows. + * \param columns is a number of the matrix columns. + * \param matrixElements is a lambda function giving matrix elements position and value. + * \param compressedRowLentghs is a lambda function returning how many non-zero matrix elements are in given row. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp + * \par Output + * \include LambdaMatrixExample_Constructor.out + */ LambdaMatrix( const IndexType& rows, const IndexType& columns, MatrixElementsLambda& matrixElements, CompressedRowLengthsLambda& compressedRowLentghs ); + /** + * \brief Copy constructor. + * + * \param matrix is input matrix. + */ + LambdaMatrix( const LambdaMatrix& matrix ) = default; + + /** + * \brief Move constructor. + * + * \param matrix is input matrix. + */ + LambdaMatrix( LambdaMatrix&& matrix ) = default; + + /** + * \brief Set number of rows and columns of this matrix. + * + * \param rows is the number of matrix rows. + * \param columns is the number of matrix columns. + */ void setDimensions( const IndexType& rows, const IndexType& columns ); + /** + * \brief Returns a number of matrix rows. + * + * \return number of matrix rows. + */ __cuda_callable__ IndexType getRows() const; + /** + * \brief Returns a number of matrix columns. + * + * \return number of matrix columns. + */ __cuda_callable__ IndexType getColumns() const; - template< typename Vector > - void getCompressedRowLengths( Vector& rowLengths ) const; - - IndexType getNumberOfNonzeroMatrixElements() const; - + /** + * \brief Computes number of non-zeros in each row. + * + * \param rowLengths is a vector into which the number of non-zeros in each row + * will be stored. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cpp + * \par Output + * \include LambdaMatrixExample_getCompressedRowLengths.out + */ + template< typename RowLentghsVector > + void getCompressedRowLengths( RowLentghsVector& rowLengths ) const; + + /** + * \brief Returns number of non-zero matrix elements. + * + * \return number of all non-zero matrix elements. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_getElementsCount.cpp + * \par Output + * \include LambdaMatrixExample_getElementsCount.out + */ + IndexType getNonzeroElementsCount() const; + + /** + * \brief Returns value of matrix element at position given by its row and column index. + * + * \param row is a row index of the matrix element. + * \param column i a column index of the matrix element. + * + * \return value of given matrix element. + */ RealType getElement( const IndexType row, const IndexType column ) const; - template< typename Vector > - __cuda_callable__ - typename Vector::RealType rowVectorProduct( const IndexType row, - const Vector& vector ) const; - - /*** - * \brief This method computes outVector = matrixMultiplicator * ( *this ) * inVector + inVectorAddition * inVector + /** + * \brief Method for performing general reduction on matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp + * \par Output + * \include LambdaMatrixExample_rowsReduction.out */ - template< typename InVector, - typename OutVector > - void vectorProduct( const InVector& inVector, - OutVector& outVector, - const RealType& matrixMultiplicator = 1.0, - const RealType& outVectorMultiplicator = 0.0 ) const; - template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; - + void rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + + /** + * \brief Method for performing general reduction on ALL matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp + * \par Output + * \include LambdaMatrixExample_allRowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; - + void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + + /** + * \brief Method for iteration over all matrix rows for constant instances. + * + * \tparam Function is type of lambda function that will operate on matrix elements. + * It is should have form like + * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx_, const RealType& value, bool& compute )`. + * The column index repeats twice only for compatibility with sparse matrices. + * If the 'compute' variable 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. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp + * \par Output + * \include LambdaMatrixExample_forRows.out + */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ) const; - template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ); - + /** + * \brief This method calls \e forRows for all matrix rows (for constant instances). + * + * See \ref LambdaMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cpp + * \par Output + * \include LambdaMatrixExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ) const; - template< typename Function > - void forAllRows( Function& function ); + /** + * \brief Computes product of matrix and vector. + * + * More precisely, it computes: + * + * `outVector = matrixMultiplicator * ( *this ) * inVector + outVectorMultiplicator * outVector` + * + * \tparam InVector is type of input vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * \tparam OutVector is type of output vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * + * \param inVector is input vector. + * \param outVector is output vector. + * \param matrixMultiplicator is a factor by which the matrix is multiplied. It is one by default. + * \param outVectorMultiplicator is a factor by which the outVector is multiplied before added + * to the result of matrix-vector product. It is zero by default. + * \param begin is the beginning of the rows range for which the vector product + * is computed. It is zero by default. + * \param end is the end of the rows range for which the vector product + * is computed. It is number if the matrix rows by default. + */ + template< typename InVector, + typename OutVector > + void vectorProduct( const InVector& inVector, + OutVector& outVector, + const RealType& matrixMultiplicator = 1.0, + const RealType& outVectorMultiplicator = 0.0, + const IndexType begin = 0, + IndexType end = 0 ) const; + template< typename Vector1, typename Vector2 > void performSORIteration( const Vector1& b, @@ -107,6 +313,11 @@ class LambdaMatrix Vector2& x, const RealType& omega = 1.0 ) const; + /** + * \brief Method for printing the matrix to output stream. + * + * \param str is the output stream. + */ void print( std::ostream& str ) const; protected: @@ -118,12 +329,27 @@ class LambdaMatrix CompressedRowLengthsLambda compressedRowLengthsLambda; }; +/** + * \brief Insertion operator for dense matrix and output stream. + * + * \param str is the output stream. + * \param matrix is the lambda matrix. + * \return reference to the stream. + */ +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Device, + typename Index > +std::ostream& operator<< ( std::ostream& str, const LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >& matrix ); /** * \brief Helper class for creating instances of LambdaMatrix. - * @param matrixElementsLambda - * @param compressedRowLengthsLambda - * @return + * + * See \ref LambdaMatrix. + * + * \param matrixElementsLambda + * \param compressedRowLengthsLambda */ template< typename Real = double, typename Device = Devices::AnyDevice, @@ -132,7 +358,20 @@ struct LambdaMatrixFactory { using RealType = Real; using IndexType = Index; - + + /** + * \brief Creates lambda matrix with given lambda functions. + * + * \param matrixElementsLambda is a lambda function evaluating matrix elements. + * \param compressedRowLengthsLambda is a lambda function returning number of + * non-zero matrix elements in given \e row. + * \return instance of LambdaMatrix. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp + * \par Output + * \include LambdaMatrixExample_Constructor.out + */ template< typename MatrixElementsLambda, typename CompressedRowLengthsLambda > static auto create( MatrixElementsLambda& matrixElementsLambda, @@ -143,7 +382,22 @@ struct LambdaMatrixFactory matrixElementsLambda, compressedRowLengthsLambda ); }; - + + /** + * \brief Creates lambda matrix with given dimensions and lambda functions. + * + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + * \param matrixElementsLambda is a lambda function evaluating matrix elements. + * \param compressedRowLengthsLambda is a lambda function returning number of + * non-zero matrix elements in given \e row. + * \return instance of LambdaMatrix. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp + * \par Output + * \include LambdaMatrixExample_Constructor.out + */ template< typename MatrixElementsLambda, typename CompressedRowLengthsLambda > static auto create( const IndexType& rows, @@ -153,6 +407,7 @@ struct LambdaMatrixFactory -> LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index > { return LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >( + rows, columns, matrixElementsLambda, compressedRowLengthsLambda ); }; diff --git a/src/TNL/Matrices/LambdaMatrix.hpp b/src/TNL/Matrices/LambdaMatrix.hpp index c992bd575..4ce0a18a5 100644 --- a/src/TNL/Matrices/LambdaMatrix.hpp +++ b/src/TNL/Matrices/LambdaMatrix.hpp @@ -10,10 +10,13 @@ #pragma once +#include +#include #include #include #include #include +#include namespace TNL { namespace Matrices { @@ -95,32 +98,16 @@ LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, In getCompressedRowLengths( Vector& rowLengths ) const { using Device_ = typename Devices::PickDevice< DeviceType >::DeviceType; - - rowLengths.setSize( this->getRows() ); - const IndexType rows = this->getRows(); - const IndexType columns = this->getColumns(); - auto rowLengthsView = rowLengths.getView(); - auto compressedRowLengths = this->compressedRowLengthsLambda; - - if( std::is_same< typename Vector::DeviceType, Device_ >::value ) - Algorithms::ParallelFor< Device_ >::exec( - ( IndexType ) 0, - this->getRows(), - [=] __cuda_callable__ ( const IndexType row ) mutable { - rowLengthsView[ row ] = compressedRowLengths( rows, columns, row ); - } ); - else - { - Containers::Vector< IndexType, Device_, IndexType > aux( this->getRows() ); - auto auxView = aux.getView(); - Algorithms::ParallelFor< Device_ >::exec( - ( IndexType ) 0, - this->getRows(), - [=] __cuda_callable__ ( const IndexType row ) mutable { - auxView[ row ] = compressedRowLengths( rows, columns, row ); - } ); - rowLengths = aux; - } + details::set_size_if_resizable( rowLengths, this->getRows() ); + rowLengths = 0; + auto rowLengths_view = rowLengths.getView(); + auto fetch = [] __cuda_callable__ ( IndexType row, IndexType column, const RealType& value ) -> IndexType { + return ( value != 0.0 ); + }; + auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { + rowLengths_view[ rowIdx ] = value; + }; + this->allRowsReduction( fetch, std::plus<>{}, keep, 0 ); } template< typename MatrixElementsLambda, @@ -130,7 +117,7 @@ template< typename MatrixElementsLambda, typename Index > Index LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -getNumberOfNonzeroMatrixElements() const +getNonzeroElementsCount() const { Containers::Vector< IndexType, typename Devices::PickDevice< DeviceType >::DeviceType, IndexType > rowLengthsVector; this->getCompressedRowLengths( rowLengthsVector ); @@ -173,21 +160,6 @@ getElement( const IndexType row, return valueView.getElement( 0 ); } -template< typename MatrixElementsLambda, - typename CompressedRowLengthsLambda, - typename Real, - typename Device, - typename Index > - template< typename Vector > -__cuda_callable__ -typename Vector::RealType -LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -rowVectorProduct( const IndexType row, - const Vector& vector ) const -{ - -} - template< typename MatrixElementsLambda, typename CompressedRowLengthsLambda, typename Real, @@ -200,20 +172,22 @@ LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, In vectorProduct( const InVector& inVector, OutVector& outVector, const RealType& matrixMultiplicator, - const RealType& outVectorMultiplicator ) const + const RealType& outVectorMultiplicator, + const IndexType begin, + IndexType end ) const { TNL_ASSERT_EQ( this->getColumns(), inVector.getSize(), "Matrix columns do not fit with input vector." ); TNL_ASSERT_EQ( this->getRows(), outVector.getSize(), "Matrix rows do not fit with output vector." ); const auto inVectorView = inVector.getConstView(); auto outVectorView = outVector.getView(); - auto fetch = [=] __cuda_callable__ ( IndexType row, IndexType localIdx, IndexType columnIdx, const RealType& value ) mutable -> RealType { + auto fetch = [=] __cuda_callable__ ( IndexType row, IndexType columnIdx, const RealType& value ) mutable -> RealType { if( value == 0.0 ) return 0.0; return value * inVectorView[ columnIdx ]; }; - auto reduce = [] __cuda_callable__ ( RealType& sum, const RealType& value ) { - sum += value; + auto reduce = [] __cuda_callable__ ( RealType& sum, const RealType& value ) -> RealType { + return sum + value; }; auto keep = [=] __cuda_callable__ ( IndexType row, const RealType& value ) mutable { if( outVectorMultiplicator == 0.0 ) @@ -221,7 +195,9 @@ vectorProduct( const InVector& inVector, else outVectorView[ row ] = outVectorMultiplicator * outVectorView[ row ] + matrixMultiplicator * value; }; - this->allRowsReduction( fetch, reduce, keep, 0.0 ); + if( ! end ) + end = this->getRows(); + this->rowsReduction( begin, end, fetch, reduce, keep, 0.0 ); } template< typename MatrixElementsLambda, @@ -232,9 +208,9 @@ template< typename MatrixElementsLambda, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const +rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - using FetchType = decltype( fetch( IndexType(), IndexType(), IndexType(), RealType() ) ); + using FetchType = decltype( fetch( IndexType(), IndexType(), RealType() ) ); using Device_ = typename Devices::PickDevice< DeviceType >::DeviceType; const IndexType rows = this->getRows(); @@ -251,8 +227,8 @@ rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Ke matrixElements( rows, columns, rowIdx, localIdx, elementColumn, elementValue ); FetchType fetchValue( zero ); if( elementValue != 0.0 ) - fetchValue = fetch( rowIdx, localIdx, elementColumn, elementValue ); - reduce( result, fetchValue ); + fetchValue = fetch( rowIdx, elementColumn, elementValue ); + result = reduce( result, fetchValue ); } keep( rowIdx, result ); }; @@ -267,7 +243,7 @@ template< typename MatrixElementsLambda, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const +allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const { this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); } @@ -282,7 +258,6 @@ void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: forRows( IndexType first, IndexType last, Function& function ) const { - using FetchType = decltype( fetch( IndexType(), IndexType(), RealType(), IndexType() ) ); using Device_ = typename Devices::PickDevice< DeviceType >::DeviceType; const IndexType rows = this->getRows(); @@ -312,11 +287,30 @@ template< typename MatrixElementsLambda, template< typename Function > void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -forRows( IndexType first, IndexType last, Function& function ) +forAllRows( Function& function ) const { - this->forRows( 0, this->getRows(), function ); + using Device_ = typename Devices::PickDevice< DeviceType >::DeviceType; + + const IndexType rows = this->getRows(); + const IndexType columns = this->getColumns(); + auto rowLengths = this->compressedRowLengthsLambda; + auto matrixElements = this->matrixElementsLambda; + auto processRow = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { + const IndexType rowLength = rowLengths( rows, columns, rowIdx ); + bool compute( true ); + for( IndexType localIdx = 0; localIdx < rowLength && compute; localIdx++ ) + { + IndexType elementColumn( 0 ); + RealType elementValue( 0.0 ); + matrixElements( rows, columns, rowIdx, localIdx, elementColumn, elementValue ); + if( elementValue != 0.0 ) + function( rowIdx, localIdx, elementColumn, elementValue, compute ); + } + }; + Algorithms::ParallelFor< Device_ >::exec( 0, this->getRows(), processRow ); } + template< typename MatrixElementsLambda, typename CompressedRowLengthsLambda, typename Real, @@ -347,13 +341,35 @@ print( std::ostream& str ) const str <<"Row: " << row << " -> "; for( IndexType column = 0; column < this->getColumns(); column++ ) { - auto value = this->getElement( row, column ); - if( value != ( RealType ) 0 ) - str << " Col:" << column << "->" << value << "\t"; + RealType value = this->getElement( row, column ); + if( value ) + { + std::stringstream str_; + str_ << std::setw( 4 ) << std::right << column << ":" << std::setw( 4 ) << std::left << value; + str << std::setw( 10 ) << str_.str(); + } } str << std::endl; } } +/** + * \brief Insertion operator for dense matrix and output stream. + * + * \param str is the output stream. + * \param matrix is the lambda matrix. + * \return reference to the stream. + */ +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Device, + typename Index > +std::ostream& operator<< ( std::ostream& str, const LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >& matrix ) +{ + matrix.print( str ); + return str; +} + } //namespace Matrices } //namespace TNL diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index ba649e5d0..00254de3d 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -749,7 +749,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * * More precisely, it computes: * - * \e outVector = \e matrixMultiplicator * ( * \e this ) * \e inVector + \e outVectorMultiplicator * \e outVector. + * `outVector = matrixMultiplicator * ( * this ) * inVector + outVectorMultiplicator * outVector` * * \tparam InVector is type of input vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index bc608696d..02143beef 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -544,7 +544,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * * More precisely, it computes: * - * \e outVector = \e matrixMultiplicator * ( * \e this ) * \e inVector + \e outVectorMultiplicator * \e outVector. + * `outVector = matrixMultiplicator * ( * this ) * inVector + outVectorMultiplicator * outVector` * * \tparam InVector is type of input vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index c95128f7a..5475faa56 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -345,22 +345,6 @@ getElement( IndexType row, return 0.0; } -/*template< typename Real, - typename Device, - typename Index, - typename MatrixType, - template< typename, typename > class SegmentsView > - template< typename Vector > -__cuda_callable__ -typename Vector::RealType -SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >:: -rowVectorProduct( const IndexType row, - const Vector& vector ) const -{ - TNL_ASSERT_TRUE( false, "TODO: rowVectorProduct is not implemented yet."); - return 0; -}*/ - template< typename Real, typename Device, typename Index, @@ -747,9 +731,12 @@ print( std::ostream& str ) const value = ( RealType ) 1.0; else value = this->values.getElement( globalIdx ); - std::stringstream str_; - str_ << std::setw( 4 ) << std::right << column << ":" << std::setw( 4 ) << std::left << value; - str << std::setw( 10 ) << str_.str(); + if( value ) + { + std::stringstream str_; + str_ << std::setw( 4 ) << std::right << column << ":" << std::setw( 4 ) << std::left << value; + str << std::setw( 10 ) << str_.str(); + } } str << std::endl; } diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.h b/src/UnitTests/Matrices/LambdaMatrixTest.h index 07d1f336c..cc2893d9b 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.h +++ b/src/UnitTests/Matrices/LambdaMatrixTest.h @@ -102,14 +102,6 @@ TYPED_TEST( LambdaMatrixTest, rowsReduction ) test_RowsReduction< LambdaMatrixParametersType >(); } - -TYPED_TEST( LambdaMatrixTest, printTest ) -{ - using LambdaMatrixParametersType = typename TestFixture::LambdaMatrixType; - - test_Print< LambdaMatrixParametersType >(); -} - #endif #include "../main.h" diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.hpp b/src/UnitTests/Matrices/LambdaMatrixTest.hpp index 23963c119..54db3b18e 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.hpp +++ b/src/UnitTests/Matrices/LambdaMatrixTest.hpp @@ -233,11 +233,11 @@ void test_RowsReduction() TNL::Containers::Vector< RealType, DeviceType, IndexType > v( size, -1.0 ); auto vView = v.getView(); - auto fetch = [=] __cuda_callable__ ( IndexType row, IndexType localIdx, IndexType columnIdx, const RealType& value ) mutable -> RealType { + auto fetch = [=] __cuda_callable__ ( IndexType row, IndexType columnIdx, const RealType& value ) mutable -> RealType { return value; }; - auto reduce = [] __cuda_callable__ ( RealType& sum, const RealType& value ) { - sum += value; + auto reduce = [] __cuda_callable__ ( RealType& sum, const RealType& value ) -> RealType { + return sum + value; }; auto keep = [=] __cuda_callable__ ( IndexType row, const RealType& value ) mutable { vView[ row ] = value; @@ -251,56 +251,4 @@ void test_RowsReduction() EXPECT_EQ( v.getElement( 4 ), 1.0 ); } -template< typename Matrix > -void test_Print() -{ - using RealType = typename Matrix::RealType; - using DeviceType = typename Matrix::DeviceType; - using IndexType = typename Matrix::IndexType; - - IndexType size = 5; - auto rowLengths = [=] __cuda_callable__ ( const IndexType rows, const IndexType columns, const IndexType rowIdx ) -> IndexType { - if( rowIdx == 0 || rowIdx == size - 1 ) - return 1; - return 3; - }; - - auto matrixElements = [=] __cuda_callable__ ( const IndexType rows, const IndexType columns, const IndexType rowIdx, const IndexType localIdx, IndexType& columnIdx, RealType& value ) { - if( rowIdx == 0 || rowIdx == size -1 ) - { - columnIdx = rowIdx; - value = 1.0; - } - else - { - columnIdx = rowIdx + localIdx - 1; - value = ( columnIdx == rowIdx ) ? -2.0 : 1.0; - } - }; - - using MatrixType = decltype( TNL::Matrices::LambdaMatrixFactory< RealType, DeviceType, IndexType >::create( matrixElements, rowLengths ) ); - - MatrixType m( size, size, matrixElements, rowLengths ); - - 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\t\n" - "Row: 1 -> Col:0->1 Col:1->-2 Col:2->1\t\n" - "Row: 2 -> Col:1->1 Col:2->-2 Col:3->1\t\n" - "Row: 3 -> Col:2->1 Col:3->-2 Col:4->1\t\n" - "Row: 4 -> Col:4->1\t\n"; - - EXPECT_EQ( printed.str(), couted.str() ); -} - - #endif // HAVE_GTEST -- GitLab From ca11c5d810e4012af230587afcffca19524d52a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 29 May 2020 19:20:16 +0200 Subject: [PATCH 21/38] Writting documentation on MultidiagonalMatrix. --- src/TNL/Matrices/MatrixInfo.h | 3 + src/TNL/Matrices/MatrixReader.h | 3 + src/TNL/Matrices/MatrixReader_impl.h | 3 + ...{Multidiagonal.h => MultidiagonalMatrix.h} | 234 +++++++++++++++--- ...tidiagonal.hpp => MultidiagonalMatrix.hpp} | 168 +++++++------ src/TNL/Matrices/MultidiagonalMatrixRowView.h | 20 ++ src/TNL/Matrices/MultidiagonalMatrixView.hpp | 2 +- src/TNL/Matrices/SparseMatrix.h | 4 +- src/TNL/Matrices/SparseMatrixView.h | 4 +- .../details/MultidiagonalMatrixIndexer.h | 1 + .../Matrices/BinarySparseMatrixCopyTest.h | 6 +- src/UnitTests/Matrices/DenseMatrixCopyTest.h | 6 +- .../Matrices/MultidiagonalMatrixTest.h | 78 +++--- src/UnitTests/Matrices/SparseMatrixCopyTest.h | 6 +- 14 files changed, 370 insertions(+), 168 deletions(-) rename src/TNL/Matrices/{Multidiagonal.h => MultidiagonalMatrix.h} (50%) rename src/TNL/Matrices/{Multidiagonal.hpp => MultidiagonalMatrix.hpp} (78%) diff --git a/src/TNL/Matrices/MatrixInfo.h b/src/TNL/Matrices/MatrixInfo.h index 1d825a769..65c2aca63 100644 --- a/src/TNL/Matrices/MatrixInfo.h +++ b/src/TNL/Matrices/MatrixInfo.h @@ -34,6 +34,8 @@ template< typename Matrix > struct MatrixInfo {}; +/// This is to prevent from appearing in Doxygen documentation. +/// \cond HIDDEN_CLASS template< typename Real, typename Device, typename Index, @@ -151,5 +153,6 @@ struct MatrixInfo< Legacy::SlicedEllpack< Real, Device, Index, SliceSize> > static String getFormat() { return "SlicedEllpack Legacy"; }; }; +/// \endcond } //namespace Matrices } //namespace TNL diff --git a/src/TNL/Matrices/MatrixReader.h b/src/TNL/Matrices/MatrixReader.h index ae0606678..c9960982a 100644 --- a/src/TNL/Matrices/MatrixReader.h +++ b/src/TNL/Matrices/MatrixReader.h @@ -17,9 +17,12 @@ namespace TNL { namespace Matrices { +/// This is to prevent from appearing in Doxygen documentation. +/// \cond HIDDEN_CLASS template< typename Device > class MatrixReaderDeviceDependentCode {}; +/// \endcond template< typename Matrix > class MatrixReader diff --git a/src/TNL/Matrices/MatrixReader_impl.h b/src/TNL/Matrices/MatrixReader_impl.h index df2c05c63..862d4a285 100644 --- a/src/TNL/Matrices/MatrixReader_impl.h +++ b/src/TNL/Matrices/MatrixReader_impl.h @@ -357,6 +357,8 @@ void MatrixReader< Matrix >::parseMtxLineWithElement( const String& line, value = ( RealType ) atof( parsedLine[ 2 ].getString() ); } +/// This is to prevent from appearing in Doxygen documentation. +/// \cond HIDDEN_CLASS template<> class MatrixReaderDeviceDependentCode< Devices::Host > { @@ -392,6 +394,7 @@ class MatrixReaderDeviceDependentCode< Devices::Cuda > MatrixReader< Matrix >::readMtxFileHostMatrix( file, matrix, rowLengths, verbose, symReader ); } }; +/// \endcond } // namespace Matrices } // namespace TNL diff --git a/src/TNL/Matrices/Multidiagonal.h b/src/TNL/Matrices/MultidiagonalMatrix.h similarity index 50% rename from src/TNL/Matrices/Multidiagonal.h rename to src/TNL/Matrices/MultidiagonalMatrix.h index 9823a7b61..b2a5df646 100644 --- a/src/TNL/Matrices/Multidiagonal.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -20,71 +20,225 @@ namespace TNL { namespace Matrices { +/** + * \brief Implementation of sparse multi-diagonal matrix. + * + * Use this matrix type for storing of matrices where the offsets of non-zero elements + * from the diagonal are the same in each row. Typically such matrices arise from + * discretization of partial differential equations on regular numerical grids. + * + * \tparam Real is a type of matrix elements. + * \tparam Device is a device where the matrix is allocated. + * \tparam Index is a type for indexing of the matrix elements. + * \tparam Organization tells the ordering of matrix elements. It is either RowMajorOrder + * or ColumnMajorOrder. + * \tparam RealAllocator is allocator for the matrix elements. + * \tparam IndexAllocator is allocator for the matrix elements offsets. + */ template< typename Real = double, typename Device = Devices::Host, typename Index = int, ElementsOrganization Organization = Containers::Segments::DefaultElementsOrganization< Device >::getOrganization(), typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index > > -class Multidiagonal : public Matrix< Real, Device, Index, RealAllocator > +class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > { public: + + // Supporting types - they are not important for the user + using BaseType = Matrix< Real, Device, Index, RealAllocator >; + using ValuesVectorType = typename BaseType::ValuesVectorType; + using ValuesViewType = typename ValuesVectorType::ViewType; + using IndexerType = details::MultidiagonalMatrixIndexer< Index, Organization >; + using DiagonalsShiftsType = Containers::Vector< Index, Device, Index, IndexAllocator >; + using DiagonalsShiftsView = typename DiagonalsShiftsType::ViewType; + using HostDiagonalsShiftsType = Containers::Vector< Index, Devices::Host, Index >; + using HostDiagonalsShiftsView = typename HostDiagonalsShiftsType::ViewType; + + /** + * \brief The type of matrix elements. + */ using RealType = Real; + + /** + * \brief The device where the matrix is allocated. + */ using DeviceType = Device; + + /** + * \brief The type used for matrix elements indexing. + */ using IndexType = Index; + + /** + * \brief The allocator for matrix elements values. + */ using RealAllocatorType = RealAllocator; + + /** + * \brief The allocator for matrix elements offsets from the diagonal. + */ using IndexAllocatorType = IndexAllocator; - using BaseType = Matrix< Real, Device, Index, RealAllocator >; - using ValuesVectorType = typename BaseType::ValuesVectorType; - using ValuesViewType = typename ValuesVectorType::ViewType; - using IndexerType = details::MultidiagonalMatrixIndexer< IndexType, Organization >; - using DiagonalsShiftsType = Containers::Vector< IndexType, DeviceType, IndexType, IndexAllocatorType >; - using DiagonalsShiftsView = typename DiagonalsShiftsType::ViewType; - using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsShiftsView >; + + /** + * \brief Type of related matrix view. + * + * See \ref MultidiagonalMatrixView. + */ using ViewType = MultidiagonalMatrixView< Real, Device, Index, Organization >; - using ConstViewType = MultidiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; - using HostDiagonalsShiftsType = Containers::Vector< IndexType, Devices::Host, IndexType >; - using HostDiagonalsShiftsView = typename HostDiagonalsShiftsType::ViewType; + /** + * \brief Matrix view type for constant instances. + * + * See \ref MutlidiagonlMatrixView. + */ + using ConstViewType = MultidiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; + /** + * \brief Type for accessing matrix rows. + */ + using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsShiftsView >; - // TODO: remove this - it is here only for compatibility with original matrix implementation - typedef Containers::Vector< IndexType, DeviceType, IndexType > CompressedRowLengthsVector; - typedef Containers::VectorView< IndexType, DeviceType, IndexType > CompressedRowLengthsVectorView; - typedef typename CompressedRowLengthsVectorView::ConstViewType ConstCompressedRowLengthsVectorView; + /** + * \brief Type for accessing constant matrix rows. + */ + using ConstRowView = typename RowView::ConstViewType; + /** + * \brief Helper type for getting self type or its modifications. + */ template< typename _Real = Real, typename _Device = Device, - typename _Index = Index > - using Self = Multidiagonal< _Real, _Device, _Index >; - + typename _Index = Index, + ElementsOrganization _Organization = Organization, + typename _RealAllocator = RealAllocator, + typename _IndexAllocator = IndexAllocator > + using Self = MultidiagonalMatrix< _Real, _Device, _Index, _Organization, _RealAllocator, _IndexAllocator >; + + /** + * \brief Elements organization getter. + */ static constexpr ElementsOrganization getOrganization() { return Organization; }; - Multidiagonal(); - - Multidiagonal( const IndexType rows, - const IndexType columns ); - + /** + * \brief Constructor with no parameters. + */ + MultidiagonalMatrix(); + + /** + * \brief Constructor with matrix dimensions. + * + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + */ + MultidiagonalMatrix( const IndexType rows, + const IndexType columns ); + + /** + * \brief Constructor with matrix dimensions and matrix elements offsets. + * + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + * \param diagonalsShifts are shifts of subdiagonals from the main diagonal. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp + * \par Output + * \include MultidiagonalMatrixExample_Constructor.out + * + */ template< typename Vector > - Multidiagonal( const IndexType rows, - const IndexType columns, - const Vector& diagonalsShifts ); - + MultidiagonalMatrix( const IndexType rows, + const IndexType columns, + const Vector& diagonalsShifts ); + + /** + * \brief Constructor with matrix dimensions and diagonals shifts. + * + * \param rows + * \param columns + * \param diagonalsShifts + */ + template< typename ListIndex > + MultidiagonalMatrix( const IndexType rows, + const IndexType columns, + const std::initializer_list< ListIndex > diagonalsShifts ); + + template< typename ListIndex, typename ListReal > + MultidiagonalMatrix( const IndexType columns, + const std::initializer_list< ListIndex > diagonalsShifts, + const std::initializer_list< std::initializer_list< ListReal > >& data ) + + + /** + * \brief Copy constructor. + * + * \param matrix is an input matrix. + */ + MultidiagonalMatrix( const MultidiagonalMatrix& matrix ) = default; + + /** + * \brief Move constructor. + * + * \param matrix is an input matrix. + */ + MultidiagonalMatrix( MultidiagonalMatrix&& matrix ) = default; + + /** + * \brief Returns a modifiable view of the mutlidiagonal matrix. + * + * See \ref MultidiagonalMatrixView. + * + * \return multidiagonal matrix view. + */ ViewType getView() const; // TODO: remove const - //ConstViewType getConstView() const; - + /** + * \brief Returns a non-modifiable view of the multidiagonal matrix. + * + * See \ref MultidiagonalMatrixView. + * + * \return multidiagonal matrix view. + */ + ConstViewType getConstView() const; + + /** + * \brief Returns string with serialization type. + * + * The string has a form `Matrices::MultidiagonalMatrix< RealType, [any_device], IndexType, ElementsOrganization, [any_allocator], [any_allocator] >`. + * + * \return \ref String with the serialization type. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp + * \par Output + * \include SparseMatrixExample_getSerializationType.out + */ static String getSerializationType(); + /** + * \brief Returns string with serialization type. + * + * See \ref SparseMatrix::getSerializationType. + * + * \return \e String with the serialization type. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp + * \par Output + * \include SparseMatrixExample_getSerializationType.out + */ virtual String getSerializationTypeVirtual() const; template< typename Vector > void setDimensions( const IndexType rows, const IndexType columns, - const Vector& diagonalsShifts ); + const Vector& diagonalsShifts ); + + template< typename RowLengthsVector > + void setCompressedRowLengths( const RowLengthsVector& rowCapacities ); - //template< typename Vector > - void setCompressedRowLengths( const ConstCompressedRowLengthsVectorView rowCapacities ); + void setElements( const std::initializer_list< std::initializer_list< ListReal > >& data ) const IndexType& getDiagonalsCount() const; @@ -101,17 +255,17 @@ class Multidiagonal : public Matrix< Real, Device, Index, RealAllocator > IndexType getMaxRowLength() const; template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - void setLike( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& m ); + void setLike( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& m ); IndexType getNumberOfNonzeroMatrixElements() const; void reset(); template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - bool operator == ( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const; + bool operator == ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const; template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - bool operator != ( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const; + bool operator != ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const; __cuda_callable__ RowView getRow( const IndexType& rowIdx ); @@ -162,12 +316,12 @@ class Multidiagonal : public Matrix< Real, Device, Index, RealAllocator > OutVector& outVector ) const; template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - void addMatrix( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix, + void addMatrix( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix, const RealType& matrixMultiplicator = 1.0, const RealType& thisMatrixMultiplicator = 1.0 ); template< typename Real2, typename Index2 > - void getTransposition( const Multidiagonal< Real2, Device, Index2 >& matrix, + void getTransposition( const MultidiagonalMatrix< Real2, Device, Index2 >& matrix, const RealType& matrixMultiplicator = 1.0 ); template< typename Vector1, typename Vector2 > @@ -178,7 +332,7 @@ class Multidiagonal : public Matrix< Real, Device, Index, RealAllocator > const RealType& omega = 1.0 ) const; // copy assignment - Multidiagonal& operator=( const Multidiagonal& matrix ); + MultidiagonalMatrix& operator=( const MultidiagonalMatrix& matrix ); // cross-device copy assignment template< typename Real_, @@ -187,7 +341,7 @@ class Multidiagonal : public Matrix< Real, Device, Index, RealAllocator > ElementsOrganization Organization_, typename RealAllocator_, typename IndexAllocator_ > - Multidiagonal& operator=( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ); + MultidiagonalMatrix& operator=( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ); void save( File& file ) const; @@ -224,4 +378,4 @@ class Multidiagonal : public Matrix< Real, Device, Index, RealAllocator > } // namespace Matrices } // namespace TNL -#include +#include diff --git a/src/TNL/Matrices/Multidiagonal.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp similarity index 78% rename from src/TNL/Matrices/Multidiagonal.hpp rename to src/TNL/Matrices/MultidiagonalMatrix.hpp index f20217dfa..b32663812 100644 --- a/src/TNL/Matrices/Multidiagonal.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -1,5 +1,5 @@ /*************************************************************************** - Multidiagonal.hpp - description + MultidiagonalMatrix.hpp - description ------------------- begin : Oct 13, 2011 copyright : (C) 2011 by Tomas Oberhuber @@ -12,14 +12,14 @@ #include #include -#include +#include #include namespace TNL { namespace Matrices { template< typename Device > -class MultidiagonalDeviceDependentCode; +class MultidiagonalMatrixDeviceDependentCode; template< typename Real, typename Device, @@ -27,8 +27,8 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -Multidiagonal() +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix() { } @@ -39,8 +39,8 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > template< typename Vector > -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -Multidiagonal( const IndexType rows, +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix( const IndexType rows, const IndexType columns, const Vector& diagonalsShifts ) { @@ -48,6 +48,24 @@ Multidiagonal( const IndexType rows, this->setDimensions( rows, columns, diagonalsShifts ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator, + typename IndexAllocator > + template< typename ListIndex > +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix( const IndexType rows, + const IndexType columns, + const std::initializer_list< ListIndex > diagonalsShifts ) +{ + Containers::Vector< IndexType, DeviceType, IndexType > shifts( diagonalsShifts ); + TNL_ASSERT_GT( shifts.getSize(), 0, "Cannot construct mutltidiagonal matrix with no diagonals shifts." ); + this->setDimensions( rows, columns, shifts ); +} + + template< typename Real, typename Device, typename Index, @@ -55,13 +73,13 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > auto -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getView() const -> ViewType { // TODO: fix when getConstView works - return ViewType( const_cast< Multidiagonal* >( this )->values.getView(), - const_cast< Multidiagonal* >( this )->diagonalsShifts.getView(), - const_cast< Multidiagonal* >( this )->hostDiagonalsShifts.getView(), + return ViewType( const_cast< MultidiagonalMatrix* >( this )->values.getView(), + const_cast< MultidiagonalMatrix* >( this )->diagonalsShifts.getView(), + const_cast< MultidiagonalMatrix* >( this )->hostDiagonalsShifts.getView(), indexer ); } @@ -72,7 +90,7 @@ getView() const -> ViewType typename RealAllocator, typename IndexAllocator > auto -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getConstView() const -> ConstViewType { return ConstViewType( this->values.getConstView(), indexer ); @@ -85,10 +103,10 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > String -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getSerializationType() { - return String( "Matrices::Multidiagonal< " ) + + return String( "Matrices::MultidiagonalMatrix< " ) + TNL::getSerializationType< RealType >() + ", [any_device], " + TNL::getSerializationType< IndexType >() + ", " + ( Organization ? "true" : "false" ) + ", [any_allocator], [any_allocator] >"; @@ -101,7 +119,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > String -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getSerializationTypeVirtual() const { return this->getSerializationType(); @@ -115,7 +133,7 @@ template< typename Real, typename IndexAllocator > template< typename Vector > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: setDimensions( const IndexType rows, const IndexType columns, const Vector& diagonalsShifts ) @@ -139,10 +157,10 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > - // template< typename Vector > + template< typename RowLengthsVector > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -setCompressedRowLengths( const ConstCompressedRowLengthsVectorView rowLengths ) +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +setCompressedRowLengths( const RowLengthsVector& rowLengths ) { if( max( rowLengths ) > 3 ) throw std::logic_error( "Too many non-zero elements per row in a tri-diagonal matrix." ); @@ -167,7 +185,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > const Index& -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getDiagonalsCount() const { return this->view.getDiagonalsCount(); @@ -180,7 +198,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > auto -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getDiagonalsShifts() const -> const DiagonalsShiftsType& { return this->diagonalsShifts; @@ -194,7 +212,7 @@ template< typename Real, typename IndexAllocator > template< typename Vector > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getCompressedRowLengths( Vector& rowLengths ) const { return this->view.getCompressedRowLengths( rowLengths ); @@ -207,7 +225,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > Index -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getNonemptyRowsCount() const { return this->indexer.getNonemptyRowsCount(); @@ -220,7 +238,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > Index -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getRowLength( const IndexType row ) const { return this->view.getRowLength( row ); @@ -233,7 +251,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > Index -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getMaxRowLength() const { return this->view.getMaxRowLength(); @@ -247,8 +265,8 @@ template< typename Real, typename IndexAllocator > template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -setLike( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& m ) +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +setLike( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& m ) { this->setDimensions( m.getRows(), m.getColumns(), m.getDiagonalsShifts() ); } @@ -260,7 +278,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > Index -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getNumberOfNonzeroMatrixElements() const { return this->view.getNumberOfNonzeroMatrixElements(); @@ -273,7 +291,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: reset() { Matrix< Real, Device, Index >::reset(); @@ -287,8 +305,8 @@ template< typename Real, typename IndexAllocator > template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > bool -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -operator == ( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +operator == ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const { if( Organization == Organization_ ) return this->values == matrix.values; @@ -306,8 +324,8 @@ template< typename Real, typename IndexAllocator > template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > bool -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -operator != ( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +operator != ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const { return ! this->operator==( matrix ); } @@ -319,7 +337,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: setValue( const RealType& v ) { this->view.setValue( v ); @@ -333,7 +351,7 @@ template< typename Real, typename IndexAllocator > __cuda_callable__ auto -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getRow( const IndexType& rowIdx ) const -> const RowView { return this->view.getRow( rowIdx ); @@ -347,7 +365,7 @@ template< typename Real, typename IndexAllocator > __cuda_callable__ auto -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getRow( const IndexType& rowIdx ) -> RowView { return this->view.getRow( rowIdx ); @@ -360,7 +378,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: setElement( const IndexType row, const IndexType column, const RealType& value ) { this->view.setElement( row, column, value ); @@ -373,7 +391,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: addElement( const IndexType row, const IndexType column, const RealType& value, @@ -389,7 +407,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > Real -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getElement( const IndexType row, const IndexType column ) const { return this->view.getElement( row, column ); @@ -403,7 +421,7 @@ template< typename Real, typename IndexAllocator > template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const { this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); @@ -417,7 +435,7 @@ template< typename Real, typename IndexAllocator > template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const { this->view.rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); @@ -431,7 +449,7 @@ template< typename Real, typename IndexAllocator > template< typename Function > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: forRows( IndexType first, IndexType last, Function& function ) const { this->view.forRows( first, last, function ); @@ -445,7 +463,7 @@ template< typename Real, typename IndexAllocator > template< typename Function > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: forRows( IndexType first, IndexType last, Function& function ) { this->view.forRows( first, last, function ); @@ -459,7 +477,7 @@ template< typename Real, typename IndexAllocator > template< typename Function > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: forAllRows( Function& function ) const { this->view.forRows( 0, this->getRows(), function ); @@ -473,7 +491,7 @@ template< typename Real, typename IndexAllocator > template< typename Function > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: forAllRows( Function& function ) { this->view.forRows( 0, this->getRows(), function ); @@ -488,7 +506,7 @@ template< typename Real, template< typename Vector > __cuda_callable__ typename Vector::RealType -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: rowVectorProduct( const IndexType row, const Vector& vector ) const { return this->view.rowVectorProduct(); @@ -503,7 +521,7 @@ template< typename Real, template< typename InVector, typename OutVector > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: vectorProduct( const InVector& inVector, OutVector& outVector ) const { this->view.vectorProduct( inVector, outVector ); @@ -517,8 +535,8 @@ template< typename Real, typename IndexAllocator > template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -addMatrix( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix, +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +addMatrix( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix, const RealType& matrixMultiplicator, const RealType& thisMatrixMultiplicator ) { @@ -530,8 +548,8 @@ template< typename Real, typename Real2, typename Index, typename Index2 > -__global__ void MultidiagonalTranspositionCudaKernel( const Multidiagonal< Real2, Devices::Cuda, Index2 >* inMatrix, - Multidiagonal< Real, Devices::Cuda, Index >* outMatrix, +__global__ void MultidiagonalMatrixTranspositionCudaKernel( const MultidiagonalMatrix< Real2, Devices::Cuda, Index2 >* inMatrix, + MultidiagonalMatrix< Real, Devices::Cuda, Index >* outMatrix, const Real matrixMultiplicator, const Index gridIdx ) { @@ -560,7 +578,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > template< typename Real2, typename Index2 > -void Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::getTransposition( const Multidiagonal< Real2, Device, Index2 >& matrix, +void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::getTransposition( const MultidiagonalMatrix< Real2, Device, Index2 >& matrix, const RealType& matrixMultiplicator ) { TNL_ASSERT( this->getRows() == matrix.getRows(), @@ -580,8 +598,8 @@ void Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAlloc if( std::is_same< Device, Devices::Cuda >::value ) { #ifdef HAVE_CUDA - Multidiagonal* kernel_this = Cuda::passToDevice( *this ); - typedef Multidiagonal< Real2, Device, Index2 > InMatrixType; + MultidiagonalMatrix* kernel_this = Cuda::passToDevice( *this ); + typedef MultidiagonalMatrix< Real2, Device, Index2 > InMatrixType; InMatrixType* kernel_inMatrix = Cuda::passToDevice( matrix ); dim3 cudaBlockSize( 256 ), cudaGridSize( Cuda::getMaxGridSize() ); const IndexType cudaBlocks = roundUpDivision( matrix.getRows(), cudaBlockSize.x ); @@ -590,7 +608,7 @@ void Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAlloc { if( gridIdx == cudaGrids - 1 ) cudaGridSize.x = cudaBlocks % Cuda::getMaxGridSize(); - MultidiagonalTranspositionCudaKernel<<< cudaGridSize, cudaBlockSize >>> + MultidiagonalMatrixTranspositionCudaKernel<<< cudaGridSize, cudaBlockSize >>> ( kernel_inMatrix, kernel_this, matrixMultiplicator, @@ -611,7 +629,7 @@ template< typename Real, typename IndexAllocator > template< typename Vector1, typename Vector2 > __cuda_callable__ -void Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::performSORIteration( const Vector1& b, +void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::performSORIteration( const Vector1& b, const IndexType row, Vector2& x, const RealType& omega ) const @@ -632,8 +650,8 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >& -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::operator=( const Multidiagonal& matrix ) +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >& +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::operator=( const MultidiagonalMatrix& matrix ) { this->setLike( matrix ); this->values = matrix.values; @@ -648,11 +666,11 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_, typename IndexAllocator_ > -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >& -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -operator=( const Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ) +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >& +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +operator=( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ) { - using RHSMatrix = Multidiagonal< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >; + using RHSMatrix = MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >; using RHSIndexType = typename RHSMatrix::IndexType; using RHSRealType = typename RHSMatrix::RealType; using RHSDeviceType = typename RHSMatrix::DeviceType; @@ -722,7 +740,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > -void Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::save( File& file ) const +void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::save( File& file ) const { Matrix< Real, Device, Index >::save( file ); file << diagonalsShifts; @@ -734,7 +752,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > -void Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::load( File& file ) +void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::load( File& file ) { Matrix< Real, Device, Index >::load( file ); file >> this->diagonalsShifts; @@ -753,7 +771,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > -void Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::save( const String& fileName ) const +void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::save( const String& fileName ) const { Object::save( fileName ); } @@ -764,7 +782,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > -void Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::load( const String& fileName ) +void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::load( const String& fileName ) { Object::load( fileName ); } @@ -776,7 +794,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > void -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: print( std::ostream& str ) const { this->view.print( str ); @@ -789,7 +807,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > auto -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getIndexer() const -> const IndexerType& { return this->indexer; @@ -802,7 +820,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > auto -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getIndexer() -> IndexerType& { return this->indexer; @@ -815,7 +833,7 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > __cuda_callable__ -Index Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +Index MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getElementIndex( const IndexType row, const IndexType column ) const { IndexType localIdx = column - row; @@ -836,7 +854,7 @@ template< typename Real, typename IndexAllocator > __cuda_callable__ Index -Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getPaddingIndex() const { return this->view.getPaddingIndex(); @@ -844,7 +862,7 @@ getPaddingIndex() const /* template<> -class MultidiagonalDeviceDependentCode< Devices::Host > +class MultidiagonalMatrixDeviceDependentCode< Devices::Host > { public: @@ -884,7 +902,7 @@ class MultidiagonalDeviceDependentCode< Devices::Host > typename Index, typename InVector, typename OutVector > - static void vectorProduct( const Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >& matrix, + static void vectorProduct( const MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >& matrix, const InVector& inVector, OutVector& outVector ) { @@ -897,7 +915,7 @@ class MultidiagonalDeviceDependentCode< Devices::Host > }; template<> -class MultidiagonalDeviceDependentCode< Devices::Cuda > +class MultidiagonalMatrixDeviceDependentCode< Devices::Cuda > { public: @@ -937,7 +955,7 @@ class MultidiagonalDeviceDependentCode< Devices::Cuda > typename Index, typename InVector, typename OutVector > - static void vectorProduct( const Multidiagonal< Real, Device, Index, Organization, RealAllocator, IndexAllocator >& matrix, + static void vectorProduct( const MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >& matrix, const InVector& inVector, OutVector& outVector ) { diff --git a/src/TNL/Matrices/MultidiagonalMatrixRowView.h b/src/TNL/Matrices/MultidiagonalMatrixRowView.h index 0825d6fb3..72e4328fb 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixRowView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixRowView.h @@ -25,6 +25,26 @@ class MultidiagonalMatrixRowView using ValuesViewType = ValuesView; using IndexerType = Indexer; using DiagonalsShiftsView = DiagonalsShiftsView_; + + /** + * \brief Type of constant container view used for storing the matrix elements values. + */ + using ConstValuesViewType = typename ValuesViewType::ConstViewType; + + /** + * \brief Type of constant container view used for storing the column indexes of the matrix elements. + */ + using ConstDiagonalsShiftsViewType = typename DiagonalsShiftsView::ConstViewType; + + /** + * \brief Type of constant indexer view. + */ + using ConstIndexerViewType = typename Indexer::ConstType; + + /** + * \brief Type of constant sparse matrix row view. + */ + using ConstViewType = MultidiagonalMatrixRowView< ConstValuesViewType, ConstIndexerViewType, ConstDiagonalsShiftsViewType >; __cuda_callable__ MultidiagonalMatrixRowView( const IndexType rowIdx, diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index 634debab4..588fd3d60 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -81,7 +81,7 @@ getSerializationType() return String( "Matrices::Multidiagonal< " ) + TNL::getSerializationType< RealType >() + ", [any_device], " + TNL::getSerializationType< IndexType >() + ", " + - ( Organization ? "true" : "false" ) + ", [any_allocator] >"; + TNL::getSerializationType( Organization ) + ", [any_allocator], [any_allocator] >"; } template< typename Real, diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 00254de3d..6125df8d9 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -299,9 +299,9 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Returns string with serialization type. * - * The string has a form \e `Matrices::SparseMatrix< RealType, [any_device], IndexType, General/Symmetric, Format, [any_allocator] >`. + * The string has a form `Matrices::SparseMatrix< RealType, [any_device], IndexType, General/Symmetric, Format, [any_allocator] >`. * - * \return \e String with the serialization type. + * \return \ref String with the serialization type. * * \par Example * \include Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index 02143beef..30d0559b2 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -188,9 +188,9 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Returns string with serialization type. * - * The string has a form \e `Matrices::SparseMatrix< RealType, [any_device], IndexType, General/Symmetric, Format, [any_allocator] >`. + * The string has a form `Matrices::SparseMatrix< RealType, [any_device], IndexType, General/Symmetric, Format, [any_allocator] >`. * - * \return \e String with the serialization type. + * \return \ref String with the serialization type. * * \par Example * \include Matrices/SparseMatrix/SparseMatrixViewExample_getSerializationType.cpp diff --git a/src/TNL/Matrices/details/MultidiagonalMatrixIndexer.h b/src/TNL/Matrices/details/MultidiagonalMatrixIndexer.h index 3597c30f7..bba7b524e 100644 --- a/src/TNL/Matrices/details/MultidiagonalMatrixIndexer.h +++ b/src/TNL/Matrices/details/MultidiagonalMatrixIndexer.h @@ -21,6 +21,7 @@ class MultidiagonalMatrixIndexer public: using IndexType = Index; + using ConstType = MultidiagonalMatrixIndexer< std::add_const_t< Index >, RowMajorOrder >; static constexpr bool getRowMajorOrder() { return RowMajorOrder; }; diff --git a/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h b/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h index d7a3a429d..0211f986d 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h +++ b/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -483,8 +483,8 @@ void multidiagonalMatrixAssignment() using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using MultidiagonalHost = TNL::Matrices::Multidiagonal< RealType, TNL::Devices::Host, IndexType >; - using MultidiagonalCuda = TNL::Matrices::Multidiagonal< RealType, TNL::Devices::Cuda, IndexType >; + using MultidiagonalHost = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Host, IndexType >; + using MultidiagonalCuda = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType >; using DiagonalsShiftsType = typename MultidiagonalHost::DiagonalsShiftsType; DiagonalsShiftsType diagonals{ -4, -2, 0, 1, 3, 5 }; diff --git a/src/UnitTests/Matrices/DenseMatrixCopyTest.h b/src/UnitTests/Matrices/DenseMatrixCopyTest.h index d311d774d..3e5f9af7f 100644 --- a/src/UnitTests/Matrices/DenseMatrixCopyTest.h +++ b/src/UnitTests/Matrices/DenseMatrixCopyTest.h @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -449,8 +449,8 @@ void multidiagonalMatrixAssignment() using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using MultidiagonalHost = TNL::Matrices::Multidiagonal< RealType, TNL::Devices::Host, IndexType >; - using MultidiagonalCuda = TNL::Matrices::Multidiagonal< RealType, TNL::Devices::Cuda, IndexType >; + using MultidiagonalHost = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Host, IndexType >; + using MultidiagonalCuda = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType >; using DiagonalsShiftsType = typename MultidiagonalHost::DiagonalsShiftsType; DiagonalsShiftsType diagonals{ -4, -2, 0, 1, 3, 5 }; diff --git a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h index 8ee8c7ffb..f9bfc95aa 100644 --- a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include @@ -19,11 +19,11 @@ #include #include -using Multidiagonal_host_float = TNL::Matrices::Multidiagonal< float, TNL::Devices::Host, int >; -using Multidiagonal_host_int = TNL::Matrices::Multidiagonal< int, TNL::Devices::Host, int >; +using Multidiagonal_host_float = TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Host, int >; +using Multidiagonal_host_int = TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Host, int >; -using Multidiagonal_cuda_float = TNL::Matrices::Multidiagonal< float, TNL::Devices::Cuda, int >; -using Multidiagonal_cuda_int = TNL::Matrices::Multidiagonal< int, TNL::Devices::Cuda, int >; +using Multidiagonal_cuda_float = TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Cuda, int >; +using Multidiagonal_cuda_int = TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Cuda, int >; static const char* TEST_FILE_NAME = "test_MultidiagonalMatrixTest.tnl"; @@ -35,14 +35,14 @@ static const char* TEST_FILE_NAME = "test_MultidiagonalMatrixTest.tnl"; void test_GetSerializationType() { using namespace TNL::Containers::Segments; - EXPECT_EQ( ( TNL::Matrices::Multidiagonal< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Multidiagonal< float, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Multidiagonal< int, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Multidiagonal< int, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Multidiagonal< float, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Multidiagonal< float, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Multidiagonal< int, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Multidiagonal< int, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Multidiagonal< float, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Multidiagonal< float, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Multidiagonal< int, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Multidiagonal< int, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Multidiagonal< float, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Multidiagonal< float, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Multidiagonal< int, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Multidiagonal< int, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); } template< typename Matrix > @@ -1150,8 +1150,8 @@ void test_AssignmentOperator() using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; constexpr TNL::Containers::Segments::ElementsOrganization organization = Matrix::getOrganization(); - using MultidiagonalHost = TNL::Matrices::Multidiagonal< RealType, TNL::Devices::Host, IndexType, organization >; - using MultidiagonalCuda = TNL::Matrices::Multidiagonal< RealType, TNL::Devices::Cuda, IndexType, + using MultidiagonalHost = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Host, IndexType, organization >; + using MultidiagonalCuda = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType, organization == TNL::Containers::Segments::RowMajorOrder ? TNL::Containers::Segments::ColumnMajorOrder : TNL::Containers::Segments::RowMajorOrder >; const IndexType rows( 10 ), columns( 10 ); @@ -1332,31 +1332,31 @@ protected: // types for which MatrixTest is instantiated using MatrixTypes = ::testing::Types < - TNL::Matrices::Multidiagonal< int, TNL::Devices::Host, short >, - TNL::Matrices::Multidiagonal< long, TNL::Devices::Host, short >, - TNL::Matrices::Multidiagonal< float, TNL::Devices::Host, short >, - TNL::Matrices::Multidiagonal< double, TNL::Devices::Host, short >, - TNL::Matrices::Multidiagonal< int, TNL::Devices::Host, int >, - TNL::Matrices::Multidiagonal< long, TNL::Devices::Host, int >, - TNL::Matrices::Multidiagonal< float, TNL::Devices::Host, int >, - TNL::Matrices::Multidiagonal< double, TNL::Devices::Host, int >, - TNL::Matrices::Multidiagonal< int, TNL::Devices::Host, long >, - TNL::Matrices::Multidiagonal< long, TNL::Devices::Host, long >, - TNL::Matrices::Multidiagonal< float, TNL::Devices::Host, long >, - TNL::Matrices::Multidiagonal< double, TNL::Devices::Host, long > + TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Host, short >, + TNL::Matrices::MultidiagonalMatrix< long, TNL::Devices::Host, short >, + TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Host, short >, + TNL::Matrices::MultidiagonalMatrix< double, TNL::Devices::Host, short >, + TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Host, int >, + TNL::Matrices::MultidiagonalMatrix< long, TNL::Devices::Host, int >, + TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Host, int >, + TNL::Matrices::MultidiagonalMatrix< double, TNL::Devices::Host, int >, + TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Host, long >, + TNL::Matrices::MultidiagonalMatrix< long, TNL::Devices::Host, long >, + TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Host, long >, + TNL::Matrices::MultidiagonalMatrix< double, TNL::Devices::Host, long > #ifdef HAVE_CUDA - ,TNL::Matrices::Multidiagonal< int, TNL::Devices::Cuda, short >, - TNL::Matrices::Multidiagonal< long, TNL::Devices::Cuda, short >, - TNL::Matrices::Multidiagonal< float, TNL::Devices::Cuda, short >, - TNL::Matrices::Multidiagonal< double, TNL::Devices::Cuda, short >, - TNL::Matrices::Multidiagonal< int, TNL::Devices::Cuda, int >, - TNL::Matrices::Multidiagonal< long, TNL::Devices::Cuda, int >, - TNL::Matrices::Multidiagonal< float, TNL::Devices::Cuda, int >, - TNL::Matrices::Multidiagonal< double, TNL::Devices::Cuda, int >, - TNL::Matrices::Multidiagonal< int, TNL::Devices::Cuda, long >, - TNL::Matrices::Multidiagonal< long, TNL::Devices::Cuda, long >, - TNL::Matrices::Multidiagonal< float, TNL::Devices::Cuda, long >, - TNL::Matrices::Multidiagonal< double, TNL::Devices::Cuda, long > + ,TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Cuda, short >, + TNL::Matrices::MultidiagonalMatrix< long, TNL::Devices::Cuda, short >, + TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Cuda, short >, + TNL::Matrices::MultidiagonalMatrix< double, TNL::Devices::Cuda, short >, + TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Cuda, int >, + TNL::Matrices::MultidiagonalMatrix< long, TNL::Devices::Cuda, int >, + TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Cuda, int >, + TNL::Matrices::MultidiagonalMatrix< double, TNL::Devices::Cuda, int >, + TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Cuda, long >, + TNL::Matrices::MultidiagonalMatrix< long, TNL::Devices::Cuda, long >, + TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Cuda, long >, + TNL::Matrices::MultidiagonalMatrix< double, TNL::Devices::Cuda, long > #endif >; diff --git a/src/UnitTests/Matrices/SparseMatrixCopyTest.h b/src/UnitTests/Matrices/SparseMatrixCopyTest.h index 6f8a142a6..535c37ea3 100644 --- a/src/UnitTests/Matrices/SparseMatrixCopyTest.h +++ b/src/UnitTests/Matrices/SparseMatrixCopyTest.h @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -486,8 +486,8 @@ void multidiagonalMatrixAssignment() using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using MultidiagonalHost = TNL::Matrices::Multidiagonal< RealType, TNL::Devices::Host, IndexType >; - using MultidiagonalCuda = TNL::Matrices::Multidiagonal< RealType, TNL::Devices::Cuda, IndexType >; + using MultidiagonalHost = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Host, IndexType >; + using MultidiagonalCuda = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType >; using DiagonalsShiftsType = typename MultidiagonalHost::DiagonalsShiftsType; DiagonalsShiftsType diagonals{ -4, -2, 0, 1, 3, 5 }; -- GitLab From 1381780052caaa1d75c814493ace280caab28591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Mon, 1 Jun 2020 15:54:49 +0200 Subject: [PATCH 22/38] Writting documentation on multidiagonal martrix + implementing constructors with initializer lists. --- .../MultidiagonalMatrix/CMakeLists.txt | 44 +++++++++---- ...MultidiagonalMatrixExample_Constructor.cpp | 48 +++++++++++++++ ...lMatrixExample_Constructor_init_list_1.cpp | 47 ++++++++++++++ ...alMatrixExample_Constructor_init_list_1.cu | 1 + ...lMatrixExample_Constructor_init_list_2.cpp | 61 +++++++++++++++++++ ...alMatrixExample_Constructor_init_list_2.cu | 1 + src/TNL/Matrices/MultidiagonalMatrix.h | 34 ++++++++--- src/TNL/Matrices/MultidiagonalMatrix.hpp | 58 +++++++++++++++++- 8 files changed, 275 insertions(+), 19 deletions(-) create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cu diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index 027c92f4f..e07e9a69b 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -1,8 +1,18 @@ IF( BUILD_CUDA ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list_cuda MultidiagonalMatrixExample_Constructor_init_list.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list.out -# OUTPUT MultidiagonalMatrixExample_Constructor_init_list.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_cuda MultidiagonalMatrixExample_Constructor.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor.out + OUTPUT MultidiagonalMatrixExample_Constructor.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list_1_cuda MultidiagonalMatrixExample_Constructor_init_list_1.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list_1_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list_1.out + OUTPUT MultidiagonalMatrixExample_Constructor_init_list_1.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list_2_cuda MultidiagonalMatrixExample_Constructor_init_list_2.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list_2_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list_2.out + OUTPUT MultidiagonalMatrixExample_Constructor_init_list_2.out ) # CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElements_cuda MultidiagonalMatrixExample_setElements.cu ) # ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElements_cuda > @@ -125,10 +135,20 @@ IF( BUILD_CUDA ) # OUTPUT MultidiagonalMatrixViewExample_forAllRows.out ) ELSE() -# ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list MultidiagonalMatrixExample_Constructor_init_list.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list.out -# OUTPUT MultidiagonalMatrixExample_Constructor_init_list.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor MultidiagonalMatrixExample_Constructor.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor.out + OUTPUT MultidiagonalMatrixExample_Constructor.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list_1 MultidiagonalMatrixExample_Constructor_init_list_1.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list_1 > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list_1.out + OUTPUT MultidiagonalMatrixExample_Constructor_init_list_1.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list_2 MultidiagonalMatrixExample_Constructor_init_list_2.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list_2 > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list_2.out + OUTPUT MultidiagonalMatrixExample_Constructor_init_list_2.out ) # ADD_EXECUTABLE( MultidiagonalMatrixExample_setElements MultidiagonalMatrixExample_setElements.cpp ) # ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElements > @@ -252,8 +272,10 @@ ELSE() ENDIF() -#ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS -# MultidiagonalMatrixExample_Constructor_init_list.out +ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS + MultidiagonalMatrixExample_Constructor.out + MultidiagonalMatrixExample_Constructor_init_list_1.out + MultidiagonalMatrixExample_Constructor_init_list_2.out # MultidiagonalMatrixExample_setElements.out # MultidiagonalMatrixExample_getCompressedRowLengths.out # MultidiagonalMatrixExample_getElementsCount.out @@ -278,5 +300,5 @@ ENDIF() # MultidiagonalMatrixViewExample_allRowsReduction.out # MultidiagonalMatrixViewExample_forRows.out # MultidiagonalMatrixViewExample_forAllRows.out -#) +) diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp new file mode 100644 index 000000000..b2321a87c --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void laplaceOperatorMatrix() +{ + /*** + * Set matrix representing approximation of the Laplace operator on regular + * grid using the finite difference method. + */ + const int gridSize( 4 ); + const int matrixSize = gridSize * gridSize; + TNL::Containers::Vector< int, Device > shifts { - gridSize, -1, 0, 1, gridSize }; + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( matrixSize, matrixSize, shifts ); + auto matrixView = matrix.getView(); + auto f = [=] __cuda_callable__ ( int i, int j ) mutable { + const int elementIdx = j * gridSize + i; + auto row = matrixView.getRow( elementIdx ); + if( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 ) + row.setElement( 2, 1.0 ); // set matrix elements corresponding to boundary grid nodes + else + { + row.setElement( 0, -1.0 ); // set matrix elements corresponding to inner grid nodes + row.setElement( 1, -1.0 ); + row.setElement( 2, 4.0 ); + row.setElement( 3, -1.0 ); + row.setElement( 4, -1.0 ); + } + }; + TNL::Algorithms::ParallelFor2D< TNL::Devices::Sequential >::exec( 0, 0, gridSize, gridSize, f ); + + std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp new file mode 100644 index 000000000..c46844812 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void laplaceOperatorMatrix() +{ + /*** + * Set matrix representing approximation of the Laplace operator on regular + * grid using the finite difference method. + */ + const int gridSize( 4 ); + const int matrixSize = gridSize * gridSize; + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( matrixSize, matrixSize, { - gridSize, -1, 0, 1, gridSize } ); + auto matrixView = matrix.getView(); + auto f = [=] __cuda_callable__ ( int i, int j ) mutable { + const int elementIdx = i * gridSize + j; + auto row = matrixView.getRow( elementIdx ); + if( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 ) + row.setElement( 2, 1.0 ); // set matrix elements corresponding to boundary grid nodes + else + { + row.setElement( 0, -1.0 ); // set matrix elements corresponding to inner grid nodes + row.setElement( 1, -1.0 ); + row.setElement( 2, 4.0 ); + row.setElement( 3, -1.0 ); + row.setElement( 4, -1.0 ); + } + }; + TNL::Algorithms::ParallelFor2D< Device >::exec( 0, 0, gridSize, gridSize, f ); + + std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; + initializerListExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cu new file mode 120000 index 000000000..6b3354ef6 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_Constructor_init_list_1.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp new file mode 100644 index 000000000..74650f9cb --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void laplaceOperatorMatrix() +{ + const int gridSize( 4 ); + const int matrixSize = gridSize * gridSize; + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + matrixSize, { - gridSize, -1, 0, 1, gridSize }, { + { 1.0 }, + { 1.0 }, + { 1.0 }, + { 1.0 }, + { 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { 1.0 }, + { 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { 1.0 }, + { 1.0 }, + { 1.0 }, + { 1.0 }, + { 1.0 } + } ); + auto matrixView = matrix.getView(); + auto f = [=] __cuda_callable__ ( int i, int j ) mutable { + const int elementIdx = i * gridSize + j; + auto row = matrixView.getRow( elementIdx ); + if( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 ) + row.setElement( 0, 1.0 ); + else + { + row.setElement( 0, -1.0 ); + row.setElement( 1, -1.0 ); + row.setElement( 2, 4.0 ); + row.setElement( 3, -1.0 ); + row.setElement( 4, -1.0 ); + } + }; + TNL::Algorithms::ParallelFor2D< Device >::exec( 0, 0, gridSize, gridSize, f ); + + std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cu new file mode 120000 index 000000000..9098df5e3 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_Constructor_init_list_2.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index b2a5df646..db079f780 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -145,7 +145,6 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp * \par Output * \include MultidiagonalMatrixExample_Constructor.out - * */ template< typename Vector > MultidiagonalMatrix( const IndexType rows, @@ -155,20 +154,40 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Constructor with matrix dimensions and diagonals shifts. * - * \param rows - * \param columns - * \param diagonalsShifts + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + * \param diagonalsShifts are shifts of sub-diagonals from the main diagonal. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp + * \par Output + * \include MultidiagonalMatrixExample_Constructor_init_list_1.out */ template< typename ListIndex > MultidiagonalMatrix( const IndexType rows, const IndexType columns, const std::initializer_list< ListIndex > diagonalsShifts ); + /** + * \brief Constructor with matrix dimensions, diagonals shifts and matrix elements. + * + * The number of matrix rows is given by the size of the initializer list \e data. + * + * \param columns is number of matrix columns. + * \param diagonalShifts are shifts of sub-diagonals from the main diagonal. + * \param data is initializer list holding matrix elements. The size of the outer list + * defines the number of matrix rows. Each inner list defines non-zero elements in each row + * and so its size must be lower or equal to the size of \e diagonalsShifts. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp + * \par Output + * \include MultidiagonalMatrixExample_Constructor_init_list_2.out + */ template< typename ListIndex, typename ListReal > MultidiagonalMatrix( const IndexType columns, const std::initializer_list< ListIndex > diagonalsShifts, - const std::initializer_list< std::initializer_list< ListReal > >& data ) - + const std::initializer_list< std::initializer_list< ListReal > >& data ); /** * \brief Copy constructor. @@ -238,7 +257,8 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename RowLengthsVector > void setCompressedRowLengths( const RowLengthsVector& rowCapacities ); - void setElements( const std::initializer_list< std::initializer_list< ListReal > >& data ) + template< typename ListReal > + void setElements( const std::initializer_list< std::initializer_list< ListReal > >& data ); const IndexType& getDiagonalsCount() const; diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index b32663812..5fab47c80 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -61,10 +61,27 @@ MultidiagonalMatrix( const IndexType rows, const std::initializer_list< ListIndex > diagonalsShifts ) { Containers::Vector< IndexType, DeviceType, IndexType > shifts( diagonalsShifts ); - TNL_ASSERT_GT( shifts.getSize(), 0, "Cannot construct mutltidiagonal matrix with no diagonals shifts." ); + TNL_ASSERT_GT( shifts.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals shifts." ); this->setDimensions( rows, columns, shifts ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator, + typename IndexAllocator > + template< typename ListIndex, typename ListReal > +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +MultidiagonalMatrix( const IndexType columns, + const std::initializer_list< ListIndex > diagonalsShifts, + const std::initializer_list< std::initializer_list< ListReal > >& data ) +{ + Containers::Vector< IndexType, DeviceType, IndexType > shifts( diagonalsShifts ); + TNL_ASSERT_GT( shifts.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals shifts." ); + this->setDimensions( data.size(), columns, shifts ); + this->setElements( data ); +} template< typename Real, typename Device, @@ -178,6 +195,45 @@ setCompressedRowLengths( const RowLengthsVector& rowLengths ) throw std::logic_error( "Too many non-zero elements per row in a tri-diagonal matrix." ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator, + typename IndexAllocator > + template< typename ListReal > +void +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +setElements( const std::initializer_list< std::initializer_list< ListReal > >& data ) +{ + if( std::is_same< DeviceType, Devices::Host >::value ) + { + auto row_it = data.begin(); + for( size_t rowIdx = 0; rowIdx < data.size(); rowIdx++ ) + { + auto data_it = row_it->begin(); + for( IndexType i = 0; i < this->diagonalsShifts.getSize(); i++ ) + { + const auto columnIdx = this->diagonalsShifts[ i ] + rowIdx; + if( columnIdx >= 0 && columnIdx < this->getColumns() && data_it != row_it->end() ) + this->getRow( rowIdx ).setElement( i, *data_it++ ); + else + this->getRow( rowIdx ).setElement( i, 0 ); + } + row_it ++; + } + } + else + { + MultidiagonalMatrix< Real, Devices::Host, Index, Organization > hostMatrix( + this->getRows(), + this->getColumns(), + this->getDiagonalsShifts() ); + hostMatrix.setElements( data ); + *this = hostMatrix; + } +} + template< typename Real, typename Device, typename Index, -- GitLab From 9ab855e25e1b7452b679802d1c3150fc3637a2bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Mon, 1 Jun 2020 20:43:13 +0200 Subject: [PATCH 23/38] Added test and fix of MultidiagionalMatrix::setElements. --- ...lMatrixExample_Constructor_init_list_2.cpp | 41 ++++------- src/TNL/Matrices/MultidiagonalMatrix.hpp | 12 ++-- src/TNL/Matrices/MultidiagonalMatrixView.hpp | 6 +- .../Matrices/MultidiagonalMatrixTest.h | 70 ++++++++++++++++++- 4 files changed, 88 insertions(+), 41 deletions(-) diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp index 74650f9cb..2d89b2303 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp @@ -12,40 +12,23 @@ void laplaceOperatorMatrix() const int matrixSize = gridSize * gridSize; TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( matrixSize, { - gridSize, -1, 0, 1, gridSize }, { - { 1.0 }, - { 1.0 }, - { 1.0 }, - { 1.0 }, - { 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, { -1.0, -1.0, 4.0, -1.0, -1.0 }, { -1.0, -1.0, 4.0, -1.0, -1.0 }, - { 1.0 }, - { 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, { -1.0, -1.0, 4.0, -1.0, -1.0 }, { -1.0, -1.0, 4.0, -1.0, -1.0 }, - { 1.0 }, - { 1.0 }, - { 1.0 }, - { 1.0 }, - { 1.0 } + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 } } ); - auto matrixView = matrix.getView(); - auto f = [=] __cuda_callable__ ( int i, int j ) mutable { - const int elementIdx = i * gridSize + j; - auto row = matrixView.getRow( elementIdx ); - if( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 ) - row.setElement( 0, 1.0 ); - else - { - row.setElement( 0, -1.0 ); - row.setElement( 1, -1.0 ); - row.setElement( 2, 4.0 ); - row.setElement( 3, -1.0 ); - row.setElement( 4, -1.0 ); - } - }; - TNL::Algorithms::ParallelFor2D< Device >::exec( 0, 0, gridSize, gridSize, f ); - std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; } diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index 5fab47c80..7be51c12e 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -208,18 +208,14 @@ setElements( const std::initializer_list< std::initializer_list< ListReal > >& d { if( std::is_same< DeviceType, Devices::Host >::value ) { + this->getValues() = 0.0; auto row_it = data.begin(); for( size_t rowIdx = 0; rowIdx < data.size(); rowIdx++ ) { auto data_it = row_it->begin(); - for( IndexType i = 0; i < this->diagonalsShifts.getSize(); i++ ) - { - const auto columnIdx = this->diagonalsShifts[ i ] + rowIdx; - if( columnIdx >= 0 && columnIdx < this->getColumns() && data_it != row_it->end() ) - this->getRow( rowIdx ).setElement( i, *data_it++ ); - else - this->getRow( rowIdx ).setElement( i, 0 ); - } + IndexType i = 0; + while( data_it != row_it->end() ) + this->getRow( rowIdx ).setElement( i++, *data_it++ ); row_it ++; } } diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index 588fd3d60..2645872c5 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -320,9 +320,9 @@ getElement( const IndexType row, const IndexType column ) const TNL_ASSERT_GE( column, 0, "" ); TNL_ASSERT_LT( column, this->getColumns(), "" ); - for( IndexType i = 0; i < hostDiagonalsShifts.getSize(); i++ ) - if( row + hostDiagonalsShifts[ i ] == column ) - return this->values.getElement( this->getElementIndex( row, i ) ); + for( IndexType localIdx = 0; localIdx < hostDiagonalsShifts.getSize(); localIdx++ ) + if( row + hostDiagonalsShifts[ localIdx ] == column ) + return this->values.getElement( this->indexer.getGlobalIndex( row, localIdx ) ); return 0.0; } diff --git a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h index f9bfc95aa..2fed8bc2e 100644 --- a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h @@ -89,6 +89,68 @@ void test_SetLike() EXPECT_EQ( m1.getColumns(), m2.getColumns() ); } +template< typename Matrix > +void test_SetElements() +{ + using RealType = typename Matrix::RealType; + using DeviceType = typename Matrix::DeviceType; + using IndexType = typename Matrix::IndexType; + + const int gridSize( 4 ); + const int matrixSize( gridSize * gridSize ); + Matrix matrix( matrixSize, matrixSize, { - gridSize, -1, 0, 1, gridSize } ); + matrix.setElements( { + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 } + } ); + + std::cerr << matrix << std::endl; + for( int i = 0; i < gridSize; i++ ) + for( int j = 0; j < gridSize; j++ ) + { + const int elementIdx = i * gridSize + j; + if( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 ) // check matrix elements corresponding to boundary grid nodes + { + for( int k = 0; k < matrixSize; k++ ) + { + if( elementIdx == k ) + EXPECT_EQ( matrix.getElement( elementIdx, k ), 1.0 ); + else + EXPECT_EQ( matrix.getElement( elementIdx, k ), 0.0 ); + } + } + else // check matrix elements corresponding to inner grid nodes + { + for( int k = 0; k < matrixSize; k++ ) + { + if( k == elementIdx - gridSize || + k == elementIdx - 1 || + k == elementIdx + 1 || + k == elementIdx + gridSize ) + EXPECT_EQ( matrix.getElement( elementIdx, k ), -1.0 ); + else if( k == elementIdx ) + EXPECT_EQ( matrix.getElement( elementIdx, k ), 4.0 ); + else + EXPECT_EQ( matrix.getElement( elementIdx, k ), 0.0 ); + } + } + } +} + template< typename Matrix > void test_GetNonemptyRowsCount() { @@ -1381,6 +1443,13 @@ TYPED_TEST( MatrixTest, setLikeTest ) test_SetLike< MatrixType, MatrixType >(); } +TYPED_TEST( MatrixTest, setElements ) +{ + using MatrixType = typename TestFixture::MatrixType; + + test_SetElements< MatrixType >(); +} + TYPED_TEST( MatrixTest, getNonemptyRowsCountTest ) { using MatrixType = typename TestFixture::MatrixType; @@ -1388,7 +1457,6 @@ TYPED_TEST( MatrixTest, getNonemptyRowsCountTest ) test_GetNonemptyRowsCount< MatrixType >(); } - TYPED_TEST( MatrixTest, getCompressedRowLengthTest ) { using MatrixType = typename TestFixture::MatrixType; -- GitLab From f02dfdd96a0b3fc295d32084ef0111e93b248d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Mon, 1 Jun 2020 21:03:47 +0200 Subject: [PATCH 24/38] Fixing MultidiagonalMatrix documentation. --- .../MultidiagonalMatrixExample_Constructor.cpp | 8 +++++--- ...tidiagonalMatrixExample_Constructor_init_list_1.cpp | 8 +++++--- ...tidiagonalMatrixExample_Constructor_init_list_2.cpp | 10 +++++----- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp index b2321a87c..412ea10a7 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp @@ -22,11 +22,13 @@ void laplaceOperatorMatrix() auto row = matrixView.getRow( elementIdx ); if( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 ) row.setElement( 2, 1.0 ); // set matrix elements corresponding to boundary grid nodes + // and Dirichlet boundary conditions, i.e. 1 on the main diagonal + // which is the third one else { - row.setElement( 0, -1.0 ); // set matrix elements corresponding to inner grid nodes - row.setElement( 1, -1.0 ); - row.setElement( 2, 4.0 ); + row.setElement( 0, -1.0 ); // set matrix elements corresponding to inner grid nodes, i.e. + row.setElement( 1, -1.0 ); // 4 on the main diagonal (the third one) and -1 to the other + row.setElement( 2, 4.0 ); // sub-diagonals row.setElement( 3, -1.0 ); row.setElement( 4, -1.0 ); } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp index c46844812..90c331bab 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp @@ -21,11 +21,13 @@ void laplaceOperatorMatrix() auto row = matrixView.getRow( elementIdx ); if( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 ) row.setElement( 2, 1.0 ); // set matrix elements corresponding to boundary grid nodes + // and Dirichlet boundary conditions, i.e. 1 on the main diagonal + // which is the third one else { - row.setElement( 0, -1.0 ); // set matrix elements corresponding to inner grid nodes - row.setElement( 1, -1.0 ); - row.setElement( 2, 4.0 ); + row.setElement( 0, -1.0 ); // set matrix elements corresponding to inner grid nodes, i.e. + row.setElement( 1, -1.0 ); // 4 on the main diagonal (the third one) and -1 to the other + row.setElement( 2, 4.0 ); // sub-diagonals row.setElement( 3, -1.0 ); row.setElement( 4, -1.0 ); } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp index 2d89b2303..158750525 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp @@ -12,13 +12,13 @@ void laplaceOperatorMatrix() const int matrixSize = gridSize * gridSize; TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( matrixSize, { - gridSize, -1, 0, 1, gridSize }, { + { 0.0, 0.0, 1.0 }, // set matrix elements corresponding to boundary grid nodes + { 0.0, 0.0, 1.0 }, // and Dirichlet boundary conditions, i.e. 1 on the main diagonal + { 0.0, 0.0, 1.0 }, // which is the third one { 0.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, // set matrix elements corresponding to inner grid nodes, i.e.4 on the main diagonal + { -1.0, -1.0, 4.0, -1.0, -1.0 }, // (the third one) and -1 to the other sub-diagonals { 0.0, 0.0, 1.0 }, { 0.0, 0.0, 1.0 }, { -1.0, -1.0, 4.0, -1.0, -1.0 }, -- GitLab From 205a15dbdd7315416f4e48e591e3d9bc284b3987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Mon, 1 Jun 2020 21:14:13 +0200 Subject: [PATCH 25/38] Renaming diagonals shifts to offsets in MultidiagonalMatrix. --- src/TNL/Matrices/MultidiagonalMatrix.h | 42 +++--- src/TNL/Matrices/MultidiagonalMatrix.hpp | 56 ++++---- src/TNL/Matrices/MultidiagonalMatrixRowView.h | 12 +- .../Matrices/MultidiagonalMatrixRowView.hpp | 32 ++--- src/TNL/Matrices/MultidiagonalMatrixView.h | 18 +-- src/TNL/Matrices/MultidiagonalMatrixView.hpp | 64 ++++----- .../Matrices/BinarySparseMatrixCopyTest.h | 4 +- src/UnitTests/Matrices/DenseMatrixCopyTest.h | 4 +- .../Matrices/MultidiagonalMatrixTest.h | 128 +++++++++--------- src/UnitTests/Matrices/SparseMatrixCopyTest.h | 4 +- 10 files changed, 184 insertions(+), 180 deletions(-) diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index db079f780..546d5ae97 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -50,10 +50,10 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > using ValuesVectorType = typename BaseType::ValuesVectorType; using ValuesViewType = typename ValuesVectorType::ViewType; using IndexerType = details::MultidiagonalMatrixIndexer< Index, Organization >; - using DiagonalsShiftsType = Containers::Vector< Index, Device, Index, IndexAllocator >; - using DiagonalsShiftsView = typename DiagonalsShiftsType::ViewType; - using HostDiagonalsShiftsType = Containers::Vector< Index, Devices::Host, Index >; - using HostDiagonalsShiftsView = typename HostDiagonalsShiftsType::ViewType; + using DiagonalsOffsetsType = Containers::Vector< Index, Device, Index, IndexAllocator >; + using DiagonalsOffsetsView = typename DiagonalsOffsetsType::ViewType; + using HostDiagonalsOffsetsType = Containers::Vector< Index, Devices::Host, Index >; + using HostDiagonalsOffsetsView = typename HostDiagonalsOffsetsType::ViewType; /** * \brief The type of matrix elements. @@ -97,7 +97,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Type for accessing matrix rows. */ - using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsShiftsView >; + using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsOffsetsView >; /** * \brief Type for accessing constant matrix rows. @@ -139,7 +139,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * * \param rows is number of matrix rows. * \param columns is number of matrix columns. - * \param diagonalsShifts are shifts of subdiagonals from the main diagonal. + * \param diagonalsOffsets are offsets of subdiagonals from the main diagonal. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp @@ -149,14 +149,14 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename Vector > MultidiagonalMatrix( const IndexType rows, const IndexType columns, - const Vector& diagonalsShifts ); + const Vector& diagonalsOffsets ); /** - * \brief Constructor with matrix dimensions and diagonals shifts. + * \brief Constructor with matrix dimensions and diagonals offsets. * * \param rows is number of matrix rows. * \param columns is number of matrix columns. - * \param diagonalsShifts are shifts of sub-diagonals from the main diagonal. + * \param diagonalsOffsets are offsets of sub-diagonals from the main diagonal. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp @@ -166,18 +166,19 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename ListIndex > MultidiagonalMatrix( const IndexType rows, const IndexType columns, - const std::initializer_list< ListIndex > diagonalsShifts ); + const std::initializer_list< ListIndex > diagonalsOffsets ); /** - * \brief Constructor with matrix dimensions, diagonals shifts and matrix elements. + * \brief Constructor with matrix dimensions, diagonals offsets and matrix elements. * * The number of matrix rows is given by the size of the initializer list \e data. * * \param columns is number of matrix columns. - * \param diagonalShifts are shifts of sub-diagonals from the main diagonal. + * \param diagonalOffsets are offsets of sub-diagonals from the main diagonal. * \param data is initializer list holding matrix elements. The size of the outer list - * defines the number of matrix rows. Each inner list defines non-zero elements in each row - * and so its size must be lower or equal to the size of \e diagonalsShifts. + * defines the number of matrix rows. Each inner list defines values of each sub-diagonal + * and so its size must be lower or equal to the size of \e diagonalsOffsets. Values + * of sub-diagonals which do not fit to given row are omitted. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp @@ -186,7 +187,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > */ template< typename ListIndex, typename ListReal > MultidiagonalMatrix( const IndexType columns, - const std::initializer_list< ListIndex > diagonalsShifts, + const std::initializer_list< ListIndex > diagonalsOffsets, const std::initializer_list< std::initializer_list< ListReal > >& data ); /** @@ -249,10 +250,13 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > */ virtual String getSerializationTypeVirtual() const; + /** + * \brief Set matrix dimensions and + */ template< typename Vector > void setDimensions( const IndexType rows, const IndexType columns, - const Vector& diagonalsShifts ); + const Vector& diagonalsOffsets ); template< typename RowLengthsVector > void setCompressedRowLengths( const RowLengthsVector& rowCapacities ); @@ -262,7 +266,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > const IndexType& getDiagonalsCount() const; - const DiagonalsShiftsType& getDiagonalsShifts() const; + const DiagonalsOffsetsType& getDiagonalsOffsets() const; template< typename Vector > void getCompressedRowLengths( Vector& rowLengths ) const; @@ -386,9 +390,9 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > IndexType getElementIndex( const IndexType row, const IndexType localIdx ) const; - DiagonalsShiftsType diagonalsShifts; + DiagonalsOffsetsType diagonalsOffsets; - HostDiagonalsShiftsType hostDiagonalsShifts; + HostDiagonalsOffsetsType hostDiagonalsOffsets; IndexerType indexer; diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index 7be51c12e..eb0cbdbe5 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -42,10 +42,10 @@ template< typename Real, MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: MultidiagonalMatrix( const IndexType rows, const IndexType columns, - const Vector& diagonalsShifts ) + const Vector& diagonalsOffsets ) { - TNL_ASSERT_GT( diagonalsShifts.getSize(), 0, "Cannot construct mutltidiagonal matrix with no diagonals shifts." ); - this->setDimensions( rows, columns, diagonalsShifts ); + TNL_ASSERT_GT( diagonalsOffsets.getSize(), 0, "Cannot construct mutltidiagonal matrix with no diagonals shifts." ); + this->setDimensions( rows, columns, diagonalsOffsets ); } template< typename Real, @@ -58,9 +58,9 @@ template< typename Real, MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: MultidiagonalMatrix( const IndexType rows, const IndexType columns, - const std::initializer_list< ListIndex > diagonalsShifts ) + const std::initializer_list< ListIndex > diagonalsOffsets ) { - Containers::Vector< IndexType, DeviceType, IndexType > shifts( diagonalsShifts ); + Containers::Vector< IndexType, DeviceType, IndexType > shifts( diagonalsOffsets ); TNL_ASSERT_GT( shifts.getSize(), 0, "Cannot construct multidiagonal matrix with no diagonals shifts." ); this->setDimensions( rows, columns, shifts ); } @@ -74,10 +74,10 @@ template< typename Real, template< typename ListIndex, typename ListReal > MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: MultidiagonalMatrix( const IndexType columns, - const std::initializer_list< ListIndex > diagonalsShifts, + const std::initializer_list< ListIndex > diagonalsOffsets, const std::initializer_list< std::initializer_list< ListReal > >& data ) { - Containers::Vector< IndexType, DeviceType, IndexType > shifts( diagonalsShifts ); + 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 ); this->setElements( data ); @@ -95,8 +95,8 @@ getView() const -> ViewType { // TODO: fix when getConstView works return ViewType( const_cast< MultidiagonalMatrix* >( this )->values.getView(), - const_cast< MultidiagonalMatrix* >( this )->diagonalsShifts.getView(), - const_cast< MultidiagonalMatrix* >( this )->hostDiagonalsShifts.getView(), + const_cast< MultidiagonalMatrix* >( this )->diagonalsOffsets.getView(), + const_cast< MultidiagonalMatrix* >( this )->hostDiagonalsOffsets.getView(), indexer ); } @@ -153,16 +153,16 @@ void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: setDimensions( const IndexType rows, const IndexType columns, - const Vector& diagonalsShifts ) + const Vector& diagonalsOffsets ) { Matrix< Real, Device, Index >::setDimensions( rows, columns ); - this->diagonalsShifts = diagonalsShifts; - this->hostDiagonalsShifts = diagonalsShifts; - const IndexType minShift = min( diagonalsShifts ); + this->diagonalsOffsets = diagonalsOffsets; + this->hostDiagonalsOffsets = diagonalsOffsets; + const IndexType minOffset = min( diagonalsOffsets ); IndexType nonemptyRows = min( rows, columns ); - if( rows > columns && minShift < 0 ) - nonemptyRows = min( rows, nonemptyRows - minShift ); - this->indexer.set( rows, columns, diagonalsShifts.getSize(), nonemptyRows ); + if( rows > columns && minOffset < 0 ) + nonemptyRows = min( rows, nonemptyRows - minOffset ); + this->indexer.set( rows, columns, diagonalsOffsets.getSize(), nonemptyRows ); this->values.setSize( this->indexer.getStorageSize() ); this->values = 0.0; this->view = this->getView(); @@ -224,7 +224,7 @@ setElements( const std::initializer_list< std::initializer_list< ListReal > >& d MultidiagonalMatrix< Real, Devices::Host, Index, Organization > hostMatrix( this->getRows(), this->getColumns(), - this->getDiagonalsShifts() ); + this->getDiagonalsOffsets() ); hostMatrix.setElements( data ); *this = hostMatrix; } @@ -251,9 +251,9 @@ template< typename Real, typename IndexAllocator > auto MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -getDiagonalsShifts() const -> const DiagonalsShiftsType& +getDiagonalsOffsets() const -> const DiagonalsOffsetsType& { - return this->diagonalsShifts; + return this->diagonalsOffsets; } template< typename Real, @@ -320,7 +320,7 @@ void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: setLike( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& m ) { - this->setDimensions( m.getRows(), m.getColumns(), m.getDiagonalsShifts() ); + this->setDimensions( m.getRows(), m.getColumns(), m.getDiagonalsOffsets() ); } template< typename Real, @@ -744,7 +744,7 @@ operator=( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, Rea } else { - const IndexType maxRowLength = this->diagonalsShifts.getSize(); + const IndexType maxRowLength = this->diagonalsOffsets.getSize(); const IndexType bufferRowsCount( 128 ); const size_t bufferSize = bufferRowsCount * maxRowLength; Containers::Vector< RHSRealType, RHSDeviceType, RHSIndexType, RHSRealAllocatorType > matrixValuesBuffer( bufferSize ); @@ -795,7 +795,7 @@ template< typename Real, void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::save( File& file ) const { Matrix< Real, Device, Index >::save( file ); - file << diagonalsShifts; + file << diagonalsOffsets; } template< typename Real, @@ -807,13 +807,13 @@ template< typename Real, void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::load( File& file ) { Matrix< Real, Device, Index >::load( file ); - file >> this->diagonalsShifts; - this->hostDiagonalsShifts = this->diagonalsShifts; - const IndexType minShift = min( diagonalsShifts ); + file >> this->diagonalsOffsets; + this->hostDiagonalsOffsets = this->diagonalsOffsets; + const IndexType minOffset = min( diagonalsOffsets ); IndexType nonemptyRows = min( this->getRows(), this->getColumns() ); - if( this->getRows() > this->getColumns() && minShift < 0 ) - nonemptyRows = min( this->getRows(), nonemptyRows - minShift ); - this->indexer.set( this->getRows(), this->getColumns(), diagonalsShifts.getSize(), nonemptyRows ); + if( this->getRows() > this->getColumns() && minOffset < 0 ) + nonemptyRows = min( this->getRows(), nonemptyRows - minOffset ); + this->indexer.set( this->getRows(), this->getColumns(), diagonalsOffsets.getSize(), nonemptyRows ); this->view = this->getView(); } diff --git a/src/TNL/Matrices/MultidiagonalMatrixRowView.h b/src/TNL/Matrices/MultidiagonalMatrixRowView.h index 72e4328fb..c2ee8a869 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixRowView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixRowView.h @@ -15,7 +15,7 @@ namespace Matrices { template< typename ValuesView, typename Indexer, - typename DiagonalsShiftsView_ > + typename DiagonalsOffsetsView_ > class MultidiagonalMatrixRowView { public: @@ -24,7 +24,7 @@ class MultidiagonalMatrixRowView using IndexType = typename ValuesView::IndexType; using ValuesViewType = ValuesView; using IndexerType = Indexer; - using DiagonalsShiftsView = DiagonalsShiftsView_; + using DiagonalsOffsetsView = DiagonalsOffsetsView_; /** * \brief Type of constant container view used for storing the matrix elements values. @@ -34,7 +34,7 @@ class MultidiagonalMatrixRowView /** * \brief Type of constant container view used for storing the column indexes of the matrix elements. */ - using ConstDiagonalsShiftsViewType = typename DiagonalsShiftsView::ConstViewType; + using ConstDiagonalsOffsetsViewType = typename DiagonalsOffsetsView::ConstViewType; /** * \brief Type of constant indexer view. @@ -44,11 +44,11 @@ class MultidiagonalMatrixRowView /** * \brief Type of constant sparse matrix row view. */ - using ConstViewType = MultidiagonalMatrixRowView< ConstValuesViewType, ConstIndexerViewType, ConstDiagonalsShiftsViewType >; + using ConstViewType = MultidiagonalMatrixRowView< ConstValuesViewType, ConstIndexerViewType, ConstDiagonalsOffsetsViewType >; __cuda_callable__ MultidiagonalMatrixRowView( const IndexType rowIdx, - const DiagonalsShiftsView& diagonalsShifts, + const DiagonalsOffsetsView& diagonalsOffsets, const ValuesViewType& values, const IndexerType& indexer); @@ -71,7 +71,7 @@ class MultidiagonalMatrixRowView IndexType rowIdx; - DiagonalsShiftsView diagonalsShifts; + DiagonalsOffsetsView diagonalsOffsets; ValuesViewType values; diff --git a/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp b/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp index 855b8463a..37cdd455a 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp @@ -13,59 +13,59 @@ namespace TNL { namespace Matrices { -template< typename ValuesView, typename Indexer, typename DiagonalsShiftsView > +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > __cuda_callable__ -MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsShiftsView >:: +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: MultidiagonalMatrixRowView( const IndexType rowIdx, - const DiagonalsShiftsView& diagonalsShifts, + const DiagonalsOffsetsView& diagonalsOffsets, const ValuesViewType& values, const IndexerType& indexer ) -: rowIdx( rowIdx ), diagonalsShifts( diagonalsShifts ), values( values ), indexer( indexer ) +: rowIdx( rowIdx ), diagonalsOffsets( diagonalsOffsets ), values( values ), indexer( indexer ) { } -template< typename ValuesView, typename Indexer, typename DiagonalsShiftsView > +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > __cuda_callable__ auto -MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsShiftsView >:: +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: getSize() const -> IndexType { - return diagonalsShifts.getSize();//indexer.getRowSize( rowIdx ); + return diagonalsOffsets.getSize();//indexer.getRowSize( rowIdx ); } -template< typename ValuesView, typename Indexer, typename DiagonalsShiftsView > +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > __cuda_callable__ auto -MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsShiftsView >:: +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: getColumnIndex( const IndexType localIdx ) const -> const IndexType { TNL_ASSERT_GE( localIdx, 0, "" ); TNL_ASSERT_LT( localIdx, indexer.getDiagonals(), "" ); - return rowIdx + diagonalsShifts[ localIdx ]; + return rowIdx + diagonalsOffsets[ localIdx ]; } -template< typename ValuesView, typename Indexer, typename DiagonalsShiftsView > +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > __cuda_callable__ auto -MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsShiftsView >:: +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: getValue( const IndexType localIdx ) const -> const RealType& { return this->values[ this->indexer.getGlobalIndex( rowIdx, localIdx ) ]; } -template< typename ValuesView, typename Indexer, typename DiagonalsShiftsView > +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > __cuda_callable__ auto -MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsShiftsView >:: +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: getValue( const IndexType localIdx ) -> RealType& { return this->values[ this->indexer.getGlobalIndex( rowIdx, localIdx ) ]; } -template< typename ValuesView, typename Indexer, typename DiagonalsShiftsView > +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > __cuda_callable__ void -MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsShiftsView >:: +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: setElement( const IndexType localIdx, const RealType& value ) { diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.h b/src/TNL/Matrices/MultidiagonalMatrixView.h index a3ebfe375..0ff8c07b2 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixView.h @@ -30,15 +30,15 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > using DeviceType = Device; using IndexType = Index; using BaseType = MatrixView< Real, Device, Index >; - //using DiagonalsShiftsType = Containers::Vector< IndexType, DeviceType, IndexType >; - using DiagonalsShiftsView = Containers::VectorView< IndexType, DeviceType, IndexType >; - //using HostDiagonalsShiftsType = Containers::Vector< IndexType, Devices::Host, IndexType >; - using HostDiagonalsShiftsView = Containers::VectorView< IndexType, Devices::Host, IndexType >; + //using DiagonalsOffsetsType = Containers::Vector< IndexType, DeviceType, IndexType >; + using DiagonalsOffsetsView = Containers::VectorView< IndexType, DeviceType, IndexType >; + //using HostDiagonalsOffsetsType = Containers::Vector< IndexType, Devices::Host, IndexType >; + using HostDiagonalsOffsetsView = Containers::VectorView< IndexType, Devices::Host, IndexType >; using IndexerType = details::MultidiagonalMatrixIndexer< IndexType, Organization >; using ValuesViewType = typename BaseType::ValuesView; using ViewType = MultidiagonalMatrixView< Real, Device, Index, Organization >; using ConstViewType = MultidiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; - using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsShiftsView >; + using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsOffsetsView >; // TODO: remove this - it is here only for compatibility with original matrix implementation typedef Containers::Vector< IndexType, DeviceType, IndexType > CompressedRowLengthsVector; @@ -54,8 +54,8 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > MultidiagonalMatrixView(); MultidiagonalMatrixView( const ValuesViewType& values, - const DiagonalsShiftsView& diagonalsShifts, - const HostDiagonalsShiftsView& hostDiagonalsShifts, + const DiagonalsOffsetsView& diagonalsOffsets, + const HostDiagonalsOffsetsView& hostDiagonalsOffsets, const IndexerType& indexer ); ViewType getView(); @@ -174,9 +174,9 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > IndexType getElementIndex( const IndexType row, const IndexType localIdx ) const; - DiagonalsShiftsView diagonalsShifts; + DiagonalsOffsetsView diagonalsOffsets; - HostDiagonalsShiftsView hostDiagonalsShifts; + HostDiagonalsOffsetsView hostDiagonalsOffsets; IndexerType indexer; }; diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index 2645872c5..621ef7068 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -32,12 +32,12 @@ template< typename Real, ElementsOrganization Organization > MultidiagonalMatrixView< Real, Device, Index, Organization >:: MultidiagonalMatrixView( const ValuesViewType& values, - const DiagonalsShiftsView& diagonalsShifts, - const HostDiagonalsShiftsView& hostDiagonalsShifts, + const DiagonalsOffsetsView& diagonalsOffsets, + const HostDiagonalsOffsetsView& hostDiagonalsOffsets, const IndexerType& indexer ) : MatrixView< Real, Device, Index >( indexer.getRows(), indexer.getColumns(), values ), - diagonalsShifts( diagonalsShifts ), - hostDiagonalsShifts( hostDiagonalsShifts ), + diagonalsOffsets( diagonalsOffsets ), + hostDiagonalsOffsets( hostDiagonalsOffsets ), indexer( indexer ) { } @@ -51,8 +51,8 @@ MultidiagonalMatrixView< Real, Device, Index, Organization >:: getView() -> ViewType { return ViewType( const_cast< MultidiagonalMatrixView* >( this )->values.getView(), - const_cast< MultidiagonalMatrixView* >( this )->diagonalsShifts.getView(), - const_cast< MultidiagonalMatrixView* >( this )->hostDiagonalsShifts.getView(), + const_cast< MultidiagonalMatrixView* >( this )->diagonalsOffsets.getView(), + const_cast< MultidiagonalMatrixView* >( this )->hostDiagonalsOffsets.getView(), indexer ); } @@ -65,8 +65,8 @@ MultidiagonalMatrixView< Real, Device, Index, Organization >:: getConstView() const -> ConstViewType { return ConstViewType( this->values.getConstView(), - this->diagonalsShifts.getConstView(), - this->hostDiagonalsShifts.getConstView(), + this->diagonalsOffsets.getConstView(), + this->hostDiagonalsOffsets.getConstView(), indexer ); } @@ -104,7 +104,7 @@ const Index& MultidiagonalMatrixView< Real, Device, Index, Organization >:: getDiagonalsCount() const { - return this->diagonalsShifts.getSize(); + return this->diagonalsOffsets.getSize(); } template< typename Real, @@ -150,7 +150,7 @@ Index MultidiagonalMatrixView< Real, Device, Index, Organization >:: getRowLength( const IndexType row ) const { - return this->diagonalsShifts.getSize(); + return this->diagonalsOffsets.getSize(); } template< typename Real, @@ -161,7 +161,7 @@ Index MultidiagonalMatrixView< Real, Device, Index, Organization >:: getMaxRowLength() const { - return this->diagonalsShifts.getSize(); + return this->diagonalsOffsets.getSize(); } template< typename Real, @@ -234,7 +234,7 @@ auto MultidiagonalMatrixView< Real, Device, Index, Organization >:: getRow( const IndexType& rowIdx ) const -> const RowView { - return RowView( rowIdx, this->diagonalsShifts.getView(), this->values.getView(), this->indexer ); + return RowView( rowIdx, this->diagonalsOffsets.getView(), this->values.getView(), this->indexer ); } template< typename Real, @@ -246,7 +246,7 @@ auto MultidiagonalMatrixView< Real, Device, Index, Organization >:: getRow( const IndexType& rowIdx ) -> RowView { - return RowView( rowIdx, this->diagonalsShifts.getView(), this->values.getView(), this->indexer ); + return RowView( rowIdx, this->diagonalsOffsets.getView(), this->values.getView(), this->indexer ); } template< typename Real, @@ -262,8 +262,8 @@ setElement( const IndexType row, const IndexType column, const RealType& value ) TNL_ASSERT_GE( column, 0, "" ); TNL_ASSERT_LT( column, this->getColumns(), "" ); - for( IndexType i = 0; i < hostDiagonalsShifts.getSize(); i++ ) - if( row + hostDiagonalsShifts[ i ] == column ) + for( IndexType i = 0; i < hostDiagonalsOffsets.getSize(); i++ ) + if( row + hostDiagonalsOffsets[ i ] == column ) { this->values.setElement( this->getElementIndex( row, i ), value ); return; @@ -292,8 +292,8 @@ addElement( const IndexType row, TNL_ASSERT_GE( column, 0, "" ); TNL_ASSERT_LT( column, this->getColumns(), "" ); - for( IndexType i = 0; i < hostDiagonalsShifts.getSize(); i++ ) - if( row + hostDiagonalsShifts[ i ] == column ) + for( IndexType i = 0; i < hostDiagonalsOffsets.getSize(); i++ ) + if( row + hostDiagonalsOffsets[ i ] == column ) { const Index idx = this->getElementIndex( row, i ); this->values.setElement( idx, thisElementMultiplicator * this->values.getElement( idx ) + value ); @@ -320,8 +320,8 @@ getElement( const IndexType row, const IndexType column ) const TNL_ASSERT_GE( column, 0, "" ); TNL_ASSERT_LT( column, this->getColumns(), "" ); - for( IndexType localIdx = 0; localIdx < hostDiagonalsShifts.getSize(); localIdx++ ) - if( row + hostDiagonalsShifts[ localIdx ] == column ) + for( IndexType localIdx = 0; localIdx < hostDiagonalsOffsets.getSize(); localIdx++ ) + if( row + hostDiagonalsOffsets[ localIdx ] == column ) return this->values.getElement( this->indexer.getGlobalIndex( row, localIdx ) ); return 0.0; } @@ -335,8 +335,8 @@ MultidiagonalMatrixView< Real, Device, Index, Organization >:: operator=( const MultidiagonalMatrixView& view ) { MatrixView< Real, Device, Index >::operator=( view ); - this->diagonalsShifts.bind( view.diagonalsShifts ); - this->hostDiagonalsShifts.bind( view.hostDiagonalsShifts ); + this->diagonalsOffsets.bind( view.diagonalsOffsets ); + this->hostDiagonalsOffsets.bind( view.hostDiagonalsOffsets ); this->indexer = view.indexer; return *this; } @@ -352,8 +352,8 @@ rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Ke { using Real_ = decltype( fetch( IndexType(), IndexType(), RealType() ) ); const auto values_view = this->values.getConstView(); - const auto diagonalsShifts_view = this->diagonalsShifts.getConstView(); - const IndexType diagonalsCount = this->diagonalsShifts.getSize(); + const auto diagonalsOffsets_view = this->diagonalsOffsets.getConstView(); + const IndexType diagonalsCount = this->diagonalsOffsets.getSize(); const IndexType columns = this->getColumns(); const auto indexer = this->indexer; const auto zero = zero_; @@ -361,7 +361,7 @@ rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Ke Real_ sum( zero ); for( IndexType localIdx = 0; localIdx < diagonalsCount; localIdx++ ) { - const IndexType columnIdx = rowIdx + diagonalsShifts_view[ localIdx ]; + const IndexType columnIdx = rowIdx + diagonalsOffsets_view[ localIdx ]; if( columnIdx >= 0 && columnIdx < columns ) reduce( sum, fetch( rowIdx, columnIdx, values_view[ indexer.getGlobalIndex( rowIdx, localIdx ) ] ) ); } @@ -392,15 +392,15 @@ MultidiagonalMatrixView< Real, Device, Index, Organization >:: forRows( IndexType first, IndexType last, Function& function ) const { const auto values_view = this->values.getConstView(); - const auto diagonalsShifts_view = this->diagonalsShifts.getConstView(); - const IndexType diagonalsCount = this->diagonalsShifts.getSize(); + const auto diagonalsOffsets_view = this->diagonalsOffsets.getConstView(); + const IndexType diagonalsCount = this->diagonalsOffsets.getSize(); const IndexType columns = this->getColumns(); const auto indexer = this->indexer; bool compute( true ); auto f = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { for( IndexType localIdx = 0; localIdx < diagonalsCount; localIdx++ ) { - const IndexType columnIdx = rowIdx + diagonalsShifts_view[ localIdx ]; + const IndexType columnIdx = rowIdx + diagonalsOffsets_view[ localIdx ]; if( columnIdx >= 0 && columnIdx < columns ) function( rowIdx, localIdx, columnIdx, values_view[ indexer.getGlobalIndex( rowIdx, localIdx ) ], compute ); } @@ -418,15 +418,15 @@ MultidiagonalMatrixView< Real, Device, Index, Organization >:: forRows( IndexType first, IndexType last, Function& function ) { auto values_view = this->values.getView(); - const auto diagonalsShifts_view = this->diagonalsShifts.getConstView(); - const IndexType diagonalsCount = this->diagonalsShifts.getSize(); + const auto diagonalsOffsets_view = this->diagonalsOffsets.getConstView(); + const IndexType diagonalsCount = this->diagonalsOffsets.getSize(); const IndexType columns = this->getColumns(); const auto indexer = this->indexer; bool compute( true ); auto f = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { for( IndexType localIdx = 0; localIdx < diagonalsCount && compute; localIdx++ ) { - const IndexType columnIdx = rowIdx + diagonalsShifts_view[ localIdx ]; + const IndexType columnIdx = rowIdx + diagonalsOffsets_view[ localIdx ]; if( columnIdx >= 0 && columnIdx < columns ) function( rowIdx, localIdx, columnIdx, values_view[ indexer.getGlobalIndex( rowIdx, localIdx ) ], compute ); } @@ -670,9 +670,9 @@ void MultidiagonalMatrixView< Real, Device, Index, Organization >::print( std::o for( IndexType rowIdx = 0; rowIdx < this->getRows(); rowIdx++ ) { str <<"Row: " << rowIdx << " -> "; - for( IndexType localIdx = 0; localIdx < this->hostDiagonalsShifts.getSize(); localIdx++ ) + for( IndexType localIdx = 0; localIdx < this->hostDiagonalsOffsets.getSize(); localIdx++ ) { - const IndexType columnIdx = rowIdx + this->hostDiagonalsShifts[ localIdx ]; + const IndexType columnIdx = rowIdx + this->hostDiagonalsOffsets[ localIdx ]; if( columnIdx >= 0 && columnIdx < this->columns ) { auto v = this->values.getElement( this->indexer.getGlobalIndex( rowIdx, localIdx ) ); diff --git a/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h b/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h index 0211f986d..15b0dc44b 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h +++ b/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h @@ -485,8 +485,8 @@ void multidiagonalMatrixAssignment() using MultidiagonalHost = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Host, IndexType >; using MultidiagonalCuda = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType >; - using DiagonalsShiftsType = typename MultidiagonalHost::DiagonalsShiftsType; - DiagonalsShiftsType diagonals{ -4, -2, 0, 1, 3, 5 }; + using DiagonalsOffsetsType = typename MultidiagonalHost::DiagonalsOffsetsType; + DiagonalsOffsetsType diagonals{ -4, -2, 0, 1, 3, 5 }; const IndexType rows( 10 ), columns( 10 ); MultidiagonalHost hostMatrix( rows, columns, diagonals ); diff --git a/src/UnitTests/Matrices/DenseMatrixCopyTest.h b/src/UnitTests/Matrices/DenseMatrixCopyTest.h index 3e5f9af7f..efe8595c8 100644 --- a/src/UnitTests/Matrices/DenseMatrixCopyTest.h +++ b/src/UnitTests/Matrices/DenseMatrixCopyTest.h @@ -451,8 +451,8 @@ void multidiagonalMatrixAssignment() using MultidiagonalHost = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Host, IndexType >; using MultidiagonalCuda = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType >; - using DiagonalsShiftsType = typename MultidiagonalHost::DiagonalsShiftsType; - DiagonalsShiftsType diagonals{ -4, -2, 0, 1, 3, 5 }; + using DiagonalsOffsetsType = typename MultidiagonalHost::DiagonalsOffsetsType; + DiagonalsOffsetsType diagonals{ -4, -2, 0, 1, 3, 5 }; const IndexType rows( 10 ), columns( 10 ); MultidiagonalHost hostMatrix( rows, columns, diagonals ); diff --git a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h index 2fed8bc2e..241e9fdfc 100644 --- a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h @@ -51,14 +51,14 @@ void test_SetDimensions() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; const IndexType rows = 9; const IndexType cols = 8; - const DiagonalsShiftsType diagonalsShifts{ -3, -1, 0, 2, 4 }; + const DiagonalsOffsetsType diagonalsOffsets{ -3, -1, 0, 2, 4 }; Matrix m; - m.setDimensions( rows, cols, diagonalsShifts ); + m.setDimensions( rows, cols, diagonalsOffsets ); EXPECT_EQ( m.getRows(), 9 ); EXPECT_EQ( m.getColumns(), 8 ); @@ -71,17 +71,17 @@ void test_SetLike() using RealType = typename Matrix1::RealType; using DeviceType = typename Matrix1::DeviceType; using IndexType = typename Matrix1::IndexType; - using DiagonalsShiftsType = typename Matrix1::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix1::DiagonalsOffsetsType; const IndexType rows = 8; const IndexType cols = 7; - const DiagonalsShiftsType diagonalsShifts{ -3, -1, 0, 2, 4 }; + const DiagonalsOffsetsType diagonalsOffsets{ -3, -1, 0, 2, 4 }; Matrix1 m1; - m1.setDimensions( rows + 1, cols + 2, diagonalsShifts ); + m1.setDimensions( rows + 1, cols + 2, diagonalsOffsets ); Matrix2 m2; - m2.setDimensions( rows, cols, diagonalsShifts ); + m2.setDimensions( rows, cols, diagonalsOffsets ); m1.setLike( m2 ); @@ -157,7 +157,7 @@ void test_GetNonemptyRowsCount() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 5x8 matrix: @@ -168,7 +168,7 @@ void test_GetNonemptyRowsCount() * | 0 1 0 1 0 0 1 0 | * \ 0 0 1 0 1 0 0 1 / */ - Matrix m1( 5, 8, DiagonalsShiftsType({ -2, 0, 3, 5 }) ); + Matrix m1( 5, 8, DiagonalsOffsetsType({ -2, 0, 3, 5 }) ); m1.setValue( 1.0 ); EXPECT_EQ( m1.getNonemptyRowsCount(), 5 ); @@ -181,7 +181,7 @@ void test_GetNonemptyRowsCount() * | 0 1 0 1 0 | * \ 0 0 1 0 1 / */ - Matrix m2( 5, 5, DiagonalsShiftsType({ -2, 0, 3, 5 }) ); + Matrix m2( 5, 5, DiagonalsOffsetsType({ -2, 0, 3, 5 }) ); m2.setValue( 1.0 ); EXPECT_EQ( m2.getNonemptyRowsCount(), 5 ); @@ -197,7 +197,7 @@ void test_GetNonemptyRowsCount() * | 0 0 0 0 1 | * \ 0 0 0 0 0 / */ - Matrix m3( 8, 5, DiagonalsShiftsType({ -2, 0, 3, 5 }) ); + Matrix m3( 8, 5, DiagonalsOffsetsType({ -2, 0, 3, 5 }) ); m3.setValue( 1.0 ); EXPECT_EQ( m3.getNonemptyRowsCount(), 7 ); } @@ -208,7 +208,7 @@ void test_GetCompressedRowLengths() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 8x8 matrix: @@ -226,7 +226,7 @@ void test_GetCompressedRowLengths() const IndexType rows = 8; const IndexType cols = 8; - Matrix m( rows, cols, DiagonalsShiftsType({ -2, 0, 3, 5 }) ); + Matrix m( rows, cols, DiagonalsOffsetsType({ -2, 0, 3, 5 }) ); m.setValue( 1.0 ); m.setElement( 0, 0, 0.0 ); m.setElement( 7, 7, 0.0 ); @@ -244,18 +244,18 @@ void test_GetAllocatedElementsCount() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; const IndexType rows = 7; const IndexType cols = 6; - Matrix m1( 7, 6, DiagonalsShiftsType( { -2, 0, 3, 5 } ) ); + Matrix m1( 7, 6, DiagonalsOffsetsType( { -2, 0, 3, 5 } ) ); EXPECT_EQ( m1.getAllocatedElementsCount(), 28 ); - Matrix m2( 8, 6, DiagonalsShiftsType( { -2, 0, 3, 5 } ) ); + Matrix m2( 8, 6, DiagonalsOffsetsType( { -2, 0, 3, 5 } ) ); EXPECT_EQ( m2.getAllocatedElementsCount(), 32 ); - Matrix m3( 9, 6, DiagonalsShiftsType( { -2, 0, 3, 5 } ) ); + Matrix m3( 9, 6, DiagonalsOffsetsType( { -2, 0, 3, 5 } ) ); EXPECT_EQ( m3.getAllocatedElementsCount(), 32 ); } @@ -265,7 +265,7 @@ void test_GetNumberOfNonzeroMatrixElements() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 7x6 matrix: @@ -283,7 +283,7 @@ void test_GetNumberOfNonzeroMatrixElements() const IndexType rows = 7; const IndexType cols = 6; - Matrix m( rows, cols, DiagonalsShiftsType( { -3, 0, 2, 4 } ) ); + Matrix m( rows, cols, DiagonalsOffsetsType( { -3, 0, 2, 4 } ) ); m.setValue( 1.0 ); m.setElement( 0, 0, 0.0 ); @@ -296,7 +296,7 @@ void test_Reset() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 5x4 matrix: @@ -310,7 +310,7 @@ void test_Reset() const IndexType rows = 5; const IndexType cols = 4; - Matrix m( rows, cols, DiagonalsShiftsType( { 0, 1, 2, 4 } ) ); + Matrix m( rows, cols, DiagonalsOffsetsType( { 0, 1, 2, 4 } ) ); m.reset(); EXPECT_EQ( m.getRows(), 0 ); @@ -323,7 +323,7 @@ void test_SetValue() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 7x6 matrix: @@ -339,7 +339,7 @@ void test_SetValue() const IndexType rows = 7; const IndexType cols = 6; - Matrix m( rows, cols, DiagonalsShiftsType( { -3, 0, 2, 4 } ) ); + Matrix m( rows, cols, DiagonalsOffsetsType( { -3, 0, 2, 4 } ) ); m.setValue( 1.0 ); EXPECT_EQ( m.getElement( 0, 0 ), 1 ); @@ -398,7 +398,7 @@ void test_SetElement() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 5x5 matrix: @@ -411,7 +411,7 @@ void test_SetElement() */ const IndexType rows = 5; const IndexType cols = 5; - DiagonalsShiftsType diagonals{-3, 0, 1, 4 }; + DiagonalsOffsetsType diagonals{-3, 0, 1, 4 }; Matrix m( rows, cols, diagonals ); RealType value = 1; @@ -461,7 +461,7 @@ void test_AddElement() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 5x5 matrix: @@ -474,7 +474,7 @@ void test_AddElement() */ const IndexType rows = 5; const IndexType cols = 5; - DiagonalsShiftsType diagonals{-3, 0, 1, 4 }; + DiagonalsOffsetsType diagonals{-3, 0, 1, 4 }; Matrix m( rows, cols, diagonals ); RealType value = 1; @@ -582,7 +582,7 @@ void test_SetRow() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 5x7 matrix: @@ -596,7 +596,7 @@ void test_SetRow() const IndexType rows = 5; const IndexType cols = 7; - Matrix m( rows, cols, DiagonalsShiftsType({ -1, 0, 2, 4 }) ); + Matrix m( rows, cols, DiagonalsOffsetsType({ -1, 0, 2, 4 }) ); auto matrix_view = m.getView(); auto f = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { @@ -659,7 +659,7 @@ void test_AddRow() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 6x5 matrix: @@ -674,7 +674,7 @@ void test_AddRow() const IndexType rows = 6; const IndexType cols = 5; - DiagonalsShiftsType diagonals( { -2, 0, 1, 2 } ); + DiagonalsOffsetsType diagonals( { -2, 0, 1, 2 } ); Matrix m( rows, cols, diagonals ); @@ -798,7 +798,7 @@ void test_VectorProduct() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 5x4 matrix: @@ -811,7 +811,7 @@ void test_VectorProduct() */ const IndexType rows = 5; const IndexType cols = 4; - DiagonalsShiftsType diagonals{ -2, 0, 2 }; + DiagonalsOffsetsType diagonals{ -2, 0, 2 }; Matrix m( rows, cols, diagonals ); @@ -847,8 +847,8 @@ void test_AddMatrix() using RealType = typename Matrix1::RealType; using DeviceType = typename Matrix1::DeviceType; using IndexType = typename Matrix1::IndexType; - using DiagonalsShiftsType1 = typename Matrix1::DiagonalsShiftsType; - using DiagonalsShiftsType2 = typename Matrix2::DiagonalsShiftsType; + using DiagonalsOffsetsType1 = typename Matrix1::DiagonalsOffsetsType; + using DiagonalsOffsetsType2 = typename Matrix2::DiagonalsOffsetsType; /* * Sets up the following 5x4 matrix: @@ -861,8 +861,8 @@ void test_AddMatrix() */ const IndexType rows = 5; const IndexType cols = 4; - DiagonalsShiftsType1 diagonals1; - DiagonalsShiftsType2 diagonals2; + DiagonalsOffsetsType1 diagonals1; + DiagonalsOffsetsType2 diagonals2; Matrix1 m( rows, cols, diagonals1 ); @@ -970,7 +970,7 @@ void test_GetMatrixProduct() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 5x4 matrix: * @@ -982,9 +982,9 @@ void test_GetMatrixProduct() */ const IndexType leftRows = 5; const IndexType leftCols = 4; - DiagonalsShiftsType diagonalsShifts( { 0, 1, 2 } ); + DiagonalsOffsetsType diagonalsOffsets( { 0, 1, 2 } ); - Matrix leftMatrix( leftRows, leftCols, diagonalsShifts ); + Matrix leftMatrix( leftRows, leftCols, diagonalsOffsets ); RealType value = 1; for( IndexType i = 0; i < leftRows; i++ ) @@ -1021,7 +1021,7 @@ void test_GetMatrixProduct() * \ 0 0 0 0 / */ - Matrix mResult( leftRows, rightCols, diagonalsShifts ); + Matrix mResult( leftRows, rightCols, diagonalsOffsets ); mResult.setValue( 0 ); RealType leftMatrixMultiplicator = 1; @@ -1073,7 +1073,7 @@ void test_GetTransposition() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 3x2 matrix: * @@ -1083,9 +1083,9 @@ void test_GetTransposition() */ const IndexType rows = 3; const IndexType cols = 2; - DiagonalsShiftsType diagonalsShifts( { 0, 1, 2 } ); + DiagonalsOffsetsType diagonalsOffsets( { 0, 1, 2 } ); - Matrix m( rows, cols, diagonalsShifts ); + Matrix m( rows, cols, diagonalsOffsets ); RealType value = 1; for( IndexType i = 0; i < rows; i++ ) @@ -1100,7 +1100,7 @@ void test_GetTransposition() * / 0 0 0 \ * \ 0 0 0 / */ - Matrix mTransposed( cols, rows, diagonalsShifts ); + Matrix mTransposed( cols, rows, diagonalsOffsets ); mTransposed.print( std::cout ); @@ -1133,7 +1133,7 @@ void test_PerformSORIteration() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 4x4 matrix: * @@ -1144,9 +1144,9 @@ void test_PerformSORIteration() */ const IndexType rows = 4; const IndexType cols = 4; - DiagonalsShiftsType diagonalsShifts( { 0, 1, 2 } ); + DiagonalsOffsetsType diagonalsOffsets( { 0, 1, 2 } ); - Matrix m( rows, cols, diagonalsShifts ); + Matrix m( rows, cols, diagonalsOffsets ); m.setElement( 0, 0, 4.0 ); // 0th row m.setElement( 0, 1, 1.0 ); @@ -1209,7 +1209,7 @@ void test_AssignmentOperator() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; constexpr TNL::Containers::Segments::ElementsOrganization organization = Matrix::getOrganization(); using MultidiagonalHost = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Host, IndexType, organization >; @@ -1217,28 +1217,28 @@ void test_AssignmentOperator() organization == TNL::Containers::Segments::RowMajorOrder ? TNL::Containers::Segments::ColumnMajorOrder : TNL::Containers::Segments::RowMajorOrder >; const IndexType rows( 10 ), columns( 10 ); - DiagonalsShiftsType diagonalsShifts( { -4, -2, 0, 2, 3, 5 } ); - MultidiagonalHost hostMatrix( rows, columns, diagonalsShifts ); + DiagonalsOffsetsType diagonalsOffsets( { -4, -2, 0, 2, 3, 5 } ); + MultidiagonalHost hostMatrix( rows, columns, diagonalsOffsets ); for( IndexType i = 0; i < rows; i++ ) for( IndexType j = 0; j < columns; j++ ) - if( diagonalsShifts.containsValue( j - i ) ) + if( diagonalsOffsets.containsValue( j - i ) ) hostMatrix.setElement( i, j, i + j ); - Matrix matrix( rows, columns, diagonalsShifts ); + Matrix matrix( rows, columns, diagonalsOffsets ); matrix.getValues() = 0.0; matrix = hostMatrix; for( IndexType i = 0; i < columns; i++ ) for( IndexType j = 0; j < rows; j++ ) - if( diagonalsShifts.containsValue( j - i ) ) + if( diagonalsOffsets.containsValue( j - i ) ) EXPECT_EQ( matrix.getElement( i, j ), i + j ); else EXPECT_EQ( matrix.getElement( i, j ), 0.0 ); #ifdef HAVE_CUDA - MultidiagonalCuda cudaMatrix( rows, columns, diagonalsShifts ); + MultidiagonalCuda cudaMatrix( rows, columns, diagonalsOffsets ); for( IndexType i = 0; i < rows; i++ ) for( IndexType j = 0; j < columns; j++ ) - if( diagonalsShifts.containsValue( j - i ) ) + if( diagonalsOffsets.containsValue( j - i ) ) cudaMatrix.setElement( i, j, i + j ); matrix.getValues() = 0.0; @@ -1246,7 +1246,7 @@ void test_AssignmentOperator() for( IndexType i = 0; i < rows; i++ ) for( IndexType j = 0; j < columns; j++ ) { - if( diagonalsShifts.containsValue( j - i ) ) + if( diagonalsOffsets.containsValue( j - i ) ) EXPECT_EQ( matrix.getElement( i, j ), i + j ); else EXPECT_EQ( matrix.getElement( i, j ), 0.0 ); @@ -1261,7 +1261,7 @@ void test_SaveAndLoad() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 4x4 matrix: @@ -1273,15 +1273,15 @@ void test_SaveAndLoad() */ const IndexType rows = 4; const IndexType cols = 4; - DiagonalsShiftsType diagonalsShifts( { -1, 0, 1 } ); + DiagonalsOffsetsType diagonalsOffsets( { -1, 0, 1 } ); - Matrix savedMatrix( rows, cols, diagonalsShifts ); + Matrix savedMatrix( rows, cols, diagonalsOffsets ); RealType value = 1; for( IndexType i = 0; i < rows; i++ ) for( IndexType j = 0; j < cols; j++ ) { - if( diagonalsShifts.containsValue( j - i ) ) + if( diagonalsOffsets.containsValue( j - i ) ) savedMatrix.setElement( i, j, value ); value++; } @@ -1339,7 +1339,7 @@ void test_Print() using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using DiagonalsShiftsType = typename Matrix::DiagonalsShiftsType; + using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; /* * Sets up the following 5x4 sparse matrix: @@ -1352,9 +1352,9 @@ void test_Print() */ const IndexType rows = 5; const IndexType cols = 4; - DiagonalsShiftsType diagonalsShifts( { -1, 0, 1 } ); + DiagonalsOffsetsType diagonalsOffsets( { -1, 0, 1 } ); - Matrix m( rows, cols, diagonalsShifts ); + Matrix m( rows, cols, diagonalsOffsets ); RealType value = 1; for( IndexType i = 0; i < rows; i++) diff --git a/src/UnitTests/Matrices/SparseMatrixCopyTest.h b/src/UnitTests/Matrices/SparseMatrixCopyTest.h index 535c37ea3..fb5d84206 100644 --- a/src/UnitTests/Matrices/SparseMatrixCopyTest.h +++ b/src/UnitTests/Matrices/SparseMatrixCopyTest.h @@ -488,8 +488,8 @@ void multidiagonalMatrixAssignment() using MultidiagonalHost = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Host, IndexType >; using MultidiagonalCuda = TNL::Matrices::MultidiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType >; - using DiagonalsShiftsType = typename MultidiagonalHost::DiagonalsShiftsType; - DiagonalsShiftsType diagonals{ -4, -2, 0, 1, 3, 5 }; + using DiagonalsOffsetsType = typename MultidiagonalHost::DiagonalsOffsetsType; + DiagonalsOffsetsType diagonals{ -4, -2, 0, 1, 3, 5 }; const IndexType rows( 10 ), columns( 10 ); MultidiagonalHost hostMatrix( rows, columns, diagonals ); -- GitLab From f510892678bd0144221c7cc73ef6af6b13469987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 2 Jun 2020 20:34:31 +0200 Subject: [PATCH 26/38] Writting documentation on multidiagonal matrix. --- .../MultidiagonalMatrix/CMakeLists.txt | 119 ++--- .../MultidiagonalMatrixExample_addElement.cpp | 39 ++ .../MultidiagonalMatrixExample_addElement.cu | 1 + ...MultidiagonalMatrixExample_getConstRow.cpp | 58 +++ .../MultidiagonalMatrixExample_getConstRow.cu | 1 + .../MultidiagonalMatrixExample_getElement.cpp | 39 ++ .../MultidiagonalMatrixExample_getElement.cu | 1 + .../MultidiagonalMatrixExample_getRow.cpp | 53 +++ .../MultidiagonalMatrixExample_getRow.cu | 1 + ...onalMatrixExample_getSerializationType.cpp | 23 + ...gonalMatrixExample_getSerializationType.cu | 1 + .../MultidiagonalMatrixExample_setElement.cpp | 51 +++ ...MultidiagonalMatrixExample_setElements.cpp | 48 ++ .../MultidiagonalMatrixExample_setElements.cu | 1 + src/TNL/Matrices/MultidiagonalMatrix.h | 423 ++++++++++++++++-- src/TNL/Matrices/MultidiagonalMatrix.hpp | 97 ++-- src/TNL/Matrices/MultidiagonalMatrixView.h | 9 + src/TNL/Matrices/MultidiagonalMatrixView.hpp | 81 +++- .../Matrices/MultidiagonalMatrixTest.h | 138 +----- 19 files changed, 910 insertions(+), 274 deletions(-) create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_addElement.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_addElement.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getConstRow.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getConstRow.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getElement.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getElement.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cu diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index e07e9a69b..405990922 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -14,10 +14,15 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list_2.out OUTPUT MultidiagonalMatrixExample_Constructor_init_list_2.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElements_cuda MultidiagonalMatrixExample_setElements.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElements_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out -# OUTPUT MultidiagonalMatrixExample_setElements.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getSerializationType_cuda MultidiagonalMatrixExample_getSerializationType.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getSerializationType_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getSerializationType.out + OUTPUT MultidiagonalMatrixExample_getSerializationType.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElements_cuda MultidiagonalMatrixExample_setElements.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElements_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out + OUTPUT MultidiagonalMatrixExample_setElements.out ) # CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths_cuda MultidiagonalMatrixExample_getCompressedRowLengths.cu ) # ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths_cuda > @@ -29,30 +34,30 @@ IF( BUILD_CUDA ) # ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElementsCount.out # OUTPUT MultidiagonalMatrixExample_getElementsCount.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getConstRow_cuda MultidiagonalMatrixExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getConstRow.out -# OUTPUT MultidiagonalMatrixExample_getConstRow.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getConstRow_cuda MultidiagonalMatrixExample_getConstRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getConstRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getConstRow.out + OUTPUT MultidiagonalMatrixExample_getConstRow.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getRow_cuda MultidiagonalMatrixExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getRow.out -# OUTPUT MultidiagonalMatrixExample_getRow.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getRow_cuda MultidiagonalMatrixExample_getRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getRow.out + OUTPUT MultidiagonalMatrixExample_getRow.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement_cuda MultidiagonalMatrixExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out -# OUTPUT MultidiagonalMatrixExample_setElement.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement_cuda MultidiagonalMatrixExample_setElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out + OUTPUT MultidiagonalMatrixExample_setElement.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_addElement_cuda MultidiagonalMatrixExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_addElement.out -# OUTPUT MultidiagonalMatrixExample_addElement.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_addElement_cuda MultidiagonalMatrixExample_addElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_addElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_addElement.out + OUTPUT MultidiagonalMatrixExample_addElement.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getElement_cuda MultidiagonalMatrixExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out -# OUTPUT MultidiagonalMatrixExample_getElement.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getElement_cuda MultidiagonalMatrixExample_getElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out + OUTPUT MultidiagonalMatrixExample_getElement.out ) # CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction_cuda MultidiagonalMatrixExample_rowsReduction.cu ) # ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction_cuda > @@ -150,10 +155,15 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list_2.out OUTPUT MultidiagonalMatrixExample_Constructor_init_list_2.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_setElements MultidiagonalMatrixExample_setElements.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElements > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out -# OUTPUT MultidiagonalMatrixExample_setElements.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_getSerializationType MultidiagonalMatrixExample_getSerializationType.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getSerializationType > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getSerializationType.out + OUTPUT MultidiagonalMatrixExample_getSerializationType.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixExample_setElements MultidiagonalMatrixExample_setElements.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElements > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out + OUTPUT MultidiagonalMatrixExample_setElements.out ) # ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths MultidiagonalMatrixExample_getCompressedRowLengths.cpp ) # ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths > @@ -165,30 +175,30 @@ ELSE() # ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElementsCount.out # OUTPUT MultidiagonalMatrixExample_getElementsCount.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_getConstRow MultidiagonalMatrixExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getConstRow.out -# OUTPUT MultidiagonalMatrixExample_getConstRow.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_getConstRow MultidiagonalMatrixExample_getConstRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getConstRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getConstRow.out + OUTPUT MultidiagonalMatrixExample_getConstRow.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_getRow MultidiagonalMatrixExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getRow.out -# OUTPUT MultidiagonalMatrixExample_getRow.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_getRow MultidiagonalMatrixExample_getRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getRow.out + OUTPUT MultidiagonalMatrixExample_getRow.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement MultidiagonalMatrixExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out -# OUTPUT MultidiagonalMatrixExample_setElement.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement MultidiagonalMatrixExample_setElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out + OUTPUT MultidiagonalMatrixExample_setElement.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_addElement MultidiagonalMatrixExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_addElement.out -# OUTPUT MultidiagonalMatrixExample_addElement.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_addElement MultidiagonalMatrixExample_addElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_addElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_addElement.out + OUTPUT MultidiagonalMatrixExample_addElement.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_getElement MultidiagonalMatrixExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out -# OUTPUT MultidiagonalMatrixExample_getElement.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_getElement MultidiagonalMatrixExample_getElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out + OUTPUT MultidiagonalMatrixExample_getElement.out ) # ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction MultidiagonalMatrixExample_rowsReduction.cpp ) # ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction > @@ -276,14 +286,15 @@ ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS MultidiagonalMatrixExample_Constructor.out MultidiagonalMatrixExample_Constructor_init_list_1.out MultidiagonalMatrixExample_Constructor_init_list_2.out -# MultidiagonalMatrixExample_setElements.out + MultidiagonalMatrixExample_getSerializationType.out + MultidiagonalMatrixExample_setElements.out # MultidiagonalMatrixExample_getCompressedRowLengths.out # MultidiagonalMatrixExample_getElementsCount.out -# MultidiagonalMatrixExample_getConstRow.out -# MultidiagonalMatrixExample_getRow.out -# MultidiagonalMatrixExample_setElement.out -# MultidiagonalMatrixExample_addElement.out -# MultidiagonalMatrixExample_getElement.out + MultidiagonalMatrixExample_getConstRow.out + MultidiagonalMatrixExample_getRow.out + MultidiagonalMatrixExample_setElement.out + MultidiagonalMatrixExample_addElement.out + MultidiagonalMatrixExample_getElement.out # MultidiagonalMatrixExample_rowsReduction.out # MultidiagonalMatrixExample_allRowsReduction.out # MultidiagonalMatrixExample_forRows.out diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_addElement.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_addElement.cpp new file mode 100644 index 000000000..a2da4e40c --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_addElement.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +template< typename Device > +void addElements() +{ + const int matrixSize( 5 ); + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize, // number of columns + { -1, 0, 1 } ); // diagonals offsets + for( int i = 0; i < matrixSize; i++ ) + matrix.setElement( i, i, i ); + + std::cout << "Initial matrix is: " << std::endl << matrix << std::endl; + + for( int i = 0; i < matrixSize; i++ ) + { + if( i > 0 ) + matrix.addElement( i, i - 1, 1.0, 5.0 ); + matrix.addElement( i, i, 1.0, 5.0 ); + if( i < matrixSize - 1 ) + matrix.addElement( i, i + 1, 1.0, 5.0 ); + } + + std::cout << "Matrix after addition is: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Add elements on host:" << std::endl; + addElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Add elements on CUDA device:" << std::endl; + addElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_addElement.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_addElement.cu new file mode 120000 index 000000000..04b6a5875 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_addElement.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getConstRow.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getConstRow.cpp new file mode 100644 index 000000000..1cbda6be7 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getConstRow.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + const int matrixSize = 5; + auto diagonalsOffsets = { -2, -1, 0 }; + using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > matrix ( + matrixSize, // number of matrix rows + matrixSize, // number of matrix columns + diagonalsOffsets ); + matrix->setElements( + { { 0.0, 0.0, 1.0 }, + { 0.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 } } ); + + /*** + * Fetch lambda function returns diagonal element in each row. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { + auto row = matrix->getRow( rowIdx ); + return row.getValue( 2 ); // get value from subdiagonal with index 2, i.e. the main diagonal + }; + + /*** + * For the case when Device is CUDA device we need to synchronize smart + * pointers. To avoid this you may use MultidiagonalMatrixView. See + * MultidiagonalMatrixView::getConstRow example for details. + */ + TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); + + /*** + * Compute the matrix trace. + */ + int trace = TNL::Algorithms::Reduction< Device >::reduce( matrix->getRows(), std::plus<>{}, fetch, 0 ); + std::cout << "Matrix reads as: " << std::endl << *matrix << std::endl; + std::cout << "Matrix trace is: " << trace << "." << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Getting matrix rows on host: " << std::endl; + getRowExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getConstRow.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getConstRow.cu new file mode 120000 index 000000000..ec42cc674 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getConstRow.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_getConstRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getElement.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getElement.cpp new file mode 100644 index 000000000..b1d7486fb --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getElement.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +template< typename Device > +void getElements() +{ + const int matrixSize( 5 ); + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + matrixSize, // number of matrix columns + { -1, 0, 1 }, // matrix diagonals offsets + { // matrix elements definition + { 0.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, 0.0 } + } ); + + + for( int i = 0; i < matrixSize; i++ ) + { + for( int j = 0; j < matrixSize; j++ ) + std::cout << std::setw( 5 ) << matrix.getElement( i, j ); + std::cout << std::endl; + } +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get elements on host:" << std::endl; + getElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get elements on CUDA device:" << std::endl; + getElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getElement.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getElement.cu new file mode 120000 index 000000000..6eb5f59c2 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getElement.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_getElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp new file mode 100644 index 000000000..82273541f --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + const int matrixSize( 5 ); + auto diagonalsOffsets = { -1, 0, 1 }; // Variadic templates in SharedPointer + // constructor do not recognize initializer + // list so we give it a hint. + using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > matrix( + matrixSize, // number of matrix rows + matrixSize, // number of matrix columns + diagonalsOffsets ); + + auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { + auto row = matrix->getRow( rowIdx ); + if( rowIdx > 0 ) + row.setElement( 0, -1.0 ); // elements below the diagonal + row.setElement( 1, 2.0 ); // elements on the diagonal + if( rowIdx < matrixSize - 1 ) // elements above the diagonal + row.setElement( 2, -1.0 ); + }; + + /*** + * For the case when Device is CUDA device we need to synchronize smart + * pointers. To avoid this you may use MultidiagonalMatrixView. See + * MultidiagonalMatrixView::getRow example for details. + */ + TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); + + /*** + * Set the matrix elements. + */ + TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix->getRows(), f ); + std::cout << std::endl << *matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Getting matrix rows on host: " << std::endl; + getRowExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cu new file mode 120000 index 000000000..6eca2f444 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_getRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cpp new file mode 100644 index 000000000..a72e90dce --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + + +template< typename Device > +void getSerializationTypeExample() +{ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix; + + std::cout << "Matrix type is: " << matrix.getSerializationType(); +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get serialization type on CPU ... " << std::endl; + getSerializationTypeExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get serialization type on CUDA GPU ... " << std::endl; + getSerializationTypeExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cu new file mode 120000 index 000000000..322cb05b8 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_getSerializationType.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp new file mode 100644 index 000000000..d900261a5 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void setElements() +{ + const int matrixSize( 5 ); + auto diagonalsOffsets = { -1, 0, 1 }; // offsets of tridiagonal matrix + using Matrix = TNL::Matrices::MultidiagonalMatrix< double, Device >; + TNL::Pointers::SharedPointer< Matrix > matrix( matrixSize, matrixSize, diagonalsOffsets ); + for( int i = 0; i < 5; i++ ) + matrix->setElement( i, i, i ); + + std::cout << "Matrix set from the host:" << std::endl; + std::cout << *matrix << std::endl; + + auto f = [=] __cuda_callable__ ( int i ) mutable { + if( i > 0 ) + matrix->setElement( i, i - 1, 1.0 ); + matrix->setElement( i, i, -i ); + if( i < matrixSize - 1 ) + matrix->setElement( i, i + 1, 1.0 ); + }; + + /*** + * For the case when Device is CUDA device we need to synchronize smart + * pointers. To avoid this you may use MultidiagonalMatrixView. See + * MultidiagonalMatrixView::getRow example for details. + */ + TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); + TNL::Algorithms::ParallelFor< Device >::exec( 0, 5, 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 HAVE_CUDA + std::cout << "Set elements on CUDA device:" << std::endl; + setElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cpp new file mode 100644 index 000000000..b868a611c --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void laplaceOperatorMatrix() +{ + const int gridSize( 4 ); + const int matrixSize = gridSize * gridSize; + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize, // number of columns + { - gridSize, -1, 0, 1, gridSize } // diagonals offsets + ); + matrix.setElements( { + { 0.0, 0.0, 1.0 }, // set matrix elements corresponding to boundary grid nodes + { 0.0, 0.0, 1.0 }, // and Dirichlet boundary conditions, i.e. 1 on the main diagonal + { 0.0, 0.0, 1.0 }, // which is the third one + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, // set matrix elements corresponding to inner grid nodes, i.e.4 on the main diagonal + { -1.0, -1.0, 4.0, -1.0, -1.0 }, // (the third one) and -1 to the other sub-diagonals + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 } + } ); + std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cu new file mode 120000 index 000000000..b5a31ea14 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_setElements.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index 546d5ae97..e152c8cda 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -90,7 +90,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Matrix view type for constant instances. * - * See \ref MutlidiagonlMatrixView. + * See \ref MultidiagonalMatrixView. */ using ConstViewType = MultidiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; @@ -230,110 +230,481 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \return \ref String with the serialization type. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cpp * \par Output - * \include SparseMatrixExample_getSerializationType.out + * \include MultidiagonalMatrixExample_getSerializationType.out */ static String getSerializationType(); /** * \brief Returns string with serialization type. * - * See \ref SparseMatrix::getSerializationType. + * See \ref MultidiagonalMatrix::getSerializationType. * * \return \e String with the serialization type. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_getSerializationType.cpp + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cpp * \par Output - * \include SparseMatrixExample_getSerializationType.out + * \include MultidiagonalMatrixExample_getSerializationType.out */ virtual String getSerializationTypeVirtual() const; /** - * \brief Set matrix dimensions and + * \brief Set matrix dimensions and diagonals offsets. + * + * \tparam Vector is type of vector like container holding the diagonals offsets. + * + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + * \param diagonalsOffsets is vector with diagonals offsets. */ template< typename Vector > void setDimensions( const IndexType rows, const IndexType columns, const Vector& diagonalsOffsets ); - template< typename RowLengthsVector > - void setCompressedRowLengths( const RowLengthsVector& rowCapacities ); + /** + * \brief This method is for compatibility with \ref SparseMatrix. + * + * It checks if the number of matrix diagonals is compatible with + * required number of non-zero matrix elements in each row. If not + * exception is thrown. + * + * \tparam RowCapacitiesVector is vector-like container type for holding required + * row capacities. + * + * \param rowCapacities is vector-like container holding required row capacities. + */ + template< typename RowCapacitiesVector > + void setRowCapacities( const RowCapacitiesVector& rowCapacities ); + /** + * \brief Set matrix elements from an initializer list. + * + * \tparam ListReal is data type of the initializer list. + * + * \param data is initializer list holding matrix elements. The size of the outer list + * defines the number of matrix rows. Each inner list defines values of each sub-diagonal + * and so its size must be lower or equal to the size of \e diagonalsOffsets. Values + * of sub-diagonals which do not fit to given row are omitted. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cpp + * \par Output + * \include MultidiagonalMatrixExample_setElements.out + */ 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. + * + * \param rowLengths is a vector into which the number of non-zeros in each row + * will be stored. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getCompressedRowLengths.cpp + * \par Output + * \include MultidiagonalMatrixExample_getCompressedRowLengths.out + */ template< typename Vector > void getCompressedRowLengths( Vector& rowLengths ) const; - IndexType getNonemptyRowsCount() const; - [[deprecated]] IndexType getRowLength( const IndexType row ) const; - IndexType getMaxRowLength() const; - - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - void setLike( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& m ); + /** + * \brief Setup the matrix dimensions and diagonals offsets based on another multidiagonal matrix. + * + * \tparam Real_ is \e Real type of the source matrix. + * \tparam Device_ is \e Device type of the source matrix. + * \tparam Index_ is \e Index type of the source matrix. + * \tparam Organization_ is \e Organization of the source matrix. + * \tparam RealAllocator_ is \e RealAllocator of the source matrix. + * \tparam IndexAllocator_ is \e IndexAllocator of the source matrix. + * + * \param matrix is the source matrix. + */ + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_, + typename RealAllocator_, + typename IndexAllocator_ > + void setLike( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ); - IndexType getNumberOfNonzeroMatrixElements() const; + /** + * \brief Returns number of non-zero matrix elements. + * + * This method really counts the non-zero matrix elements and so + * it returns zero for matrix having all allocated elements set to zero. + * + * \return number of non-zero matrix elements. + */ + IndexType getNonzeroElementsCount() const; + /** + * \brief Resets the matrix to zero dimensions. + */ void reset(); - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - bool operator == ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const; + /** + * \brief Comparison operator with another multidiagonal matrix. + * + * \tparam Real_ is \e Real type of the source matrix. + * \tparam Device_ is \e Device type of the source matrix. + * \tparam Index_ is \e Index type of the source matrix. + * \tparam Organization_ is \e Organization of the source matrix. + * \tparam RealAllocator_ is \e RealAllocator of the source matrix. + * \tparam IndexAllocator_ is \e IndexAllocator of the source matrix. + * + * \return \e true if both matrices are identical and \e false otherwise. + */ + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_, + typename RealAllocator_, + typename IndexAllocator_ > + bool operator == ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ) const; - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - bool operator != ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const; + /** + * \brief Comparison operator with another multidiagonal matrix. + * + * \tparam Real_ is \e Real type of the source matrix. + * \tparam Device_ is \e Device type of the source matrix. + * \tparam Index_ is \e Index type of the source matrix. + * \tparam Organization_ is \e Organization of the source matrix. + * \tparam RealAllocator_ is \e RealAllocator of the source matrix. + * \tparam IndexAllocator_ is \e IndexAllocator of the source matrix. + * + * \param matrix is the source matrix. + * + * \return \e true if both matrices are NOT identical and \e false otherwise. + */ + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_, + typename RealAllocator_, + typename IndexAllocator_ > + bool operator != ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ) 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/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp + * \par Output + * \include MultidiagonalMatrixExample_getRow.out + * + * See \ref MultidiagonalMatrixRowView. + */ __cuda_callable__ RowView getRow( const IndexType& rowIdx ); + /** + * \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/MultidiagonalMatrix/MultidiagonalMatrixExample_getConstRow.cpp + * \par Output + * \include MultidiagonalMatrixExample_getConstRow.out + * + * See \ref MultidiagonalMatrixRowView. + */ __cuda_callable__ const RowView getRow( const IndexType& rowIdx ) const; - void setValue( const RealType& v ); + /** + * \brief Set all matrix elements to given value. + * + * \param value is the new value of all matrix elements. + */ + void setValue( const RealType& value ); + /** + * \brief Sets element at given \e row and \e column to given \e 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. \ref MultidiagonalMatrix::getRow + * or \ref MultidiagonalMatrix::forRows and \ref MultidiagonalMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp + * \par Output + * \include MultidiagonalMatrixExample_setElement.out + */ + __cuda_callable__ void setElement( const IndexType row, const IndexType column, const RealType& value ); + /** + * \brief Add element at given \e row and \e column to given \e 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. \ref MultidiagonalMatrix::getRow + * or \ref MultidiagonalMatrix::forRows and \ref MultidiagonalMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * \param thisElementMultiplicator is multiplicator the original matrix element + * value is multiplied by before addition of given \e value. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_addElement.cpp + * \par Output + * \include MultidiagonalMatrixExample_addElement.out + * + */ + __cuda_callable__ void addElement( const IndexType row, const IndexType column, const RealType& value, const RealType& thisElementMultiplicator = 1.0 ); + /** + * \brief 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. \ref MultidiagonalMatrix::getRow + * or \ref MultidiagonalMatrix::forRows and \ref MultidiagonalMatrix::forAllRows. + * + * \param row is a row index of the matrix element. + * \param column i a column index of the matrix element. + * + * \return value of given matrix element. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getElement.cpp + * \par Output + * \include MultidiagonalMatrixExample_getElement.out + * + */ + __cuda_callable__ RealType getElement( const IndexType row, const IndexType column ) const; + /** + * \brief Method for performing general reduction on matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp + * \par Output + * \include MultidiagonalMatrixExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + + /** + * \brief Method for performing general reduction on matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp + * \par Output + * \include MultidiagonalMatrixExample_rowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for performing general reduction on all matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp + * \par Output + * \include MultidiagonalMatrixExample_allRowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + + /** + * \brief Method for performing general reduction on all matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp + * \par Output + * \include MultidiagonalMatrixExample_allRowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for iteration over all matrix rows for constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixExample_forRows.out + */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ) const; + /** + * \brief Method for iteration over all matrix rows for non-constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixExample_forRows.out + */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ); + /** + * \brief This method calls \e forRows for all matrix rows (for constant instances). + * + * See \ref MultidiagonalMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cpp + * \par Output + * \include MultidiagonalMatrixExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ) const; + /** + * \brief This method calls \e forRows for all matrix rows. + * + * See \ref MultidiagonalMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cpp + * \par Output + * \include MultidiagonalMatrixExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ); - template< typename Vector > - __cuda_callable__ - typename Vector::RealType rowVectorProduct( const IndexType row, - const Vector& vector ) const; - template< typename InVector, typename OutVector > void vectorProduct( const InVector& inVector, diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index eb0cbdbe5..6df831b42 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -123,10 +123,7 @@ String MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getSerializationType() { - return String( "Matrices::MultidiagonalMatrix< " ) + - TNL::getSerializationType< RealType >() + ", [any_device], " + - TNL::getSerializationType< IndexType >() + ", " + - ( Organization ? "true" : "false" ) + ", [any_allocator], [any_allocator] >"; + return ViewType::getSerializationType(); } template< typename Real, @@ -174,10 +171,10 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > - template< typename RowLengthsVector > + template< typename RowCapacitiesVector > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -setCompressedRowLengths( const RowLengthsVector& rowLengths ) +setRowCapacities( const RowCapacitiesVector& rowLengths ) { if( max( rowLengths ) > 3 ) throw std::logic_error( "Too many non-zero elements per row in a tri-diagonal matrix." ); @@ -270,19 +267,6 @@ getCompressedRowLengths( Vector& rowLengths ) const return this->view.getCompressedRowLengths( rowLengths ); } -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization, - typename RealAllocator, - typename IndexAllocator > -Index -MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -getNonemptyRowsCount() const -{ - return this->indexer.getNonemptyRowsCount(); -} - template< typename Real, typename Device, typename Index, @@ -302,25 +286,12 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > -Index -MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -getMaxRowLength() const -{ - return this->view.getMaxRowLength(); -} - -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization, - typename RealAllocator, - typename IndexAllocator > - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > + template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_, typename IndexAllocator_ > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -setLike( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& m ) +setLike( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ) { - this->setDimensions( m.getRows(), m.getColumns(), m.getDiagonalsOffsets() ); + this->setDimensions( matrix.getRows(), matrix.getColumns(), matrix.getDiagonalsOffsets() ); } template< typename Real, @@ -331,9 +302,9 @@ template< typename Real, typename IndexAllocator > Index MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -getNumberOfNonzeroMatrixElements() const +getNonzeroElementsCount() const { - return this->view.getNumberOfNonzeroMatrixElements(); + return this->view.getNonzeroElementsCount(); } template< typename Real, @@ -355,10 +326,10 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > + template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_, typename IndexAllocator_ > bool MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -operator == ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const +operator == ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ) const { if( Organization == Organization_ ) return this->values == matrix.values; @@ -374,10 +345,10 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > + template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_, typename IndexAllocator_ > bool MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -operator != ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const +operator != ( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ) const { return ! this->operator==( matrix ); } @@ -429,6 +400,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > +__cuda_callable__ void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: setElement( const IndexType row, const IndexType column, const RealType& value ) @@ -442,6 +414,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > +__cuda_callable__ void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: addElement( const IndexType row, @@ -458,6 +431,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > +__cuda_callable__ Real MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: getElement( const IndexType row, const IndexType column ) const @@ -479,6 +453,20 @@ rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Ke this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator, + typename IndexAllocator > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +{ + this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, @@ -499,12 +487,12 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > - template< typename Function > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -forRows( IndexType first, IndexType last, Function& function ) const +allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->view.forRows( first, last, function ); + this->view.rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, @@ -513,10 +501,10 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > - template< typename Function > + template< typename Function > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -forRows( IndexType first, IndexType last, Function& function ) +forRows( IndexType first, IndexType last, Function& function ) const { this->view.forRows( first, last, function ); } @@ -527,12 +515,12 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > - template< typename Function > + template< typename Function > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -forAllRows( Function& function ) const +forRows( IndexType first, IndexType last, Function& function ) { - this->view.forRows( 0, this->getRows(), function ); + this->view.forRows( first, last, function ); } template< typename Real, @@ -544,7 +532,7 @@ template< typename Real, template< typename Function > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -forAllRows( Function& function ) +forAllRows( Function& function ) const { this->view.forRows( 0, this->getRows(), function ); } @@ -555,13 +543,12 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator, typename IndexAllocator > -template< typename Vector > -__cuda_callable__ -typename Vector::RealType + template< typename Function > +void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -rowVectorProduct( const IndexType row, const Vector& vector ) const +forAllRows( Function& function ) { - return this->view.rowVectorProduct(); + this->view.forRows( 0, this->getRows(), function ); } template< typename Real, diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.h b/src/TNL/Matrices/MultidiagonalMatrixView.h index 0ff8c07b2..26b8fd4df 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixView.h @@ -95,15 +95,18 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > void setValue( const RealType& v ); + __cuda_callable__ void setElement( const IndexType row, const IndexType column, const RealType& value ); + __cuda_callable__ void addElement( const IndexType row, const IndexType column, const RealType& value, const RealType& thisElementMultiplicator = 1.0 ); + __cuda_callable__ RealType getElement( const IndexType row, const IndexType column ) const; @@ -112,9 +115,15 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + template< typename Function > void forRows( IndexType first, IndexType last, Function& function ) const; diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index 621ef7068..bc0403bce 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -10,6 +10,7 @@ #pragma once +#include #include #include #include @@ -78,7 +79,7 @@ String MultidiagonalMatrixView< Real, Device, Index, Organization >:: getSerializationType() { - return String( "Matrices::Multidiagonal< " ) + + return String( "Matrices::MultidiagonalMatrix< " ) + TNL::getSerializationType< RealType >() + ", [any_device], " + TNL::getSerializationType< IndexType >() + ", " + TNL::getSerializationType( Organization ) + ", [any_allocator], [any_allocator] >"; @@ -217,7 +218,7 @@ MultidiagonalMatrixView< Real, Device, Index, Organization >:: setValue( const RealType& v ) { // we dont do this->values = v here because it would set even elements 'outside' the matrix - // method getNumberOfNonzeroElements would not well + // method getNumberOfNonzeroElements would not work well then const RealType newValue = v; auto f = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType columnIdx, RealType& value, bool& compute ) mutable { value = newValue; @@ -253,6 +254,7 @@ template< typename Real, typename Device, typename Index, ElementsOrganization Organization > +__cuda_callable__ void MultidiagonalMatrixView< Real, Device, Index, Organization >:: setElement( const IndexType row, const IndexType column, const RealType& value ) @@ -262,17 +264,21 @@ setElement( const IndexType row, const IndexType column, const RealType& value ) TNL_ASSERT_GE( column, 0, "" ); TNL_ASSERT_LT( column, this->getColumns(), "" ); - for( IndexType i = 0; i < hostDiagonalsOffsets.getSize(); i++ ) - if( row + hostDiagonalsOffsets[ i ] == column ) + for( IndexType i = 0; i < diagonalsOffsets.getSize(); i++ ) + if( row + diagonalsOffsets.getElement( i ) == column ) { - this->values.setElement( this->getElementIndex( row, i ), value ); + this->values.setElement( this->indexer.getGlobalIndex( row, i ), value ); return; } if( value != 0.0 ) { +#ifdef __CUDA_ARCH__ + TNL_ASSERT_TRUE( false, "" ); +#else std::stringstream msg; msg << "Wrong matrix element coordinates ( " << row << ", " << column << " ) in multidiagonal matrix."; throw std::logic_error( msg.str() ); +#endif } } @@ -280,6 +286,7 @@ template< typename Real, typename Device, typename Index, ElementsOrganization Organization > +__cuda_callable__ void MultidiagonalMatrixView< Real, Device, Index, Organization >:: addElement( const IndexType row, @@ -292,8 +299,8 @@ addElement( const IndexType row, TNL_ASSERT_GE( column, 0, "" ); TNL_ASSERT_LT( column, this->getColumns(), "" ); - for( IndexType i = 0; i < hostDiagonalsOffsets.getSize(); i++ ) - if( row + hostDiagonalsOffsets[ i ] == column ) + for( IndexType i = 0; i < diagonalsOffsets.getSize(); i++ ) + if( row + diagonalsOffsets.getElement( i ) == column ) { const Index idx = this->getElementIndex( row, i ); this->values.setElement( idx, thisElementMultiplicator * this->values.getElement( idx ) + value ); @@ -301,9 +308,13 @@ addElement( const IndexType row, } if( value != 0.0 ) { +#ifdef __CUDA_ARCH__ + TNL_ASSERT_TRUE( false, "" ); +#else std::stringstream msg; msg << "Wrong matrix element coordinates ( " << row << ", " << column << " ) in multidiagonal matrix."; throw std::logic_error( msg.str() ); +#endif } } @@ -311,6 +322,7 @@ template< typename Real, typename Device, typename Index, ElementsOrganization Organization > +__cuda_callable__ Real MultidiagonalMatrixView< Real, Device, Index, Organization >:: getElement( const IndexType row, const IndexType column ) const @@ -320,8 +332,8 @@ getElement( const IndexType row, const IndexType column ) const TNL_ASSERT_GE( column, 0, "" ); TNL_ASSERT_LT( column, this->getColumns(), "" ); - for( IndexType localIdx = 0; localIdx < hostDiagonalsOffsets.getSize(); localIdx++ ) - if( row + hostDiagonalsOffsets[ localIdx ] == column ) + for( IndexType localIdx = 0; localIdx < diagonalsOffsets.getSize(); localIdx++ ) + if( row + diagonalsOffsets.getElement( localIdx ) == column ) return this->values.getElement( this->indexer.getGlobalIndex( row, localIdx ) ); return 0.0; } @@ -370,6 +382,35 @@ rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Ke Algorithms::ParallelFor< DeviceType >::exec( first, last, f ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +MultidiagonalMatrixView< Real, Device, Index, Organization >:: +rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero_ ) +{ + using Real_ = decltype( fetch( IndexType(), IndexType(), RealType() ) ); + const auto values_view = this->values.getConstView(); + const auto diagonalsOffsets_view = this->diagonalsOffsets.getConstView(); + const IndexType diagonalsCount = this->diagonalsOffsets.getSize(); + const IndexType columns = this->getColumns(); + const auto indexer = this->indexer; + const auto zero = zero_; + auto f = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { + Real_ sum( zero ); + for( IndexType localIdx = 0; localIdx < diagonalsCount; localIdx++ ) + { + const IndexType columnIdx = rowIdx + diagonalsOffsets_view[ localIdx ]; + if( columnIdx >= 0 && columnIdx < columns ) + reduce( sum, fetch( rowIdx, columnIdx, values_view[ indexer.getGlobalIndex( rowIdx, localIdx ) ] ) ); + } + keep( rowIdx, sum ); + }; + Algorithms::ParallelFor< DeviceType >::exec( first, last, f ); +} + template< typename Real, typename Device, typename Index, @@ -382,6 +423,18 @@ allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zer this->rowsReduction( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +MultidiagonalMatrixView< Real, Device, Index, Organization >:: +allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +{ + this->rowsReduction( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, @@ -675,9 +728,13 @@ void MultidiagonalMatrixView< Real, Device, Index, Organization >::print( std::o const IndexType columnIdx = rowIdx + this->hostDiagonalsOffsets[ localIdx ]; if( columnIdx >= 0 && columnIdx < this->columns ) { - auto v = this->values.getElement( this->indexer.getGlobalIndex( rowIdx, localIdx ) ); - if( v ) - str << " Col:" << columnIdx << "->" << v << "\t"; + auto value = this->values.getElement( this->indexer.getGlobalIndex( rowIdx, localIdx ) ); + if( value ) + { + std::stringstream str_; + str_ << std::setw( 4 ) << std::right << columnIdx << ":" << std::setw( 4 ) << std::left << value; + str << std::setw( 10 ) << str_.str(); + } } } str << std::endl; diff --git a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h index 241e9fdfc..75aeda824 100644 --- a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h @@ -35,14 +35,14 @@ static const char* TEST_FILE_NAME = "test_MultidiagonalMatrixTest.tnl"; void test_GetSerializationType() { using namespace TNL::Containers::Segments; - EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, true, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, false, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, RowMajorOrder, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, RowMajorOrder, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, RowMajorOrder, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, RowMajorOrder, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, ColumnMajorOrder, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, ColumnMajorOrder, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< float, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< float, [any_device], int, ColumnMajorOrder, [any_allocator], [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::MultidiagonalMatrix< int, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::MultidiagonalMatrix< int, [any_device], int, ColumnMajorOrder, [any_allocator], [any_allocator] >" ) ); } template< typename Matrix > @@ -118,7 +118,6 @@ void test_SetElements() { 0.0, 0.0, 1.0 } } ); - std::cerr << matrix << std::endl; for( int i = 0; i < gridSize; i++ ) for( int j = 0; j < gridSize; j++ ) { @@ -151,57 +150,6 @@ void test_SetElements() } } -template< typename Matrix > -void test_GetNonemptyRowsCount() -{ - using RealType = typename Matrix::RealType; - using DeviceType = typename Matrix::DeviceType; - using IndexType = typename Matrix::IndexType; - using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; - - /* - * Sets up the following 5x8 matrix: - * - * / 1 0 0 1 0 1 0 0 \ - * | 0 1 0 0 1 0 1 0 | - * | 1 0 1 0 0 1 0 1 | - * | 0 1 0 1 0 0 1 0 | - * \ 0 0 1 0 1 0 0 1 / - */ - Matrix m1( 5, 8, DiagonalsOffsetsType({ -2, 0, 3, 5 }) ); - m1.setValue( 1.0 ); - EXPECT_EQ( m1.getNonemptyRowsCount(), 5 ); - - /* - * Sets up the following 5x5 matrix: - * - * / 1 0 0 1 0 \ - * | 0 1 0 0 1 | - * | 1 0 1 0 0 | - * | 0 1 0 1 0 | - * \ 0 0 1 0 1 / - */ - Matrix m2( 5, 5, DiagonalsOffsetsType({ -2, 0, 3, 5 }) ); - m2.setValue( 1.0 ); - EXPECT_EQ( m2.getNonemptyRowsCount(), 5 ); - - /* - * Sets up the following 8x5 matrix: - * - * / 1 0 0 1 0 \ - * | 0 1 0 0 1 | - * | 1 0 1 0 0 | - * | 0 1 0 1 0 | - * | 0 0 1 0 1 | - * | 0 0 0 1 0 | - * | 0 0 0 0 1 | - * \ 0 0 0 0 0 / - */ - Matrix m3( 8, 5, DiagonalsOffsetsType({ -2, 0, 3, 5 }) ); - m3.setValue( 1.0 ); - EXPECT_EQ( m3.getNonemptyRowsCount(), 7 ); -} - template< typename Matrix > void test_GetCompressedRowLengths() { @@ -260,7 +208,7 @@ void test_GetAllocatedElementsCount() } template< typename Matrix > -void test_GetNumberOfNonzeroMatrixElements() +void test_GetNonzeroElementsCount() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -287,7 +235,7 @@ void test_GetNumberOfNonzeroMatrixElements() m.setValue( 1.0 ); m.setElement( 0, 0, 0.0 ); - EXPECT_EQ( m.getNumberOfNonzeroMatrixElements(), 15 ); + EXPECT_EQ( m.getNonzeroElementsCount(), 15 ); } template< typename Matrix > @@ -1333,56 +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; - using DiagonalsOffsetsType = typename Matrix::DiagonalsOffsetsType; - - /* - * Sets up the following 5x4 sparse matrix: - * - * / 1 2 0 0 \ - * | 5 6 7 0 | - * | 0 10 11 12 | - * | 0 0 15 16 | - * \ 0 0 0 20 / - */ - const IndexType rows = 5; - const IndexType cols = 4; - DiagonalsOffsetsType diagonalsOffsets( { -1, 0, 1 } ); - - Matrix m( rows, cols, diagonalsOffsets ); - - RealType value = 1; - for( IndexType i = 0; i < rows; i++) - for( IndexType j = 0; j < cols; j++) - { - if( abs( i - j ) <= 1 ) - m.setElement( i, j, value ); - value++; - } - - 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\t Col:1->2\t\n" - "Row: 1 -> Col:0->5\t Col:1->6\t Col:2->7\t\n" - "Row: 2 -> Col:1->10\t Col:2->11\t Col:3->12\t\n" - "Row: 3 -> Col:2->15\t Col:3->16\t\n" - "Row: 4 -> Col:3->20\t\n"; - - EXPECT_EQ( printed.str(), couted.str() ); -} - // test fixture for typed tests template< typename Matrix > class MatrixTest : public ::testing::Test @@ -1450,13 +1348,6 @@ TYPED_TEST( MatrixTest, setElements ) test_SetElements< MatrixType >(); } -TYPED_TEST( MatrixTest, getNonemptyRowsCountTest ) -{ - using MatrixType = typename TestFixture::MatrixType; - - test_GetNonemptyRowsCount< MatrixType >(); -} - TYPED_TEST( MatrixTest, getCompressedRowLengthTest ) { using MatrixType = typename TestFixture::MatrixType; @@ -1475,7 +1366,7 @@ TYPED_TEST( MatrixTest, getNumberOfNonzeroMatrixElementsTest ) { using MatrixType = typename TestFixture::MatrixType; - test_GetNumberOfNonzeroMatrixElements< MatrixType >(); + test_GetNonzeroElementsCount< MatrixType >(); } TYPED_TEST( MatrixTest, resetTest ) @@ -1548,13 +1439,6 @@ TYPED_TEST( MatrixTest, saveAndLoadTest ) test_SaveAndLoad< MatrixType >(); } -TYPED_TEST( MatrixTest, printTest ) -{ - using MatrixType = typename TestFixture::MatrixType; - - test_Print< MatrixType >(); -} - /*TEST( MultidiagonalMatrixTest, Multidiagonal_getMatrixProductTest_Host ) { bool testRan = false; -- GitLab From 211b9f88475345bc4d2667012071bf9e11a7c6e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 3 Jun 2020 20:45:36 +0200 Subject: [PATCH 27/38] Writting documentation on MultidiagonalMatrix. --- .../MultidiagonalMatrix/CMakeLists.txt | 90 +++++++------- ...lMatrixExample_Constructor_init_list_2.cpp | 65 +++++++---- ...diagonalMatrixExample_allRowsReduction.cpp | 80 +++++++++++++ ...idiagonalMatrixExample_allRowsReduction.cu | 1 + .../MultidiagonalMatrixExample_forAllRows.cpp | 56 +++++++++ .../MultidiagonalMatrixExample_forAllRows.cu | 1 + .../MultidiagonalMatrixExample_forRows.cpp | 56 +++++++++ .../MultidiagonalMatrixExample_forRows.cu | 1 + ...lMatrixExample_getCompressedRowLengths.cpp | 51 ++++++++ ...alMatrixExample_getCompressedRowLengths.cu | 1 + ...ltidiagonalMatrixExample_rowsReduction.cpp | 80 +++++++++++++ ...ultidiagonalMatrixExample_rowsReduction.cu | 1 + ...MultidiagonalMatrixExample_setElements.cpp | 70 ++++++----- src/TNL/Matrices/MultidiagonalMatrix.h | 110 ++++++++++++++++-- src/TNL/Matrices/MultidiagonalMatrix.hpp | 4 +- src/TNL/Matrices/MultidiagonalMatrixView.hpp | 2 +- src/TNL/Matrices/SparseMatrix.h | 1 - 17 files changed, 561 insertions(+), 109 deletions(-) create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getCompressedRowLengths.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getCompressedRowLengths.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cu diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index 405990922..bd01ced9c 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -24,10 +24,10 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out OUTPUT MultidiagonalMatrixExample_setElements.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths_cuda MultidiagonalMatrixExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out -# OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths_cuda MultidiagonalMatrixExample_getCompressedRowLengths.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out + OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out ) # CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getElementsCount_cuda MultidiagonalMatrixExample_getElementsCount.cu ) # ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElementsCount_cuda > @@ -59,25 +59,25 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out OUTPUT MultidiagonalMatrixExample_getElement.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction_cuda MultidiagonalMatrixExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out -# OUTPUT MultidiagonalMatrixExample_rowsReduction.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction_cuda MultidiagonalMatrixExample_rowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out + OUTPUT MultidiagonalMatrixExample_rowsReduction.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction_cuda MultidiagonalMatrixExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out -# OUTPUT MultidiagonalMatrixExample_allRowsReduction.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction_cuda MultidiagonalMatrixExample_allRowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out + OUTPUT MultidiagonalMatrixExample_allRowsReduction.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forRows_cuda MultidiagonalMatrixExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forRows.out -# OUTPUT MultidiagonalMatrixExample_forRows.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forRows_cuda MultidiagonalMatrixExample_forRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forRows.out + OUTPUT MultidiagonalMatrixExample_forRows.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllRows_cuda MultidiagonalMatrixExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out -# OUTPUT MultidiagonalMatrixExample_forAllRows.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllRows_cuda MultidiagonalMatrixExample_forAllRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out + OUTPUT MultidiagonalMatrixExample_forAllRows.out ) # CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_constructor_cuda MultidiagonalMatrixViewExample_constructor.cu ) # ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_constructor_cuda > @@ -165,10 +165,10 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out OUTPUT MultidiagonalMatrixExample_setElements.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths MultidiagonalMatrixExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out -# OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths MultidiagonalMatrixExample_getCompressedRowLengths.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out + OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out ) # ADD_EXECUTABLE( MultidiagonalMatrixExample_getElementsCount MultidiagonalMatrixExample_getElementsCount.cpp ) # ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElementsCount > @@ -200,25 +200,25 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out OUTPUT MultidiagonalMatrixExample_getElement.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction MultidiagonalMatrixExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out -# OUTPUT MultidiagonalMatrixExample_rowsReduction.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction MultidiagonalMatrixExample_rowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out + OUTPUT MultidiagonalMatrixExample_rowsReduction.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction MultidiagonalMatrixExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out -# OUTPUT MultidiagonalMatrixExample_allRowsReduction.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction MultidiagonalMatrixExample_allRowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out + OUTPUT MultidiagonalMatrixExample_allRowsReduction.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_forRows MultidiagonalMatrixExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forRows.out -# OUTPUT MultidiagonalMatrixExample_forRows.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_forRows MultidiagonalMatrixExample_forRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forRows.out + OUTPUT MultidiagonalMatrixExample_forRows.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllRows MultidiagonalMatrixExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out -# OUTPUT MultidiagonalMatrixExample_forAllRows.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllRows MultidiagonalMatrixExample_forAllRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out + OUTPUT MultidiagonalMatrixExample_forAllRows.out ) # ADD_EXECUTABLE( MultidiagonalMatrixViewExample_constructor MultidiagonalMatrixViewExample_constructor.cpp ) # ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_constructor > @@ -288,17 +288,17 @@ ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS MultidiagonalMatrixExample_Constructor_init_list_2.out MultidiagonalMatrixExample_getSerializationType.out MultidiagonalMatrixExample_setElements.out -# MultidiagonalMatrixExample_getCompressedRowLengths.out + MultidiagonalMatrixExample_getCompressedRowLengths.out # MultidiagonalMatrixExample_getElementsCount.out MultidiagonalMatrixExample_getConstRow.out MultidiagonalMatrixExample_getRow.out MultidiagonalMatrixExample_setElement.out MultidiagonalMatrixExample_addElement.out MultidiagonalMatrixExample_getElement.out -# MultidiagonalMatrixExample_rowsReduction.out -# MultidiagonalMatrixExample_allRowsReduction.out -# MultidiagonalMatrixExample_forRows.out -# MultidiagonalMatrixExample_forAllRows.out + MultidiagonalMatrixExample_rowsReduction.out + MultidiagonalMatrixExample_allRowsReduction.out + MultidiagonalMatrixExample_forRows.out + MultidiagonalMatrixExample_forAllRows.out # MultidiagonalMatrixViewExample_constructor.out # MultidiagonalMatrixViewExample_getCompressedRowLengths.out # MultidiagonalMatrixViewExample_getElementsCount.out diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp index 158750525..60bcde7fc 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp @@ -6,39 +6,54 @@ template< typename Device > -void laplaceOperatorMatrix() +void createMultidiagonalMatrix() { - const int gridSize( 4 ); - const int matrixSize = gridSize * gridSize; + const int matrixSize = 6; + + /*** + * Setup the following matrix (dots represent zeros): + * + * / 4 -1 . -1 . . \ + * | -1 4 -1 . -1 . | + * | . -1 4 -1 . -1 | + * | -1 . -1 4 -1 . | + * | . -1 . -1 4 -1 | + * \ . . 1 . -1 4 / + * + * The diagonals offsets are { -3, -1, 0, 1, 3 }. + */ TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( - matrixSize, { - gridSize, -1, 0, 1, gridSize }, { - { 0.0, 0.0, 1.0 }, // set matrix elements corresponding to boundary grid nodes - { 0.0, 0.0, 1.0 }, // and Dirichlet boundary conditions, i.e. 1 on the main diagonal - { 0.0, 0.0, 1.0 }, // which is the third one - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, // set matrix elements corresponding to inner grid nodes, i.e.4 on the main diagonal - { -1.0, -1.0, 4.0, -1.0, -1.0 }, // (the third one) and -1 to the other sub-diagonals - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 } + matrixSize, { -3, -1, 0, 1, 3 }, { + /*** + * To set the matrix elements we first extend the diagonals to their full + * lengths even outside the matrix (dots represent zeros and zeros are + * artificial zeros used for memory alignment): + * + * 0 . 0 / 4 -1 . -1 . . \ -> { 0, 0, 4, -1, -1 } + * . 0 . | -1 4 -1 . -1 . | . -> { 0, -1, 4, -1, -1 } + * . . 0 | . -1 4 -1 . -1 | . . -> { 0, -1, 4, -1, -1 } + * . . | -1 . -1 4 -1 . | 0 . . -> { -1, -1, 4, -1, 0 } + * . | . -1 . -1 4 -1 | . 0 . . -> { -1, -1, 4, -1, 0 } + * \ . . 1 . -1 4 / 0 . 0 . . -> { -1, -1, 4, 0, 0 } + * + */ + { 0, 0, 4, -1, -1 }, + { 0, -1, 4, -1, -1 }, + { 0, -1, 4, -1, -1 }, + { -1, -1, 4, -1, 0 }, + { -1, -1, 4, -1, 0 }, + { -1, -1, 4, 0, 0 } } ); - std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; } int main( int argc, char* argv[] ) { - std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl; - laplaceOperatorMatrix< TNL::Devices::Host >(); + std::cout << "Create multidiagonal matrix on CPU ... " << std::endl; + createMultidiagonalMatrix< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; - laplaceOperatorMatrix< TNL::Devices::Cuda >(); + std::cout << "Creating multidiagonal matrix on CUDA GPU ... " << std::endl; + createMultidiagonalMatrix< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp new file mode 100644 index 000000000..4b102c73b --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void allRowsReduction() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + 5, // number of matrix columns + { -2, -1, 0 }, // diagonals offsets + { { 0, 0, 1 }, // matrix elements + { 0, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 } } ); + + /*** + * 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.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Rows reduction on host:" << std::endl; + allRowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + allRowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cu new file mode 120000 index 000000000..c8659a5f4 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cpp new file mode 100644 index 000000000..0114acf63 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +template< typename Device > +void forAllRowsExample() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + 5, // number of matrix rows + 5, // number of matrix columns + { -2, -1, 0 } ); // matrix diagonals offsets + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) { + /*** + * 'forRows' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ------- + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + */ + value = 3 - localIdx; + }; + matrix.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forAllRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forAllRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cu new file mode 120000 index 000000000..b18e48f2b --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_forAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp new file mode 100644 index 000000000..07382c2e3 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + 5, // number of matrix rows + 5, // number of matrix columns + { -2, -1, 0 } ); // matrix diagonals offsets + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) { + /*** + * 'forRows' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ------- + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + */ + value = 3 - localIdx; + }; + matrix.forRows( 0, matrix.getRows(), f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cu new file mode 120000 index 000000000..aff0dad0c --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getCompressedRowLengths.cpp new file mode 100644 index 000000000..5617b514d --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getCompressedRowLengths.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void laplaceOperatorMatrix() +{ + const int gridSize( 4 ); + const int matrixSize = gridSize * gridSize; + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize, // number of columns + { - gridSize, -1, 0, 1, gridSize } // diagonals offsets + ); + matrix.setElements( { + { 0.0, 0.0, 1.0 }, // set matrix elements corresponding to boundary grid nodes + { 0.0, 0.0, 1.0 }, // and Dirichlet boundary conditions, i.e. 1 on the main diagonal + { 0.0, 0.0, 1.0 }, // which is the third one + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, // set matrix elements corresponding to inner grid nodes, i.e. 4 on the main diagonal + { -1.0, -1.0, 4.0, -1.0, -1.0 }, // (the third one) and -1 to the other sub-diagonals + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 } + } ); + TNL::Containers::Vector< int, Device > rowLengths; + matrix.getCompressedRowLengths( rowLengths ); + std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; + std::cout << "Compressed row lengths: " << rowLengths << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getCompressedRowLengths.cu new file mode 120000 index 000000000..b711bdfdf --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getCompressedRowLengths.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_getCompressedRowLengths.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp new file mode 100644 index 000000000..dc3d40483 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void rowsReduction() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + 5, // number of matrix columns + { -2, -1, 0 }, // diagonals offsets + { { 0, 0, 1 }, // matrix elements + { 0, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 } } ); + + /*** + * 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.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Rows reduction on host:" << std::endl; + rowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + rowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cu new file mode 120000 index 000000000..adaff28e8 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cpp index b868a611c..b43f2e341 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cpp @@ -6,43 +6,57 @@ template< typename Device > -void laplaceOperatorMatrix() +void createMultidiagonalMatrix() { - const int gridSize( 4 ); - const int matrixSize = gridSize * gridSize; + const int matrixSize = 6; + + /*** + * Setup the following matrix (dots represent zeros): + * + * / 4 -1 . -1 . . \ + * | -1 4 -1 . -1 . | + * | . -1 4 -1 . -1 | + * | -1 . -1 4 -1 . | + * | . -1 . -1 4 -1 | + * \ . . 1 . -1 4 / + * + * The diagonals offsets are { -3, -1, 0, 1, 3 }. + */ TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( - matrixSize, // number of rows - matrixSize, // number of columns - { - gridSize, -1, 0, 1, gridSize } // diagonals offsets - ); + matrixSize, // number of matrix rows + matrixSize, // number of matrix columns + { -3, -1, 0, 1, 3 } ); // matrix diagonals offsets matrix.setElements( { - { 0.0, 0.0, 1.0 }, // set matrix elements corresponding to boundary grid nodes - { 0.0, 0.0, 1.0 }, // and Dirichlet boundary conditions, i.e. 1 on the main diagonal - { 0.0, 0.0, 1.0 }, // which is the third one - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, // set matrix elements corresponding to inner grid nodes, i.e.4 on the main diagonal - { -1.0, -1.0, 4.0, -1.0, -1.0 }, // (the third one) and -1 to the other sub-diagonals - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 } + /*** + * To set the matrix elements we first extend the diagonals to their full + * lengths even outside the matrix (dots represent zeros and zeros are + * artificial zeros used for memory alignment): + * + * 0 . 0 / 4 -1 . -1 . . \ -> { 0, 0, 4, -1, -1 } + * . 0 . | -1 4 -1 . -1 . | . -> { 0, -1, 4, -1, -1 } + * . . 0 | . -1 4 -1 . -1 | . . -> { 0, -1, 4, -1, -1 } + * . . | -1 . -1 4 -1 . | 0 . . -> { -1, -1, 4, -1, 0 } + * . | . -1 . -1 4 -1 | . 0 . . -> { -1, -1, 4, -1, 0 } + * \ . . 1 . -1 4 / 0 . 0 . . -> { -1, -1, 4, 0, 0 } + * + */ + { 0, 0, 4, -1, -1 }, + { 0, -1, 4, -1, -1 }, + { 0, -1, 4, -1, -1 }, + { -1, -1, 4, -1, 0 }, + { -1, -1, 4, -1, 0 }, + { -1, -1, 4, 0, 0 } } ); - std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; } int main( int argc, char* argv[] ) { - std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl; - laplaceOperatorMatrix< TNL::Devices::Host >(); + std::cout << "Create multidiagonal matrix on CPU ... " << std::endl; + createMultidiagonalMatrix< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; - laplaceOperatorMatrix< TNL::Devices::Cuda >(); + std::cout << "Creating multidiagonal matrix on CUDA GPU ... " << std::endl; + createMultidiagonalMatrix< TNL::Devices::Cuda >(); #endif } diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index e152c8cda..b58c2709b 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -25,7 +25,36 @@ namespace Matrices { * * Use this matrix type for storing of matrices where the offsets of non-zero elements * from the diagonal are the same in each row. Typically such matrices arise from - * discretization of partial differential equations on regular numerical grids. + * discretization of partial differential equations on regular numerical grids. This is + * one example (dots represent zero matrix elements): + * + * \f[ + * \left( + * \begin{array}{ccccccc} + * 4 & -1 & . & -1 & . & . \\ + * -1 & 4 & -1 & . & -1 & . \\ + * . & -1 & 4 & -1 & . & -1 \\ + * -1 & . & -1 & 4 & -1 & . \\ + * . & -1 & . & -1 & 4 & -1 \\ + * . & . & -1 & . & -1 & 4 + * \end{array} + * \right) + * \f] + * + * In this matrix, the column indexes in each row \f$i\f$ can be expressed as + * \f$\{i-3, i-1, i, i+1, i+3\}\f$ (where the resulting index is non-negative and + * smaller than the number of matrix columns). Therefore the diagonals offsets + * 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. + * + * 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% * * \tparam Real is a type of matrix elements. * \tparam Device is a device where the matrix is allocated. @@ -171,7 +200,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Constructor with matrix dimensions, diagonals offsets and matrix elements. * - * The number of matrix rows is given by the size of the initializer list \e data. + * The number of matrix rows is deduced from the size of the initializer list \e data. * * \param columns is number of matrix columns. * \param diagonalOffsets are offsets of sub-diagonals from the main diagonal. @@ -705,6 +734,28 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename Function > void forAllRows( Function& function ); + /** + * \brief Computes product of matrix and vector. + * + * More precisely, it computes: + * + * `outVector = matrixMultiplicator * ( * this ) * inVector + outVectorMultiplicator * outVector` + * + * \tparam InVector is type of input vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * \tparam OutVector is type of output vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * + * \param inVector is input vector. + * \param outVector is output vector. + * \param matrixMultiplicator is a factor by which the matrix is multiplied. It is one by default. + * \param outVectorMultiplicator is a factor by which the outVector is multiplied before added + * to the result of matrix-vector product. It is zero by default. + * \param begin is the beginning of the rows range for which the vector product + * is computed. It is zero by default. + * \param end is the end of the rows range for which the vector product + * is computed. It is number if the matrix rows by default. + */ template< typename InVector, typename OutVector > void vectorProduct( const InVector& inVector, @@ -726,10 +777,20 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > Vector2& x, const RealType& omega = 1.0 ) const; - // copy assignment + /** + * \brief Assignment of exactly the same matrix type. + * + * \param matrix is input matrix for the assignment. + * \return reference to this matrix. + */ MultidiagonalMatrix& operator=( const MultidiagonalMatrix& matrix ); - // cross-device copy assignment + /** + * \brief Assignment of another multidiagonal matrix + * + * \param matrix is input matrix for the assignment. + * \return reference to this matrix. + */ template< typename Real_, typename Device_, typename Index_, @@ -738,18 +799,53 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > typename IndexAllocator_ > MultidiagonalMatrix& operator=( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_, IndexAllocator_ >& matrix ); + /** + * \brief Method for saving the matrix to a file. + * + * \param fileName is name of the file. + */ void save( File& file ) const; + /** + * \brief Method for loading the matrix from a file. + * + * \param fileName is name of the file. + */ void load( File& file ); + /** + * \brief Method for saving the matrix to the file with given filename. + * + * \param fileName is name of the file. + */ void save( const String& fileName ) const; + /** + * \brief Method for loading the matrix from the file with given filename. + * + * \param fileName is name of the file. + */ void load( const String& fileName ); + /** + * \brief Method for printing the matrix to output stream. + * + * \param str is the output stream. + */ void print( std::ostream& str ) const; + /** + * \brief This method returns matrix elements indexer used by this matrix. + * + * \return constant reference to the indexer. + */ const IndexerType& getIndexer() const; + /** + * \brief This method returns matrix elements indexer used by this matrix. + * + * \return non-constant reference to the indexer. + */ IndexerType& getIndexer(); __cuda_callable__ @@ -757,9 +853,9 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > protected: - __cuda_callable__ - IndexType getElementIndex( const IndexType row, - const IndexType localIdx ) const; + //__cuda_callable__ + //IndexType getElementIndex( const IndexType row, + // const IndexType localIdx ) const; DiagonalsOffsetsType diagonalsOffsets; diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index 6df831b42..e06e011fd 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -865,7 +865,7 @@ getIndexer() -> IndexerType& return this->indexer; } -template< typename Real, +/*template< typename Real, typename Device, typename Index, ElementsOrganization Organization, @@ -883,7 +883,7 @@ getElementIndex( const IndexType row, const IndexType column ) const TNL_ASSERT_LT( localIdx, 3, "" ); return this->indexer.getGlobalIndex( row, localIdx ); -} +}*/ template< typename Real, typename Device, diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index bc0403bce..a29286e71 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -404,7 +404,7 @@ rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Ke { const IndexType columnIdx = rowIdx + diagonalsOffsets_view[ localIdx ]; if( columnIdx >= 0 && columnIdx < columns ) - reduce( sum, fetch( rowIdx, columnIdx, values_view[ indexer.getGlobalIndex( rowIdx, localIdx ) ] ) ); + sum = reduce( sum, fetch( rowIdx, columnIdx, values_view[ indexer.getGlobalIndex( rowIdx, localIdx ) ] ) ); } keep( rowIdx, sum ); }; diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 6125df8d9..268af8a33 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -842,7 +842,6 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param fileName is name of the file. */ void save( const String& fileName ) const; - /** * \brief Method for loading the matrix from the file with given filename. -- GitLab From 53d3728d49c1239242b42bdcc2d3c6087cc99702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 4 Jun 2020 17:17:56 +0200 Subject: [PATCH 28/38] Writting documentation on multidiagonal matrix. --- .../MultidiagonalMatrix/CMakeLists.txt | 249 ++++----- ...lMatrixExample_Constructor_init_list_1.cpp | 2 +- .../MultidiagonalMatrixExample_setElement.cpp | 2 +- ...tidiagonalMatrixViewExample_addElement.cpp | 40 ++ ...ltidiagonalMatrixViewExample_addElement.cu | 1 + ...onalMatrixViewExample_allRowsReduction.cpp | 81 +++ ...gonalMatrixViewExample_allRowsReduction.cu | 1 + ...tidiagonalMatrixViewExample_forAllRows.cpp | 57 ++ ...ltidiagonalMatrixViewExample_forAllRows.cu | 1 + ...MultidiagonalMatrixViewExample_forRows.cpp | 57 ++ .../MultidiagonalMatrixViewExample_forRows.cu | 1 + ...rixViewExample_getCompressedRowLengths.cpp | 52 ++ ...trixViewExample_getCompressedRowLengths.cu | 1 + ...idiagonalMatrixViewExample_getConstRow.cpp | 49 ++ ...tidiagonalMatrixViewExample_getConstRow.cu | 1 + ...tidiagonalMatrixViewExample_getElement.cpp | 39 ++ ...ltidiagonalMatrixViewExample_getElement.cu | 1 + .../MultidiagonalMatrixViewExample_getRow.cpp | 46 ++ .../MultidiagonalMatrixViewExample_getRow.cu | 1 + ...iagonalMatrixViewExample_rowsReduction.cpp | 81 +++ ...diagonalMatrixViewExample_rowsReduction.cu | 1 + ...tidiagonalMatrixViewExample_setElement.cpp | 47 ++ ...ltidiagonalMatrixViewExample_setElement.cu | 1 + src/TNL/Matrices/MultidiagonalMatrix.h | 21 +- src/TNL/Matrices/MultidiagonalMatrix.hpp | 148 +----- src/TNL/Matrices/MultidiagonalMatrixView.h | 499 +++++++++++++++++- src/TNL/Matrices/MultidiagonalMatrixView.hpp | 64 +-- src/TNL/Matrices/SparseMatrixView.h | 12 +- 28 files changed, 1197 insertions(+), 359 deletions(-) create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_addElement.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_addElement.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllRows.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllRows.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getCompressedRowLengths.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getConstRow.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getConstRow.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getElement.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getElement.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_setElement.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_setElement.cu diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index bd01ced9c..e20d8a6d0 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -29,11 +29,6 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getElementsCount_cuda MultidiagonalMatrixExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElementsCount.out -# OUTPUT MultidiagonalMatrixExample_getElementsCount.out ) - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getConstRow_cuda MultidiagonalMatrixExample_getConstRow.cu ) ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getConstRow_cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getConstRow.out @@ -79,65 +74,55 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out OUTPUT MultidiagonalMatrixExample_forAllRows.out ) -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_constructor_cuda MultidiagonalMatrixViewExample_constructor.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_constructor_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_constructor.out -# OUTPUT MultidiagonalMatrixViewExample_constructor.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getCompressedRowLengths_cuda MultidiagonalMatrixViewExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getCompressedRowLengths.out -# OUTPUT MultidiagonalMatrixViewExample_getCompressedRowLengths.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElementsCount_cuda MultidiagonalMatrixViewExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElementsCount.out -# OUTPUT MultidiagonalMatrixViewExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getConstRow_cuda MultidiagonalMatrixViewExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getConstRow.out -# OUTPUT MultidiagonalMatrixViewExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getRow_cuda MultidiagonalMatrixViewExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getRow.out -# OUTPUT MultidiagonalMatrixViewExample_getRow.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_setElement_cuda MultidiagonalMatrixViewExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_setElement.out -# OUTPUT MultidiagonalMatrixViewExample_setElement.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_addElement_cuda MultidiagonalMatrixViewExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_addElement.out -# OUTPUT MultidiagonalMatrixViewExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElement_cuda MultidiagonalMatrixViewExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElement.out -# OUTPUT MultidiagonalMatrixViewExample_getElement.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_rowsReduction_cuda MultidiagonalMatrixViewExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_rowsReduction.out -# OUTPUT MultidiagonalMatrixViewExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_allRowsReduction_cuda MultidiagonalMatrixViewExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_allRowsReduction.out -# OUTPUT MultidiagonalMatrixViewExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forRows_cuda MultidiagonalMatrixViewExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forRows.out -# OUTPUT MultidiagonalMatrixViewExample_forRows.out ) - -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forAllRows_cuda MultidiagonalMatrixViewExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forAllRows.out -# OUTPUT MultidiagonalMatrixViewExample_forAllRows.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getCompressedRowLengths_cuda MultidiagonalMatrixViewExample_getCompressedRowLengths.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getCompressedRowLengths_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getCompressedRowLengths.out + OUTPUT MultidiagonalMatrixViewExample_getCompressedRowLengths.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getConstRow_cuda MultidiagonalMatrixViewExample_getConstRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getConstRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getConstRow.out + OUTPUT MultidiagonalMatrixViewExample_getConstRow.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getRow_cuda MultidiagonalMatrixViewExample_getRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getRow.out + OUTPUT MultidiagonalMatrixViewExample_getRow.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_setElement_cuda MultidiagonalMatrixViewExample_setElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_setElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_setElement.out + OUTPUT MultidiagonalMatrixViewExample_setElement.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_addElement_cuda MultidiagonalMatrixViewExample_addElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_addElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_addElement.out + OUTPUT MultidiagonalMatrixViewExample_addElement.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElement_cuda MultidiagonalMatrixViewExample_getElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElement.out + OUTPUT MultidiagonalMatrixViewExample_getElement.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_rowsReduction_cuda MultidiagonalMatrixViewExample_rowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_rowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_rowsReduction.out + OUTPUT MultidiagonalMatrixViewExample_rowsReduction.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_allRowsReduction_cuda MultidiagonalMatrixViewExample_allRowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_allRowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_allRowsReduction.out + OUTPUT MultidiagonalMatrixViewExample_allRowsReduction.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forRows_cuda MultidiagonalMatrixViewExample_forRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forRows.out + OUTPUT MultidiagonalMatrixViewExample_forRows.out ) + + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forAllRows_cuda MultidiagonalMatrixViewExample_forAllRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forAllRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forAllRows.out + OUTPUT MultidiagonalMatrixViewExample_forAllRows.out ) ELSE() ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor MultidiagonalMatrixExample_Constructor.cpp ) @@ -170,11 +155,6 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixExample_getElementsCount MultidiagonalMatrixExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElementsCount.out -# OUTPUT MultidiagonalMatrixExample_getElementsCount.out ) - ADD_EXECUTABLE( MultidiagonalMatrixExample_getConstRow MultidiagonalMatrixExample_getConstRow.cpp ) ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getConstRow > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getConstRow.out @@ -220,65 +200,55 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out OUTPUT MultidiagonalMatrixExample_forAllRows.out ) -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_constructor MultidiagonalMatrixViewExample_constructor.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_constructor > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_constructor.out -# OUTPUT MultidiagonalMatrixViewExample_constructor.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getCompressedRowLengths MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getCompressedRowLengths.out -# OUTPUT MultidiagonalMatrixViewExample_getCompressedRowLengths.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElementsCount MultidiagonalMatrixViewExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElementsCount.out -# OUTPUT MultidiagonalMatrixViewExample_getElementsCount.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getConstRow MultidiagonalMatrixViewExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getConstRow.out -# OUTPUT MultidiagonalMatrixViewExample_getConstRow.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getRow MultidiagonalMatrixViewExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getRow.out -# OUTPUT MultidiagonalMatrixViewExample_getRow.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_setElement MultidiagonalMatrixViewExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_setElement.out -# OUTPUT MultidiagonalMatrixViewExample_setElement.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_addElement MultidiagonalMatrixViewExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_addElement.out -# OUTPUT MultidiagonalMatrixViewExample_addElement.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElement MultidiagonalMatrixViewExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElement.out -# OUTPUT MultidiagonalMatrixViewExample_getElement.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_rowsReduction MultidiagonalMatrixViewExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_rowsReduction.out -# OUTPUT MultidiagonalMatrixViewExample_rowsReduction.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_allRowsReduction MultidiagonalMatrixViewExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_allRowsReduction.out -# OUTPUT MultidiagonalMatrixViewExample_allRowsReduction.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forRows MultidiagonalMatrixViewExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forRows.out -# OUTPUT MultidiagonalMatrixViewExample_forRows.out ) - -# ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forAllRows MultidiagonalMatrixViewExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forAllRows.out -# OUTPUT MultidiagonalMatrixViewExample_forAllRows.out ) + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getCompressedRowLengths MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getCompressedRowLengths > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getCompressedRowLengths.out + OUTPUT MultidiagonalMatrixViewExample_getCompressedRowLengths.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getConstRow MultidiagonalMatrixViewExample_getConstRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getConstRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getConstRow.out + OUTPUT MultidiagonalMatrixViewExample_getConstRow.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getRow MultidiagonalMatrixViewExample_getRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getRow.out + OUTPUT MultidiagonalMatrixViewExample_getRow.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_setElement MultidiagonalMatrixViewExample_setElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_setElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_setElement.out + OUTPUT MultidiagonalMatrixViewExample_setElement.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_addElement MultidiagonalMatrixViewExample_addElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_addElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_addElement.out + OUTPUT MultidiagonalMatrixViewExample_addElement.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElement MultidiagonalMatrixViewExample_getElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElement.out + OUTPUT MultidiagonalMatrixViewExample_getElement.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_rowsReduction MultidiagonalMatrixViewExample_rowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_rowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_rowsReduction.out + OUTPUT MultidiagonalMatrixViewExample_rowsReduction.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_allRowsReduction MultidiagonalMatrixViewExample_allRowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_allRowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_allRowsReduction.out + OUTPUT MultidiagonalMatrixViewExample_allRowsReduction.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forRows MultidiagonalMatrixViewExample_forRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forRows.out + OUTPUT MultidiagonalMatrixViewExample_forRows.out ) + + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forAllRows MultidiagonalMatrixViewExample_forAllRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forAllRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forAllRows.out + OUTPUT MultidiagonalMatrixViewExample_forAllRows.out ) ENDIF() @@ -289,7 +259,6 @@ ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS MultidiagonalMatrixExample_getSerializationType.out MultidiagonalMatrixExample_setElements.out MultidiagonalMatrixExample_getCompressedRowLengths.out -# MultidiagonalMatrixExample_getElementsCount.out MultidiagonalMatrixExample_getConstRow.out MultidiagonalMatrixExample_getRow.out MultidiagonalMatrixExample_setElement.out @@ -299,17 +268,15 @@ ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS MultidiagonalMatrixExample_allRowsReduction.out MultidiagonalMatrixExample_forRows.out MultidiagonalMatrixExample_forAllRows.out -# MultidiagonalMatrixViewExample_constructor.out -# MultidiagonalMatrixViewExample_getCompressedRowLengths.out -# MultidiagonalMatrixViewExample_getElementsCount.out -# MultidiagonalMatrixViewExample_getConstRow.out -# MultidiagonalMatrixViewExample_getRow.out -# MultidiagonalMatrixViewExample_setElement.out -# MultidiagonalMatrixViewExample_addElement.out -# MultidiagonalMatrixViewExample_getElement.out -# MultidiagonalMatrixViewExample_rowsReduction.out -# MultidiagonalMatrixViewExample_allRowsReduction.out -# MultidiagonalMatrixViewExample_forRows.out -# MultidiagonalMatrixViewExample_forAllRows.out + MultidiagonalMatrixViewExample_getCompressedRowLengths.out + MultidiagonalMatrixViewExample_getConstRow.out + MultidiagonalMatrixViewExample_getRow.out + MultidiagonalMatrixViewExample_setElement.out + MultidiagonalMatrixViewExample_addElement.out + MultidiagonalMatrixViewExample_getElement.out + MultidiagonalMatrixViewExample_rowsReduction.out + MultidiagonalMatrixViewExample_allRowsReduction.out + MultidiagonalMatrixViewExample_forRows.out + MultidiagonalMatrixViewExample_forAllRows.out ) diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp index 90c331bab..1056ab9c6 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp @@ -44,6 +44,6 @@ int main( int argc, char* argv[] ) #ifdef HAVE_CUDA std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; - initializerListExample< TNL::Devices::Cuda >(); + laplaceOperatorMatrix< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp index d900261a5..8dee79f47 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp @@ -33,7 +33,7 @@ void setElements() * MultidiagonalMatrixView::getRow example for details. */ TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); - TNL::Algorithms::ParallelFor< Device >::exec( 0, 5, f ); + TNL::Algorithms::ParallelFor< Device >::exec( 0, matrixSize, f ); std::cout << "Matrix set from its native device:" << std::endl; std::cout << *matrix << std::endl; diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_addElement.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_addElement.cpp new file mode 100644 index 000000000..46f92d7a3 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_addElement.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +template< typename Device > +void addElements() +{ + const int matrixSize( 5 ); + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize, // number of columns + { -1, 0, 1 } ); // diagonals offsets + auto view = matrix.getView(); + for( int i = 0; i < matrixSize; i++ ) + view.setElement( i, i, i ); + + std::cout << "Initial matrix is: " << std::endl << matrix << std::endl; + + for( int i = 0; i < matrixSize; i++ ) + { + if( i > 0 ) + view.addElement( i, i - 1, 1.0, 5.0 ); + view.addElement( i, i, 1.0, 5.0 ); + if( i < matrixSize - 1 ) + view.addElement( i, i + 1, 1.0, 5.0 ); + } + + std::cout << "Matrix after addition is: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Add elements on host:" << std::endl; + addElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Add elements on CUDA device:" << std::endl; + addElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_addElement.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_addElement.cu new file mode 120000 index 000000000..7d6a43822 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_addElement.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp new file mode 100644 index 000000000..26dac464c --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void allRowsReduction() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + 5, // number of matrix columns + { -2, -1, 0 }, // diagonals offsets + { { 0, 0, 1 }, // matrix elements + { 0, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 } } ); + auto view = matrix.getView(); + + /*** + * Find largest element in each row. + */ + TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() ); + + /*** + * Prepare vector view for lambdas. + */ + auto rowMaxView = rowMax.getView(); + + /*** + * Fetch lambda just returns absolute value of matrix elements. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx, int columnIdx, const double& value ) -> double { + return TNL::abs( value ); + }; + + /*** + * Reduce lambda return maximum of given values. + */ + auto reduce = [=] __cuda_callable__ ( double& a, const double& b ) -> double { + return TNL::max( a, b ); + }; + + /*** + * Keep lambda store the largest value in each row to the vector rowMax. + */ + auto keep = [=] __cuda_callable__ ( int rowIdx, const double& value ) mutable { + rowMaxView[ rowIdx ] = value; + }; + + /*** + * Compute the largest values in each row. + */ + view.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Rows reduction on host:" << std::endl; + allRowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + allRowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cu new file mode 120000 index 000000000..5e14692de --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllRows.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllRows.cpp new file mode 100644 index 000000000..143aa864c --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllRows.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +template< typename Device > +void forAllRowsExample() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + 5, // number of matrix rows + 5, // number of matrix columns + { -2, -1, 0 } ); // matrix diagonals offsets + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) { + /*** + * 'forRows' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ------- + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + */ + value = 3 - localIdx; + }; + view.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forAllRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forAllRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllRows.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllRows.cu new file mode 120000 index 000000000..2138ba26b --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllRows.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_forAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp new file mode 100644 index 000000000..23aa06753 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + 5, // number of matrix rows + 5, // number of matrix columns + { -2, -1, 0 } ); // matrix diagonals offsets + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) { + /*** + * 'forRows' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ------- + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + */ + value = 3 - localIdx; + }; + view.forRows( 0, matrix.getRows(), f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cu new file mode 120000 index 000000000..ec3f1ad70 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp new file mode 100644 index 000000000..2b366ab3d --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void laplaceOperatorMatrix() +{ + const int gridSize( 4 ); + const int matrixSize = gridSize * gridSize; + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize, // number of columns + { - gridSize, -1, 0, 1, gridSize } // diagonals offsets + ); + matrix.setElements( { + { 0.0, 0.0, 1.0 }, // set matrix elements corresponding to boundary grid nodes + { 0.0, 0.0, 1.0 }, // and Dirichlet boundary conditions, i.e. 1 on the main diagonal + { 0.0, 0.0, 1.0 }, // which is the third one + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, // set matrix elements corresponding to inner grid nodes, i.e. 4 on the main diagonal + { -1.0, -1.0, 4.0, -1.0, -1.0 }, // (the third one) and -1 to the other sub-diagonals + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 } + } ); + auto view = matrix.getView(); + TNL::Containers::Vector< int, Device > rowLengths; + view.getCompressedRowLengths( rowLengths ); + std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; + std::cout << "Compressed row lengths: " << rowLengths << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getCompressedRowLengths.cu new file mode 120000 index 000000000..77d01be55 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getCompressedRowLengths.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getConstRow.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getConstRow.cpp new file mode 100644 index 000000000..748c95665 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getConstRow.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + const int matrixSize = 5; + auto diagonalsOffsets = { -2, -1, 0 }; + using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + MatrixType matrix ( + matrixSize, // number of matrix columns + diagonalsOffsets, + { { 0.0, 0.0, 1.0 }, // matrix elements + { 0.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 } } ); + auto view = matrix.getView(); + + /*** + * Fetch lambda function returns diagonal element in each row. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { + auto row = view.getRow( rowIdx ); + return row.getValue( 2 ); // get value from subdiagonal with index 2, i.e. the main diagonal + }; + + /*** + * Compute the matrix trace. + */ + int trace = TNL::Algorithms::Reduction< Device >::reduce( matrix.getRows(), std::plus<>{}, fetch, 0 ); + std::cout << "Matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Matrix trace is: " << trace << "." << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Getting matrix rows on host: " << std::endl; + getRowExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getConstRow.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getConstRow.cu new file mode 120000 index 000000000..bb31b7a5d --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getConstRow.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_getConstRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getElement.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getElement.cpp new file mode 100644 index 000000000..bf1226662 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getElement.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +template< typename Device > +void getElements() +{ + const int matrixSize( 5 ); + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + matrixSize, // number of matrix columns + { -1, 0, 1 }, // matrix diagonals offsets + { // matrix elements definition + { 0.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, 0.0 } + } ); + auto view = matrix.getView(); + + for( int i = 0; i < matrixSize; i++ ) + { + for( int j = 0; j < matrixSize; j++ ) + std::cout << std::setw( 5 ) << view.getElement( i, j ); + std::cout << std::endl; + } +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get elements on host:" << std::endl; + getElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get elements on CUDA device:" << std::endl; + getElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getElement.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getElement.cu new file mode 120000 index 000000000..a246e2dd9 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getElement.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_getElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cpp new file mode 100644 index 000000000..ac322f9aa --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + const int matrixSize( 5 ); + auto diagonalsOffsets = { -1, 0, 1 }; // Variadic templates in SharedPointer + // constructor do not recognize initializer + // list so we give it a hint. + using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + MatrixType matrix( + matrixSize, // number of matrix rows + matrixSize, // number of matrix columns + diagonalsOffsets ); + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { + auto row = view.getRow( rowIdx ); + if( rowIdx > 0 ) + row.setElement( 0, -1.0 ); // elements below the diagonal + row.setElement( 1, 2.0 ); // elements on the diagonal + if( rowIdx < matrixSize - 1 ) // elements above the diagonal + row.setElement( 2, -1.0 ); + }; + + /*** + * Set the matrix elements. + */ + TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix.getRows(), f ); + std::cout << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Getting matrix rows on host: " << std::endl; + getRowExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cu new file mode 120000 index 000000000..2ac03ebc3 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_getRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp new file mode 100644 index 000000000..6793d8bb2 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void rowsReduction() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + 5, // number of matrix columns + { -2, -1, 0 }, // diagonals offsets + { { 0, 0, 1 }, // matrix elements + { 0, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 } } ); + auto view = matrix.getView(); + + /*** + * Find largest element in each row. + */ + TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() ); + + /*** + * Prepare vector view for lambdas. + */ + auto rowMaxView = rowMax.getView(); + + /*** + * Fetch lambda just returns absolute value of matrix elements. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx, int columnIdx, const double& value ) -> double { + return TNL::abs( value ); + }; + + /*** + * Reduce lambda return maximum of given values. + */ + auto reduce = [=] __cuda_callable__ ( double& a, const double& b ) -> double { + return TNL::max( a, b ); + }; + + /*** + * Keep lambda store the largest value in each row to the vector rowMax. + */ + auto keep = [=] __cuda_callable__ ( int rowIdx, const double& value ) mutable { + rowMaxView[ rowIdx ] = value; + }; + + /*** + * Compute the largest values in each row. + */ + view.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Rows reduction on host:" << std::endl; + rowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + rowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cu new file mode 120000 index 000000000..44df3d3ed --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_setElement.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_setElement.cpp new file mode 100644 index 000000000..41f936670 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_setElement.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void setElements() +{ + const int matrixSize( 5 ); + auto diagonalsOffsets = { -1, 0, 1 }; // offsets of tridiagonal matrix + using Matrix = TNL::Matrices::MultidiagonalMatrix< double, Device >; + Matrix matrix( matrixSize, matrixSize, diagonalsOffsets ); + auto view = matrix.getView(); + + for( int i = 0; i < 5; i++ ) + view.setElement( i, i, i ); + + std::cout << "Matrix set from the host:" << std::endl; + std::cout << matrix << std::endl; + + auto f = [=] __cuda_callable__ ( int i ) mutable { + if( i > 0 ) + view.setElement( i, i - 1, 1.0 ); + view.setElement( i, i, -i ); + if( i < matrixSize - 1 ) + view.setElement( i, i + 1, 1.0 ); + }; + + TNL::Algorithms::ParallelFor< Device >::exec( 0, matrixSize, 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 HAVE_CUDA + std::cout << "Set elements on CUDA device:" << std::endl; + setElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_setElement.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_setElement.cu new file mode 120000 index 000000000..05c193058 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_setElement.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_setElement.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index b58c2709b..f5c3e6c44 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -759,7 +759,11 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename InVector, typename OutVector > void vectorProduct( const InVector& inVector, - OutVector& outVector ) const; + OutVector& outVector, + const RealType matrixMultiplicator = 1.0, + const RealType outVectorMultiplicator = 0.0, + const IndexType begin = 0, + IndexType end = 0 ) const; template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > void addMatrix( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix, @@ -802,14 +806,14 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Method for saving the matrix to a file. * - * \param fileName is name of the file. + * \param file is the output file. */ void save( File& file ) const; /** * \brief Method for loading the matrix from a file. * - * \param fileName is name of the file. + * \param file is the input file. */ void load( File& file ); @@ -848,15 +852,18 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > */ IndexerType& getIndexer(); + /** + * \brief Returns padding index denoting padding zero elements. + * + * These elements are used for efficient data alignment in memory. + * + * \return value of the padding index. + */ __cuda_callable__ IndexType getPaddingIndex() const; protected: - //__cuda_callable__ - //IndexType getElementIndex( const IndexType row, - // const IndexType localIdx ) const; - DiagonalsOffsetsType diagonalsOffsets; HostDiagonalsOffsetsType hostDiagonalsOffsets; diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index e06e011fd..4cd4902f3 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -561,9 +561,15 @@ template< typename Real, typename OutVector > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -vectorProduct( const InVector& inVector, OutVector& outVector ) const +vectorProduct( const InVector& inVector, + OutVector& outVector, + const RealType matrixMultiplicator, + const RealType outVectorMultiplicator, + const IndexType firstRow, + IndexType lastRow ) const { - this->view.vectorProduct( inVector, outVector ); + this->view.vectorProduct( inVector, outVector, matrixMultiplicator, + outVectorMultiplicator, firstRow, lastRow ); } template< typename Real, @@ -617,8 +623,9 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > template< typename Real2, typename Index2 > -void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::getTransposition( const MultidiagonalMatrix< Real2, Device, Index2 >& matrix, - const RealType& matrixMultiplicator ) +void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +getTransposition( const MultidiagonalMatrix< Real2, Device, Index2 >& matrix, + const RealType& matrixMultiplicator ) { TNL_ASSERT( this->getRows() == matrix.getRows(), std::cerr << "This matrix rows: " << this->getRows() << std::endl @@ -668,10 +675,11 @@ template< typename Real, typename IndexAllocator > template< typename Vector1, typename Vector2 > __cuda_callable__ -void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >::performSORIteration( const Vector1& b, - const IndexType row, - Vector2& x, - const RealType& omega ) const +void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +performSORIteration( const Vector1& b, + const IndexType row, + Vector2& x, + const RealType& omega ) const { RealType sum( 0.0 ); if( row > 0 ) @@ -865,26 +873,6 @@ getIndexer() -> IndexerType& return this->indexer; } -/*template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization, - typename RealAllocator, - typename IndexAllocator > -__cuda_callable__ -Index MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -getElementIndex( const IndexType row, const IndexType column ) const -{ - IndexType localIdx = column - row; - if( row > 0 ) - localIdx++; - - TNL_ASSERT_GE( localIdx, 0, "" ); - TNL_ASSERT_LT( localIdx, 3, "" ); - - return this->indexer.getGlobalIndex( row, localIdx ); -}*/ - template< typename Real, typename Device, typename Index, @@ -899,109 +887,5 @@ getPaddingIndex() const return this->view.getPaddingIndex(); } -/* -template<> -class MultidiagonalMatrixDeviceDependentCode< Devices::Host > -{ - public: - - typedef Devices::Host Device; - - template< typename Index > - __cuda_callable__ - static Index getElementIndex( const Index rows, - const Index row, - const Index column ) - { - return 2*row + column; - } - - template< typename Vector, - typename Index, - typename ValuesType > - __cuda_callable__ - static typename Vector::RealType rowVectorProduct( const Index rows, - const ValuesType& values, - const Index row, - const Vector& vector ) - { - if( row == 0 ) - return vector[ 0 ] * values[ 0 ] + - vector[ 1 ] * values[ 1 ]; - Index i = 3 * row; - if( row == rows - 1 ) - return vector[ row - 1 ] * values[ i - 1 ] + - vector[ row ] * values[ i ]; - return vector[ row - 1 ] * values[ i - 1 ] + - vector[ row ] * values[ i ] + - vector[ row + 1 ] * values[ i + 1 ]; - } - - template< typename Real, - typename Index, - typename InVector, - typename OutVector > - static void vectorProduct( const MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >& matrix, - const InVector& inVector, - OutVector& outVector ) - { -#ifdef HAVE_OPENMP -#pragma omp parallel for if( Devices::Host::isOMPEnabled() ) -#endif - for( Index row = 0; row < matrix.getRows(); row ++ ) - outVector[ row ] = matrix.rowVectorProduct( row, inVector ); - } -}; - -template<> -class MultidiagonalMatrixDeviceDependentCode< Devices::Cuda > -{ - public: - - typedef Devices::Cuda Device; - - template< typename Index > - __cuda_callable__ - static Index getElementIndex( const Index rows, - const Index row, - const Index column ) - { - return ( column - row + 1 )*rows + row - 1; - } - - template< typename Vector, - typename Index, - typename ValuesType > - __cuda_callable__ - static typename Vector::RealType rowVectorProduct( const Index rows, - const ValuesType& values, - const Index row, - const Vector& vector ) - { - if( row == 0 ) - return vector[ 0 ] * values[ 0 ] + - vector[ 1 ] * values[ rows - 1 ]; - Index i = row - 1; - if( row == rows - 1 ) - return vector[ row - 1 ] * values[ i ] + - vector[ row ] * values[ i + rows ]; - return vector[ row - 1 ] * values[ i ] + - vector[ row ] * values[ i + rows ] + - vector[ row + 1 ] * values[ i + 2*rows ]; - } - - template< typename Real, - typename Index, - typename InVector, - typename OutVector > - static void vectorProduct( const MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >& matrix, - const InVector& inVector, - OutVector& outVector ) - { - MatrixVectorProductCuda( matrix, inVector, outVector ); - } -}; - */ - } // namespace Matrices } // namespace TNL diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.h b/src/TNL/Matrices/MultidiagonalMatrixView.h index 26b8fd4df..38eff8218 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixView.h @@ -19,6 +19,20 @@ namespace TNL { namespace Matrices { +/** + * \brief Implementation of sparse multi-diagonal matrix. + * + * It serves as an accessor to \ref SparseMatrix for example when passing the + * matrix to lambda functions. SparseMatrix view can be also created in CUDA kernels. + * + * See \ref MultidiagonalMatrix for more details. + * + * \tparam Real is a type of matrix elements. + * \tparam Device is a device where the matrix is allocated. + * \tparam Index is a type for indexing of the matrix elements. + * \tparam Organization tells the ordering of matrix elements. It is either RowMajorOrder + * or ColumnMajorOrder. + */ template< typename Real = double, typename Device = Devices::Host, typename Index = int, @@ -26,125 +40,512 @@ template< typename Real = double, class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > { public: + + // Supporting types - they are not important for the user + using BaseType = MatrixView< Real, Device, Index >; + using ValuesViewType = typename BaseType::ValuesView; + using IndexerType = details::MultidiagonalMatrixIndexer< Index, Organization >; + using DiagonalsOffsetsView = Containers::VectorView< Index, Device, Index >; + using HostDiagonalsOffsetsView = Containers::VectorView< Index, Devices::Host, Index >; + + /** + * \brief The type of matrix elements. + */ using RealType = Real; + + /** + * \brief The device where the matrix is allocated. + */ using DeviceType = Device; + + /** + * \brief The type used for matrix elements indexing. + */ using IndexType = Index; - using BaseType = MatrixView< Real, Device, Index >; - //using DiagonalsOffsetsType = Containers::Vector< IndexType, DeviceType, IndexType >; - using DiagonalsOffsetsView = Containers::VectorView< IndexType, DeviceType, IndexType >; - //using HostDiagonalsOffsetsType = Containers::Vector< IndexType, Devices::Host, IndexType >; - using HostDiagonalsOffsetsView = Containers::VectorView< IndexType, Devices::Host, IndexType >; - using IndexerType = details::MultidiagonalMatrixIndexer< IndexType, Organization >; - using ValuesViewType = typename BaseType::ValuesView; + + /** + * \brief Type of related matrix view. + */ using ViewType = MultidiagonalMatrixView< Real, Device, Index, Organization >; + + /** + * \brief Matrix view type for constant instances. + */ using ConstViewType = MultidiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; - using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsOffsetsView >; - // TODO: remove this - it is here only for compatibility with original matrix implementation - typedef Containers::Vector< IndexType, DeviceType, IndexType > CompressedRowLengthsVector; - typedef Containers::VectorView< IndexType, DeviceType, IndexType > CompressedRowLengthsVectorView; - typedef typename CompressedRowLengthsVectorView::ConstViewType ConstCompressedRowLengthsVectorView; + /** + * \brief Type for accessing matrix rows. + */ + using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsOffsetsView >; + /** + * \brief Helper type for getting self type or its modifications. + */ template< typename _Real = Real, typename _Device = Device, typename _Index = Index, ElementsOrganization Organization_ = Containers::Segments::DefaultElementsOrganization< Device >::getOrganization() > using Self = MultidiagonalMatrixView< _Real, _Device, _Index, Organization_ >; + /** + * \brief Constructor with no parameters. + */ + __cuda_callable__ MultidiagonalMatrixView(); + /** + * \brief Constructor with all necessary data and views. + * + * \param values is a vector view with matrix elements values + * \param diagonalsOffsets is a vector view with diagonals offsets + * \param hostDiagonalsOffsets is a vector view with a copy of diagonals offsets on the host + * \param indexer is an indexer of matrix elements + */ + __cuda_callable__ MultidiagonalMatrixView( const ValuesViewType& values, const DiagonalsOffsetsView& diagonalsOffsets, const HostDiagonalsOffsetsView& hostDiagonalsOffsets, const IndexerType& indexer ); + /** + * \brief Copy constructor. + * + * \param matrix is an input multidiagonal matrix view. + */ + __cuda_callable__ + MultidiagonalMatrixView( const MultidiagonalMatrixView& view ) = default; + + /** + * \brief Move constructor. + * + * \param matrix is an input multidiagonal matrix view. + */ + __cuda_callable__ + MultidiagonalMatrixView( MultidiagonalMatrixView&& view ) = default; + + /** + * \brief Returns a modifiable view of the multidiagonal matrix. + * + * \return multidiagonal matrix view. + */ ViewType getView(); + /** + * \brief Returns a non-modifiable view of the multidiagonal matrix. + * + * \return multidiagonal matrix view. + */ ConstViewType getConstView() const; + /** + * \brief Returns string with serialization type. + * + * The string has a form `Matrices::MultidiagonalMatrix< RealType, [any_device], IndexType, Organization, [any_allocator], [any_allocator] >`. + * + * See \ref MultidiagonalMatrix::getSerializationType. + * + * \return \ref String with the serialization type. + */ static String getSerializationType(); + /** + * \brief Returns string with serialization type. + * + * See \ref MultidiagonalMatrix::getSerializationType. + * + * \return \ref String with the serialization type. + */ virtual String getSerializationTypeVirtual() const; + /** + * \brief Returns number of diagonals. + * + * \return Number of diagonals. + */ __cuda_callable__ const IndexType& getDiagonalsCount() const; + /** + * \brief Computes number of non-zeros in each row. + * + * \param rowLengths is a vector into which the number of non-zeros in each row + * will be stored. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_getCompressedRowLengths.out + */ template< typename Vector > void getCompressedRowLengths( Vector& rowLengths ) const; - IndexType getNonemptyRowsCount() const; - [[deprecated]] IndexType getRowLength( const IndexType row ) const; - IndexType getMaxRowLength() const; - - IndexType getNumberOfNonzeroMatrixElements() const; - + IndexType getNonzeroElementsCount() const; + + /** + * \brief Comparison operator with another multidiagonal matrix. + * + * \tparam Real_ is \e Real type of the source matrix. + * \tparam Device_ is \e Device type of the source matrix. + * \tparam Index_ is \e Index type of the source matrix. + * \tparam Organization_ is \e Organization of the source matrix. + * \tparam RealAllocator_ is \e RealAllocator of the source matrix. + * \tparam IndexAllocator_ is \e IndexAllocator of the source matrix. + * + * \return \e true if both matrices are identical and \e false otherwise. + */ template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_ > bool operator == ( const MultidiagonalMatrixView< Real_, Device_, Index_, Organization_ >& matrix ) const; + /** + * \brief Comparison operator with another multidiagonal matrix. + * + * \tparam Real_ is \e Real type of the source matrix. + * \tparam Device_ is \e Device type of the source matrix. + * \tparam Index_ is \e Index type of the source matrix. + * \tparam Organization_ is \e Organization of the source matrix. + * \tparam RealAllocator_ is \e RealAllocator of the source matrix. + * \tparam IndexAllocator_ is \e IndexAllocator of the source matrix. + * + * \param matrix is the source matrix. + * + * \return \e true if both matrices are NOT identical and \e false otherwise. + */ template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_ > bool operator != ( const MultidiagonalMatrixView< Real_, Device_, Index_, Organization_ >& matrix ) 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/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_getRow.out + * + * See \ref MultidiagonalMatrixRowView. + */ __cuda_callable__ RowView getRow( const IndexType& rowIdx ); + /** + * \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/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getConstRow.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_getConstRow.out + * + * See \ref MultidiagonalMatrixRowView. + */ __cuda_callable__ const RowView getRow( const IndexType& rowIdx ) const; + /** + * \brief Set all matrix elements to given value. + * + * \param value is the new value of all matrix elements. + */ void setValue( const RealType& v ); + /** + * \brief Sets element at given \e row and \e column to given \e 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. \ref MultidiagonalMatrix::getRow + * or \ref MultidiagonalMatrix::forRows and \ref MultidiagonalMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_setElement.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_setElement.out + */ __cuda_callable__ void setElement( const IndexType row, const IndexType column, const RealType& value ); + /** + * \brief Add element at given \e row and \e column to given \e 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. \ref MultidiagonalMatrix::getRow + * or \ref MultidiagonalMatrix::forRows and \ref MultidiagonalMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * \param thisElementMultiplicator is multiplicator the original matrix element + * value is multiplied by before addition of given \e value. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_addElement.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_addElement.out + * + */ __cuda_callable__ void addElement( const IndexType row, const IndexType column, const RealType& value, const RealType& thisElementMultiplicator = 1.0 ); + /** + * \brief 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. \ref MultidiagonalMatrix::getRow + * or \ref MultidiagonalMatrix::forRows and \ref MultidiagonalMatrix::forAllRows. + * + * \param row is a row index of the matrix element. + * \param column i a column index of the matrix element. + * + * \return value of given matrix element. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getElement.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_getElement.out + * + */ __cuda_callable__ RealType getElement( const IndexType row, const IndexType column ) const; - MultidiagonalMatrixView& operator=( const MultidiagonalMatrixView& view ); - + /** + * \brief Method for performing general reduction on matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_rowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for performing general reduction on matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_rowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + /** + * \brief Method for performing general reduction on all matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_allRowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for performing general reduction on all matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_allRowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + /** + * \brief Method for iteration over all matrix rows for constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_forRows.out + */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ) const; + /** + * \brief Method for iteration over all matrix rows for non-constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_forRows.out + */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ); + /** + * \brief This method calls \e forRows for all matrix rows (for constant instances). + * + * See \ref MultidiagonalMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllRows.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ) const; + /** + * \brief This method calls \e forRows for all matrix rows. + * + * See \ref MultidiagonalMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllRows.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ); - template< typename Vector > - __cuda_callable__ - typename Vector::RealType rowVectorProduct( const IndexType row, - const Vector& vector ) const; - + /** + * \brief Computes product of matrix and vector. + * + * More precisely, it computes: + * + * `outVector = matrixMultiplicator * ( * this ) * inVector + outVectorMultiplicator * outVector` + * + * \tparam InVector is type of input vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * \tparam OutVector is type of output vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * + * \param inVector is input vector. + * \param outVector is output vector. + * \param matrixMultiplicator is a factor by which the matrix is multiplied. It is one by default. + * \param outVectorMultiplicator is a factor by which the outVector is multiplied before added + * to the result of matrix-vector product. It is zero by default. + * \param begin is the beginning of the rows range for which the vector product + * is computed. It is zero by default. + * \param end is the end of the rows range for which the vector product + * is computed. It is number if the matrix rows by default. + */ template< typename InVector, typename OutVector > void vectorProduct( const InVector& inVector, - OutVector& outVector ) const; + OutVector& outVector, + const RealType matrixMultiplicator = 1.0, + const RealType outVectorMultiplicator = 0.0, + const IndexType firstRow = 0, + IndexType lastRow = 0 ) const; template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_ > void addMatrix( const MultidiagonalMatrixView< Real_, Device_, Index_, Organization_ >& matrix, @@ -162,26 +563,66 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > Vector2& x, const RealType& omega = 1.0 ) const; + /** + * \brief Assignment of exactly the same matrix type. + * + * \param matrix is input matrix for the assignment. + * \return reference to this matrix. + */ + MultidiagonalMatrixView& operator=( const MultidiagonalMatrixView& view ); + + /** + * \brief Method for saving the matrix to a file. + * + * \param file is the output file. + */ void save( File& file ) const; + /** + * \brief Method for saving the matrix to the file with given filename. + * + * \param fileName is name of the file. + */ void save( const String& fileName ) const; + /** + * \brief Method for printing the matrix to output stream. + * + * \param str is the output stream. + */ void print( std::ostream& str ) const; + /** + * \brief This method returns matrix elements indexer used by this matrix. + * + * \return constant reference to the indexer. + */ __cuda_callable__ const IndexerType& getIndexer() const; + /** + * \brief This method returns matrix elements indexer used by this matrix. + * + * \return non-constant reference to the indexer. + */ __cuda_callable__ IndexerType& getIndexer(); + /** + * \brief Returns padding index denoting padding zero elements. + * + * These elements are used for efficient data alignment in memory. + * + * \return value of the padding index. + */ __cuda_callable__ IndexType getPaddingIndex() const; protected: - __cuda_callable__ - IndexType getElementIndex( const IndexType row, - const IndexType localIdx ) const; + //__cuda_callable__ + //IndexType getElementIndex( const IndexType row, + // const IndexType localIdx ) const; DiagonalsOffsetsView diagonalsOffsets; diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index a29286e71..0bc3c2d36 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -132,17 +132,6 @@ getCompressedRowLengths( Vector& rowLengths ) const this->allRowsReduction( fetch, reduce, keep, 0 ); } -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization > -Index -MultidiagonalMatrixView< Real, Device, Index, Organization >:: -getNonemptyRowsCount() const -{ - return this->indexer.getNonemptyRowsCount(); -} - template< typename Real, typename Device, typename Index, @@ -160,18 +149,7 @@ template< typename Real, ElementsOrganization Organization > Index MultidiagonalMatrixView< Real, Device, Index, Organization >:: -getMaxRowLength() const -{ - return this->diagonalsOffsets.getSize(); -} - -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization > -Index -MultidiagonalMatrixView< Real, Device, Index, Organization >:: -getNumberOfNonzeroMatrixElements() const +getNonzeroElementsCount() const { const auto values_view = this->values.getConstView(); auto fetch = [=] __cuda_callable__ ( const IndexType i ) -> IndexType { @@ -302,7 +280,7 @@ addElement( const IndexType row, for( IndexType i = 0; i < diagonalsOffsets.getSize(); i++ ) if( row + diagonalsOffsets.getElement( i ) == column ) { - const Index idx = this->getElementIndex( row, i ); + const Index idx = this->indexer.getGlobalIndex( row, i ); this->values.setElement( idx, thisElementMultiplicator * this->values.getElement( idx ) + value ); return; } @@ -511,18 +489,6 @@ forAllRows( Function& function ) this->forRows( 0, this->indexer.getNonemptyRowsCount(), function ); } -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization > -template< typename Vector > -__cuda_callable__ -typename Vector::RealType -MultidiagonalMatrixView< Real, Device, Index, Organization >:: -rowVectorProduct( const IndexType row, const Vector& vector ) const -{ -} - template< typename Real, typename Device, typename Index, @@ -531,7 +497,12 @@ template< typename Real, typename OutVector > void MultidiagonalMatrixView< Real, Device, Index, Organization >:: -vectorProduct( const InVector& inVector, OutVector& outVector ) const +vectorProduct( const InVector& inVector, + OutVector& outVector, + const RealType matrixMultiplicator, + const RealType outVectorMultiplicator, + const IndexType begin, + IndexType end ) const { TNL_ASSERT_EQ( this->getColumns(), inVector.getSize(), "Matrix columns do not fit with input vector." ); TNL_ASSERT_EQ( this->getRows(), outVector.getSize(), "Matrix rows do not fit with output vector." ); @@ -544,10 +515,19 @@ vectorProduct( const InVector& inVector, OutVector& outVector ) const auto reduction = [] __cuda_callable__ ( RealType& sum, const RealType& value ) { sum += value; }; - auto keeper = [=] __cuda_callable__ ( IndexType row, const RealType& value ) mutable { - outVectorView[ row ] = value; + auto keeper1 = [=] __cuda_callable__ ( IndexType row, const RealType& value ) mutable { + outVectorView[ row ] = matrixMultiplicator * value; + }; + auto keeper2 = [=] __cuda_callable__ ( IndexType row, const RealType& value ) mutable { + outVectorView[ row ] = outVectorMultiplicator * outVectorView[ row ] + matrixMultiplicator * value; }; - this->allRowsReduction( fetch, reduction, keeper, ( RealType ) 0.0 ); + + if( end == 0 ) + end = this->getRows(); + if( outVectorMultiplicator == ( RealType ) 0.0 ) + this->rowsReduction( begin, end, fetch, reduction, keeper1, ( RealType ) 0.0 ); + else + this->rowsReduction( begin, end, fetch, reduction, keeper2, ( RealType ) 0.0 ); } template< typename Real, @@ -765,7 +745,7 @@ getIndexer() -> IndexerType& return this->indexer; } -template< typename Real, +/*template< typename Real, typename Device, typename Index, ElementsOrganization Organization > @@ -775,7 +755,7 @@ MultidiagonalMatrixView< Real, Device, Index, Organization >:: getElementIndex( const IndexType row, const IndexType localIdx ) const { return this->indexer.getGlobalIndex( row, localIdx ); -} +}*/ template< typename Real, typename Device, diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index 30d0559b2..d80db7717 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -156,18 +156,18 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Copy constructor. * - * \param m is an input sparse matrix view. + * \param matrix is an input sparse matrix view. */ __cuda_callable__ - SparseMatrixView( const SparseMatrixView& m ) = default; + SparseMatrixView( const SparseMatrixView& matrix ) = default; /** * \brief Move constructor. * - * \param m is an input sparse matrix view. + * \param matrix is an input sparse matrix view. */ __cuda_callable__ - SparseMatrixView( SparseMatrixView&& m ) = default; + SparseMatrixView( SparseMatrixView&& matrix ) = default; /** * \brief Returns a modifiable view of the sparse matrix. @@ -567,8 +567,8 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > OutVector& outVector, const RealType matrixMultiplicator = 1.0, const RealType outVectorMultiplicator = 0.0, - const IndexType firstRow = 0, - IndexType lastRow = 0 ) const; + const IndexType begin = 0, + IndexType end = 0 ) const; template< typename Vector1, typename Vector2 > bool performSORIteration( const Vector1& b, -- GitLab From b836dc19d0b9f1bd9fa0c6cd435e85657cee3246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 5 Jun 2020 11:22:06 +0200 Subject: [PATCH 29/38] Fixing examples for multidiagonal matrix. --- .../LambdaMatrixExample_allRowsReduction.cpp | 2 +- .../LambdaMatrixExample_forAllRows.cpp | 2 +- .../LambdaMatrixExample_rowsReduction.cpp | 2 +- .../MultidiagonalMatrix/CMakeLists.txt | 24 ++++++++++++------- ...MultidiagonalMatrixExample_Constructor.cpp | 2 +- .../MultidiagonalMatrixExample_getRow.cpp | 14 ++++++++--- .../MultidiagonalMatrixExample_setElement.cpp | 7 ++++-- .../MultidiagonalMatrixExample_setElement.cu | 1 + .../SparseMatrixExample_getRow.cpp | 2 +- src/UnitTests/Matrices/LambdaMatrixTest.hpp | 4 ++-- 10 files changed, 40 insertions(+), 20 deletions(-) create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cu diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp index cfffd0534..a39a1f33b 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp @@ -17,7 +17,7 @@ void allRowsReduction() value = TNL::max( rowIdx - columnIdx + 1, 0 ); }; - using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >; + using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, Device, int >; auto matrix = MatrixFactory::create( 5, 5, matrixElements, rowLengths ); /*** diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cpp index 3dae8fd06..72ff96101 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllRows.cpp @@ -16,7 +16,7 @@ void forRowsExample() value = TNL::max( rowIdx - columnIdx + 1, 0 ); }; - using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >; + using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, Device, int >; auto matrix = MatrixFactory::create( 5, 5, matrixElements, rowLengths ); TNL::Matrices::DenseMatrix< double, Device > denseMatrix( 5, 5 ); diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp index d7613735a..17f3ace0d 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp @@ -17,7 +17,7 @@ void rowsReduction() value = TNL::max( rowIdx - columnIdx + 1, 0 ); }; - using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >; + using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, Device, int >; auto matrix = MatrixFactory::create( 5, 5, matrixElements, rowLengths ); /*** diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index e20d8a6d0..6d17bf778 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -39,10 +39,11 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getRow.out OUTPUT MultidiagonalMatrixExample_getRow.out ) - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement_cuda MultidiagonalMatrixExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out - OUTPUT MultidiagonalMatrixExample_setElement.out ) +# This example does not work with nvcc 10.1. Restore it here when it works. +# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement_cuda MultidiagonalMatrixExample_setElement.cu ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement_cuda > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out +# OUTPUT MultidiagonalMatrixExample_setElement.out ) CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_addElement_cuda MultidiagonalMatrixExample_addElement.cu ) ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_addElement_cuda > @@ -165,10 +166,11 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getRow.out OUTPUT MultidiagonalMatrixExample_getRow.out ) - ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement MultidiagonalMatrixExample_setElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out - OUTPUT MultidiagonalMatrixExample_setElement.out ) +# This example does not work with nvcc 10.1. Restore it here when it works. +# ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement MultidiagonalMatrixExample_setElement.cpp ) +# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement > +# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out +# OUTPUT MultidiagonalMatrixExample_setElement.out ) ADD_EXECUTABLE( MultidiagonalMatrixExample_addElement MultidiagonalMatrixExample_addElement.cpp ) ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_addElement > @@ -252,6 +254,12 @@ ELSE() ENDIF() + ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement MultidiagonalMatrixExample_setElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out + OUTPUT MultidiagonalMatrixExample_setElement.out ) + + ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS MultidiagonalMatrixExample_Constructor.out MultidiagonalMatrixExample_Constructor_init_list_1.out diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp index 412ea10a7..8f8b8139b 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp @@ -33,7 +33,7 @@ void laplaceOperatorMatrix() row.setElement( 4, -1.0 ); } }; - TNL::Algorithms::ParallelFor2D< TNL::Devices::Sequential >::exec( 0, 0, gridSize, gridSize, f ); + TNL::Algorithms::ParallelFor2D< Device >::exec( 0, 0, gridSize, gridSize, f ); std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp index 82273541f..856848bd3 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp @@ -19,7 +19,12 @@ void getRowExample() diagonalsOffsets ); auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { - auto row = matrix->getRow( rowIdx ); + //auto row = matrix->getRow( rowIdx ); + // For some reason the previous line of code is not accepted by nvcc 10.1 + // so we replace it with the following two lines. + auto ref = matrix.modifyData(); + auto row = ref.getRow( rowIdx ); + if( rowIdx > 0 ) row.setElement( 0, -1.0 ); // elements below the diagonal row.setElement( 1, 2.0 ); // elements on the diagonal @@ -47,7 +52,10 @@ int main( int argc, char* argv[] ) getRowExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Getting matrix rows on CUDA device: " << std::endl; - getRowExample< TNL::Devices::Cuda >(); + // It seems that nvcc 10.1 does not handle lambda functions properly. + // It is hard to make nvcc to compile this example and it does not work + // properly. We will try it with later version of CUDA. + //std::cout << "Getting matrix rows on CUDA device: " << std::endl; + //getRowExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp index 8dee79f47..bf28ec226 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp @@ -45,7 +45,10 @@ int main( int argc, char* argv[] ) setElements< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Set elements on CUDA device:" << std::endl; - setElements< TNL::Devices::Cuda >(); + // It seems that nvcc 10.1 does not handle lambda functions properly. + // It is hard to make nvcc to compile this example and it does not work + // properly. We will try it with later version of CUDA. + //std::cout << "Set elements on CUDA device:" << std::endl; + //setElements< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cu new file mode 120000 index 000000000..88a0fa864 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_setElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cpp index d07577ff9..d52602f08 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_getRow.cpp @@ -30,7 +30,7 @@ void getRowExample() * Set the matrix elements. */ TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix->getRows(), f ); - std::cout << matrix << std::endl; + std::cout << *matrix << std::endl; } int main( int argc, char* argv[] ) diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.hpp b/src/UnitTests/Matrices/LambdaMatrixTest.hpp index 54db3b18e..256a84bf7 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.hpp +++ b/src/UnitTests/Matrices/LambdaMatrixTest.hpp @@ -94,10 +94,10 @@ void test_GetCompressedRowLengths() MatrixType m( size, size, matrixElements, rowLengths ); TNL::Containers::Vector< IndexType > correctRowLengths{ 1, 3, 3, 3, 1 }; - TNL::Containers::Vector< IndexType > rowLengthsVector; + TNL::Containers::Vector< IndexType, DeviceType > rowLengthsVector; m.getCompressedRowLengths( rowLengthsVector ); for( int i = 0; i < size; i++ ) - EXPECT_EQ( correctRowLengths[ i ], rowLengthsVector[ i ] ); + EXPECT_EQ( correctRowLengths.getElement( i ), rowLengthsVector.getElement( i ) ); } template< typename Matrix > -- GitLab From 3986178a2853d98f8ad1b1a18d3e506dc41dfc16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 5 Jun 2020 11:25:01 +0200 Subject: [PATCH 30/38] Added missing source file. --- .../MultidiagonalMatrixExample_Constructor.cu | 1 + 1 file changed, 1 insertion(+) create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cu diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cu new file mode 120000 index 000000000..7d790b5c9 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_Constructor.cpp \ No newline at end of file -- GitLab From b6be0ba6d300b44d69c3390fe738dba23a26a459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 5 Jun 2020 12:39:20 +0200 Subject: [PATCH 31/38] Fixed device definition in LambdaMatrix. --- .../LambdaMatrixExample_Constructor.cpp | 4 ++-- .../LambdaMatrixExample_forRows.cpp | 2 +- ...aMatrixExample_getCompressedRowLengths.cpp | 2 +- ...aMatrixExample_getNonzeroElementsCount.cpp | 2 +- src/TNL/Matrices/LambdaMatrix.h | 9 ++++----- src/TNL/Matrices/LambdaMatrix.hpp | 20 ++++++------------- 6 files changed, 15 insertions(+), 24 deletions(-) diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp index e0b2e52a3..2c418dd54 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cpp @@ -21,13 +21,13 @@ int main( int argc, char* argv[] ) /*** * Matrix construction with explicit type definition. */ - using MatrixType = decltype( TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >::create( matrixElements1, rowLengths ) ); + using MatrixType = decltype( TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::Host, int >::create( matrixElements1, rowLengths ) ); MatrixType m1( size, size, matrixElements1, rowLengths ); /*** * Matrix construction using 'auto'. */ - auto m2 = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >::create( matrixElements2, rowLengths ); + auto m2 = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::Host, int >::create( matrixElements2, rowLengths ); m2.setDimensions( size, size ); std::cout << "The first lambda matrix: " << std::endl << m1 << std::endl; diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp index 036543ac8..d5cf660a6 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp @@ -16,7 +16,7 @@ void forRowsExample() value = TNL::max( rowIdx - columnIdx + 1, 0 ); }; - using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >; + using MatrixFactory = TNL::Matrices::LambdaMatrixFactory< double, Device, int >; auto matrix = MatrixFactory::create( 5, 5, matrixElements, rowLengths ); TNL::Matrices::DenseMatrix< double, Device > denseMatrix( 5, 5 ); diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cpp index ed1326dc1..797a0bbbe 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getCompressedRowLengths.cpp @@ -13,7 +13,7 @@ int main( int argc, char* argv[] ) }; const int size = 5; - auto matrix = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >::create( size, size, matrixElements, rowLengths ); + auto matrix = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::Host, int >::create( size, size, matrixElements, rowLengths ); TNL::Containers::Vector< int > rowLengthsVector; matrix.getCompressedRowLengths( rowLengthsVector ); diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cpp index 4d0811959..43f015c48 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cpp @@ -13,7 +13,7 @@ int main( int argc, char* argv[] ) }; const int size = 5; - auto matrix = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::AnyDevice, int >::create( size, size, matrixElements, rowLengths ); + auto matrix = TNL::Matrices::LambdaMatrixFactory< double, TNL::Devices::Host, int >::create( size, size, matrixElements, rowLengths ); std::cout << "Matrix looks as:" << std::endl << matrix << std::endl; std::cout << "Non-zero elements count is: " << matrix.getNonzeroElementsCount() << std::endl; diff --git a/src/TNL/Matrices/LambdaMatrix.h b/src/TNL/Matrices/LambdaMatrix.h index ed7c30e91..1692510e7 100644 --- a/src/TNL/Matrices/LambdaMatrix.h +++ b/src/TNL/Matrices/LambdaMatrix.h @@ -11,7 +11,7 @@ #pragma once #include -#include +#include namespace TNL { namespace Matrices { @@ -40,14 +40,13 @@ namespace Matrices { * where \e rows is the number of matrix rows, \e columns is the number of matrix columns and \e row is an index of the row being queried. * * \tparam Real is a type of matrix elements values. - * \tparam Device is a device on which the lambda functions can evaluated. - * Devices::AnyDevice can be used for lambdas with no restriction. + * \tparam Device is a device on which the lambda functions will be evaluated. * \ẗparam Index is a type used for indexing. */ template< typename MatrixElementsLambda, typename CompressedRowLengthsLambda, typename Real = double, - typename Device = Devices::AnyDevice, + typename Device = Devices::Host, typename Index = int > class LambdaMatrix { @@ -352,7 +351,7 @@ std::ostream& operator<< ( std::ostream& str, const LambdaMatrix< MatrixElements * \param compressedRowLengthsLambda */ template< typename Real = double, - typename Device = Devices::AnyDevice, + typename Device = Devices::Host, typename Index = int > struct LambdaMatrixFactory { diff --git a/src/TNL/Matrices/LambdaMatrix.hpp b/src/TNL/Matrices/LambdaMatrix.hpp index 4ce0a18a5..7e606d1e7 100644 --- a/src/TNL/Matrices/LambdaMatrix.hpp +++ b/src/TNL/Matrices/LambdaMatrix.hpp @@ -97,7 +97,6 @@ void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: getCompressedRowLengths( Vector& rowLengths ) const { - using Device_ = typename Devices::PickDevice< DeviceType >::DeviceType; details::set_size_if_resizable( rowLengths, this->getRows() ); rowLengths = 0; auto rowLengths_view = rowLengths.getView(); @@ -119,7 +118,7 @@ Index LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: getNonzeroElementsCount() const { - Containers::Vector< IndexType, typename Devices::PickDevice< DeviceType >::DeviceType, IndexType > rowLengthsVector; + Containers::Vector< IndexType, DeviceType, IndexType > rowLengthsVector; this->getCompressedRowLengths( rowLengthsVector ); return sum( rowLengthsVector ); } @@ -134,8 +133,7 @@ LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, In getElement( const IndexType row, const IndexType column ) const { - using Device_ = typename Devices::PickDevice< Devices::Host >::DeviceType; - Containers::Array< RealType, Device_ > value( 1 ); + Containers::Array< RealType, DeviceType > value( 1 ); auto valueView = value.getView(); auto rowLengths = this->compressedRowLengthsLambda; auto matrixElements = this->matrixElementsLambda; @@ -156,7 +154,7 @@ getElement( const IndexType row, } } }; - Algorithms::ParallelFor< Device_ >::exec( row, row + 1, getValue ); + Algorithms::ParallelFor< DeviceType >::exec( row, row + 1, getValue ); return valueView.getElement( 0 ); } @@ -211,7 +209,6 @@ LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, In rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const { using FetchType = decltype( fetch( IndexType(), IndexType(), RealType() ) ); - using Device_ = typename Devices::PickDevice< DeviceType >::DeviceType; const IndexType rows = this->getRows(); const IndexType columns = this->getColumns(); @@ -232,7 +229,7 @@ rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& redu } keep( rowIdx, result ); }; - Algorithms::ParallelFor< Device_ >::exec( first, last, processRow ); + Algorithms::ParallelFor< DeviceType >::exec( first, last, processRow ); } template< typename MatrixElementsLambda, @@ -258,8 +255,6 @@ void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: forRows( IndexType first, IndexType last, Function& function ) const { - using Device_ = typename Devices::PickDevice< DeviceType >::DeviceType; - const IndexType rows = this->getRows(); const IndexType columns = this->getColumns(); auto rowLengths = this->compressedRowLengthsLambda; @@ -276,7 +271,7 @@ forRows( IndexType first, IndexType last, Function& function ) const function( rowIdx, localIdx, elementColumn, elementValue, compute ); } }; - Algorithms::ParallelFor< Device_ >::exec( first, last, processRow ); + Algorithms::ParallelFor< DeviceType >::exec( first, last, processRow ); } template< typename MatrixElementsLambda, @@ -289,8 +284,6 @@ void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: forAllRows( Function& function ) const { - using Device_ = typename Devices::PickDevice< DeviceType >::DeviceType; - const IndexType rows = this->getRows(); const IndexType columns = this->getColumns(); auto rowLengths = this->compressedRowLengthsLambda; @@ -307,10 +300,9 @@ forAllRows( Function& function ) const function( rowIdx, localIdx, elementColumn, elementValue, compute ); } }; - Algorithms::ParallelFor< Device_ >::exec( 0, this->getRows(), processRow ); + Algorithms::ParallelFor< DeviceType >::exec( 0, this->getRows(), processRow ); } - template< typename MatrixElementsLambda, typename CompressedRowLengthsLambda, typename Real, -- GitLab From ca9ae9644d35804e0a54a72b4332a337dcaf363b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 5 Jun 2020 14:12:34 +0200 Subject: [PATCH 32/38] Writting documentation on MultidiagonalMatrixRowView. --- src/TNL/Matrices/MultidiagonalMatrixRowView.h | 86 ++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/src/TNL/Matrices/MultidiagonalMatrixRowView.h b/src/TNL/Matrices/MultidiagonalMatrixRowView.h index c2ee8a869..181974b72 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixRowView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixRowView.h @@ -13,6 +13,27 @@ namespace TNL { namespace Matrices { +/** + * \brief RowView is a simple structure for accessing rows of multidiagonal matrix. + * + * \tparam ValuesView is a vector view storing the matrix elements values. + * \tparam Indexer is type of object responsible for indexing and organization of + * matrix elements. + * \tparam DiagonalsOffsetsView_ is a container view holding offsets of + * diagonals of multidiagonal matrix. + * + * See \ref MultidiagonalMatrix and \ref MultidiagonalMatrixView. + * + * \par Example + * \include Matrices/MultidiagonalMatrixExample_getRow.cpp + * \par Output + * \include MultidiagonalatrixExample_getRow.out + * + * \par Example + * \include Matrices/MultidiagonalMatrixViewExample_getRow.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_getRow.out + */ template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView_ > @@ -20,12 +41,33 @@ class MultidiagonalMatrixRowView { public: + /** + * \brief The type of matrix elements. + */ using RealType = typename ValuesView::RealType; + + /** + * \brief The type used for matrix elements indexing. + */ using IndexType = typename ValuesView::IndexType; + + /** + * \brief Type of container view used for storing the matrix elements values. + */ using ValuesViewType = ValuesView; + + /** + * \brief Type of object responsible for indexing and organization of + * matrix elements. + */ using IndexerType = Indexer; + + /** + * \brief Type of a container view holding offsets of + * diagonals of multidiagonal matrix. + */ using DiagonalsOffsetsView = DiagonalsOffsetsView_; - + /** * \brief Type of constant container view used for storing the matrix elements values. */ @@ -46,24 +88,64 @@ class MultidiagonalMatrixRowView */ using ConstViewType = MultidiagonalMatrixRowView< ConstValuesViewType, ConstIndexerViewType, ConstDiagonalsOffsetsViewType >; + /** + * \brief Constructor with all necessary data. + * + * \param rowIdx is index of the matrix row this RowView refer to. + * \param diagonalsOffsets is a vector view holding offsets of matrix diagonals, + * \param values is a vector view holding values of matrix elements. + * \param indexer is object responsible for indexing and organization of matrix elements + */ __cuda_callable__ MultidiagonalMatrixRowView( const IndexType rowIdx, const DiagonalsOffsetsView& diagonalsOffsets, const ValuesViewType& values, - const IndexerType& indexer); + const IndexerType& indexer ); + /** + * \brief Returns number of diagonals of the multidiagonal matrix. + * + * \return number of diagonals of the multidiagonal matrix. + */ __cuda_callable__ IndexType getSize() const; + /** + * \brief Computes column index of matrix element on given subdiagonal. + * + * \param localIdx is an index of the subdiagonal. + * + * \return column index of matrix element on given subdiagonal. + */ __cuda_callable__ const IndexType getColumnIndex( const IndexType localIdx ) const; + /** + * \brief Returns value of matrix element on given subdiagonal. + * + * \param localIdx is an index of the subdiagonal. + * + * \return constant reference to matrix element value. + */ __cuda_callable__ const RealType& getValue( const IndexType localIdx ) const; + /** + * \brief Returns value of matrix element on given subdiagonal. + * + * \param localIdx is an index of the subdiagonal. + * + * \return non-constant reference to matrix element value. + */ __cuda_callable__ RealType& getValue( const IndexType localIdx ); + /** + * \brief Changes value of matrix element on given subdiagonal. + * + * \param localIdx is an index of the matrix subdiagonal. + * \param value is the new value of the matrix element. + */ __cuda_callable__ void setElement( const IndexType localIdx, const RealType& value ); -- GitLab From 90d4cebc726590bcc8cc473e075bb219efb9e34a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Mon, 8 Jun 2020 07:33:32 +0200 Subject: [PATCH 33/38] Added examples for tridiagonal matrix. --- ...lMatrixExample_Constructor_init_list_1.cpp | 28 ++++++++ ...alMatrixExample_Constructor_init_list_1.cu | 1 + ...lMatrixExample_Constructor_init_list_2.cpp | 31 +++++++++ ...alMatrixExample_Constructor_init_list_2.cu | 1 + ...gonalMatrixExample_Constructor_std_map.cpp | 38 +++++++++++ ...agonalMatrixExample_Constructor_std_map.cu | 1 + .../TridiagonalMatrixExample_addElement.cpp | 30 +++++++++ .../TridiagonalMatrixExample_addElement.cu | 1 + ...diagonalMatrixExample_allRowsReduction.cpp | 67 +++++++++++++++++++ ...idiagonalMatrixExample_allRowsReduction.cu | 1 + .../TridiagonalMatrixExample_forAllRows.cpp | 35 ++++++++++ .../TridiagonalMatrixExample_forAllRows.cu | 1 + .../TridiagonalMatrixExample_forRows.cpp | 35 ++++++++++ .../TridiagonalMatrixExample_forRows.cu | 1 + ...lMatrixExample_getCompressedRowLengths.cpp | 34 ++++++++++ ...alMatrixExample_getCompressedRowLengths.cu | 1 + .../TridiagonalMatrixExample_getConstRow.cpp | 52 ++++++++++++++ .../TridiagonalMatrixExample_getConstRow.cu | 1 + .../TridiagonalMatrixExample_getElement.cpp | 37 ++++++++++ .../TridiagonalMatrixExample_getElement.cu | 1 + .../TridiagonalMatrixExample_getRow.cpp | 45 +++++++++++++ .../TridiagonalMatrixExample_getRow.cu | 1 + ...onalMatrixExample_getSerializationType.cpp | 23 +++++++ ...gonalMatrixExample_getSerializationType.cu | 1 + ...TridiagonalMatrixExample_rowsReduction.cpp | 66 ++++++++++++++++++ .../TridiagonalMatrixExample_rowsReduction.cu | 1 + .../TridiagonalMatrixExample_setElement.cpp | 45 +++++++++++++ .../TridiagonalMatrixExample_setElement.cu | 1 + .../TridiagonalMatrixExample_setElements.cpp | 29 ++++++++ .../TridiagonalMatrixExample_setElements.cu | 1 + ...idiagonalMatrixExample_setElements_map.cpp | 39 +++++++++++ ...ridiagonalMatrixExample_setElements_map.cu | 1 + ...diagonalMatrixExample_setRowCapacities.cpp | 29 ++++++++ ...idiagonalMatrixExample_setRowCapacities.cu | 1 + 34 files changed, 680 insertions(+) create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cu diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cpp new file mode 100644 index 000000000..92524b173 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + + +template< typename Device > +void initializerListExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix { + { 1, 2, 3, 4, 5 }, // row capacities + 6 }; // number of matrix columns + + for( int row = 0; row < matrix.getRows(); row++ ) + for( int column = 0; column <= row; column++ ) + matrix.setElement( row, column, row - column + 1 ); + std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrices on CPU ... " << std::endl; + initializerListExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + initializerListExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cu new file mode 120000 index 000000000..5c6620a5d --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_Constructor_init_list_1.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cpp new file mode 100644 index 000000000..e14f06188 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + + +template< typename Device > +void initializerListExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( + 5, // number of matrix rows + 5, // number of matrix columns + { // matrix elements definition + { 0, 0, 2.0 }, + { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, + { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, + { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, + { 4, 4, 2.0 } } ); + + std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrices on CPU ... " << std::endl; + initializerListExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + initializerListExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cu new file mode 120000 index 000000000..96822f2c3 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_Constructor_init_list_2.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cpp new file mode 100644 index 000000000..8f64d0e9b --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void initializerListExample() +{ + std::map< std::pair< int, int >, double > map; + map.insert( std::make_pair( std::make_pair( 0, 0 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 0 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 1 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 2 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 1 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 2 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 3 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 2 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 3 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 4 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 4, 4 ), 2.0 ) ); + + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, map ); + + std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrices on CPU ... " << std::endl; + initializerListExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + initializerListExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cu new file mode 120000 index 000000000..e1943cb92 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_Constructor_std_map.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cpp new file mode 100644 index 000000000..1b51f494f --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +template< typename Device > +void addElements() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 5, 5, 5, 5, 5 }, 5 ); + 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 HAVE_CUDA + std::cout << "Add elements on CUDA device:" << std::endl; + addElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cu new file mode 120000 index 000000000..be78eddc3 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp new file mode 100644 index 000000000..9ce31f09e --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void allRowsReduction() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { + { 0, 0, 1 }, + { 1, 1, 1 }, { 1, 2, 8 }, + { 2, 2, 1 }, { 2, 3, 9 }, + { 3, 3, 1 }, { 3, 4, 9 }, + { 4, 4, 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.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "All rows reduction on host:" << std::endl; + allRowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "All rows reduction on CUDA device:" << std::endl; + allRowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cu new file mode 120000 index 000000000..795c4febf --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp new file mode 100644 index 000000000..739600539 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +template< typename Device > +void forAllRowsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 2, 3, 4, 5 }, 5 ); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int& columnIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) // This is important, some matrix formats may allocate more matrix elements + // than we requested. These padding elements are processed here as well. + compute = false; + else + { + columnIdx = localIdx; + value = rowIdx + localIdx; + } + }; + + matrix.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forAllRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forAllRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cu new file mode 120000 index 000000000..43736be3f --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_forAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp new file mode 100644 index 000000000..2330c2ca5 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 2, 3, 4, 5 }, 5 ); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int& columnIdx, double& value, bool& compute ) { + if( rowIdx < columnIdx ) // This is important, some matrix formats may allocate more matrix elements + // than we requested. These padding elements are processed here as well. + compute = false; + else + { + columnIdx = localIdx; + value = rowIdx + localIdx; + } + }; + + matrix.forRows( 0, matrix.getRows(), f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cu new file mode 120000 index 000000000..a187b1e67 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cpp new file mode 100644 index 000000000..e56abe484 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +template< typename Device > +void getCompressedRowLengthsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > triangularMatrix( 5, 5 ); + triangularMatrix.setElements( { + { 0, 0, 1 }, + { 1, 0, 2 }, { 1, 1, 3 }, + { 2, 0, 4 }, { 2, 1, 5 }, { 2, 2, 6 }, + { 3, 0, 7 }, { 3, 1, 8 }, { 3, 2, 9 }, { 3, 3, 10 }, + { 4, 0, 11 }, { 4, 1, 12 }, { 4, 2, 13 }, { 4, 3, 14 }, { 4, 4, 15 } } ); + + std::cout << triangularMatrix << std::endl; + + TNL::Containers::Vector< int, Device > rowLengths; + 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 HAVE_CUDA + std::cout << "Getting compressed row lengths on CUDA device: " << std::endl; + getCompressedRowLengthsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cu new file mode 120000 index 000000000..84d47b446 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_getCompressedRowLengths.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cpp new file mode 100644 index 000000000..747a3c825 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > matrix ( 5, 5 ); + matrix->setElements( + { { 0, 0, 1 }, + { 1, 0, 1 }, { 1, 1, 2 }, + { 2, 0, 1 }, { 2, 1, 2 }, { 2, 2, 3 }, + { 3, 0, 1 }, { 3, 1, 2 }, { 3, 2, 3 }, { 3, 3, 4 }, + { 4, 0, 1 }, { 4, 1, 2 }, { 4, 2, 3 }, { 4, 3, 4 }, { 4, 4, 5 } } ); + + /*** + * Fetch lambda function returns diagonal element in each row. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { + auto row = matrix->getRow( rowIdx ); + return row.getValue( rowIdx ); + }; + + /*** + * For the case when Device is CUDA device we need to synchronize smart + * pointers. To avoid this you may use SparseMatrixView. See + * SparseMatrixView::getConstRow example for details. + */ + TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); + + /*** + * Compute the matrix trace. + */ + int trace = TNL::Algorithms::Reduction< Device >::reduce( matrix->getRows(), std::plus<>{}, fetch, 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 HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cu new file mode 120000 index 000000000..5bfb46072 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_getConstRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cpp new file mode 100644 index 000000000..4db765979 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +template< typename Device > +void getElements() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( + 5, // number of matrix rows + 5, // number of matrix columns + { // matrix elements definition + { 0, 0, 2.0 }, + { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, + { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, + { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, + { 4, 4, 2.0 } } ); + + + for( int i = 0; i < 5; i++ ) + { + for( int j = 0; j < 5; j++ ) + std::cout << std::setw( 5 ) << matrix.getElement( i, j ); + std::cout << std::endl; + } +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get elements on host:" << std::endl; + getElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get elements on CUDA device:" << std::endl; + getElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cu new file mode 120000 index 000000000..a5089da55 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_getElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cpp new file mode 100644 index 000000000..d07577ff9 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + auto rowCapacities = { 1, 1, 1, 1, 1 }; // Variadic templates in SharedPointer + // constructor do not recognize initializer + // list so we give it a hint. + using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > matrix( rowCapacities, 5 ); + + auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { + auto row = matrix->getRow( rowIdx ); + row.setElement( 0, rowIdx, 10 * ( rowIdx + 1 ) ); + }; + + /*** + * For the case when Device is CUDA device we need to synchronize smart + * pointers. To avoid this you may use SparseMatrixView. See + * SparseMatrixView::getRow example for details. + */ + TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); + + /*** + * Set the matrix elements. + */ + TNL::Algorithms::ParallelFor< Device >::exec( 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 HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cu new file mode 120000 index 000000000..3e31db140 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_getRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cpp new file mode 100644 index 000000000..35dbf0406 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + + +template< typename Device > +void getSerializationTypeExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix; + + std::cout << "Matrix type is: " << matrix.getSerializationType(); +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get serialization type on CPU ... " << std::endl; + getSerializationTypeExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get serialization type on CUDA GPU ... " << std::endl; + getSerializationTypeExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cu new file mode 120000 index 000000000..4feca977e --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_getSerializationType.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp new file mode 100644 index 000000000..20279888a --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void rowsReduction() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { + { 0, 0, 1 }, + { 1, 1, 1 }, { 1, 2, 8 }, + { 2, 2, 1 }, { 2, 3, 9 }, + { 3, 3, 1 }, { 3, 4, 9 }, + { 4, 4, 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.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Rows reduction on host:" << std::endl; + rowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + rowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cu new file mode 120000 index 000000000..69a58007c --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp new file mode 100644 index 000000000..178e502dc --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void setElements() +{ + auto rowCapacities = { 1, 1, 1, 1, 1 }; + TNL::Pointers::SharedPointer< TNL::Matrices::SparseMatrix< double, Device > > matrix( rowCapacities, 5 ); + for( int i = 0; i < 5; i++ ) + matrix->setElement( i, i, i ); + + std::cout << "Matrix set from the host:" << std::endl; + std::cout << *matrix << std::endl; + + auto f = [=] __cuda_callable__ ( int i ) mutable { + matrix->setElement( i, i, -i ); + }; + + /*** + * For the case when Device is CUDA device we need to synchronize smart + * pointers. To avoid this you may use SparseMatrixView. See + * SparseMatrixView::getRow example for details. + */ + TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); + TNL::Algorithms::ParallelFor< Device >::exec( 0, 5, 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 HAVE_CUDA + std::cout << "Set elements on CUDA device:" << std::endl; + setElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cu new file mode 120000 index 000000000..c693c5c23 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_setElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cpp new file mode 100644 index 000000000..ff0c0bde8 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +template< typename Device > +void setElementsExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5 ); // matrix dimensions + matrix.setElements( { // matrix elements definition + { 0, 0, 2.0 }, + { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, + { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, + { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, + { 4, 4, 2.0 } } ); + + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Setting matrix elements on host: " << std::endl; + setElementsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Setting matrix elements on CUDA device: " << std::endl; + setElementsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cu new file mode 120000 index 000000000..4a08aa926 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_setElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cpp new file mode 100644 index 000000000..c7bdbbc17 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void setElementsExample() +{ + std::map< std::pair< int, int >, double > map; + map.insert( std::make_pair( std::make_pair( 0, 0 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 0 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 1 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 1, 2 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 1 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 2 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 2, 3 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 2 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 3 ), 2.0 ) ); + map.insert( std::make_pair( std::make_pair( 3, 4 ), -1.0 ) ); + map.insert( std::make_pair( std::make_pair( 4, 4 ), 2.0 ) ); + + TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5 ); + matrix.setElements( map ); + + std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrices on CPU ... " << std::endl; + setElementsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + setElementsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cu new file mode 120000 index 000000000..7dd1ac977 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_setElements_map.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cpp new file mode 100644 index 000000000..f282aee6d --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void setRowCapacitiesExample() +{ + TNL::Matrices::SparseMatrix< double, Device > matrix( 5, 5 ); + TNL::Containers::Vector< int, Device > rowCapacities{ 1, 2, 3, 4, 5 }; + matrix.setRowCapacities( rowCapacities ); + for( int row = 0; row < 5; row++ ) + for( int column = 0; column <= row; column++ ) + matrix.setElement( row, column, row - column + 1 ); + + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrices on CPU ... " << std::endl; + setRowCapacitiesExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrices on CUDA GPU ... " << std::endl; + setRowCapacitiesExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cu new file mode 120000 index 000000000..6212f7467 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_setRowCapacities.cpp \ No newline at end of file -- GitLab From 584516d4f5a36776537ba18bc3670e3ca0ac89c6 Mon Sep 17 00:00:00 2001 From: Tomas Oberhuber Date: Tue, 9 Jun 2020 16:24:43 +0200 Subject: [PATCH 34/38] Writting documentation on tridiagonal matrix. --- src/TNL/Matrices/MultidiagonalMatrix.h | 257 +++--- src/TNL/Matrices/Tridiagonal.h | 196 ----- src/TNL/Matrices/TridiagonalMatrix.h | 815 ++++++++++++++++++ ...{Tridiagonal.hpp => TridiagonalMatrix.hpp} | 277 ++---- src/TNL/Matrices/TridiagonalMatrixView.h | 6 +- src/TNL/Matrices/TridiagonalMatrixView.hpp | 27 +- .../Matrices/BinarySparseMatrixCopyTest.h | 2 +- src/UnitTests/Matrices/DenseMatrixCopyTest.h | 6 +- src/UnitTests/Matrices/SparseMatrixCopyTest.h | 6 +- .../Matrices/TridiagonalMatrixTest.h | 80 +- 10 files changed, 1097 insertions(+), 575 deletions(-) delete mode 100644 src/TNL/Matrices/Tridiagonal.h create mode 100644 src/TNL/Matrices/TridiagonalMatrix.h rename src/TNL/Matrices/{Tridiagonal.hpp => TridiagonalMatrix.hpp} (66%) diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index f5c3e6c44..74cd7a4fe 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -21,26 +21,26 @@ namespace TNL { namespace Matrices { /** - * \brief Implementation of sparse multi-diagonal matrix. - * + * \brief Implementation of sparse multidiagonal matrix. + * * Use this matrix type for storing of matrices where the offsets of non-zero elements * from the diagonal are the same in each row. Typically such matrices arise from * discretization of partial differential equations on regular numerical grids. This is * one example (dots represent zero matrix elements): - * + * * \f[ * \left( * \begin{array}{ccccccc} * 4 & -1 & . & -1 & . & . \\ - * -1 & 4 & -1 & . & -1 & . \\ + * -1 & 4 & -1 & . & -1 & . \\ * . & -1 & 4 & -1 & . & -1 \\ * -1 & . & -1 & 4 & -1 & . \\ * . & -1 & . & -1 & 4 & -1 \\ - * . & . & -1 & . & -1 & 4 + * . & . & -1 & . & -1 & 4 * \end{array} * \right) * \f] - * + * * In this matrix, the column indexes in each row \f$i\f$ can be expressed as * \f$\{i-3, i-1, i, i+1, i+3\}\f$ (where the resulting index is non-negative and * smaller than the number of matrix columns). Therefore the diagonals offsets @@ -48,14 +48,14 @@ namespace Matrices { * 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. - * - * Data types | SparseMatrix | MultidiagonalMatrix | Ratio + * + * 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% - * + * 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. * \tparam Index is a type for indexing of the matrix elements. @@ -110,15 +110,15 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > using IndexAllocatorType = IndexAllocator; /** - * \brief Type of related matrix view. - * + * \brief Type of related matrix view. + * * See \ref MultidiagonalMatrixView. */ using ViewType = MultidiagonalMatrixView< Real, Device, Index, Organization >; /** * \brief Matrix view type for constant instances. - * + * * See \ref MultidiagonalMatrixView. */ using ConstViewType = MultidiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; @@ -156,7 +156,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Constructor with matrix dimensions. - * + * * \param rows is number of matrix rows. * \param columns is number of matrix columns. */ @@ -165,11 +165,11 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Constructor with matrix dimensions and matrix elements offsets. - * + * * \param rows is number of matrix rows. * \param columns is number of matrix columns. * \param diagonalsOffsets are offsets of subdiagonals from the main diagonal. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp * \par Output @@ -182,11 +182,11 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Constructor with matrix dimensions and diagonals offsets. - * + * * \param rows is number of matrix rows. * \param columns is number of matrix columns. * \param diagonalsOffsets are offsets of sub-diagonals from the main diagonal. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp * \par Output @@ -199,16 +199,19 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Constructor with matrix dimensions, diagonals offsets and matrix elements. - * + * * The number of matrix rows is deduced from the size of the initializer list \e data. - * + * + * \tparam ListIndex is type used in the initializer list defining matrix diagonals offsets. + * \tparam ListReal is type used in the initializer list defining matrix elements values. + * * \param columns is number of matrix columns. * \param diagonalOffsets are offsets of sub-diagonals from the main diagonal. * \param data is initializer list holding matrix elements. The size of the outer list * defines the number of matrix rows. Each inner list defines values of each sub-diagonal - * and so its size must be lower or equal to the size of \e diagonalsOffsets. Values + * and so its size should be lower or equal to the size of \e diagonalsOffsets. Values * of sub-diagonals which do not fit to given row are omitted. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_2.cpp * \par Output @@ -221,43 +224,43 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Copy constructor. - * + * * \param matrix is an input matrix. */ MultidiagonalMatrix( const MultidiagonalMatrix& matrix ) = default; /** * \brief Move constructor. - * + * * \param matrix is an input matrix. */ MultidiagonalMatrix( MultidiagonalMatrix&& matrix ) = default; /** * \brief Returns a modifiable view of the mutlidiagonal matrix. - * + * * See \ref MultidiagonalMatrixView. - * + * * \return multidiagonal matrix view. */ ViewType getView() const; // TODO: remove const /** * \brief Returns a non-modifiable view of the multidiagonal matrix. - * + * * See \ref MultidiagonalMatrixView. - * + * * \return multidiagonal matrix view. */ ConstViewType getConstView() const; /** * \brief Returns string with serialization type. - * + * * The string has a form `Matrices::MultidiagonalMatrix< RealType, [any_device], IndexType, ElementsOrganization, [any_allocator], [any_allocator] >`. - * + * * \return \ref String with the serialization type. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cpp * \par Output @@ -267,11 +270,11 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Returns string with serialization type. - * + * * See \ref MultidiagonalMatrix::getSerializationType. - * + * * \return \e String with the serialization type. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getSerializationType.cpp * \par Output @@ -281,9 +284,9 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Set matrix dimensions and diagonals offsets. - * + * * \tparam Vector is type of vector like container holding the diagonals offsets. - * + * * \param rows is number of matrix rows. * \param columns is number of matrix columns. * \param diagonalsOffsets is vector with diagonals offsets. @@ -295,14 +298,14 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief This method is for compatibility with \ref SparseMatrix. - * + * * It checks if the number of matrix diagonals is compatible with * required number of non-zero matrix elements in each row. If not * exception is thrown. - * - * \tparam RowCapacitiesVector is vector-like container type for holding required + * + * \tparam RowCapacitiesVector is vector-like container type for holding required * row capacities. - * + * * \param rowCapacities is vector-like container holding required row capacities. */ template< typename RowCapacitiesVector > @@ -310,14 +313,14 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Set matrix elements from an initializer list. - * + * * \tparam ListReal is data type of the initializer list. - * + * * \param data is initializer list holding matrix elements. The size of the outer list * defines the number of matrix rows. Each inner list defines values of each sub-diagonal - * and so its size must be lower or equal to the size of \e diagonalsOffsets. Values + * and so its size should be lower or equal to the size of \e diagonalsOffsets. Values * of sub-diagonals which do not fit to given row are omitted. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElements.cpp * \par Output @@ -328,24 +331,24 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \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. - * + * * \param rowLengths is a vector into which the number of non-zeros in each row * will be stored. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getCompressedRowLengths.cpp * \par Output @@ -359,14 +362,14 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Setup the matrix dimensions and diagonals offsets based on another multidiagonal matrix. - * + * * \tparam Real_ is \e Real type of the source matrix. * \tparam Device_ is \e Device type of the source matrix. * \tparam Index_ is \e Index type of the source matrix. * \tparam Organization_ is \e Organization of the source matrix. * \tparam RealAllocator_ is \e RealAllocator of the source matrix. * \tparam IndexAllocator_ is \e IndexAllocator of the source matrix. - * + * * \param matrix is the source matrix. */ template< typename Real_, @@ -379,10 +382,10 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Returns number of non-zero matrix elements. - * + * * This method really counts the non-zero matrix elements and so * it returns zero for matrix having all allocated elements set to zero. - * + * * \return number of non-zero matrix elements. */ IndexType getNonzeroElementsCount() const; @@ -394,14 +397,14 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Comparison operator with another multidiagonal matrix. - * + * * \tparam Real_ is \e Real type of the source matrix. * \tparam Device_ is \e Device type of the source matrix. * \tparam Index_ is \e Index type of the source matrix. * \tparam Organization_ is \e Organization of the source matrix. * \tparam RealAllocator_ is \e RealAllocator of the source matrix. * \tparam IndexAllocator_ is \e IndexAllocator of the source matrix. - * + * * \return \e true if both matrices are identical and \e false otherwise. */ template< typename Real_, @@ -414,16 +417,16 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Comparison operator with another multidiagonal matrix. - * + * * \tparam Real_ is \e Real type of the source matrix. * \tparam Device_ is \e Device type of the source matrix. * \tparam Index_ is \e Index type of the source matrix. * \tparam Organization_ is \e Organization of the source matrix. * \tparam RealAllocator_ is \e RealAllocator of the source matrix. * \tparam IndexAllocator_ is \e IndexAllocator of the source matrix. - * + * * \param matrix is the source matrix. - * + * * \return \e true if both matrices are NOT identical and \e false otherwise. */ template< typename Real_, @@ -436,16 +439,16 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \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/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp * \par Output * \include MultidiagonalMatrixExample_getRow.out - * + * * See \ref MultidiagonalMatrixRowView. */ __cuda_callable__ @@ -453,16 +456,16 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \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/MultidiagonalMatrix/MultidiagonalMatrixExample_getConstRow.cpp * \par Output * \include MultidiagonalMatrixExample_getConstRow.out - * + * * See \ref MultidiagonalMatrixRowView. */ __cuda_callable__ @@ -470,14 +473,14 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Set all matrix elements to given value. - * + * * \param value is the new value of all matrix elements. */ void setValue( const RealType& value ); /** * \brief Sets element at given \e row and \e column to given \e 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 @@ -485,11 +488,11 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * performance is very low. For higher performance see. \ref MultidiagonalMatrix::getRow * or \ref MultidiagonalMatrix::forRows and \ref MultidiagonalMatrix::forAllRows. * The call may fail if the matrix row capacity is exhausted. - * + * * \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. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_setElement.cpp * \par Output @@ -502,7 +505,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Add element at given \e row and \e column to given \e 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 @@ -510,18 +513,18 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * performance is very low. For higher performance see. \ref MultidiagonalMatrix::getRow * or \ref MultidiagonalMatrix::forRows and \ref MultidiagonalMatrix::forAllRows. * The call may fail if the matrix row capacity is exhausted. - * + * * \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. * \param thisElementMultiplicator is multiplicator the original matrix element * value is multiplied by before addition of given \e value. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_addElement.cpp * \par Output * \include MultidiagonalMatrixExample_addElement.out - * + * */ __cuda_callable__ void addElement( const IndexType row, @@ -531,24 +534,24 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief 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. \ref MultidiagonalMatrix::getRow * or \ref MultidiagonalMatrix::forRows and \ref MultidiagonalMatrix::forAllRows. - * + * * \param row is a row index of the matrix element. * \param column i a column index of the matrix element. - * + * * \return value of given matrix element. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getElement.cpp * \par Output * \include MultidiagonalMatrixExample_getElement.out - * + * */ __cuda_callable__ RealType getElement( const IndexType row, @@ -556,7 +559,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Method for performing general reduction on matrix rows. - * + * * \tparam Fetch is a type of lambda function for data fetch declared as * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. * The return type of this lambda can be any non void. @@ -565,14 +568,14 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \tparam Keep is a type of lambda function for storing results of reduction in each row. * It is declared as `keep( const IndexType rowIdx, const double& value )`. * \tparam FetchValue is type returned by the Fetch lambda function. - * + * * \param begin defines beginning of the range [begin,end) of rows to be processed. * \param end defines ending of the range [begin,end) of rows to be processed. * \param fetch is an instance of lambda function for data fetch. * \param reduce is an instance of lambda function for reduction. * \param keep in an instance of lambda function for storing results. * \param zero is zero of given reduction operation also known as idempotent element. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp * \par Output @@ -583,7 +586,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Method for performing general reduction on matrix rows for constant instances. - * + * * \tparam Fetch is a type of lambda function for data fetch declared as * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. * The return type of this lambda can be any non void. @@ -592,14 +595,14 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \tparam Keep is a type of lambda function for storing results of reduction in each row. * It is declared as `keep( const IndexType rowIdx, const double& value )`. * \tparam FetchValue is type returned by the Fetch lambda function. - * + * * \param begin defines beginning of the range [begin,end) of rows to be processed. * \param end defines ending of the range [begin,end) of rows to be processed. * \param fetch is an instance of lambda function for data fetch. * \param reduce is an instance of lambda function for reduction. * \param keep in an instance of lambda function for storing results. * \param zero is zero of given reduction operation also known as idempotent element. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp * \par Output @@ -610,7 +613,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Method for performing general reduction on all matrix rows. - * + * * \tparam Fetch is a type of lambda function for data fetch declared as * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. * The return type of this lambda can be any non void. @@ -619,12 +622,12 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \tparam Keep is a type of lambda function for storing results of reduction in each row. * It is declared as `keep( const IndexType rowIdx, const double& value )`. * \tparam FetchValue is type returned by the Fetch lambda function. - * + * * \param fetch is an instance of lambda function for data fetch. * \param reduce is an instance of lambda function for reduction. * \param keep in an instance of lambda function for storing results. * \param zero is zero of given reduction operation also known as idempotent element. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp * \par Output @@ -635,7 +638,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Method for performing general reduction on all matrix rows for constant instances. - * + * * \tparam Fetch is a type of lambda function for data fetch declared as * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. * The return type of this lambda can be any non void. @@ -644,12 +647,12 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \tparam Keep is a type of lambda function for storing results of reduction in each row. * It is declared as `keep( const IndexType rowIdx, const double& value )`. * \tparam FetchValue is type returned by the Fetch lambda function. - * + * * \param fetch is an instance of lambda function for data fetch. * \param reduce is an instance of lambda function for reduction. * \param keep in an instance of lambda function for storing results. * \param zero is zero of given reduction operation also known as idempotent element. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp * \par Output @@ -659,57 +662,57 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** - * \brief Method for iteration over all matrix rows for constant instances. - * + * \brief Method for iteration over matrix rows for constant instances. + * * \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 + * 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 * be interrupted. - * + * * \param begin defines beginning of the range [begin,end) of rows to be processed. * \param end defines ending of the range [begin,end) of rows to be processed. * \param function is an instance of the lambda function to be called in each row. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp * \par Output * \include MultidiagonalMatrixExample_forRows.out */ template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ) const; + void forRows( IndexType begin, IndexType end, Function& function ) const; /** - * \brief Method for iteration over all matrix rows for non-constant instances. - * + * \brief Method for iteration over matrix rows for non-constant instances. + * * \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 + * 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 * be interrupted. - * + * * \param begin defines beginning of the range [begin,end) of rows to be processed. * \param end defines ending of the range [begin,end) of rows to be processed. * \param function is an instance of the lambda function to be called in each row. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp * \par Output * \include MultidiagonalMatrixExample_forRows.out */ template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ); + void forRows( IndexType begin, IndexType end, Function& function ); /** * \brief This method calls \e forRows for all matrix rows (for constant instances). - * + * * See \ref MultidiagonalMatrix::forRows. - * + * * \tparam Function is a type of lambda function that will operate on matrix elements. * \param function is an instance of the lambda function to be called in each row. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cpp * \par Output @@ -720,12 +723,12 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief This method calls \e forRows for all matrix rows. - * + * * See \ref MultidiagonalMatrix::forRows. - * + * * \tparam Function is a type of lambda function that will operate on matrix elements. * \param function is an instance of the lambda function to be called in each row. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllRows.cpp * \par Output @@ -736,16 +739,16 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Computes product of matrix and vector. - * + * * More precisely, it computes: - * + * * `outVector = matrixMultiplicator * ( * this ) * inVector + outVectorMultiplicator * outVector` - * + * * \tparam InVector is type of input vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. * \tparam OutVector is type of output vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. - * + * * \param inVector is input vector. * \param outVector is output vector. * \param matrixMultiplicator is a factor by which the matrix is multiplied. It is one by default. @@ -783,7 +786,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Assignment of exactly the same matrix type. - * + * * \param matrix is input matrix for the assignment. * \return reference to this matrix. */ @@ -791,7 +794,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Assignment of another multidiagonal matrix - * + * * \param matrix is input matrix for the assignment. * \return reference to this matrix. */ @@ -805,58 +808,58 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Method for saving the matrix to a file. - * + * * \param file is the output file. */ void save( File& file ) const; /** * \brief Method for loading the matrix from a file. - * + * * \param file is the input file. */ void load( File& file ); /** * \brief Method for saving the matrix to the file with given filename. - * + * * \param fileName is name of the file. */ void save( const String& fileName ) const; /** * \brief Method for loading the matrix from the file with given filename. - * + * * \param fileName is name of the file. */ void load( const String& fileName ); /** * \brief Method for printing the matrix to output stream. - * + * * \param str is the output stream. */ void print( std::ostream& str ) const; /** * \brief This method returns matrix elements indexer used by this matrix. - * + * * \return constant reference to the indexer. */ const IndexerType& getIndexer() const; /** * \brief This method returns matrix elements indexer used by this matrix. - * + * * \return non-constant reference to the indexer. */ IndexerType& getIndexer(); /** * \brief Returns padding index denoting padding zero elements. - * + * * These elements are used for efficient data alignment in memory. - * + * * \return value of the padding index. */ __cuda_callable__ diff --git a/src/TNL/Matrices/Tridiagonal.h b/src/TNL/Matrices/Tridiagonal.h deleted file mode 100644 index 25472aa3c..000000000 --- a/src/TNL/Matrices/Tridiagonal.h +++ /dev/null @@ -1,196 +0,0 @@ -/*************************************************************************** - Tridiagonal.h - description - ------------------- - begin : Nov 30, 2013 - copyright : (C) 2013 by Tomas Oberhuber - email : tomas.oberhuber@fjfi.cvut.cz - ***************************************************************************/ - -/* See Copyright Notice in tnl/Copyright */ - -#pragma once - -#include -#include -#include -#include -#include -#include - -namespace TNL { -namespace Matrices { - -template< typename Real = double, - typename Device = Devices::Host, - typename Index = int, - ElementsOrganization Organization = Containers::Segments::DefaultElementsOrganization< Device >::getOrganization(), - typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real > > -class Tridiagonal : public Matrix< Real, Device, Index, RealAllocator > -{ - public: - using RealType = Real; - using DeviceType = Device; - using IndexType = Index; - using RealAllocatorType = RealAllocator; - using BaseType = Matrix< Real, Device, Index, RealAllocator >; - using IndexerType = details::TridiagonalMatrixIndexer< IndexType, Organization >; - using ValuesVectorType = typename BaseType::ValuesVectorType; - using ValuesViewType = typename ValuesVectorType::ViewType; - using ViewType = TridiagonalMatrixView< Real, Device, Index, Organization >; - using ConstViewType = TridiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; - using RowView = TridiagonalMatrixRowView< ValuesViewType, IndexerType >; - - // TODO: remove this - it is here only for compatibility with original matrix implementation - typedef Containers::Vector< IndexType, DeviceType, IndexType > CompressedRowLengthsVector; - typedef Containers::VectorView< IndexType, DeviceType, IndexType > CompressedRowLengthsVectorView; - typedef typename CompressedRowLengthsVectorView::ConstViewType ConstCompressedRowLengthsVectorView; - - template< typename _Real = Real, - typename _Device = Device, - typename _Index = Index > - using Self = Tridiagonal< _Real, _Device, _Index >; - - static constexpr ElementsOrganization getOrganization() { return Organization; }; - - Tridiagonal(); - - Tridiagonal( const IndexType rows, const IndexType columns ); - - ViewType getView() const; // TODO: remove const - - //ConstViewType getConstView() const; - - static String getSerializationType(); - - virtual String getSerializationTypeVirtual() const; - - void setDimensions( const IndexType rows, - const IndexType columns ); - - //template< typename Vector > - void setCompressedRowLengths( const ConstCompressedRowLengthsVectorView rowCapacities ); - - template< typename Vector > - void getCompressedRowLengths( Vector& rowLengths ) const; - - [[deprecated]] - IndexType getRowLength( const IndexType row ) const; - - IndexType getMaxRowLength() const; - - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - void setLike( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& m ); - - IndexType getNumberOfNonzeroMatrixElements() const; - - void reset(); - - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - bool operator == ( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const; - - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - bool operator != ( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const; - - __cuda_callable__ - RowView getRow( const IndexType& rowIdx ); - - __cuda_callable__ - const RowView getRow( const IndexType& rowIdx ) const; - - void setValue( const RealType& v ); - - void setElement( const IndexType row, - const IndexType column, - const RealType& value ); - - void addElement( const IndexType row, - const IndexType column, - const RealType& value, - const RealType& thisElementMultiplicator = 1.0 ); - - RealType getElement( const IndexType row, - const IndexType column ) const; - - template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; - - template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; - - template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ) const; - - template< typename Function > - void forRows( IndexType first, IndexType last, Function& function ); - - template< typename Function > - void forAllRows( Function& function ) const; - - template< typename Function > - void forAllRows( Function& function ); - - template< typename Vector > - __cuda_callable__ - typename Vector::RealType rowVectorProduct( const IndexType row, - const Vector& vector ) const; - - template< typename InVector, - typename OutVector > - void vectorProduct( const InVector& inVector, - OutVector& outVector ) const; - - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - void addMatrix( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix, - const RealType& matrixMultiplicator = 1.0, - const RealType& thisMatrixMultiplicator = 1.0 ); - - template< typename Real2, typename Index2 > - void getTransposition( const Tridiagonal< Real2, Device, Index2 >& matrix, - const RealType& matrixMultiplicator = 1.0 ); - - template< typename Vector1, typename Vector2 > - __cuda_callable__ - void performSORIteration( const Vector1& b, - const IndexType row, - Vector2& x, - const RealType& omega = 1.0 ) const; - - // copy assignment - Tridiagonal& operator=( const Tridiagonal& matrix ); - - // cross-device copy assignment - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > - Tridiagonal& operator=( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ); - - void save( File& file ) const; - - void load( File& file ); - - void save( const String& fileName ) const; - - void load( const String& fileName ); - - void print( std::ostream& str ) const; - - const IndexerType& getIndexer() const; - - IndexerType& getIndexer(); - - __cuda_callable__ - IndexType getPaddingIndex() const; - - protected: - - __cuda_callable__ - IndexType getElementIndex( const IndexType row, - const IndexType localIdx ) const; - - IndexerType indexer; - - ViewType view; -}; - -} // namespace Matrices -} // namespace TNL - -#include diff --git a/src/TNL/Matrices/TridiagonalMatrix.h b/src/TNL/Matrices/TridiagonalMatrix.h new file mode 100644 index 000000000..0f0fb8d1c --- /dev/null +++ b/src/TNL/Matrices/TridiagonalMatrix.h @@ -0,0 +1,815 @@ +/*************************************************************************** + TridiagonalMatrix.h - description + ------------------- + begin : Nov 30, 2013 + copyright : (C) 2013 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace TNL { +namespace Matrices { + +/** + * \brief Implementation of sparse tridiagonal matrix. + * + * Use this matrix type for storing of tridiagonal matrices i.e., matrices having + * non-zero matrix elements only on its diagonal and immediately above and bellow the diagonal. + * This is an example: + * \f[ + * \left( + * \begin{array}{ccccccc} + * 4 & -1 & . & . & . & . \\ + * -1 & 4 & -1 & . & . & . \\ + * . & -1 & 4 & -1 & . & . \\ + * . & . & -1 & 4 & -1 & . \\ + * . & . & . & -1 & 4 & -1 \\ + * . & . & . & . & -1 & 4 + * \end{array} + * \right) + * \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. + * + * 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% + * + * \tparam Real is a type of matrix elements. + * \tparam Device is a device where the matrix is allocated. + * \tparam Index is a type for indexing of the matrix elements. + * \tparam Organization tells the ordering of matrix elements. It is either RowMajorOrder + * or ColumnMajorOrder. + * \tparam RealAllocator is allocator for the matrix elements. + */ +template< typename Real = double, + typename Device = Devices::Host, + typename Index = int, + ElementsOrganization Organization = Containers::Segments::DefaultElementsOrganization< Device >::getOrganization(), + typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real > > +class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > +{ + public: + + // Supporting types - they are not important for the user + using BaseType = Matrix< Real, Device, Index, RealAllocator >; + using IndexerType = details::TridiagonalMatrixIndexer< Index, Organization >; + using ValuesVectorType = typename BaseType::ValuesVectorType; + using ValuesViewType = typename ValuesVectorType::ViewType; + + /** + * \brief The type of matrix elements. + */ + using RealType = Real; + + /** + * \brief The device where the matrix is allocated. + */ + using DeviceType = Device; + + /** + * \brief The type used for matrix elements indexing. + */ + using IndexType = Index; + + /** + * \brief The allocator for matrix elements values. + */ + using RealAllocatorType = RealAllocator; + + /** + * \brief Type of related matrix view. + * + * See \ref TridiagonalMatrixView. + */ + using ViewType = TridiagonalMatrixView< Real, Device, Index, Organization >; + + /** + * \brief Matrix view type for constant instances. + * + * See \ref TridiagonalMatrixView. + */ + using ConstViewType = TridiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; + + /** + * \brief Type for accessing matrix rows. + */ + using RowView = TridiagonalMatrixRowView< ValuesViewType, IndexerType >; + + + /** + * \brief Helper type for getting self type or its modifications. + */ + template< typename _Real = Real, + typename _Device = Device, + typename _Index = Index > + using Self = TridiagonalMatrix< _Real, _Device, _Index >; + + // TODO: remove this - it is here only for compatibility with original matrix implementation + //typedef Containers::Vector< IndexType, DeviceType, IndexType > CompressedRowLengthsVector; + //typedef Containers::VectorView< IndexType, DeviceType, IndexType > CompressedRowLengthsVectorView; + //typedef typename CompressedRowLengthsVectorView::ConstViewType ConstCompressedRowLengthsVectorView; + + static constexpr ElementsOrganization getOrganization() { return Organization; }; + + /** + * \brief Constructor with no parameters. + */ + TridiagonalMatrix(); + + /** + * \brief Constructor with matrix dimensions. + * + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + */ + TridiagonalMatrix( const IndexType rows, const IndexType columns ); + + /** + * \brief Constructor with matrix dimensions, diagonals offsets and matrix elements. + * + * The number of matrix rows is deduced from the size of the initializer list \e data. + * + * \tparam ListReal is type used in the initializer list defining matrix elements values. + * + * \param columns is number of matrix columns. + * \param data is initializer list holding matrix elements. The size of the outer list + * defines the number of matrix rows. Each inner list defines values of each sub-diagonal + * and so its size should be lower or equal to three. Values + * of sub-diagonals which do not fit to given row are omitted. + * + * \par Example + * \include Matrices/TridiagonalMatrix/tridiagonalMatrixExample_Constructor_init_list_1.cpp + * \par Output + * \include TridiagonalMatrixExample_Constructor_init_list_1.out + */ + template< typename ListReal > + TridiagonalMatrix( const IndexType columns, + const std::initializer_list< std::initializer_list< ListReal > >& data ); + /** + * \brief Copy constructor. + * + * \param matrix is an input matrix. + */ + TridiagonalMatrix( const TridiagonalMatrix& matrix ) = default; + + /** + * \brief Move constructor. + * + * \param matrix is an input matrix. + */ + TridiagonalMatrix( TridiagonalMatrix&& matrix ) = default; + + /** + * \brief Returns a modifiable view of the tridiagonal matrix. + * + * See \ref TridiagonalMatrixView. + * + * \return tridiagonal matrix view. + */ + ViewType getView() const; // TODO: remove const + + /** + * \brief Returns a non-modifiable view of the tridiagonal matrix. + * + * See \ref TridiagonalMatrixView. + * + * \return tridiagonal matrix view. + */ + ConstViewType getConstView() const; + + /** + * \brief Returns string with serialization type. + * + * The string has a form `Matrices::TridiagonalMatrix< RealType, [any_device], IndexType, ElementsOrganization, [any_allocator] >`. + * + * \return \ref String with the serialization type. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cpp + * \par Output + * \include TridiagonalMatrixExample_getSerializationType.out + */ + static String getSerializationType(); + + /** + * \brief Returns string with serialization type. + * + * See \ref TridiagonalMatrix::getSerializationType. + * + * \return \e String with the serialization type. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cpp + * \par Output + * \include TridiagonalMatrixExample_getSerializationType.out + */ + virtual String getSerializationTypeVirtual() const; + + /** + * \brief Set matrix dimensions. + * + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + */ + void setDimensions( const IndexType rows, + const IndexType columns ); + + /** + * \brief This method is for compatibility with \ref SparseMatrix. + * + * It checks if the number of matrix diagonals is compatible with + * required number of non-zero matrix elements in each row. If not + * exception is thrown. + * + * \tparam RowCapacitiesVector is vector-like container type for holding required + * row capacities. + * + * \param rowCapacities is vector-like container holding required row capacities. + */ + template< typename RowCapacitiesVector > + void setRowCapacities( const RowCapacitiesVector& rowCapacities ); + + /** + * \brief Set matrix elements from an initializer list. + * + * \tparam ListReal is data type of the initializer list. + * + * \param data is initializer list holding matrix elements. The size of the outer list + * defines the number of matrix rows. Each inner list defines values of each sub-diagonal + * and so its size should be lower or equal to three. Values + * of sub-diagonals which do not fit to given row are omitted. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cpp + * \par Output + * \include TridiagonalMatrixExample_setElements.out + */ + template< typename ListReal > + void setElements( const std::initializer_list< std::initializer_list< ListReal > >& data ); + + /** + * \brief Computes number of non-zeros in each row. + * + * \param rowLengths is a vector into which the number of non-zeros in each row + * will be stored. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cpp + * \par Output + * \include TridiagonalMatrixExample_getCompressedRowLengths.out + */ + template< typename Vector > + void getCompressedRowLengths( Vector& rowLengths ) const; + + [[deprecated]] + IndexType getRowLength( const IndexType row ) const; + + //IndexType getMaxRowLength() const; + + /** + * \brief Setup the matrix dimensions and diagonals offsets based on another tridiagonal matrix. + * + * \tparam Real_ is \e Real type of the source matrix. + * \tparam Device_ is \e Device type of the source matrix. + * \tparam Index_ is \e Index type of the source matrix. + * \tparam Organization_ is \e Organization of the source matrix. + * \tparam RealAllocator_ is \e RealAllocator of the source matrix. + * + * \param matrix is the source matrix. + */ + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_, + typename RealAllocator_ > + void setLike( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ); + + /** + * \brief Returns number of non-zero matrix elements. + * + * This method really counts the non-zero matrix elements and so + * it returns zero for matrix having all allocated elements set to zero. + * + * \return number of non-zero matrix elements. + */ + IndexType getNonzeroElementsCount() const; + + /** + * \brief Resets the matrix to zero dimensions. + */ + void reset(); + + /** + * \brief Comparison operator with another tridiagonal matrix. + * + * \tparam Real_ is \e Real type of the source matrix. + * \tparam Device_ is \e Device type of the source matrix. + * \tparam Index_ is \e Index type of the source matrix. + * \tparam Organization_ is \e Organization of the source matrix. + * \tparam RealAllocator_ is \e RealAllocator of the source matrix. + * + * \return \e true if both matrices are identical and \e false otherwise. + */ + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_, + typename RealAllocator_ > + bool operator == ( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const; + + /** + * \brief Comparison operator with another tridiagonal matrix. + * + * \tparam Real_ is \e Real type of the source matrix. + * \tparam Device_ is \e Device type of the source matrix. + * \tparam Index_ is \e Index type of the source matrix. + * \tparam Organization_ is \e Organization of the source matrix. + * \tparam RealAllocator_ is \e RealAllocator of the source matrix. + * + * \param matrix is the source matrix. + * + * \return \e true if both matrices are NOT identical and \e false otherwise. + */ + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_, + typename RealAllocator_ > + bool operator != ( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) 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/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cpp + * \par Output + * \include TridiagonalMatrixExample_getRow.out + * + * See \ref TridiagonalMatrixRowView. + */ + __cuda_callable__ + RowView getRow( const IndexType& rowIdx ); + + /** + * \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/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cpp + * \par Output + * \include TridiagonalMatrixExample_getConstRow.out + * + * See \ref TridiagonalMatrixRowView. + */ + __cuda_callable__ + const RowView getRow( const IndexType& rowIdx ) const; + + /** + * \brief Set all matrix elements to given value. + * + * \param value is the new value of all matrix elements. + */ + void setValue( const RealType& value ); + + /** + * \brief Sets element at given \e row and \e column to given \e 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. \ref TridiagonalMatrix::getRow + * or \ref TridiagonalMatrix::forRows and \ref TridiagonalMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp + * \par Output + * \include TridiagonalMatrixExample_setElement.out + */ + void setElement( const IndexType row, + const IndexType column, + const RealType& value ); + + /** + * \brief Add element at given \e row and \e column to given \e 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. \ref TridiagonalMatrix::getRow + * or \ref TridiagonalMatrix::forRows and \ref TridiagonalMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * \param thisElementMultiplicator is multiplicator the original matrix element + * value is multiplied by before addition of given \e value. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cpp + * \par Output + * \include TridiagonalMatrixExample_addElement.out + * + */ + void addElement( const IndexType row, + const IndexType column, + const RealType& value, + const RealType& thisElementMultiplicator = 1.0 ); + + /** + * \brief 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. \ref TridiagonalMatrix::getRow + * or \ref TridiagonalMatrix::forRows and \ref TridiagonalMatrix::forAllRows. + * + * \param row is a row index of the matrix element. + * \param column i a column index of the matrix element. + * + * \return value of given matrix element. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cpp + * \par Output + * \include TridiagonalMatrixExample_getElement.out + */ + RealType getElement( const IndexType row, + const IndexType column ) const; + + /** + * \brief Method for performing general reduction on matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp + * \par Output + * \include TridiagonalMatrixExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + + /** + * \brief Method for performing general reduction on matrix rows of constant matrix instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp + * \par Output + * \include TridiagonalMatrixExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + + /** + * \brief Method for performing general reduction on all matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp + * \par Output + * \include TridiagonalMatrixExample_allRowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + + /** + * \brief Method for performing general reduction on all matrix rows of constant matrix instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp + * \par Output + * \include TridiagonalMatrixExample_allRowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + + /** + * \brief Method for iteration over matrix rows for constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/TridiagonalMatrixExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function& function ) const; + + /** + * \brief Method for iteration over matrix rows for non-constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/TridiagonalMatrixExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function& function ); + + /** + * \brief Method for iteration over all matrix rows for constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp + * \par Output + * \include TridiagonalMatrixExample_forAllRows.out + */ + template< typename Function > + void forAllRows( Function& function ) const; + + /** + * \brief Method for iteration over all matrix rows for non-constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp + * \par Output + * \include TridiagonalMatrixExample_forAllRows.out + */ + template< typename Function > + void forAllRows( Function& function ); + + /*template< typename Vector > + __cuda_callable__ + typename Vector::RealType rowVectorProduct( const IndexType row, + const Vector& vector ) const;*/ + + /** + * \brief Computes product of matrix and vector. + * + * More precisely, it computes: + * + * `outVector = matrixMultiplicator * ( * this ) * inVector + outVectorMultiplicator * outVector` + * + * \tparam InVector is type of input vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * \tparam OutVector is type of output vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * + * \param inVector is input vector. + * \param outVector is output vector. + * \param matrixMultiplicator is a factor by which the matrix is multiplied. It is one by default. + * \param outVectorMultiplicator is a factor by which the outVector is multiplied before added + * to the result of matrix-vector product. It is zero by default. + * \param begin is the beginning of the rows range for which the vector product + * is computed. It is zero by default. + * \param end is the end of the rows range for which the vector product + * is computed. It is number if the matrix rows by default. + */ + template< typename InVector, + typename OutVector > + void vectorProduct( const InVector& inVector, + OutVector& outVector, + const RealType matrixMultiplicator = 1.0, + const RealType outVectorMultiplicator = 0.0, + const IndexType begin = 0, + IndexType end = 0 ) const; + + template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > + void addMatrix( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix, + const RealType& matrixMultiplicator = 1.0, + const RealType& thisMatrixMultiplicator = 1.0 ); + + template< typename Real2, typename Index2 > + void getTransposition( const TridiagonalMatrix< Real2, Device, Index2 >& matrix, + const RealType& matrixMultiplicator = 1.0 ); + + template< typename Vector1, typename Vector2 > + __cuda_callable__ + void performSORIteration( const Vector1& b, + const IndexType row, + Vector2& x, + const RealType& omega = 1.0 ) const; + + /** + * \brief Assignment of exactly the same matrix type. + * + * \param matrix is input matrix for the assignment. + * \return reference to this matrix. + */ + TridiagonalMatrix& operator=( const TridiagonalMatrix& matrix ); + + /** + * \brief Assignment of another tridiagonal matrix + * + * \param matrix is input matrix for the assignment. + * \return reference to this matrix. + */ + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_, + typename RealAllocator_ > + TridiagonalMatrix& operator=( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ); + + /** + * \brief Method for saving the matrix to a file. + * + * \param file is the output file. + */ + void save( File& file ) const; + + /** + * \brief Method for loading the matrix from a file. + * + * \param file is the input file. + */ + void load( File& file ); + + /** + * \brief Method for saving the matrix to the file with given filename. + * + * \param fileName is name of the file. + */ + void save( const String& fileName ) const; + + /** + * \brief Method for loading the matrix from the file with given filename. + * + * \param fileName is name of the file. + */ + void load( const String& fileName ); + + /** + * \brief Method for printing the matrix to output stream. + * + * \param str is the output stream. + */ + void print( std::ostream& str ) const; + + /** + * \brief This method returns matrix elements indexer used by this matrix. + * + * \return constant reference to the indexer. + */ + const IndexerType& getIndexer() const; + + /** + * \brief This method returns matrix elements indexer used by this matrix. + * + * \return non-constant reference to the indexer. + */ + IndexerType& getIndexer(); + + /** + * \brief Returns padding index denoting padding zero elements. + * + * These elements are used for efficient data alignment in memory. + * + * \return value of the padding index. + */ + __cuda_callable__ + IndexType getPaddingIndex() const; + + protected: + + __cuda_callable__ + IndexType getElementIndex( const IndexType row, + const IndexType localIdx ) const; + + IndexerType indexer; + + ViewType view; +}; + +} // namespace Matrices +} // namespace TNL + +#include diff --git a/src/TNL/Matrices/Tridiagonal.hpp b/src/TNL/Matrices/TridiagonalMatrix.hpp similarity index 66% rename from src/TNL/Matrices/Tridiagonal.hpp rename to src/TNL/Matrices/TridiagonalMatrix.hpp index e4ec42563..76d9afc9f 100644 --- a/src/TNL/Matrices/Tridiagonal.hpp +++ b/src/TNL/Matrices/TridiagonalMatrix.hpp @@ -1,5 +1,5 @@ /*************************************************************************** - Tridiagonal.hpp - description + TridiagonalMatrix.hpp - description ------------------- begin : Nov 30, 2013 copyright : (C) 2013 by Tomas Oberhuber @@ -12,22 +12,19 @@ #include #include -#include +#include #include namespace TNL { namespace Matrices { -template< typename Device > -class TridiagonalDeviceDependentCode; - template< typename Real, typename Device, typename Index, ElementsOrganization Organization, typename RealAllocator > -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -Tridiagonal() +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix() { } @@ -36,8 +33,8 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -Tridiagonal( const IndexType rows, const IndexType columns ) +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix( const IndexType rows, const IndexType columns ) { this->setDimensions( rows, columns ); } @@ -48,11 +45,11 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > auto -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getView() const -> ViewType { // TODO: fix when getConstView works - return ViewType( const_cast< Tridiagonal* >( this )->values.getView(), indexer ); + return ViewType( const_cast< TridiagonalMatrix* >( this )->values.getView(), indexer ); } /*template< typename Real, @@ -61,7 +58,7 @@ getView() const -> ViewType ElementsOrganization Organization, typename RealAllocator > auto -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getConstView() const -> ConstViewType { return ConstViewType( this->values.getConstView(), indexer ); @@ -73,10 +70,10 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > String -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getSerializationType() { - return String( "Matrices::Tridiagonal< " ) + + return String( "Matrices::TridiagonalMatrix< " ) + TNL::getSerializationType< RealType >() + ", [any_device], " + TNL::getSerializationType< IndexType >() + ", " + ( Organization ? "true" : "false" ) + ", [any_allocator] >"; @@ -88,7 +85,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > String -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getSerializationTypeVirtual() const { return this->getSerializationType(); @@ -100,7 +97,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: setDimensions( const IndexType rows, const IndexType columns ) { Matrix< Real, Device, Index >::setDimensions( rows, columns ); @@ -115,24 +112,24 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > - // template< typename Vector > + template< typename RowCapacitiesVector > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -setCompressedRowLengths( const ConstCompressedRowLengthsVectorView rowLengths ) +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +setRowCapacities( const RowCapacitiesVector& rowCapacities ) { - if( max( rowLengths ) > 3 ) + if( max( rowCapacities ) > 3 ) throw std::logic_error( "Too many non-zero elements per row in a tri-diagonal matrix." ); - if( rowLengths.getElement( 0 ) > 2 ) + if( rowCapacities.getElement( 0 ) > 2 ) throw std::logic_error( "Too many non-zero elements per row in a tri-diagonal matrix." ); const IndexType diagonalLength = min( this->getRows(), this->getColumns() ); if( this->getRows() > this->getColumns() ) - if( rowLengths.getElement( this->getRows()-1 ) > 1 ) + if( rowCapacities.getElement( this->getRows()-1 ) > 1 ) throw std::logic_error( "Too many non-zero elements per row in a tri-diagonal matrix." ); if( this->getRows() == this->getColumns() ) - if( rowLengths.getElement( this->getRows()-1 ) > 2 ) + if( rowCapacities.getElement( this->getRows()-1 ) > 2 ) throw std::logic_error( "Too many non-zero elements per row in a tri-diagonal matrix." ); if( this->getRows() < this->getColumns() ) - if( rowLengths.getElement( this->getRows()-1 ) > 3 ) + if( rowCapacities.getElement( this->getRows()-1 ) > 3 ) throw std::logic_error( "Too many non-zero elements per row in a tri-diagonal matrix." ); } @@ -143,7 +140,7 @@ template< typename Real, typename RealAllocator > template< typename Vector > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getCompressedRowLengths( Vector& rowLengths ) const { return this->view.getCompressedRowLengths( rowLengths ); @@ -155,23 +152,23 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > Index -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getRowLength( const IndexType row ) const { return this->view.getRowLength( row ); } -template< typename Real, +/*template< typename Real, typename Device, typename Index, ElementsOrganization Organization, typename RealAllocator > Index -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getMaxRowLength() const { return this->view.getMaxRowLength(); -} +}*/ template< typename Real, typename Device, @@ -180,8 +177,8 @@ template< typename Real, typename RealAllocator > template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -setLike( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& m ) +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +setLike( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& m ) { this->setDimensions( m.getRows(), m.getColumns() ); } @@ -192,8 +189,8 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > Index -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -getNumberOfNonzeroMatrixElements() const +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +getNonzeroElementsCount() const { return this->view.getNumberOfNonzeroMatrixElements(); } @@ -204,7 +201,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: reset() { Matrix< Real, Device, Index >::reset(); @@ -217,8 +214,8 @@ template< typename Real, typename RealAllocator > template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > bool -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -operator == ( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +operator == ( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const { if( Organization == Organization_ ) return this->values == matrix.values; @@ -235,8 +232,8 @@ template< typename Real, typename RealAllocator > template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > bool -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -operator != ( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +operator != ( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) const { return ! this->operator==( matrix ); } @@ -247,7 +244,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: setValue( const RealType& v ) { this->view.setValue( v ); @@ -260,7 +257,7 @@ template< typename Real, typename RealAllocator > __cuda_callable__ auto -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getRow( const IndexType& rowIdx ) const -> const RowView { return this->view.getRow( rowIdx ); @@ -273,7 +270,7 @@ template< typename Real, typename RealAllocator > __cuda_callable__ auto -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getRow( const IndexType& rowIdx ) -> RowView { return this->view.getRow( rowIdx ); @@ -285,7 +282,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: setElement( const IndexType row, const IndexType column, const RealType& value ) { this->view.setElement( row, column, value ); @@ -297,7 +294,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: addElement( const IndexType row, const IndexType column, const RealType& value, @@ -312,7 +309,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > Real -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getElement( const IndexType row, const IndexType column ) const { return this->view.getElement( row, column ); @@ -325,7 +322,7 @@ template< typename Real, typename RealAllocator > template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const { this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); @@ -338,7 +335,7 @@ template< typename Real, typename RealAllocator > template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const { this->view.rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); @@ -351,7 +348,7 @@ template< typename Real, typename RealAllocator > template< typename Function > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: forRows( IndexType first, IndexType last, Function& function ) const { this->view.forRows( first, last, function ); @@ -364,7 +361,7 @@ template< typename Real, typename RealAllocator > template< typename Function > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: forRows( IndexType first, IndexType last, Function& function ) { this->view.forRows( first, last, function ); @@ -377,7 +374,7 @@ template< typename Real, typename RealAllocator > template< typename Function > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: forAllRows( Function& function ) const { this->view.forRows( 0, this->getRows(), function ); @@ -390,7 +387,7 @@ template< typename Real, typename RealAllocator > template< typename Function > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: forAllRows( Function& function ) { this->view.forRows( 0, this->getRows(), function ); @@ -401,27 +398,17 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > -template< typename Vector > -__cuda_callable__ -typename Vector::RealType -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -rowVectorProduct( const IndexType row, const Vector& vector ) const -{ - return this->view.rowVectorProduct(); -} - -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization, - typename RealAllocator > - template< typename InVector, - typename OutVector > + template< typename InVector, typename OutVector > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -vectorProduct( const InVector& inVector, OutVector& outVector ) const +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +vectorProduct( const InVector& inVector, + OutVector& outVector, + const RealType matrixMultiplicator, + const RealType outVectorMultiplicator, + const IndexType begin, + IndexType end ) const { - this->view.vectorProduct( inVector, outVector ); + this->view.vectorProduct( inVector, outVector, matrixMultiplicator, outVectorMultiplicator, begin, end ); } template< typename Real, @@ -431,8 +418,8 @@ template< typename Real, typename RealAllocator > template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -addMatrix( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix, +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +addMatrix( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix, const RealType& matrixMultiplicator, const RealType& thisMatrixMultiplicator ) { @@ -444,8 +431,8 @@ template< typename Real, typename Real2, typename Index, typename Index2 > -__global__ void TridiagonalTranspositionCudaKernel( const Tridiagonal< Real2, Devices::Cuda, Index2 >* inMatrix, - Tridiagonal< Real, Devices::Cuda, Index >* outMatrix, +__global__ void TridiagonalMatrixTranspositionCudaKernel( const TridiagonalMatrix< Real2, Devices::Cuda, Index2 >* inMatrix, + TridiagonalMatrix< Real, Devices::Cuda, Index >* outMatrix, const Real matrixMultiplicator, const Index gridIdx ) { @@ -473,7 +460,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > template< typename Real2, typename Index2 > -void Tridiagonal< Real, Device, Index, Organization, RealAllocator >::getTransposition( const Tridiagonal< Real2, Device, Index2 >& matrix, +void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::getTransposition( const TridiagonalMatrix< Real2, Device, Index2 >& matrix, const RealType& matrixMultiplicator ) { TNL_ASSERT( this->getRows() == matrix.getRows(), @@ -493,8 +480,8 @@ void Tridiagonal< Real, Device, Index, Organization, RealAllocator >::getTranspo if( std::is_same< Device, Devices::Cuda >::value ) { #ifdef HAVE_CUDA - Tridiagonal* kernel_this = Cuda::passToDevice( *this ); - typedef Tridiagonal< Real2, Device, Index2 > InMatrixType; + TridiagonalMatrix* kernel_this = Cuda::passToDevice( *this ); + typedef TridiagonalMatrix< Real2, Device, Index2 > InMatrixType; InMatrixType* kernel_inMatrix = Cuda::passToDevice( matrix ); dim3 cudaBlockSize( 256 ), cudaGridSize( Cuda::getMaxGridSize() ); const IndexType cudaBlocks = roundUpDivision( matrix.getRows(), cudaBlockSize.x ); @@ -503,7 +490,7 @@ void Tridiagonal< Real, Device, Index, Organization, RealAllocator >::getTranspo { if( gridIdx == cudaGrids - 1 ) cudaGridSize.x = cudaBlocks % Cuda::getMaxGridSize(); - TridiagonalTranspositionCudaKernel<<< cudaGridSize, cudaBlockSize >>> + TridiagonalMatrixTranspositionCudaKernel<<< cudaGridSize, cudaBlockSize >>> ( kernel_inMatrix, kernel_this, matrixMultiplicator, @@ -523,7 +510,7 @@ template< typename Real, typename RealAllocator > template< typename Vector1, typename Vector2 > __cuda_callable__ -void Tridiagonal< Real, Device, Index, Organization, RealAllocator >::performSORIteration( const Vector1& b, +void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::performSORIteration( const Vector1& b, const IndexType row, Vector2& x, const RealType& omega ) const @@ -543,8 +530,8 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > -Tridiagonal< Real, Device, Index, Organization, RealAllocator >& -Tridiagonal< Real, Device, Index, Organization, RealAllocator >::operator=( const Tridiagonal& matrix ) +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >& +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::operator=( const TridiagonalMatrix& matrix ) { this->setLike( matrix ); this->values = matrix.values; @@ -558,9 +545,9 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > -Tridiagonal< Real, Device, Index, Organization, RealAllocator >& -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: -operator=( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >& +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +operator=( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix ) { static_assert( std::is_same< Device, Devices::Host >::value || std::is_same< Device, Devices::Cuda >::value, "unknown device" ); @@ -582,7 +569,7 @@ operator=( const Tridiagonal< Real_, Device_, Index_, Organization_, RealAllocat } else { - Tridiagonal< Real, Device, Index, Organization_ > auxMatrix; + TridiagonalMatrix< Real, Device, Index, Organization_ > auxMatrix; auxMatrix = matrix; const auto matrix_view = auxMatrix.getView(); auto f = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value ) mutable { @@ -599,7 +586,7 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > -void Tridiagonal< Real, Device, Index, Organization, RealAllocator >::save( File& file ) const +void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::save( File& file ) const { Matrix< Real, Device, Index >::save( file ); } @@ -609,7 +596,7 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > -void Tridiagonal< Real, Device, Index, Organization, RealAllocator >::load( File& file ) +void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::load( File& file ) { Matrix< Real, Device, Index >::load( file ); this->indexer.setDimensions( this->getRows(), this->getColumns() ); @@ -621,7 +608,7 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > -void Tridiagonal< Real, Device, Index, Organization, RealAllocator >::save( const String& fileName ) const +void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::save( const String& fileName ) const { Object::save( fileName ); } @@ -631,7 +618,7 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > -void Tridiagonal< Real, Device, Index, Organization, RealAllocator >::load( const String& fileName ) +void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >::load( const String& fileName ) { Object::load( fileName ); } @@ -642,7 +629,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > void -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: print( std::ostream& str ) const { this->view.print( str ); @@ -654,7 +641,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > auto -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getIndexer() const -> const IndexerType& { return this->indexer; @@ -666,7 +653,7 @@ template< typename Real, ElementsOrganization Organization, typename RealAllocator > auto -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getIndexer() -> IndexerType& { return this->indexer; @@ -679,7 +666,7 @@ template< typename Real, typename RealAllocator > __cuda_callable__ Index -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getElementIndex( const IndexType row, const IndexType column ) const { IndexType localIdx = column - row; @@ -699,115 +686,11 @@ template< typename Real, typename RealAllocator > __cuda_callable__ Index -Tridiagonal< Real, Device, Index, Organization, RealAllocator >:: +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getPaddingIndex() const { return this->view.getPaddingIndex(); } -/* -template<> -class TridiagonalDeviceDependentCode< Devices::Host > -{ - public: - - typedef Devices::Host Device; - - template< typename Index > - __cuda_callable__ - static Index getElementIndex( const Index rows, - const Index row, - const Index column ) - { - return 2*row + column; - } - - template< typename Vector, - typename Index, - typename ValuesType > - __cuda_callable__ - static typename Vector::RealType rowVectorProduct( const Index rows, - const ValuesType& values, - const Index row, - const Vector& vector ) - { - if( row == 0 ) - return vector[ 0 ] * values[ 0 ] + - vector[ 1 ] * values[ 1 ]; - Index i = 3 * row; - if( row == rows - 1 ) - return vector[ row - 1 ] * values[ i - 1 ] + - vector[ row ] * values[ i ]; - return vector[ row - 1 ] * values[ i - 1 ] + - vector[ row ] * values[ i ] + - vector[ row + 1 ] * values[ i + 1 ]; - } - - template< typename Real, - typename Index, - typename InVector, - typename OutVector > - static void vectorProduct( const Tridiagonal< Real, Device, Index, Organization, RealAllocator >& matrix, - const InVector& inVector, - OutVector& outVector ) - { -#ifdef HAVE_OPENMP -#pragma omp parallel for if( Devices::Host::isOMPEnabled() ) -#endif - for( Index row = 0; row < matrix.getRows(); row ++ ) - outVector[ row ] = matrix.rowVectorProduct( row, inVector ); - } -}; - -template<> -class TridiagonalDeviceDependentCode< Devices::Cuda > -{ - public: - - typedef Devices::Cuda Device; - - template< typename Index > - __cuda_callable__ - static Index getElementIndex( const Index rows, - const Index row, - const Index column ) - { - return ( column - row + 1 )*rows + row - 1; - } - - template< typename Vector, - typename Index, - typename ValuesType > - __cuda_callable__ - static typename Vector::RealType rowVectorProduct( const Index rows, - const ValuesType& values, - const Index row, - const Vector& vector ) - { - if( row == 0 ) - return vector[ 0 ] * values[ 0 ] + - vector[ 1 ] * values[ rows - 1 ]; - Index i = row - 1; - if( row == rows - 1 ) - return vector[ row - 1 ] * values[ i ] + - vector[ row ] * values[ i + rows ]; - return vector[ row - 1 ] * values[ i ] + - vector[ row ] * values[ i + rows ] + - vector[ row + 1 ] * values[ i + 2*rows ]; - } - - template< typename Real, - typename Index, - typename InVector, - typename OutVector > - static void vectorProduct( const Tridiagonal< Real, Device, Index, Organization, RealAllocator >& matrix, - const InVector& inVector, - OutVector& outVector ) - { - MatrixVectorProductCuda( matrix, inVector, outVector ); - } -}; - */ - } // namespace Matrices } // namespace TNL diff --git a/src/TNL/Matrices/TridiagonalMatrixView.h b/src/TNL/Matrices/TridiagonalMatrixView.h index 9496136dc..734907033 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.h +++ b/src/TNL/Matrices/TridiagonalMatrixView.h @@ -121,7 +121,11 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > template< typename InVector, typename OutVector > void vectorProduct( const InVector& inVector, - OutVector& outVector ) const; + OutVector& outVector, + const RealType matrixMultiplicator = 1.0, + const RealType outVectorMultiplicator = 0.0, + const IndexType begin = 0, + IndexType end = 0 ) const; template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_ > void addMatrix( const TridiagonalMatrixView< Real_, Device_, Index_, Organization_ >& matrix, diff --git a/src/TNL/Matrices/TridiagonalMatrixView.hpp b/src/TNL/Matrices/TridiagonalMatrixView.hpp index 949e46317..a482f69cb 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixView.hpp @@ -346,7 +346,7 @@ forRows( IndexType first, IndexType last, Function& function ) const { function( 0, 0, 0, values_view[ indexer.getGlobalIndex( 0, 0 ) ], compute ); function( 0, 1, 1, values_view[ indexer.getGlobalIndex( 0, 1 ) ], compute ); - } + } else if( rowIdx + 1 < indexer.getColumns() ) { function( rowIdx, 0, rowIdx - 1, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ], compute ); @@ -380,7 +380,7 @@ forRows( IndexType first, IndexType last, Function& function ) { function( 0, 0, 0, values_view[ indexer.getGlobalIndex( 0, 0 ) ] ); function( 0, 1, 1, values_view[ indexer.getGlobalIndex( 0, 1 ) ] ); - } + } else if( rowIdx + 1 < indexer.getColumns() ) { function( rowIdx, 0, rowIdx - 1, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ] ); @@ -428,7 +428,7 @@ template< typename Real, ElementsOrganization Organization > template< typename Vector > __cuda_callable__ -typename Vector::RealType +typename Vector::RealType TridiagonalMatrixView< Real, Device, Index, Organization >:: rowVectorProduct( const IndexType row, const Vector& vector ) const { @@ -440,9 +440,14 @@ template< typename Real, ElementsOrganization Organization > template< typename InVector, typename OutVector > -void +void TridiagonalMatrixView< Real, Device, Index, Organization >:: -vectorProduct( const InVector& inVector, OutVector& outVector ) const +vectorProduct( const InVector& inVector, + OutVector& outVector, + const RealType matrixMultiplicator, + const RealType outVectorMultiplicator, + const IndexType begin, + IndexType end ) const { TNL_ASSERT_EQ( this->getColumns(), inVector.getSize(), "Matrix columns do not fit with input vector." ); TNL_ASSERT_EQ( this->getRows(), outVector.getSize(), "Matrix rows do not fit with output vector." ); @@ -455,10 +460,18 @@ vectorProduct( const InVector& inVector, OutVector& outVector ) const auto reduction = [] __cuda_callable__ ( RealType& sum, const RealType& value ) { sum += value; }; - auto keeper = [=] __cuda_callable__ ( IndexType row, const RealType& value ) mutable { + auto keeper1 = [=] __cuda_callable__ ( IndexType row, const RealType& value ) mutable { outVectorView[ row ] = value; }; - this->allRowsReduction( fetch, reduction, keeper, ( RealType ) 0.0 ); + auto keeper2 = [=] __cuda_callable__ ( IndexType row, const RealType& value ) mutable { + outVectorView[ row ] = outVectorMultiplicator * outVectorView[ row ] + matrixMultiplicator * value; + }; + if( end == 0 ) + end = this->getRows(); + if( matrixMultiplicator == 1.0 && outVectorMultiplicator == 0.0 ) + this->rowsReduction( begin, end, fetch, reduction, keeper1, ( RealType ) 0.0 ); + else + this->rowsReduction( begin, end, fetch, reduction, keeper2, ( RealType ) 0.0 ); } template< typename Real, diff --git a/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h b/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h index 15b0dc44b..f520ec22f 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h +++ b/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/UnitTests/Matrices/DenseMatrixCopyTest.h b/src/UnitTests/Matrices/DenseMatrixCopyTest.h index efe8595c8..0d502cf23 100644 --- a/src/UnitTests/Matrices/DenseMatrixCopyTest.h +++ b/src/UnitTests/Matrices/DenseMatrixCopyTest.h @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -400,8 +400,8 @@ void tridiagonalMatrixAssignment() using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using TridiagonalHost = TNL::Matrices::Tridiagonal< RealType, TNL::Devices::Host, IndexType >; - using TridiagonalCuda = TNL::Matrices::Tridiagonal< RealType, TNL::Devices::Cuda, IndexType >; + using TridiagonalHost = TNL::Matrices::TridiagonalMatrix< RealType, TNL::Devices::Host, IndexType >; + using TridiagonalCuda = TNL::Matrices::TridiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType >; const IndexType rows( 10 ), columns( 10 ); TridiagonalHost hostMatrix( rows, columns ); diff --git a/src/UnitTests/Matrices/SparseMatrixCopyTest.h b/src/UnitTests/Matrices/SparseMatrixCopyTest.h index fb5d84206..ffcca4b60 100644 --- a/src/UnitTests/Matrices/SparseMatrixCopyTest.h +++ b/src/UnitTests/Matrices/SparseMatrixCopyTest.h @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -436,8 +436,8 @@ void tridiagonalMatrixAssignment() using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using TridiagonalHost = TNL::Matrices::Tridiagonal< RealType, TNL::Devices::Host, IndexType >; - using TridiagonalCuda = TNL::Matrices::Tridiagonal< RealType, TNL::Devices::Cuda, IndexType >; + using TridiagonalHost = TNL::Matrices::TridiagonalMatrix< RealType, TNL::Devices::Host, IndexType >; + using TridiagonalCuda = TNL::Matrices::TridiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType >; const IndexType rows( 10 ), columns( 10 ); TridiagonalHost hostMatrix( rows, columns ); diff --git a/src/UnitTests/Matrices/TridiagonalMatrixTest.h b/src/UnitTests/Matrices/TridiagonalMatrixTest.h index bb613a17d..1b88ba823 100644 --- a/src/UnitTests/Matrices/TridiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/TridiagonalMatrixTest.h @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include @@ -20,11 +20,11 @@ #include #include -using Tridiagonal_host_float = TNL::Matrices::Tridiagonal< float, TNL::Devices::Host, int >; -using Tridiagonal_host_int = TNL::Matrices::Tridiagonal< int, TNL::Devices::Host, int >; +using Tridiagonal_host_float = TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Host, int >; +using Tridiagonal_host_int = TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Host, int >; -using Tridiagonal_cuda_float = TNL::Matrices::Tridiagonal< float, TNL::Devices::Cuda, int >; -using Tridiagonal_cuda_int = TNL::Matrices::Tridiagonal< int, TNL::Devices::Cuda, int >; +using Tridiagonal_cuda_float = TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Cuda, int >; +using Tridiagonal_cuda_int = TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Cuda, int >; static const char* TEST_FILE_NAME = "test_TridiagonalMatrixTest.tnl"; @@ -36,14 +36,14 @@ static const char* TEST_FILE_NAME = "test_TridiagonalMatrixTest.tnl"; void test_GetSerializationType() { using namespace TNL::Containers::Segments; - EXPECT_EQ( ( TNL::Matrices::Tridiagonal< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Tridiagonal< float, [any_device], int, true, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Tridiagonal< int, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Tridiagonal< int, [any_device], int, true, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Tridiagonal< float, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Tridiagonal< float, [any_device], int, true, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Tridiagonal< int, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Tridiagonal< int, [any_device], int, true, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Tridiagonal< float, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Tridiagonal< float, [any_device], int, false, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Tridiagonal< int, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Tridiagonal< int, [any_device], int, false, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Tridiagonal< float, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Tridiagonal< float, [any_device], int, false, [any_allocator] >" ) ); - EXPECT_EQ( ( TNL::Matrices::Tridiagonal< int, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::Tridiagonal< int, [any_device], int, false, [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::TridiagonalMatrix< float, [any_device], int, true, [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::TridiagonalMatrix< int, [any_device], int, true, [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::TridiagonalMatrix< float, [any_device], int, true, [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::TridiagonalMatrix< int, [any_device], int, true, [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::TridiagonalMatrix< float, [any_device], int, false, [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::TridiagonalMatrix< int, [any_device], int, false, [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::TridiagonalMatrix< float, [any_device], int, false, [any_allocator] >" ) ); + EXPECT_EQ( ( TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Cuda, int, ColumnMajorOrder >::getSerializationType() ), TNL::String( "Matrices::TridiagonalMatrix< int, [any_device], int, false, [any_allocator] >" ) ); } template< typename Matrix > @@ -1170,8 +1170,8 @@ void test_AssignmentOperator() using IndexType = typename Matrix::IndexType; constexpr TNL::Containers::Segments::ElementsOrganization organization = Matrix::getOrganization(); - using TridiagonalHost = TNL::Matrices::Tridiagonal< RealType, TNL::Devices::Host, IndexType, organization >; - using TridiagonalCuda = TNL::Matrices::Tridiagonal< RealType, TNL::Devices::Cuda, IndexType, + using TridiagonalHost = TNL::Matrices::TridiagonalMatrix< RealType, TNL::Devices::Host, IndexType, organization >; + using TridiagonalCuda = TNL::Matrices::TridiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType, organization == TNL::Containers::Segments::RowMajorOrder ? TNL::Containers::Segments::ColumnMajorOrder : TNL::Containers::Segments::RowMajorOrder >; const IndexType rows( 10 ), columns( 10 ); @@ -1347,31 +1347,31 @@ protected: // types for which MatrixTest is instantiated using MatrixTypes = ::testing::Types < - TNL::Matrices::Tridiagonal< int, TNL::Devices::Host, short >, - TNL::Matrices::Tridiagonal< long, TNL::Devices::Host, short >, - TNL::Matrices::Tridiagonal< float, TNL::Devices::Host, short >, - TNL::Matrices::Tridiagonal< double, TNL::Devices::Host, short >, - TNL::Matrices::Tridiagonal< int, TNL::Devices::Host, int >, - TNL::Matrices::Tridiagonal< long, TNL::Devices::Host, int >, - TNL::Matrices::Tridiagonal< float, TNL::Devices::Host, int >, - TNL::Matrices::Tridiagonal< double, TNL::Devices::Host, int >, - TNL::Matrices::Tridiagonal< int, TNL::Devices::Host, long >, - TNL::Matrices::Tridiagonal< long, TNL::Devices::Host, long >, - TNL::Matrices::Tridiagonal< float, TNL::Devices::Host, long >, - TNL::Matrices::Tridiagonal< double, TNL::Devices::Host, long > + TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Host, short >, + TNL::Matrices::TridiagonalMatrix< long, TNL::Devices::Host, short >, + TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Host, short >, + TNL::Matrices::TridiagonalMatrix< double, TNL::Devices::Host, short >, + TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Host, int >, + TNL::Matrices::TridiagonalMatrix< long, TNL::Devices::Host, int >, + TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Host, int >, + TNL::Matrices::TridiagonalMatrix< double, TNL::Devices::Host, int >, + TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Host, long >, + TNL::Matrices::TridiagonalMatrix< long, TNL::Devices::Host, long >, + TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Host, long >, + TNL::Matrices::TridiagonalMatrix< double, TNL::Devices::Host, long > #ifdef HAVE_CUDA - ,TNL::Matrices::Tridiagonal< int, TNL::Devices::Cuda, short >, - TNL::Matrices::Tridiagonal< long, TNL::Devices::Cuda, short >, - TNL::Matrices::Tridiagonal< float, TNL::Devices::Cuda, short >, - TNL::Matrices::Tridiagonal< double, TNL::Devices::Cuda, short >, - TNL::Matrices::Tridiagonal< int, TNL::Devices::Cuda, int >, - TNL::Matrices::Tridiagonal< long, TNL::Devices::Cuda, int >, - TNL::Matrices::Tridiagonal< float, TNL::Devices::Cuda, int >, - TNL::Matrices::Tridiagonal< double, TNL::Devices::Cuda, int >, - TNL::Matrices::Tridiagonal< int, TNL::Devices::Cuda, long >, - TNL::Matrices::Tridiagonal< long, TNL::Devices::Cuda, long >, - TNL::Matrices::Tridiagonal< float, TNL::Devices::Cuda, long >, - TNL::Matrices::Tridiagonal< double, TNL::Devices::Cuda, long > + ,TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Cuda, short >, + TNL::Matrices::TridiagonalMatrix< long, TNL::Devices::Cuda, short >, + TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Cuda, short >, + TNL::Matrices::TridiagonalMatrix< double, TNL::Devices::Cuda, short >, + TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Cuda, int >, + TNL::Matrices::TridiagonalMatrix< long, TNL::Devices::Cuda, int >, + TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Cuda, int >, + TNL::Matrices::TridiagonalMatrix< double, TNL::Devices::Cuda, int >, + TNL::Matrices::TridiagonalMatrix< int, TNL::Devices::Cuda, long >, + TNL::Matrices::TridiagonalMatrix< long, TNL::Devices::Cuda, long >, + TNL::Matrices::TridiagonalMatrix< float, TNL::Devices::Cuda, long >, + TNL::Matrices::TridiagonalMatrix< double, TNL::Devices::Cuda, long > #endif >; @@ -1488,7 +1488,7 @@ TYPED_TEST( MatrixTest, addMatrixTest_differentOrdering ) using DeviceType = typename MatrixType::DeviceType; using IndexType = typename MatrixType::IndexType; using RealAllocatorType = typename MatrixType::RealAllocatorType; - using MatrixType2 = TNL::Matrices::Tridiagonal< RealType, DeviceType, IndexType, + using MatrixType2 = TNL::Matrices::TridiagonalMatrix< RealType, DeviceType, IndexType, MatrixType::getOrganization() == TNL::Containers::Segments::RowMajorOrder ? TNL::Containers::Segments::ColumnMajorOrder : TNL::Containers::Segments::RowMajorOrder, RealAllocatorType >; -- GitLab From 3805be01946822c62927c5f99737e78056884cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 10 Jun 2020 13:55:59 +0200 Subject: [PATCH 35/38] Writting documentation on tridiagonal matrix. --- .../MultidiagonalMatrix/CMakeLists.txt | 2 +- .../Matrices/TridiagonalMatrix/CMakeLists.txt | 515 +++++++++--------- ...lMatrixExample_Constructor_init_list_1.cpp | 56 +- ...lMatrixExample_Constructor_init_list_2.cpp | 31 -- ...alMatrixExample_Constructor_init_list_2.cu | 1 - ...gonalMatrixExample_Constructor_std_map.cpp | 38 -- ...agonalMatrixExample_Constructor_std_map.cu | 1 - .../TridiagonalMatrixExample_addElement.cpp | 21 +- ...diagonalMatrixExample_allRowsReduction.cpp | 39 +- .../TridiagonalMatrixExample_forAllRows.cpp | 48 +- .../TridiagonalMatrixExample_forRows.cpp | 42 +- ...lMatrixExample_getCompressedRowLengths.cpp | 44 +- .../TridiagonalMatrixExample_getConstRow.cpp | 29 +- .../TridiagonalMatrixExample_getElement.cpp | 25 +- .../TridiagonalMatrixExample_getRow.cpp | 39 +- ...onalMatrixExample_getSerializationType.cpp | 4 +- ...TridiagonalMatrixExample_rowsReduction.cpp | 26 +- .../TridiagonalMatrixExample_setElement.cpp | 20 +- .../TridiagonalMatrixExample_setElements.cpp | 57 +- ...idiagonalMatrixExample_setElements_map.cpp | 39 -- ...ridiagonalMatrixExample_setElements_map.cu | 1 - ...diagonalMatrixExample_setRowCapacities.cpp | 29 - ...idiagonalMatrixExample_setRowCapacities.cu | 1 - ...ridiagonalMatrixViewExample_addElement.cpp | 40 ++ ...TridiagonalMatrixViewExample_addElement.cu | 1 + ...onalMatrixViewExample_allRowsReduction.cpp | 81 +++ ...gonalMatrixViewExample_allRowsReduction.cu | 1 + ...ridiagonalMatrixViewExample_forAllRows.cpp | 57 ++ ...TridiagonalMatrixViewExample_forAllRows.cu | 1 + .../TridiagonalMatrixViewExample_forRows.cpp | 57 ++ .../TridiagonalMatrixViewExample_forRows.cu | 1 + ...rixViewExample_getCompressedRowLengths.cpp | 52 ++ ...trixViewExample_getCompressedRowLengths.cu | 1 + ...idiagonalMatrixViewExample_getConstRow.cpp | 49 ++ ...ridiagonalMatrixViewExample_getConstRow.cu | 1 + ...ridiagonalMatrixViewExample_getElement.cpp | 39 ++ ...TridiagonalMatrixViewExample_getElement.cu | 1 + .../TridiagonalMatrixViewExample_getRow.cpp | 46 ++ .../TridiagonalMatrixViewExample_getRow.cu | 1 + ...iagonalMatrixViewExample_rowsReduction.cpp | 81 +++ ...diagonalMatrixViewExample_rowsReduction.cu | 1 + ...ridiagonalMatrixViewExample_setElement.cpp | 47 ++ ...TridiagonalMatrixViewExample_setElement.cu | 1 + .../Matrices/TridiagonalMatrix/genfiles | 53 ++ src/TNL/Matrices/MultidiagonalMatrix.hpp | 13 - src/TNL/Matrices/TridiagonalMatrix.h | 40 +- src/TNL/Matrices/TridiagonalMatrix.hpp | 92 +++- src/TNL/Matrices/TridiagonalMatrixView.h | 6 + src/TNL/Matrices/TridiagonalMatrixView.hpp | 93 +++- .../Matrices/BinarySparseMatrixCopyTest.h | 4 +- .../Matrices/TridiagonalMatrixTest.h | 14 +- 51 files changed, 1346 insertions(+), 636 deletions(-) delete mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cpp delete mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cu delete mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cpp delete mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cu delete mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cpp delete mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cu delete mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cpp delete mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cu create mode 100755 Documentation/Examples/Matrices/TridiagonalMatrix/genfiles diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index 6d17bf778..10a1ed732 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -260,7 +260,7 @@ ENDIF() OUTPUT MultidiagonalMatrixExample_setElement.out ) -ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS +ADD_CUSTOM_TARGET( RunMultidiagonalMatricesExamples ALL DEPENDS MultidiagonalMatrixExample_Constructor.out MultidiagonalMatrixExample_Constructor_init_list_1.out MultidiagonalMatrixExample_Constructor_init_list_2.out diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt index 6c7aef592..0f66e71a4 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt @@ -1,282 +1,269 @@ IF( BUILD_CUDA ) -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_Constructor_init_list_cuda TridiagonalMatrixExample_Constructor_init_list.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_Constructor_init_list_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_Constructor_init_list.out -# OUTPUT TridiagonalMatrixExample_Constructor_init_list.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_setElements_cuda TridiagonalMatrixExample_setElements.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElements_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElements.out -# OUTPUT TridiagonalMatrixExample_setElements.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getCompressedRowLengths_cuda TridiagonalMatrixExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getCompressedRowLengths.out -# OUTPUT TridiagonalMatrixExample_getCompressedRowLengths.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getElementsCount_cuda TridiagonalMatrixExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElementsCount.out -# OUTPUT TridiagonalMatrixExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getConstRow_cuda TridiagonalMatrixExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getConstRow.out -# OUTPUT TridiagonalMatrixExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getRow_cuda TridiagonalMatrixExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getRow.out -# OUTPUT TridiagonalMatrixExample_getRow.out ) - + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_Constructor_init_list_1_cuda TridiagonalMatrixExample_Constructor_init_list_1.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_Constructor_init_list_1_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_Constructor_init_list_1.out + OUTPUT TridiagonalMatrixExample_Constructor_init_list_1.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getSerializationType_cuda TridiagonalMatrixExample_getSerializationType.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getSerializationType_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getSerializationType.out + OUTPUT TridiagonalMatrixExample_getSerializationType.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_setElements_cuda TridiagonalMatrixExample_setElements.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElements_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElements.out + OUTPUT TridiagonalMatrixExample_setElements.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getCompressedRowLengths_cuda TridiagonalMatrixExample_getCompressedRowLengths.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getCompressedRowLengths_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getCompressedRowLengths.out + OUTPUT TridiagonalMatrixExample_getCompressedRowLengths.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getConstRow_cuda TridiagonalMatrixExample_getConstRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getConstRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getConstRow.out + OUTPUT TridiagonalMatrixExample_getConstRow.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getRow_cuda TridiagonalMatrixExample_getRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getRow.out + OUTPUT TridiagonalMatrixExample_getRow.out ) + +# This example does not work with nvcc 10.1. Restore it here when it works. # CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_setElement_cuda TridiagonalMatrixExample_setElement.cu ) # ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElement_cuda > # ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElement.out # OUTPUT TridiagonalMatrixExample_setElement.out ) -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_addElement_cuda TridiagonalMatrixExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_addElement.out -# OUTPUT TridiagonalMatrixExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getElement_cuda TridiagonalMatrixExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElement.out -# OUTPUT TridiagonalMatrixExample_getElement.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_rowsReduction_cuda TridiagonalMatrixExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_rowsReduction.out -# OUTPUT TridiagonalMatrixExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_allRowsReduction_cuda TridiagonalMatrixExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_allRowsReduction.out -# OUTPUT TridiagonalMatrixExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_forRows_cuda TridiagonalMatrixExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forRows.out -# OUTPUT TridiagonalMatrixExample_forRows.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_forAllRows_cuda TridiagonalMatrixExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forAllRows.out -# OUTPUT TridiagonalMatrixExample_forAllRows.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_constructor_cuda TridiagonalMatrixViewExample_constructor.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_constructor_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_constructor.out -# OUTPUT TridiagonalMatrixViewExample_constructor.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getCompressedRowLengths_cuda TridiagonalMatrixViewExample_getCompressedRowLengths.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getCompressedRowLengths_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getCompressedRowLengths.out -# OUTPUT TridiagonalMatrixViewExample_getCompressedRowLengths.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElementsCount_cuda TridiagonalMatrixViewExample_getElementsCount.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElementsCount_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElementsCount.out -# OUTPUT TridiagonalMatrixViewExample_getElementsCount.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getConstRow_cuda TridiagonalMatrixViewExample_getConstRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getConstRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getConstRow.out -# OUTPUT TridiagonalMatrixViewExample_getConstRow.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getRow_cuda TridiagonalMatrixViewExample_getRow.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getRow_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getRow.out -# OUTPUT TridiagonalMatrixViewExample_getRow.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_setElement_cuda TridiagonalMatrixViewExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_setElement.out -# OUTPUT TridiagonalMatrixViewExample_setElement.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_addElement_cuda TridiagonalMatrixViewExample_addElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_addElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_addElement.out -# OUTPUT TridiagonalMatrixViewExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElement_cuda TridiagonalMatrixViewExample_getElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElement.out -# OUTPUT TridiagonalMatrixViewExample_getElement.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_rowsReduction_cuda TridiagonalMatrixViewExample_rowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_rowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_rowsReduction.out -# OUTPUT TridiagonalMatrixViewExample_rowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_allRowsReduction_cuda TridiagonalMatrixViewExample_allRowsReduction.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_allRowsReduction_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_allRowsReduction.out -# OUTPUT TridiagonalMatrixViewExample_allRowsReduction.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_forRows_cuda TridiagonalMatrixViewExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forRows.out -# OUTPUT TridiagonalMatrixViewExample_forRows.out ) - -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_forAllRows_cuda TridiagonalMatrixViewExample_forAllRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forAllRows_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forAllRows.out -# OUTPUT TridiagonalMatrixViewExample_forAllRows.out ) + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_addElement_cuda TridiagonalMatrixExample_addElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_addElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_addElement.out + OUTPUT TridiagonalMatrixExample_addElement.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getElement_cuda TridiagonalMatrixExample_getElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElement.out + OUTPUT TridiagonalMatrixExample_getElement.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_rowsReduction_cuda TridiagonalMatrixExample_rowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_rowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_rowsReduction.out + OUTPUT TridiagonalMatrixExample_rowsReduction.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_allRowsReduction_cuda TridiagonalMatrixExample_allRowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_allRowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_allRowsReduction.out + OUTPUT TridiagonalMatrixExample_allRowsReduction.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_forRows_cuda TridiagonalMatrixExample_forRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forRows.out + OUTPUT TridiagonalMatrixExample_forRows.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_forAllRows_cuda TridiagonalMatrixExample_forAllRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forAllRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forAllRows.out + OUTPUT TridiagonalMatrixExample_forAllRows.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getCompressedRowLengths_cuda TridiagonalMatrixViewExample_getCompressedRowLengths.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getCompressedRowLengths_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getCompressedRowLengths.out + OUTPUT TridiagonalMatrixViewExample_getCompressedRowLengths.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getConstRow_cuda TridiagonalMatrixViewExample_getConstRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getConstRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getConstRow.out + OUTPUT TridiagonalMatrixViewExample_getConstRow.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getRow_cuda TridiagonalMatrixViewExample_getRow.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getRow_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getRow.out + OUTPUT TridiagonalMatrixViewExample_getRow.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_setElement_cuda TridiagonalMatrixViewExample_setElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_setElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_setElement.out + OUTPUT TridiagonalMatrixViewExample_setElement.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_addElement_cuda TridiagonalMatrixViewExample_addElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_addElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_addElement.out + OUTPUT TridiagonalMatrixViewExample_addElement.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElement_cuda TridiagonalMatrixViewExample_getElement.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElement_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElement.out + OUTPUT TridiagonalMatrixViewExample_getElement.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_rowsReduction_cuda TridiagonalMatrixViewExample_rowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_rowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_rowsReduction.out + OUTPUT TridiagonalMatrixViewExample_rowsReduction.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_allRowsReduction_cuda TridiagonalMatrixViewExample_allRowsReduction.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_allRowsReduction_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_allRowsReduction.out + OUTPUT TridiagonalMatrixViewExample_allRowsReduction.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_forRows_cuda TridiagonalMatrixViewExample_forRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forRows.out + OUTPUT TridiagonalMatrixViewExample_forRows.out ) + + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_forAllRows_cuda TridiagonalMatrixViewExample_forAllRows.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forAllRows_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forAllRows.out + OUTPUT TridiagonalMatrixViewExample_forAllRows.out ) ELSE() -# ADD_EXECUTABLE( TridiagonalMatrixExample_Constructor_init_list TridiagonalMatrixExample_Constructor_init_list.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_Constructor_init_list > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_Constructor_init_list.out -# OUTPUT TridiagonalMatrixExample_Constructor_init_list.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixExample_setElements TridiagonalMatrixExample_setElements.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElements > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElements.out -# OUTPUT TridiagonalMatrixExample_setElements.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixExample_getCompressedRowLengths TridiagonalMatrixExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getCompressedRowLengths.out -# OUTPUT TridiagonalMatrixExample_getCompressedRowLengths.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixExample_getElementsCount TridiagonalMatrixExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElementsCount.out -# OUTPUT TridiagonalMatrixExample_getElementsCount.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixExample_getConstRow TridiagonalMatrixExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getConstRow.out -# OUTPUT TridiagonalMatrixExample_getConstRow.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixExample_getRow TridiagonalMatrixExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getRow.out -# OUTPUT TridiagonalMatrixExample_getRow.out ) + ADD_EXECUTABLE( TridiagonalMatrixExample_Constructor_init_list_1 TridiagonalMatrixExample_Constructor_init_list_1.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_Constructor_init_list_1 > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_Constructor_init_list_1.out + OUTPUT TridiagonalMatrixExample_Constructor_init_list_1.out ) + + ADD_EXECUTABLE( TridiagonalMatrixExample_getSerializationType TridiagonalMatrixExample_getSerializationType.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getSerializationType > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getSerializationType.out + OUTPUT TridiagonalMatrixExample_getSerializationType.out ) + + ADD_EXECUTABLE( TridiagonalMatrixExample_setElements TridiagonalMatrixExample_setElements.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElements > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElements.out + OUTPUT TridiagonalMatrixExample_setElements.out ) + + ADD_EXECUTABLE( TridiagonalMatrixExample_getCompressedRowLengths TridiagonalMatrixExample_getCompressedRowLengths.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getCompressedRowLengths > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getCompressedRowLengths.out + OUTPUT TridiagonalMatrixExample_getCompressedRowLengths.out ) + + ADD_EXECUTABLE( TridiagonalMatrixExample_getConstRow TridiagonalMatrixExample_getConstRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getConstRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getConstRow.out + OUTPUT TridiagonalMatrixExample_getConstRow.out ) + + ADD_EXECUTABLE( TridiagonalMatrixExample_getRow TridiagonalMatrixExample_getRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getRow.out + OUTPUT TridiagonalMatrixExample_getRow.out ) + +# This example does not work with nvcc 10.1. Restore it here when it works. # ADD_EXECUTABLE( TridiagonalMatrixExample_setElement TridiagonalMatrixExample_setElement.cpp ) # ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElement > # ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElement.out # OUTPUT TridiagonalMatrixExample_setElement.out ) -# ADD_EXECUTABLE( TridiagonalMatrixExample_addElement TridiagonalMatrixExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_addElement.out -# OUTPUT TridiagonalMatrixExample_addElement.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixExample_getElement TridiagonalMatrixExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElement.out -# OUTPUT TridiagonalMatrixExample_getElement.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixExample_rowsReduction TridiagonalMatrixExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_rowsReduction.out -# OUTPUT TridiagonalMatrixExample_rowsReduction.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixExample_allRowsReduction TridiagonalMatrixExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_allRowsReduction.out -# OUTPUT TridiagonalMatrixExample_allRowsReduction.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixExample_forRows TridiagonalMatrixExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forRows.out -# OUTPUT TridiagonalMatrixExample_forRows.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixExample_forAllRows TridiagonalMatrixExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forAllRows.out -# OUTPUT TridiagonalMatrixExample_forAllRows.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_constructor TridiagonalMatrixViewExample_constructor.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_constructor > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_constructor.out -# OUTPUT TridiagonalMatrixViewExample_constructor.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_getCompressedRowLengths TridiagonalMatrixViewExample_getCompressedRowLengths.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getCompressedRowLengths > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getCompressedRowLengths.out -# OUTPUT TridiagonalMatrixViewExample_getCompressedRowLengths.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElementsCount TridiagonalMatrixViewExample_getElementsCount.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElementsCount > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElementsCount.out -# OUTPUT TridiagonalMatrixViewExample_getElementsCount.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_getConstRow TridiagonalMatrixViewExample_getConstRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getConstRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getConstRow.out -# OUTPUT TridiagonalMatrixViewExample_getConstRow.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_getRow TridiagonalMatrixViewExample_getRow.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getRow > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getRow.out -# OUTPUT TridiagonalMatrixViewExample_getRow.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_setElement TridiagonalMatrixViewExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_setElement.out -# OUTPUT TridiagonalMatrixViewExample_setElement.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_addElement TridiagonalMatrixViewExample_addElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_addElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_addElement.out -# OUTPUT TridiagonalMatrixViewExample_addElement.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElement TridiagonalMatrixViewExample_getElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElement.out -# OUTPUT TridiagonalMatrixViewExample_getElement.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_rowsReduction TridiagonalMatrixViewExample_rowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_rowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_rowsReduction.out -# OUTPUT TridiagonalMatrixViewExample_rowsReduction.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_allRowsReduction TridiagonalMatrixViewExample_allRowsReduction.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_allRowsReduction > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_allRowsReduction.out -# OUTPUT TridiagonalMatrixViewExample_allRowsReduction.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_forRows TridiagonalMatrixViewExample_forRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forRows.out -# OUTPUT TridiagonalMatrixViewExample_forRows.out ) - -# ADD_EXECUTABLE( TridiagonalMatrixViewExample_forAllRows TridiagonalMatrixViewExample_forAllRows.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forAllRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forAllRows.out -# OUTPUT TridiagonalMatrixViewExample_forAllRows.out ) + ADD_EXECUTABLE( TridiagonalMatrixExample_addElement TridiagonalMatrixExample_addElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_addElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_addElement.out + OUTPUT TridiagonalMatrixExample_addElement.out ) + + ADD_EXECUTABLE( TridiagonalMatrixExample_getElement TridiagonalMatrixExample_getElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElement.out + OUTPUT TridiagonalMatrixExample_getElement.out ) + + ADD_EXECUTABLE( TridiagonalMatrixExample_rowsReduction TridiagonalMatrixExample_rowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_rowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_rowsReduction.out + OUTPUT TridiagonalMatrixExample_rowsReduction.out ) + + ADD_EXECUTABLE( TridiagonalMatrixExample_allRowsReduction TridiagonalMatrixExample_allRowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_allRowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_allRowsReduction.out + OUTPUT TridiagonalMatrixExample_allRowsReduction.out ) + + ADD_EXECUTABLE( TridiagonalMatrixExample_forRows TridiagonalMatrixExample_forRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forRows.out + OUTPUT TridiagonalMatrixExample_forRows.out ) + + ADD_EXECUTABLE( TridiagonalMatrixExample_forAllRows TridiagonalMatrixExample_forAllRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forAllRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forAllRows.out + OUTPUT TridiagonalMatrixExample_forAllRows.out ) + + ADD_EXECUTABLE( TridiagonalMatrixViewExample_getCompressedRowLengths TridiagonalMatrixViewExample_getCompressedRowLengths.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getCompressedRowLengths > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getCompressedRowLengths.out + OUTPUT TridiagonalMatrixViewExample_getCompressedRowLengths.out ) + + ADD_EXECUTABLE( TridiagonalMatrixViewExample_getConstRow TridiagonalMatrixViewExample_getConstRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getConstRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getConstRow.out + OUTPUT TridiagonalMatrixViewExample_getConstRow.out ) + + ADD_EXECUTABLE( TridiagonalMatrixViewExample_getRow TridiagonalMatrixViewExample_getRow.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getRow > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getRow.out + OUTPUT TridiagonalMatrixViewExample_getRow.out ) + + ADD_EXECUTABLE( TridiagonalMatrixViewExample_setElement TridiagonalMatrixViewExample_setElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_setElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_setElement.out + OUTPUT TridiagonalMatrixViewExample_setElement.out ) + + ADD_EXECUTABLE( TridiagonalMatrixViewExample_addElement TridiagonalMatrixViewExample_addElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_addElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_addElement.out + OUTPUT TridiagonalMatrixViewExample_addElement.out ) + + ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElement TridiagonalMatrixViewExample_getElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElement.out + OUTPUT TridiagonalMatrixViewExample_getElement.out ) + + ADD_EXECUTABLE( TridiagonalMatrixViewExample_rowsReduction TridiagonalMatrixViewExample_rowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_rowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_rowsReduction.out + OUTPUT TridiagonalMatrixViewExample_rowsReduction.out ) + + ADD_EXECUTABLE( TridiagonalMatrixViewExample_allRowsReduction TridiagonalMatrixViewExample_allRowsReduction.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_allRowsReduction > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_allRowsReduction.out + OUTPUT TridiagonalMatrixViewExample_allRowsReduction.out ) + + ADD_EXECUTABLE( TridiagonalMatrixViewExample_forRows TridiagonalMatrixViewExample_forRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forRows.out + OUTPUT TridiagonalMatrixViewExample_forRows.out ) + + ADD_EXECUTABLE( TridiagonalMatrixViewExample_forAllRows TridiagonalMatrixViewExample_forAllRows.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forAllRows > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forAllRows.out + OUTPUT TridiagonalMatrixViewExample_forAllRows.out ) ENDIF() -#ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS -# TridiagonalMatrixExample_Constructor_init_list.out -# TridiagonalMatrixExample_setElements.out -# TridiagonalMatrixExample_getCompressedRowLengths.out -# TridiagonalMatrixExample_getElementsCount.out -# TridiagonalMatrixExample_getConstRow.out -# TridiagonalMatrixExample_getRow.out -# TridiagonalMatrixExample_setElement.out -# TridiagonalMatrixExample_addElement.out -# TridiagonalMatrixExample_getElement.out -# TridiagonalMatrixExample_rowsReduction.out -# TridiagonalMatrixExample_allRowsReduction.out -# TridiagonalMatrixExample_forRows.out -# TridiagonalMatrixExample_forAllRows.out -# TridiagonalMatrixViewExample_constructor.out -# TridiagonalMatrixViewExample_getCompressedRowLengths.out -# TridiagonalMatrixViewExample_getElementsCount.out -# TridiagonalMatrixViewExample_getConstRow.out -# TridiagonalMatrixViewExample_getRow.out -# TridiagonalMatrixViewExample_setElement.out -# TridiagonalMatrixViewExample_addElement.out -# TridiagonalMatrixViewExample_getElement.out -# TridiagonalMatrixViewExample_rowsReduction.out -# TridiagonalMatrixViewExample_allRowsReduction.out -# TridiagonalMatrixViewExample_forRows.out -# TridiagonalMatrixViewExample_forAllRows.out -#) + ADD_EXECUTABLE( TridiagonalMatrixExample_setElement TridiagonalMatrixExample_setElement.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElement > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElement.out + OUTPUT TridiagonalMatrixExample_setElement.out ) + + +ADD_CUSTOM_TARGET( RunTridiagonalMatricesExamples ALL DEPENDS + TridiagonalMatrixExample_Constructor_init_list_1.out + TridiagonalMatrixExample_getSerializationType.out + TridiagonalMatrixExample_setElements.out + TridiagonalMatrixExample_getCompressedRowLengths.out + TridiagonalMatrixExample_getConstRow.out + TridiagonalMatrixExample_getRow.out + TridiagonalMatrixExample_setElement.out + TridiagonalMatrixExample_addElement.out + TridiagonalMatrixExample_getElement.out + TridiagonalMatrixExample_rowsReduction.out + TridiagonalMatrixExample_allRowsReduction.out + TridiagonalMatrixExample_forRows.out + TridiagonalMatrixExample_forAllRows.out + TridiagonalMatrixViewExample_getCompressedRowLengths.out + TridiagonalMatrixViewExample_getConstRow.out + TridiagonalMatrixViewExample_getRow.out + TridiagonalMatrixViewExample_setElement.out + TridiagonalMatrixViewExample_addElement.out + TridiagonalMatrixViewExample_getElement.out + TridiagonalMatrixViewExample_rowsReduction.out + TridiagonalMatrixViewExample_allRowsReduction.out + TridiagonalMatrixViewExample_forRows.out + TridiagonalMatrixViewExample_forAllRows.out +) diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cpp index 92524b173..c4d8c8934 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cpp @@ -1,28 +1,58 @@ #include -#include +#include +#include #include +#include template< typename Device > -void initializerListExample() +void createTridiagonalMatrix() { - TNL::Matrices::SparseMatrix< double, Device > matrix { - { 1, 2, 3, 4, 5 }, // row capacities - 6 }; // number of matrix columns + const int matrixSize = 6; - for( int row = 0; row < matrix.getRows(); row++ ) - for( int column = 0; column <= row; column++ ) - matrix.setElement( row, column, row - column + 1 ); - std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; + /*** + * Setup the following matrix (dots represent zeros): + * + * / 2 -1 . . . . \ + * | -1 2 -1 . . . | + * | . -1 2 -1 . . | + * | . . -1 2 -1 . | + * | . . . -1 2 -1 | + * \ . . . . -1 2 / + * + */ + TNL::Matrices::TridiagonalMatrix< double, Device > matrix( + matrixSize, { + /*** + * To set the matrix elements we first extend the diagonals to their full + * lengths even outside the matrix (dots represent zeros and zeros are + * artificial zeros used for memory alignment): + * + * 0 / 2 -1 . . . . \ -> { 0, 2, -1 } + * | -1 2 -1 . . . | -> { -1, 2, -1 } + * | . -1 2 -1 . . | -> { -1, 2, -1 } + * | . . -1 2 -1 . | -> { -1, 2, -1 } + * | . . . -1 2 -1 | -> { -1, 2, -1 } + * \ . . . . -1 2 / 0 -> { -1, 2, 0 } + * + */ + { 0, 2, -1 }, + { -1, 2, -1 }, + { -1, 2, -1 }, + { -1, 2, -1 }, + { -1, 2, -1 }, + { -1, 2, 0 } + } ); + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; } int main( int argc, char* argv[] ) { - std::cout << "Creating matrices on CPU ... " << std::endl; - initializerListExample< TNL::Devices::Host >(); + std::cout << "Creating tridiagonal matrix on CPU ... " << std::endl; + createTridiagonalMatrix< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Creating matrices on CUDA GPU ... " << std::endl; - initializerListExample< TNL::Devices::Cuda >(); + std::cout << "Creating tridiagonal matrix on CUDA GPU ... " << std::endl; + createTridiagonalMatrix< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cpp deleted file mode 100644 index e14f06188..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include - - -template< typename Device > -void initializerListExample() -{ - TNL::Matrices::SparseMatrix< double, Device > matrix ( - 5, // number of matrix rows - 5, // number of matrix columns - { // matrix elements definition - { 0, 0, 2.0 }, - { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, - { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, - { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, - { 4, 4, 2.0 } } ); - - std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Creating matrices on CPU ... " << std::endl; - initializerListExample< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Creating matrices on CUDA GPU ... " << std::endl; - initializerListExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cu deleted file mode 120000 index 96822f2c3..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_2.cu +++ /dev/null @@ -1 +0,0 @@ -TridiagonalMatrixExample_Constructor_init_list_2.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cpp deleted file mode 100644 index 8f64d0e9b..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -#include -#include -#include - - -template< typename Device > -void initializerListExample() -{ - std::map< std::pair< int, int >, double > map; - map.insert( std::make_pair( std::make_pair( 0, 0 ), 2.0 ) ); - map.insert( std::make_pair( std::make_pair( 1, 0 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 1, 1 ), 2.0 ) ); - map.insert( std::make_pair( std::make_pair( 1, 2 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 2, 1 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 2, 2 ), 2.0 ) ); - map.insert( std::make_pair( std::make_pair( 2, 3 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 3, 2 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 3, 3 ), 2.0 ) ); - map.insert( std::make_pair( std::make_pair( 3, 4 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 4, 4 ), 2.0 ) ); - - TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, map ); - - std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Creating matrices on CPU ... " << std::endl; - initializerListExample< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Creating matrices on CUDA GPU ... " << std::endl; - initializerListExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cu deleted file mode 120000 index e1943cb92..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_std_map.cu +++ /dev/null @@ -1 +0,0 @@ -TridiagonalMatrixExample_Constructor_std_map.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cpp index 1b51f494f..66876f1e3 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_addElement.cpp @@ -1,19 +1,28 @@ #include -#include +#include #include template< typename Device > void addElements() { - TNL::Matrices::SparseMatrix< double, Device > matrix( { 5, 5, 5, 5, 5 }, 5 ); - for( int i = 0; i < 5; i++ ) + const int matrixSize( 5 ); + TNL::Matrices::TridiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize // number of columns + ); + for( int i = 0; i < matrixSize; i++ ) matrix.setElement( i, i, i ); std::cout << "Initial matrix is: " << std::endl << matrix << std::endl; - for( int i = 0; i < 5; i++ ) - for( int j = 0; j < 5; j++ ) - matrix.addElement( i, j, 1.0, 5.0 ); + for( int i = 0; i < matrixSize; i++ ) + { + if( i > 0 ) + matrix.addElement( i, i - 1, 1.0, 5.0 ); + matrix.addElement( i, i, 1.0, 5.0 ); + if( i < matrixSize - 1 ) + matrix.addElement( i, i + 1, 1.0, 5.0 ); + } std::cout << "Matrix after addition is: " << std::endl << matrix << std::endl; } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp index 9ce31f09e..043dcc82e 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp @@ -1,19 +1,30 @@ #include #include #include -#include +#include #include -#include template< typename Device > -void allRowsReduction() +void rowsReduction() { - TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { - { 0, 0, 1 }, - { 1, 1, 1 }, { 1, 2, 8 }, - { 2, 2, 1 }, { 2, 3, 9 }, - { 3, 3, 1 }, { 3, 4, 9 }, - { 4, 4, 1 } } ); + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } + * + */ + TNL::Matrices::TridiagonalMatrix< double, Device > matrix ( + 5, // number of matrix columns + { { 0, 1, 3 }, // matrix elements + { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 } } ); /*** * Find largest element in each row. @@ -21,7 +32,7 @@ void allRowsReduction() TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() ); /*** - * Prepare vector view and matrix view for lambdas. + * Prepare vector view for lambdas. */ auto rowMaxView = rowMax.getView(); @@ -57,11 +68,11 @@ void allRowsReduction() int main( int argc, char* argv[] ) { - std::cout << "All rows reduction on host:" << std::endl; - allRowsReduction< TNL::Devices::Host >(); + std::cout << "Rows reduction on host:" << std::endl; + rowsReduction< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "All rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); + std::cout << "Rows reduction on CUDA device:" << std::endl; + rowsReduction< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp index 739600539..ff3fdee91 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp @@ -1,24 +1,42 @@ #include -#include +#include #include #include template< typename Device > -void forAllRowsExample() +void forRowsExample() { - TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 2, 3, 4, 5 }, 5 ); + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } + */ + TNL::Matrices::TridiagonalMatrix< double, Device > matrix( + 5, // number of matrix rows + 5 ); // number of matrix columns - auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int& columnIdx, double& value, bool& compute ) { - if( rowIdx < columnIdx ) // This is important, some matrix formats may allocate more matrix elements - // than we requested. These padding elements are processed here as well. - compute = false; - else - { - columnIdx = localIdx; - value = rowIdx + localIdx; - } + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) { + /*** + * 'forRows' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ------- + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } + * + */ + value = 3 - localIdx; }; - matrix.forAllRows( f ); std::cout << matrix << std::endl; } @@ -26,10 +44,10 @@ void forAllRowsExample() int main( int argc, char* argv[] ) { std::cout << "Creating matrix on host: " << std::endl; - forAllRowsExample< TNL::Devices::Host >(); + forRowsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Creating matrix on CUDA device: " << std::endl; - forAllRowsExample< TNL::Devices::Cuda >(); + forRowsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp index 2330c2ca5..3ba17df51 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp @@ -1,24 +1,42 @@ #include -#include +#include #include #include template< typename Device > void forRowsExample() { - TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 2, 3, 4, 5 }, 5 ); + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } + */ + TNL::Matrices::TridiagonalMatrix< double, Device > matrix( + 5, // number of matrix rows + 5 ); // number of matrix columns - auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int& columnIdx, double& value, bool& compute ) { - if( rowIdx < columnIdx ) // This is important, some matrix formats may allocate more matrix elements - // than we requested. These padding elements are processed here as well. - compute = false; - else - { - columnIdx = localIdx; - value = rowIdx + localIdx; - } + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) { + /*** + * 'forRows' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ------- + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } + * + */ + value = 3 - localIdx; }; - matrix.forRows( 0, matrix.getRows(), f ); std::cout << matrix << std::endl; } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cpp index e56abe484..ebe40c5ac 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getCompressedRowLengths.cpp @@ -1,34 +1,40 @@ #include -#include +#include +#include #include #include + template< typename Device > -void getCompressedRowLengthsExample() +void laplaceOperatorMatrix() { - TNL::Matrices::SparseMatrix< double, Device > triangularMatrix( 5, 5 ); - triangularMatrix.setElements( { - { 0, 0, 1 }, - { 1, 0, 2 }, { 1, 1, 3 }, - { 2, 0, 4 }, { 2, 1, 5 }, { 2, 2, 6 }, - { 3, 0, 7 }, { 3, 1, 8 }, { 3, 2, 9 }, { 3, 3, 10 }, - { 4, 0, 11 }, { 4, 1, 12 }, { 4, 2, 13 }, { 4, 3, 14 }, { 4, 4, 15 } } ); - - std::cout << triangularMatrix << std::endl; - + const int gridSize( 6 ); + const int matrixSize = gridSize; + TNL::Matrices::TridiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize // number of columns + ); + matrix.setElements( { + { 0.0, 1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { 0.0, 1.0 } + } ); TNL::Containers::Vector< int, Device > rowLengths; - triangularMatrix.getCompressedRowLengths( rowLengths ); - - std::cout << "Compressed row lengths are: " << rowLengths << std::endl; + matrix.getCompressedRowLengths( rowLengths ); + std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; + std::cout << "Compressed row lengths: " << rowLengths << std::endl; } int main( int argc, char* argv[] ) { - std::cout << "Getting compressed row lengths on host: " << std::endl; - getCompressedRowLengthsExample< TNL::Devices::Host >(); + std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Getting compressed row lengths on CUDA device: " << std::endl; - getCompressedRowLengthsExample< TNL::Devices::Cuda >(); + std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cpp index 747a3c825..bac9303ae 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getConstRow.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include @@ -9,27 +9,31 @@ template< typename Device > void getRowExample() { - using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; - TNL::Pointers::SharedPointer< MatrixType > matrix ( 5, 5 ); + const int matrixSize = 5; + using MatrixType = TNL::Matrices::TridiagonalMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > matrix ( + matrixSize, // number of matrix rows + matrixSize // number of matrix columns + ); matrix->setElements( - { { 0, 0, 1 }, - { 1, 0, 1 }, { 1, 1, 2 }, - { 2, 0, 1 }, { 2, 1, 2 }, { 2, 2, 3 }, - { 3, 0, 1 }, { 3, 1, 2 }, { 3, 2, 3 }, { 3, 3, 4 }, - { 4, 0, 1 }, { 4, 1, 2 }, { 4, 2, 3 }, { 4, 3, 4 }, { 4, 4, 5 } } ); + { { 0.0, 2.0, 1.0 }, + { 0.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 }, + { 0.0, 2.0, 1.0 } } ); /*** * Fetch lambda function returns diagonal element in each row. */ auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { auto row = matrix->getRow( rowIdx ); - return row.getValue( rowIdx ); + return row.getValue( 2 ); // get value from subdiagonal with index 2, i.e. the main diagonal }; /*** * For the case when Device is CUDA device we need to synchronize smart - * pointers. To avoid this you may use SparseMatrixView. See - * SparseMatrixView::getConstRow example for details. + * pointers. To avoid this you may use TridiagonalMatrixView. See + * TridiagonalMatrixView::getConstRow example for details. */ TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); @@ -37,7 +41,8 @@ void getRowExample() * Compute the matrix trace. */ int trace = TNL::Algorithms::Reduction< Device >::reduce( matrix->getRows(), std::plus<>{}, fetch, 0 ); - std::cout << "Matrix trace is " << trace << "." << std::endl; + std::cout << "Matrix reads as: " << std::endl << *matrix << std::endl; + std::cout << "Matrix trace is: " << trace << "." << std::endl; } int main( int argc, char* argv[] ) diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cpp index 4db765979..5ba2bf302 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getElement.cpp @@ -1,25 +1,26 @@ #include #include -#include +#include #include template< typename Device > void getElements() { - TNL::Matrices::SparseMatrix< double, Device > matrix ( - 5, // number of matrix rows - 5, // number of matrix columns - { // matrix elements definition - { 0, 0, 2.0 }, - { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, - { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, - { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, - { 4, 4, 2.0 } } ); + const int matrixSize( 5 ); + TNL::Matrices::TridiagonalMatrix< double, Device > matrix ( + matrixSize, // number of matrix columns + { // matrix elements definition + { 0.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, 0.0 } + } ); - for( int i = 0; i < 5; i++ ) + for( int i = 0; i < matrixSize; i++ ) { - for( int j = 0; j < 5; j++ ) + for( int j = 0; j < matrixSize; j++ ) std::cout << std::setw( 5 ) << matrix.getElement( i, j ); std::cout << std::endl; } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cpp index d07577ff9..cfbdce919 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include @@ -8,21 +8,31 @@ template< typename Device > void getRowExample() { - auto rowCapacities = { 1, 1, 1, 1, 1 }; // Variadic templates in SharedPointer - // constructor do not recognize initializer - // list so we give it a hint. - using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; - TNL::Pointers::SharedPointer< MatrixType > matrix( rowCapacities, 5 ); + const int matrixSize( 5 ); + using MatrixType = TNL::Matrices::TridiagonalMatrix< double, Device >; + TNL::Pointers::SharedPointer< MatrixType > matrix( + matrixSize, // number of matrix rows + matrixSize // number of matrix columns + ); auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { - auto row = matrix->getRow( rowIdx ); - row.setElement( 0, rowIdx, 10 * ( rowIdx + 1 ) ); + //auto row = matrix->getRow( rowIdx ); + // For some reason the previous line of code is not accepted by nvcc 10.1 + // so we replace it with the following two lines. + auto ref = matrix.modifyData(); + auto row = ref.getRow( rowIdx ); + + if( rowIdx > 0 ) + row.setElement( 0, -1.0 ); // elements below the diagonal + row.setElement( 1, 2.0 ); // elements on the diagonal + if( rowIdx < matrixSize - 1 ) // elements above the diagonal + row.setElement( 2, -1.0 ); }; /*** * For the case when Device is CUDA device we need to synchronize smart - * pointers. To avoid this you may use SparseMatrixView. See - * SparseMatrixView::getRow example for details. + * pointers. To avoid this you may use TridiagonalMatrixView. See + * TridiagonalMatrixView::getRow example for details. */ TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); @@ -30,7 +40,7 @@ void getRowExample() * Set the matrix elements. */ TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix->getRows(), f ); - std::cout << matrix << std::endl; + std::cout << std::endl << *matrix << std::endl; } int main( int argc, char* argv[] ) @@ -39,7 +49,10 @@ int main( int argc, char* argv[] ) getRowExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Getting matrix rows on CUDA device: " << std::endl; - getRowExample< TNL::Devices::Cuda >(); + // It seems that nvcc 10.1 does not handle lambda functions properly. + // It is hard to make nvcc to compile this example and it does not work + // properly. We will try it with later version of CUDA. + //std::cout << "Getting matrix rows on CUDA device: " << std::endl; + //getRowExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cpp index 35dbf0406..e76f87e1a 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getSerializationType.cpp @@ -1,12 +1,12 @@ #include -#include +#include #include template< typename Device > void getSerializationTypeExample() { - TNL::Matrices::SparseMatrix< double, Device > matrix; + TNL::Matrices::TridiagonalMatrix< double, Device > matrix; std::cout << "Matrix type is: " << matrix.getSerializationType(); } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp index 20279888a..792dc98d3 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp @@ -1,18 +1,30 @@ #include #include #include -#include +#include #include template< typename Device > void rowsReduction() { - TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { - { 0, 0, 1 }, - { 1, 1, 1 }, { 1, 2, 8 }, - { 2, 2, 1 }, { 2, 3, 9 }, - { 3, 3, 1 }, { 3, 4, 9 }, - { 4, 4, 1 } } ); + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } + * + */ + TNL::Matrices::TridiagonalMatrix< double, Device > matrix ( + 5, // number of matrix columns + { { 0, 1, 3 }, // matrix elements + { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 } } ); /*** * Find largest element in each row. diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp index 178e502dc..751aca3a5 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include @@ -9,8 +9,9 @@ template< typename Device > void setElements() { - auto rowCapacities = { 1, 1, 1, 1, 1 }; - TNL::Pointers::SharedPointer< TNL::Matrices::SparseMatrix< double, Device > > matrix( rowCapacities, 5 ); + const int matrixSize( 5 ); + using Matrix = TNL::Matrices::TridiagonalMatrix< double, Device >; + TNL::Pointers::SharedPointer< Matrix > matrix( matrixSize, matrixSize ); for( int i = 0; i < 5; i++ ) matrix->setElement( i, i, i ); @@ -18,16 +19,20 @@ void setElements() std::cout << *matrix << std::endl; auto f = [=] __cuda_callable__ ( int i ) mutable { + if( i > 0 ) + matrix->setElement( i, i - 1, 1.0 ); matrix->setElement( i, i, -i ); + if( i < matrixSize - 1 ) + matrix->setElement( i, i + 1, 1.0 ); }; /*** * For the case when Device is CUDA device we need to synchronize smart - * pointers. To avoid this you may use SparseMatrixView. See - * SparseMatrixView::getRow example for details. + * pointers. To avoid this you may use TridiagonalMatrixView. See + * TridiagonalMatrixView::getRow example for details. */ TNL::Pointers::synchronizeSmartPointersOnDevice< Device >(); - TNL::Algorithms::ParallelFor< Device >::exec( 0, 5, f ); + TNL::Algorithms::ParallelFor< Device >::exec( 0, matrixSize, f ); std::cout << "Matrix set from its native device:" << std::endl; std::cout << *matrix << std::endl; @@ -39,6 +44,9 @@ int main( int argc, char* argv[] ) setElements< TNL::Devices::Host >(); #ifdef HAVE_CUDA + // It seems that nvcc 10.1 does not handle lambda functions properly. + // It is hard to make nvcc to compile this example and it does not work + // properly. We will try it with later version of CUDA. std::cout << "Set elements on CUDA device:" << std::endl; setElements< TNL::Devices::Cuda >(); #endif diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cpp index ff0c0bde8..dcc6f3d2a 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cpp @@ -1,29 +1,58 @@ #include -#include +#include +#include #include #include + template< typename Device > -void setElementsExample() +void createTridiagonalMatrix() { - TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5 ); // matrix dimensions - matrix.setElements( { // matrix elements definition - { 0, 0, 2.0 }, - { 1, 0, -1.0 }, { 1, 1, 2.0 }, { 1, 2, -1.0 }, - { 2, 1, -1.0 }, { 2, 2, 2.0 }, { 2, 3, -1.0 }, - { 3, 2, -1.0 }, { 3, 3, 2.0 }, { 3, 4, -1.0 }, - { 4, 4, 2.0 } } ); + const int matrixSize = 6; - std::cout << matrix << std::endl; + /*** + * Setup the following matrix (dots represent zeros): + * + * / 2 -1 . . . . \ + * | -1 2 -1 . . . | + * | . -1 2 -1 . . | + * | . . -1 2 -1 . | + * | . . . -1 2 -1 | + * \ . . . . -1 2 / + * + */ + TNL::Matrices::TridiagonalMatrix< double, Device > matrix( matrixSize, matrixSize ); + matrix.setElements( { + /*** + * To set the matrix elements we first extend the diagonals to their full + * lengths even outside the matrix (dots represent zeros and zeros are + * artificial zeros used for memory alignment): + * + * 0 / 2 -1 . . . . \ -> { 0, 2, -1 } + * | -1 2 -1 . . . | -> { -1, 2, -1 } + * | . -1 2 -1 . . | -> { -1, 2, -1 } + * | . . -1 2 -1 . | -> { -1, 2, -1 } + * | . . . -1 2 -1 | -> { -1, 2, -1 } + * \ . . . . -1 2 / 0 -> { -1, 2, 0 } + * + */ + { 0, 2, -1 }, + { -1, 2, -1 }, + { -1, 2, -1 }, + { -1, 2, -1 }, + { -1, 2, -1 }, + { -1, 2, 0 } + } ); + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; } int main( int argc, char* argv[] ) { - std::cout << "Setting matrix elements on host: " << std::endl; - setElementsExample< TNL::Devices::Host >(); + std::cout << "Creating tridiagonal matrix on CPU ... " << std::endl; + createTridiagonalMatrix< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Setting matrix elements on CUDA device: " << std::endl; - setElementsExample< TNL::Devices::Cuda >(); + std::cout << "Creating tridiagonal matrix on CUDA GPU ... " << std::endl; + createTridiagonalMatrix< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cpp deleted file mode 100644 index c7bdbbc17..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include -#include -#include -#include - - -template< typename Device > -void setElementsExample() -{ - std::map< std::pair< int, int >, double > map; - map.insert( std::make_pair( std::make_pair( 0, 0 ), 2.0 ) ); - map.insert( std::make_pair( std::make_pair( 1, 0 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 1, 1 ), 2.0 ) ); - map.insert( std::make_pair( std::make_pair( 1, 2 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 2, 1 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 2, 2 ), 2.0 ) ); - map.insert( std::make_pair( std::make_pair( 2, 3 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 3, 2 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 3, 3 ), 2.0 ) ); - map.insert( std::make_pair( std::make_pair( 3, 4 ), -1.0 ) ); - map.insert( std::make_pair( std::make_pair( 4, 4 ), 2.0 ) ); - - TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5 ); - matrix.setElements( map ); - - std::cout << "General sparse matrix: " << std::endl << matrix << std::endl; -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Creating matrices on CPU ... " << std::endl; - setElementsExample< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Creating matrices on CUDA GPU ... " << std::endl; - setElementsExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cu deleted file mode 120000 index 7dd1ac977..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements_map.cu +++ /dev/null @@ -1 +0,0 @@ -TridiagonalMatrixExample_setElements_map.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cpp deleted file mode 100644 index f282aee6d..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include -#include -#include -#include - -template< typename Device > -void setRowCapacitiesExample() -{ - TNL::Matrices::SparseMatrix< double, Device > matrix( 5, 5 ); - TNL::Containers::Vector< int, Device > rowCapacities{ 1, 2, 3, 4, 5 }; - matrix.setRowCapacities( rowCapacities ); - for( int row = 0; row < 5; row++ ) - for( int column = 0; column <= row; column++ ) - matrix.setElement( row, column, row - column + 1 ); - - std::cout << matrix << std::endl; -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Creating matrices on CPU ... " << std::endl; - setRowCapacitiesExample< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Creating matrices on CUDA GPU ... " << std::endl; - setRowCapacitiesExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cu deleted file mode 120000 index 6212f7467..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setRowCapacities.cu +++ /dev/null @@ -1 +0,0 @@ -TridiagonalMatrixExample_setRowCapacities.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cpp new file mode 100644 index 000000000..46f92d7a3 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +template< typename Device > +void addElements() +{ + const int matrixSize( 5 ); + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize, // number of columns + { -1, 0, 1 } ); // diagonals offsets + auto view = matrix.getView(); + for( int i = 0; i < matrixSize; i++ ) + view.setElement( i, i, i ); + + std::cout << "Initial matrix is: " << std::endl << matrix << std::endl; + + for( int i = 0; i < matrixSize; i++ ) + { + if( i > 0 ) + view.addElement( i, i - 1, 1.0, 5.0 ); + view.addElement( i, i, 1.0, 5.0 ); + if( i < matrixSize - 1 ) + view.addElement( i, i + 1, 1.0, 5.0 ); + } + + std::cout << "Matrix after addition is: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Add elements on host:" << std::endl; + addElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Add elements on CUDA device:" << std::endl; + addElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cu new file mode 120000 index 000000000..9eb313e81 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp new file mode 100644 index 000000000..26dac464c --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void allRowsReduction() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + 5, // number of matrix columns + { -2, -1, 0 }, // diagonals offsets + { { 0, 0, 1 }, // matrix elements + { 0, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 } } ); + auto view = matrix.getView(); + + /*** + * Find largest element in each row. + */ + TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() ); + + /*** + * Prepare vector view for lambdas. + */ + auto rowMaxView = rowMax.getView(); + + /*** + * Fetch lambda just returns absolute value of matrix elements. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx, int columnIdx, const double& value ) -> double { + return TNL::abs( value ); + }; + + /*** + * Reduce lambda return maximum of given values. + */ + auto reduce = [=] __cuda_callable__ ( double& a, const double& b ) -> double { + return TNL::max( a, b ); + }; + + /*** + * Keep lambda store the largest value in each row to the vector rowMax. + */ + auto keep = [=] __cuda_callable__ ( int rowIdx, const double& value ) mutable { + rowMaxView[ rowIdx ] = value; + }; + + /*** + * Compute the largest values in each row. + */ + view.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Rows reduction on host:" << std::endl; + allRowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + allRowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cu new file mode 120000 index 000000000..7b330650f --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cpp new file mode 100644 index 000000000..143aa864c --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +template< typename Device > +void forAllRowsExample() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + 5, // number of matrix rows + 5, // number of matrix columns + { -2, -1, 0 } ); // matrix diagonals offsets + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) { + /*** + * 'forRows' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ------- + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + */ + value = 3 - localIdx; + }; + view.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forAllRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forAllRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cu new file mode 120000 index 000000000..fae202888 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_forAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp new file mode 100644 index 000000000..23aa06753 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + 5, // number of matrix rows + 5, // number of matrix columns + { -2, -1, 0 } ); // matrix diagonals offsets + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) { + /*** + * 'forRows' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ------- + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + */ + value = 3 - localIdx; + }; + view.forRows( 0, matrix.getRows(), f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cu new file mode 120000 index 000000000..ea70e5b9e --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cpp new file mode 100644 index 000000000..2b366ab3d --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void laplaceOperatorMatrix() +{ + const int gridSize( 4 ); + const int matrixSize = gridSize * gridSize; + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize, // number of columns + { - gridSize, -1, 0, 1, gridSize } // diagonals offsets + ); + matrix.setElements( { + { 0.0, 0.0, 1.0 }, // set matrix elements corresponding to boundary grid nodes + { 0.0, 0.0, 1.0 }, // and Dirichlet boundary conditions, i.e. 1 on the main diagonal + { 0.0, 0.0, 1.0 }, // which is the third one + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, // set matrix elements corresponding to inner grid nodes, i.e. 4 on the main diagonal + { -1.0, -1.0, 4.0, -1.0, -1.0 }, // (the third one) and -1 to the other sub-diagonals + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { -1.0, -1.0, 4.0, -1.0, -1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 }, + { 0.0, 0.0, 1.0 } + } ); + auto view = matrix.getView(); + TNL::Containers::Vector< int, Device > rowLengths; + view.getCompressedRowLengths( rowLengths ); + std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; + std::cout << "Compressed row lengths: " << rowLengths << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl; + laplaceOperatorMatrix< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cu new file mode 120000 index 000000000..c44da63b3 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_getCompressedRowLengths.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cpp new file mode 100644 index 000000000..748c95665 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + const int matrixSize = 5; + auto diagonalsOffsets = { -2, -1, 0 }; + using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + MatrixType matrix ( + matrixSize, // number of matrix columns + diagonalsOffsets, + { { 0.0, 0.0, 1.0 }, // matrix elements + { 0.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 }, + { 3.0, 2.0, 1.0 } } ); + auto view = matrix.getView(); + + /*** + * Fetch lambda function returns diagonal element in each row. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { + auto row = view.getRow( rowIdx ); + return row.getValue( 2 ); // get value from subdiagonal with index 2, i.e. the main diagonal + }; + + /*** + * Compute the matrix trace. + */ + int trace = TNL::Algorithms::Reduction< Device >::reduce( matrix.getRows(), std::plus<>{}, fetch, 0 ); + std::cout << "Matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Matrix trace is: " << trace << "." << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Getting matrix rows on host: " << std::endl; + getRowExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cu new file mode 120000 index 000000000..c43061eb1 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_getConstRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cpp new file mode 100644 index 000000000..bf1226662 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +template< typename Device > +void getElements() +{ + const int matrixSize( 5 ); + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + matrixSize, // number of matrix columns + { -1, 0, 1 }, // matrix diagonals offsets + { // matrix elements definition + { 0.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, 0.0 } + } ); + auto view = matrix.getView(); + + for( int i = 0; i < matrixSize; i++ ) + { + for( int j = 0; j < matrixSize; j++ ) + std::cout << std::setw( 5 ) << view.getElement( i, j ); + std::cout << std::endl; + } +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Get elements on host:" << std::endl; + getElements< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Get elements on CUDA device:" << std::endl; + getElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cu new file mode 120000 index 000000000..70bf71224 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_getElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp new file mode 100644 index 000000000..ac322f9aa --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void getRowExample() +{ + const int matrixSize( 5 ); + auto diagonalsOffsets = { -1, 0, 1 }; // Variadic templates in SharedPointer + // constructor do not recognize initializer + // list so we give it a hint. + using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + MatrixType matrix( + matrixSize, // number of matrix rows + matrixSize, // number of matrix columns + diagonalsOffsets ); + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { + auto row = view.getRow( rowIdx ); + if( rowIdx > 0 ) + row.setElement( 0, -1.0 ); // elements below the diagonal + row.setElement( 1, 2.0 ); // elements on the diagonal + if( rowIdx < matrixSize - 1 ) // elements above the diagonal + row.setElement( 2, -1.0 ); + }; + + /*** + * Set the matrix elements. + */ + TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix.getRows(), f ); + std::cout << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Getting matrix rows on host: " << std::endl; + getRowExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Getting matrix rows on CUDA device: " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cu new file mode 120000 index 000000000..12e2f392c --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_getRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp new file mode 100644 index 000000000..6793d8bb2 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include + +template< typename Device > +void rowsReduction() +{ + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 0 / 1 . . . . \ -> { 0, 0, 1 } + * 0 | 2 1 . . . | -> { 0, 2, 1 } + * | 3 2 1 . . | -> { 3, 2, 1 } + * | . 3 2 1 . | -> { 3, 2, 1 } + * \ . . 3 2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -2, -1, 0 }. + */ + TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + 5, // number of matrix columns + { -2, -1, 0 }, // diagonals offsets + { { 0, 0, 1 }, // matrix elements + { 0, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 }, + { 3, 2, 1 } } ); + auto view = matrix.getView(); + + /*** + * Find largest element in each row. + */ + TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() ); + + /*** + * Prepare vector view for lambdas. + */ + auto rowMaxView = rowMax.getView(); + + /*** + * Fetch lambda just returns absolute value of matrix elements. + */ + auto fetch = [=] __cuda_callable__ ( int rowIdx, int columnIdx, const double& value ) -> double { + return TNL::abs( value ); + }; + + /*** + * Reduce lambda return maximum of given values. + */ + auto reduce = [=] __cuda_callable__ ( double& a, const double& b ) -> double { + return TNL::max( a, b ); + }; + + /*** + * Keep lambda store the largest value in each row to the vector rowMax. + */ + auto keep = [=] __cuda_callable__ ( int rowIdx, const double& value ) mutable { + rowMaxView[ rowIdx ] = value; + }; + + /*** + * Compute the largest values in each row. + */ + view.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + + std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; + std::cout << "Max. elements in rows are: " << rowMax << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Rows reduction on host:" << std::endl; + rowsReduction< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Rows reduction on CUDA device:" << std::endl; + rowsReduction< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cu new file mode 120000 index 000000000..f749c1ef4 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp new file mode 100644 index 000000000..41f936670 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include + +template< typename Device > +void setElements() +{ + const int matrixSize( 5 ); + auto diagonalsOffsets = { -1, 0, 1 }; // offsets of tridiagonal matrix + using Matrix = TNL::Matrices::MultidiagonalMatrix< double, Device >; + Matrix matrix( matrixSize, matrixSize, diagonalsOffsets ); + auto view = matrix.getView(); + + for( int i = 0; i < 5; i++ ) + view.setElement( i, i, i ); + + std::cout << "Matrix set from the host:" << std::endl; + std::cout << matrix << std::endl; + + auto f = [=] __cuda_callable__ ( int i ) mutable { + if( i > 0 ) + view.setElement( i, i - 1, 1.0 ); + view.setElement( i, i, -i ); + if( i < matrixSize - 1 ) + view.setElement( i, i + 1, 1.0 ); + }; + + TNL::Algorithms::ParallelFor< Device >::exec( 0, matrixSize, 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 HAVE_CUDA + std::cout << "Set elements on CUDA device:" << std::endl; + setElements< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cu new file mode 120000 index 000000000..9510661c1 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_setElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/genfiles b/Documentation/Examples/Matrices/TridiagonalMatrix/genfiles new file mode 100755 index 000000000..0f17c7d02 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/genfiles @@ -0,0 +1,53 @@ +#!/bin/bash + +mv MultidiagonalMatrixExample_addElement.cpp TridiagonalMatrixExample_addElement.cpp +mv MultidiagonalMatrixExample_allRowsReduction.cpp TridiagonalMatrixExample_allRowsReduction.cpp +mv MultidiagonalMatrixExample_Constructor.cpp TridiagonalMatrixExample_Constructor.cpp +mv MultidiagonalMatrixExample_Constructor_init_list_1.cpp TridiagonalMatrixExample_Constructor_init_list_1.cpp +mv MultidiagonalMatrixExample_Constructor_init_list_2.cpp TridiagonalMatrixExample_Constructor_init_list_2.cpp +mv MultidiagonalMatrixExample_forAllRows.cpp TridiagonalMatrixExample_forAllRows.cpp +mv MultidiagonalMatrixExample_forRows.cpp TridiagonalMatrixExample_forRows.cpp +mv MultidiagonalMatrixExample_getCompressedRowLengths.cpp TridiagonalMatrixExample_getCompressedRowLengths.cpp +mv MultidiagonalMatrixExample_getConstRow.cpp TridiagonalMatrixExample_getConstRow.cpp +mv MultidiagonalMatrixExample_getElement.cpp TridiagonalMatrixExample_getElement.cpp +mv MultidiagonalMatrixExample_getRow.cpp TridiagonalMatrixExample_getRow.cpp +mv MultidiagonalMatrixExample_getSerializationType.cpp TridiagonalMatrixExample_getSerializationType.cpp +mv MultidiagonalMatrixExample_rowsReduction.cpp TridiagonalMatrixExample_rowsReduction.cpp +mv MultidiagonalMatrixExample_setElement.cpp TridiagonalMatrixExample_setElement.cpp +mv MultidiagonalMatrixExample_setElements.cpp TridiagonalMatrixExample_setElements.cpp +mv MultidiagonalMatrixViewExample_addElement.cpp TridiagonalMatrixViewExample_addElement.cpp +mv MultidiagonalMatrixViewExample_allRowsReduction.cpp TridiagonalMatrixViewExample_allRowsReduction.cpp +mv MultidiagonalMatrixViewExample_forAllRows.cpp TridiagonalMatrixViewExample_forAllRows.cpp +mv MultidiagonalMatrixViewExample_forRows.cpp TridiagonalMatrixViewExample_forRows.cpp +mv MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp TridiagonalMatrixViewExample_getCompressedRowLengths.cpp +mv MultidiagonalMatrixViewExample_getConstRow.cpp TridiagonalMatrixViewExample_getConstRow.cpp +mv MultidiagonalMatrixViewExample_getElement.cpp TridiagonalMatrixViewExample_getElement.cpp +mv MultidiagonalMatrixViewExample_getRow.cpp TridiagonalMatrixViewExample_getRow.cpp +mv MultidiagonalMatrixViewExample_rowsReduction.cpp TridiagonalMatrixViewExample_rowsReduction.cpp +mv MultidiagonalMatrixViewExample_setElement.cpp TridiagonalMatrixViewExample_setElement.cpp + +ln -s TridiagonalMatrixExample_addElement.cpp TridiagonalMatrixExample_addElement.cu +ln -s TridiagonalMatrixExample_allRowsReduction.cpp TridiagonalMatrixExample_allRowsReduction.cu +ln -s TridiagonalMatrixExample_Constructor.cpp TridiagonalMatrixExample_Constructor.cu +ln -s TridiagonalMatrixExample_Constructor_init_list_1.cpp TridiagonalMatrixExample_Constructor_init_list_1.cu +ln -s TridiagonalMatrixExample_Constructor_init_list_2.cpp TridiagonalMatrixExample_Constructor_init_list_2.cu +ln -s TridiagonalMatrixExample_forAllRows.cpp TridiagonalMatrixExample_forAllRows.cu +ln -s TridiagonalMatrixExample_forRows.cpp TridiagonalMatrixExample_forRows.cu +ln -s TridiagonalMatrixExample_getCompressedRowLengths.cpp TridiagonalMatrixExample_getCompressedRowLengths.cu +ln -s TridiagonalMatrixExample_getConstRow.cpp TridiagonalMatrixExample_getConstRow.cu +ln -s TridiagonalMatrixExample_getElement.cpp TridiagonalMatrixExample_getElement.cu +ln -s TridiagonalMatrixExample_getRow.cpp TridiagonalMatrixExample_getRow.cu +ln -s TridiagonalMatrixExample_getSerializationType.cpp TridiagonalMatrixExample_getSerializationType.cu +ln -s TridiagonalMatrixExample_rowsReduction.cpp TridiagonalMatrixExample_rowsReduction.cu +ln -s TridiagonalMatrixExample_setElement.cpp TridiagonalMatrixExample_setElement.cu +ln -s TridiagonalMatrixExample_setElements.cpp TridiagonalMatrixExample_setElements.cu +ln -s TridiagonalMatrixViewExample_addElement.cpp TridiagonalMatrixViewExample_addElement.cu +ln -s TridiagonalMatrixViewExample_allRowsReduction.cpp TridiagonalMatrixViewExample_allRowsReduction.cu +ln -s TridiagonalMatrixViewExample_forAllRows.cpp TridiagonalMatrixViewExample_forAllRows.cu +ln -s TridiagonalMatrixViewExample_forRows.cpp TridiagonalMatrixViewExample_forRows.cu +ln -s TridiagonalMatrixViewExample_getCompressedRowLengths.cpp TridiagonalMatrixViewExample_getCompressedRowLengths.cu +ln -s TridiagonalMatrixViewExample_getConstRow.cpp TridiagonalMatrixViewExample_getConstRow.cu +ln -s TridiagonalMatrixViewExample_getElement.cpp TridiagonalMatrixViewExample_getElement.cu +ln -s TridiagonalMatrixViewExample_getRow.cpp TridiagonalMatrixViewExample_getRow.cu +ln -s TridiagonalMatrixViewExample_rowsReduction.cpp TridiagonalMatrixViewExample_rowsReduction.cu +ln -s TridiagonalMatrixViewExample_setElement.cpp TridiagonalMatrixViewExample_setElement.cu diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index 4cd4902f3..e50782d9a 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -100,19 +100,6 @@ getView() const -> ViewType indexer ); } -/*template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization, - typename RealAllocator, - typename IndexAllocator > -auto -MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -getConstView() const -> ConstViewType -{ - return ConstViewType( this->values.getConstView(), indexer ); -}*/ - template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Matrices/TridiagonalMatrix.h b/src/TNL/Matrices/TridiagonalMatrix.h index 0f0fb8d1c..436bf6272 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.h +++ b/src/TNL/Matrices/TridiagonalMatrix.h @@ -42,9 +42,9 @@ namespace Matrices { * 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 TridiagonalMatrix and \ref SparseMatrix. * - * Data types | SparseMatrix | MultidiagonalMatrix | Ratio + * Data types | SparseMatrix | TridiagonalMatrix | Ratio * --------------------|----------------------|---------------------|-------- * float + 32-bit int | 8 bytes per element | 4 bytes per element | 50% * double + 32-bit int| 12 bytes per element | 8 bytes per element | 75% @@ -121,11 +121,6 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > typename _Index = Index > using Self = TridiagonalMatrix< _Real, _Device, _Index >; - // TODO: remove this - it is here only for compatibility with original matrix implementation - //typedef Containers::Vector< IndexType, DeviceType, IndexType > CompressedRowLengthsVector; - //typedef Containers::VectorView< IndexType, DeviceType, IndexType > CompressedRowLengthsVectorView; - //typedef typename CompressedRowLengthsVectorView::ConstViewType ConstCompressedRowLengthsVectorView; - static constexpr ElementsOrganization getOrganization() { return Organization; }; /** @@ -155,13 +150,14 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * of sub-diagonals which do not fit to given row are omitted. * * \par Example - * \include Matrices/TridiagonalMatrix/tridiagonalMatrixExample_Constructor_init_list_1.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cpp * \par Output * \include TridiagonalMatrixExample_Constructor_init_list_1.out */ template< typename ListReal > TridiagonalMatrix( const IndexType columns, const std::initializer_list< std::initializer_list< ListReal > >& data ); + /** * \brief Copy constructor. * @@ -433,7 +429,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \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. - * \param thisElementMultiplicator is multiplicator the original matrix element + * \param thisElementTriplicator is multiplicator the original matrix element * value is multiplied by before addition of given \e value. * * \par Example @@ -445,7 +441,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > void addElement( const IndexType row, const IndexType column, const RealType& value, - const RealType& thisElementMultiplicator = 1.0 ); + const RealType& thisElementTriplicator = 1.0 ); /** * \brief Returns value of matrix element at position given by its row and column index. @@ -593,7 +589,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/MultidiagonalMatrix/TridiagonalMatrixExample_forRows.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp * \par Output * \include TridiagonalMatrixExample_forRows.out */ @@ -615,7 +611,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/MultidiagonalMatrix/TridiagonalMatrixExample_forRows.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp * \par Output * \include TridiagonalMatrixExample_forRows.out */ @@ -637,7 +633,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/MultidiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp * \par Output * \include TridiagonalMatrixExample_forAllRows.out */ @@ -659,7 +655,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called in each row. * * \par Example - * \include Matrices/MultidiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllRows.cpp * \par Output * \include TridiagonalMatrixExample_forAllRows.out */ @@ -676,7 +672,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * * More precisely, it computes: * - * `outVector = matrixMultiplicator * ( * this ) * inVector + outVectorMultiplicator * outVector` + * `outVector = matrixTriplicator * ( * this ) * inVector + outVectorTriplicator * outVector` * * \tparam InVector is type of input vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. @@ -685,8 +681,8 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * * \param inVector is input vector. * \param outVector is output vector. - * \param matrixMultiplicator is a factor by which the matrix is multiplied. It is one by default. - * \param outVectorMultiplicator is a factor by which the outVector is multiplied before added + * \param matrixTriplicator is a factor by which the matrix is multiplied. It is one by default. + * \param outVectorTriplicator is a factor by which the outVector is multiplied before added * to the result of matrix-vector product. It is zero by default. * \param begin is the beginning of the rows range for which the vector product * is computed. It is zero by default. @@ -697,19 +693,19 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > typename OutVector > void vectorProduct( const InVector& inVector, OutVector& outVector, - const RealType matrixMultiplicator = 1.0, - const RealType outVectorMultiplicator = 0.0, + const RealType matrixTriplicator = 1.0, + const RealType outVectorTriplicator = 0.0, const IndexType begin = 0, IndexType end = 0 ) const; template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_, typename RealAllocator_ > void addMatrix( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealAllocator_ >& matrix, - const RealType& matrixMultiplicator = 1.0, - const RealType& thisMatrixMultiplicator = 1.0 ); + const RealType& matrixTriplicator = 1.0, + const RealType& thisMatrixTriplicator = 1.0 ); template< typename Real2, typename Index2 > void getTransposition( const TridiagonalMatrix< Real2, Device, Index2 >& matrix, - const RealType& matrixMultiplicator = 1.0 ); + const RealType& matrixTriplicator = 1.0 ); template< typename Vector1, typename Vector2 > __cuda_callable__ diff --git a/src/TNL/Matrices/TridiagonalMatrix.hpp b/src/TNL/Matrices/TridiagonalMatrix.hpp index 76d9afc9f..78fffdeab 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.hpp +++ b/src/TNL/Matrices/TridiagonalMatrix.hpp @@ -44,25 +44,27 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > -auto + template< typename ListReal > TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -getView() const -> ViewType +TridiagonalMatrix( const IndexType columns, + const std::initializer_list< std::initializer_list< ListReal > >& data ) { - // TODO: fix when getConstView works - return ViewType( const_cast< TridiagonalMatrix* >( this )->values.getView(), indexer ); + this->setDimensions( data.size(), columns ); + this->setElements( data ); } -/*template< typename Real, +template< typename Real, typename Device, typename Index, ElementsOrganization Organization, typename RealAllocator > auto TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -getConstView() const -> ConstViewType +getView() const -> ViewType { - return ConstViewType( this->values.getConstView(), indexer ); -}*/ + // TODO: fix when getConstView works + return ViewType( const_cast< TridiagonalMatrix* >( this )->values.getView(), indexer ); +} template< typename Real, typename Device, @@ -138,37 +140,59 @@ template< typename Real, typename Index, ElementsOrganization Organization, typename RealAllocator > - template< typename Vector > + template< typename ListReal > void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -getCompressedRowLengths( Vector& rowLengths ) const +setElements( const std::initializer_list< std::initializer_list< ListReal > >& data ) { - return this->view.getCompressedRowLengths( rowLengths ); + if( std::is_same< DeviceType, Devices::Host >::value ) + { + this->getValues() = 0.0; + auto row_it = data.begin(); + for( size_t rowIdx = 0; rowIdx < data.size(); rowIdx++ ) + { + auto data_it = row_it->begin(); + IndexType i = 0; + while( data_it != row_it->end() ) + this->getRow( rowIdx ).setElement( i++, *data_it++ ); + row_it ++; + } + } + else + { + TridiagonalMatrix< Real, Devices::Host, Index, Organization > hostMatrix( + this->getRows(), + this->getColumns() ); + hostMatrix.setElements( data ); + *this = hostMatrix; + } } + template< typename Real, typename Device, typename Index, ElementsOrganization Organization, typename RealAllocator > -Index + template< typename Vector > +void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -getRowLength( const IndexType row ) const +getCompressedRowLengths( Vector& rowLengths ) const { - return this->view.getRowLength( row ); + return this->view.getCompressedRowLengths( rowLengths ); } -/*template< typename Real, +template< typename Real, typename Device, typename Index, ElementsOrganization Organization, typename RealAllocator > Index TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -getMaxRowLength() const +getRowLength( const IndexType row ) const { - return this->view.getMaxRowLength(); -}*/ + return this->view.getRowLength( row ); +} template< typename Real, typename Device, @@ -328,6 +352,19 @@ rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Ke this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +{ + this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, @@ -341,6 +378,19 @@ allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zer this->view.rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +{ + this->view.rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, @@ -669,9 +719,9 @@ Index TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getElementIndex( const IndexType row, const IndexType column ) const { - IndexType localIdx = column - row; - if( row > 0 ) - localIdx++; + IndexType localIdx = column - row + 1; + //if( row > 0 ) + // localIdx++; TNL_ASSERT_GE( localIdx, 0, "" ); TNL_ASSERT_LT( localIdx, 3, "" ); diff --git a/src/TNL/Matrices/TridiagonalMatrixView.h b/src/TNL/Matrices/TridiagonalMatrixView.h index 734907033..d558d8c34 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.h +++ b/src/TNL/Matrices/TridiagonalMatrixView.h @@ -98,9 +98,15 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + template< typename Function > void forRows( IndexType first, IndexType last, Function& function ) const; diff --git a/src/TNL/Matrices/TridiagonalMatrixView.hpp b/src/TNL/Matrices/TridiagonalMatrixView.hpp index a482f69cb..3e758be68 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixView.hpp @@ -66,10 +66,10 @@ String TridiagonalMatrixView< Real, Device, Index, Organization >:: getSerializationType() { - return String( "Matrices::Tridiagonal< " ) + + return String( "Matrices::TridiagonalMatrix< " ) + TNL::getSerializationType< RealType >() + ", [any_device], " + TNL::getSerializationType< IndexType >() + ", " + - ( Organization ? "true" : "false" ) + ", [any_allocator] >"; + TNL::getSerializationType( Organization ) + ", [any_allocator] >"; } template< typename Real, @@ -290,8 +290,52 @@ rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Ke Real_ sum( zero ); if( rowIdx == 0 ) { - reduce( sum, fetch( 0, 0, values_view[ indexer.getGlobalIndex( 0, 0 ) ] ) ); reduce( sum, fetch( 0, 1, values_view[ indexer.getGlobalIndex( 0, 1 ) ] ) ); + reduce( sum, fetch( 0, 2, values_view[ indexer.getGlobalIndex( 0, 2 ) ] ) ); + keep( 0, sum ); + return; + } + if( rowIdx + 1 < indexer.getColumns() ) + { + reduce( sum, fetch( rowIdx, rowIdx - 1, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ] ) ); + reduce( sum, fetch( rowIdx, rowIdx, values_view[ indexer.getGlobalIndex( rowIdx, 1 ) ] ) ); + reduce( sum, fetch( rowIdx, rowIdx + 1, values_view[ indexer.getGlobalIndex( rowIdx, 2 ) ] ) ); + keep( rowIdx, sum ); + return; + } + if( rowIdx < indexer.getColumns() ) + { + reduce( sum, fetch( rowIdx, rowIdx - 1, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ] ) ); + reduce( sum, fetch( rowIdx, rowIdx, values_view[ indexer.getGlobalIndex( rowIdx, 1 ) ] ) ); + keep( rowIdx, sum ); + } + else + { + keep( rowIdx, fetch( rowIdx, rowIdx - 1, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ] ) ); + } + }; + Algorithms::ParallelFor< DeviceType >::exec( first, last, f ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +TridiagonalMatrixView< Real, Device, Index, Organization >:: +rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero_ ) +{ + using Real_ = decltype( fetch( IndexType(), IndexType(), RealType() ) ); + auto values_view = this->values.getConstView(); + const auto indexer = this->indexer; + const auto zero = zero_; + auto f = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { + Real_ sum( zero ); + if( rowIdx == 0 ) + { + reduce( sum, fetch( 0, 1, values_view[ indexer.getGlobalIndex( 0, 1 ) ] ) ); + reduce( sum, fetch( 0, 2, values_view[ indexer.getGlobalIndex( 0, 2 ) ] ) ); keep( 0, sum ); return; } @@ -329,6 +373,18 @@ allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zer this->rowsReduction( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > +void +TridiagonalMatrixView< Real, Device, Index, Organization >:: +allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +{ + this->rowsReduction( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); +} + template< typename Real, typename Device, typename Index, @@ -344,8 +400,8 @@ forRows( IndexType first, IndexType last, Function& function ) const auto f = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { if( rowIdx == 0 ) { - function( 0, 0, 0, values_view[ indexer.getGlobalIndex( 0, 0 ) ], compute ); - function( 0, 1, 1, values_view[ indexer.getGlobalIndex( 0, 1 ) ], compute ); + function( 0, 1, 0, values_view[ indexer.getGlobalIndex( 0, 1 ) ], compute ); + function( 0, 2, 1, values_view[ indexer.getGlobalIndex( 0, 2 ) ], compute ); } else if( rowIdx + 1 < indexer.getColumns() ) { @@ -375,25 +431,26 @@ forRows( IndexType first, IndexType last, Function& function ) { auto values_view = this->values.getView(); const auto indexer = this->indexer; + bool compute( true ); auto f = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { if( rowIdx == 0 ) { - function( 0, 0, 0, values_view[ indexer.getGlobalIndex( 0, 0 ) ] ); - function( 0, 1, 1, values_view[ indexer.getGlobalIndex( 0, 1 ) ] ); + function( 0, 1, 0, values_view[ indexer.getGlobalIndex( 0, 1 ) ], compute ); + function( 0, 2, 1, values_view[ indexer.getGlobalIndex( 0, 2 ) ], compute ); } else if( rowIdx + 1 < indexer.getColumns() ) { - function( rowIdx, 0, rowIdx - 1, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ] ); - function( rowIdx, 1, rowIdx, values_view[ indexer.getGlobalIndex( rowIdx, 1 ) ] ); - function( rowIdx, 2, rowIdx + 1, values_view[ indexer.getGlobalIndex( rowIdx, 2 ) ] ); + function( rowIdx, 0, rowIdx - 1, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ], compute ); + function( rowIdx, 1, rowIdx, values_view[ indexer.getGlobalIndex( rowIdx, 1 ) ], compute ); + function( rowIdx, 2, rowIdx + 1, values_view[ indexer.getGlobalIndex( rowIdx, 2 ) ], compute ); } else if( rowIdx < indexer.getColumns() ) { - function( rowIdx, 0, rowIdx - 1, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ] ); - function( rowIdx, 1, rowIdx, values_view[ indexer.getGlobalIndex( rowIdx, 1 ) ] ); + function( rowIdx, 0, rowIdx - 1, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ], compute ); + function( rowIdx, 1, rowIdx, values_view[ indexer.getGlobalIndex( rowIdx, 1 ) ], compute ); } else - function( rowIdx, 0, rowIdx, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ] ); + function( rowIdx, 0, rowIdx, values_view[ indexer.getGlobalIndex( rowIdx, 0 ) ], compute ); }; Algorithms::ParallelFor< DeviceType >::exec( first, last, f ); } @@ -500,13 +557,13 @@ addMatrix( const TridiagonalMatrixView< Real_, Device_, Index_, Organization_ >& const auto matrix_view = matrix; const auto matrixMult = matrixMultiplicator; const auto thisMult = thisMatrixMultiplicator; - auto add0 = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value ) mutable { + auto add0 = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value, bool& compute ) mutable { value = matrixMult * matrix.getValues()[ matrix.getIndexer().getGlobalIndex( rowIdx, localIdx ) ]; }; - auto add1 = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value ) mutable { + auto add1 = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value, bool& compute ) mutable { value += matrixMult * matrix.getValues()[ matrix.getIndexer().getGlobalIndex( rowIdx, localIdx ) ]; }; - auto addGen = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value ) mutable { + auto addGen = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value, bool& compute ) mutable { value = thisMult * value + matrixMult * matrix.getValues()[ matrix.getIndexer().getGlobalIndex( rowIdx, localIdx ) ]; }; if( thisMult == 0.0 ) @@ -691,9 +748,7 @@ Index TridiagonalMatrixView< Real, Device, Index, Organization >:: getElementIndex( const IndexType row, const IndexType column ) const { - IndexType localIdx = column - row; - if( row > 0 ) - localIdx++; + IndexType localIdx = column - row + 1; TNL_ASSERT_GE( localIdx, 0, "" ); TNL_ASSERT_LT( localIdx, 3, "" ); diff --git a/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h b/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h index f520ec22f..8412d3dad 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h +++ b/src/UnitTests/Matrices/BinarySparseMatrixCopyTest.h @@ -433,8 +433,8 @@ void tridiagonalMatrixAssignment() using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - using TridiagonalHost = TNL::Matrices::Tridiagonal< RealType, TNL::Devices::Host, IndexType >; - using TridiagonalCuda = TNL::Matrices::Tridiagonal< RealType, TNL::Devices::Cuda, IndexType >; + using TridiagonalHost = TNL::Matrices::TridiagonalMatrix< RealType, TNL::Devices::Host, IndexType >; + using TridiagonalCuda = TNL::Matrices::TridiagonalMatrix< RealType, TNL::Devices::Cuda, IndexType >; const IndexType rows( 10 ), columns( 10 ); TridiagonalHost hostMatrix( rows, columns ); diff --git a/src/UnitTests/Matrices/TridiagonalMatrixTest.h b/src/UnitTests/Matrices/TridiagonalMatrixTest.h index 1b88ba823..977e4b251 100644 --- a/src/UnitTests/Matrices/TridiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/TridiagonalMatrixTest.h @@ -177,7 +177,7 @@ void test_GetAllocatedElementsCount() } template< typename Matrix > -void test_GetNumberOfNonzeroMatrixElements() +void test_GetNonzeroElementsCount() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -206,7 +206,7 @@ void test_GetNumberOfNonzeroMatrixElements() m.setElement( 5, 5, 0); - EXPECT_EQ( m.getNumberOfNonzeroMatrixElements(), 15 ); + EXPECT_EQ( m.getNonzeroElementsCount(), 15 ); } template< typename Matrix > @@ -579,14 +579,12 @@ void test_SetRow() auto matrix_view = m.getView(); auto f = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { RealType values[ 3 ][ 3 ] { - { 1, 2, 0 }, + { 0, 1, 2 }, { 8, 9, 10 }, { 16, 17, 18 } }; auto row = matrix_view.getRow( rowIdx ); for( IndexType i = 0; i < 3; i++ ) { - if( rowIdx == 0 && i > 1 ) - break; row.setElement( i, values[ rowIdx ][ i ] ); } }; @@ -700,7 +698,7 @@ void test_AddRow() auto matrix_view = m.getView(); auto f = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { RealType values[ 6 ][ 3 ] { - { 11, 11, 0 }, + { 0, 11, 11 }, { 22, 22, 22 }, { 33, 33, 33 }, { 44, 44, 44 }, @@ -1417,11 +1415,11 @@ TYPED_TEST( MatrixTest, getAllocatedElementsCountTest ) test_GetAllocatedElementsCount< MatrixType >(); } -TYPED_TEST( MatrixTest, getNumberOfNonzeroMatrixElementsTest ) +TYPED_TEST( MatrixTest, getNonzeroElementsCountTest ) { using MatrixType = typename TestFixture::MatrixType; - test_GetNumberOfNonzeroMatrixElements< MatrixType >(); + test_GetNonzeroElementsCount< MatrixType >(); } TYPED_TEST( MatrixTest, resetTest ) -- GitLab From 76d2f76da394b02880e404266cad3ce6db7e6487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 10 Jun 2020 13:56:35 +0200 Subject: [PATCH 36/38] Deleting file genfiles. --- .../Matrices/TridiagonalMatrix/genfiles | 53 ------------------- 1 file changed, 53 deletions(-) delete mode 100755 Documentation/Examples/Matrices/TridiagonalMatrix/genfiles diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/genfiles b/Documentation/Examples/Matrices/TridiagonalMatrix/genfiles deleted file mode 100755 index 0f17c7d02..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/genfiles +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash - -mv MultidiagonalMatrixExample_addElement.cpp TridiagonalMatrixExample_addElement.cpp -mv MultidiagonalMatrixExample_allRowsReduction.cpp TridiagonalMatrixExample_allRowsReduction.cpp -mv MultidiagonalMatrixExample_Constructor.cpp TridiagonalMatrixExample_Constructor.cpp -mv MultidiagonalMatrixExample_Constructor_init_list_1.cpp TridiagonalMatrixExample_Constructor_init_list_1.cpp -mv MultidiagonalMatrixExample_Constructor_init_list_2.cpp TridiagonalMatrixExample_Constructor_init_list_2.cpp -mv MultidiagonalMatrixExample_forAllRows.cpp TridiagonalMatrixExample_forAllRows.cpp -mv MultidiagonalMatrixExample_forRows.cpp TridiagonalMatrixExample_forRows.cpp -mv MultidiagonalMatrixExample_getCompressedRowLengths.cpp TridiagonalMatrixExample_getCompressedRowLengths.cpp -mv MultidiagonalMatrixExample_getConstRow.cpp TridiagonalMatrixExample_getConstRow.cpp -mv MultidiagonalMatrixExample_getElement.cpp TridiagonalMatrixExample_getElement.cpp -mv MultidiagonalMatrixExample_getRow.cpp TridiagonalMatrixExample_getRow.cpp -mv MultidiagonalMatrixExample_getSerializationType.cpp TridiagonalMatrixExample_getSerializationType.cpp -mv MultidiagonalMatrixExample_rowsReduction.cpp TridiagonalMatrixExample_rowsReduction.cpp -mv MultidiagonalMatrixExample_setElement.cpp TridiagonalMatrixExample_setElement.cpp -mv MultidiagonalMatrixExample_setElements.cpp TridiagonalMatrixExample_setElements.cpp -mv MultidiagonalMatrixViewExample_addElement.cpp TridiagonalMatrixViewExample_addElement.cpp -mv MultidiagonalMatrixViewExample_allRowsReduction.cpp TridiagonalMatrixViewExample_allRowsReduction.cpp -mv MultidiagonalMatrixViewExample_forAllRows.cpp TridiagonalMatrixViewExample_forAllRows.cpp -mv MultidiagonalMatrixViewExample_forRows.cpp TridiagonalMatrixViewExample_forRows.cpp -mv MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp TridiagonalMatrixViewExample_getCompressedRowLengths.cpp -mv MultidiagonalMatrixViewExample_getConstRow.cpp TridiagonalMatrixViewExample_getConstRow.cpp -mv MultidiagonalMatrixViewExample_getElement.cpp TridiagonalMatrixViewExample_getElement.cpp -mv MultidiagonalMatrixViewExample_getRow.cpp TridiagonalMatrixViewExample_getRow.cpp -mv MultidiagonalMatrixViewExample_rowsReduction.cpp TridiagonalMatrixViewExample_rowsReduction.cpp -mv MultidiagonalMatrixViewExample_setElement.cpp TridiagonalMatrixViewExample_setElement.cpp - -ln -s TridiagonalMatrixExample_addElement.cpp TridiagonalMatrixExample_addElement.cu -ln -s TridiagonalMatrixExample_allRowsReduction.cpp TridiagonalMatrixExample_allRowsReduction.cu -ln -s TridiagonalMatrixExample_Constructor.cpp TridiagonalMatrixExample_Constructor.cu -ln -s TridiagonalMatrixExample_Constructor_init_list_1.cpp TridiagonalMatrixExample_Constructor_init_list_1.cu -ln -s TridiagonalMatrixExample_Constructor_init_list_2.cpp TridiagonalMatrixExample_Constructor_init_list_2.cu -ln -s TridiagonalMatrixExample_forAllRows.cpp TridiagonalMatrixExample_forAllRows.cu -ln -s TridiagonalMatrixExample_forRows.cpp TridiagonalMatrixExample_forRows.cu -ln -s TridiagonalMatrixExample_getCompressedRowLengths.cpp TridiagonalMatrixExample_getCompressedRowLengths.cu -ln -s TridiagonalMatrixExample_getConstRow.cpp TridiagonalMatrixExample_getConstRow.cu -ln -s TridiagonalMatrixExample_getElement.cpp TridiagonalMatrixExample_getElement.cu -ln -s TridiagonalMatrixExample_getRow.cpp TridiagonalMatrixExample_getRow.cu -ln -s TridiagonalMatrixExample_getSerializationType.cpp TridiagonalMatrixExample_getSerializationType.cu -ln -s TridiagonalMatrixExample_rowsReduction.cpp TridiagonalMatrixExample_rowsReduction.cu -ln -s TridiagonalMatrixExample_setElement.cpp TridiagonalMatrixExample_setElement.cu -ln -s TridiagonalMatrixExample_setElements.cpp TridiagonalMatrixExample_setElements.cu -ln -s TridiagonalMatrixViewExample_addElement.cpp TridiagonalMatrixViewExample_addElement.cu -ln -s TridiagonalMatrixViewExample_allRowsReduction.cpp TridiagonalMatrixViewExample_allRowsReduction.cu -ln -s TridiagonalMatrixViewExample_forAllRows.cpp TridiagonalMatrixViewExample_forAllRows.cu -ln -s TridiagonalMatrixViewExample_forRows.cpp TridiagonalMatrixViewExample_forRows.cu -ln -s TridiagonalMatrixViewExample_getCompressedRowLengths.cpp TridiagonalMatrixViewExample_getCompressedRowLengths.cu -ln -s TridiagonalMatrixViewExample_getConstRow.cpp TridiagonalMatrixViewExample_getConstRow.cu -ln -s TridiagonalMatrixViewExample_getElement.cpp TridiagonalMatrixViewExample_getElement.cu -ln -s TridiagonalMatrixViewExample_getRow.cpp TridiagonalMatrixViewExample_getRow.cu -ln -s TridiagonalMatrixViewExample_rowsReduction.cpp TridiagonalMatrixViewExample_rowsReduction.cu -ln -s TridiagonalMatrixViewExample_setElement.cpp TridiagonalMatrixViewExample_setElement.cu -- GitLab From e3e89082a84a9fba1940047547ccf8526c15b6fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 11 Jun 2020 12:25:54 +0200 Subject: [PATCH 37/38] Writting documentation on tridiagonal matrix view. --- ...ridiagonalMatrixViewExample_addElement.cpp | 11 +- ...onalMatrixViewExample_allRowsReduction.cpp | 32 +- ...ridiagonalMatrixViewExample_forAllRows.cpp | 41 +- .../TridiagonalMatrixViewExample_forRows.cpp | 35 +- ...rixViewExample_getCompressedRowLengths.cpp | 36 +- ...idiagonalMatrixViewExample_getConstRow.cpp | 12 +- ...ridiagonalMatrixViewExample_getElement.cpp | 5 +- .../TridiagonalMatrixViewExample_getRow.cpp | 21 +- ...iagonalMatrixViewExample_rowsReduction.cpp | 28 +- ...ridiagonalMatrixViewExample_setElement.cpp | 13 +- src/TNL/Matrices/MultidiagonalMatrixView.h | 28 +- src/TNL/Matrices/TridiagonalMatrix.h | 4 +- src/TNL/Matrices/TridiagonalMatrix.hpp | 16 +- src/TNL/Matrices/TridiagonalMatrixView.h | 493 +++++++++++++++++- src/TNL/Matrices/TridiagonalMatrixView.hpp | 50 +- .../Matrices/TridiagonalMatrixTest.h | 29 -- 16 files changed, 613 insertions(+), 241 deletions(-) diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cpp index 46f92d7a3..183710215 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cpp @@ -1,16 +1,17 @@ #include -#include +#include #include template< typename Device > void addElements() { const int matrixSize( 5 ); - TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( - matrixSize, // number of rows - matrixSize, // number of columns - { -1, 0, 1 } ); // diagonals offsets + TNL::Matrices::TridiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize // number of columns + ); auto view = matrix.getView(); + for( int i = 0; i < matrixSize; i++ ) view.setElement( i, i, i ); diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp index 26dac464c..bacb98bee 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp @@ -1,32 +1,30 @@ #include #include #include -#include +#include #include template< typename Device > -void allRowsReduction() +void rowsReduction() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): * - * 0 0 / 1 . . . . \ -> { 0, 0, 1 } - * 0 | 2 1 . . . | -> { 0, 2, 1 } - * | 3 2 1 . . | -> { 3, 2, 1 } - * | . 3 2 1 . | -> { 3, 2, 1 } - * \ . . 3 2 1 / -> { 3, 2, 1 } + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } * - * The diagonals offsets are { -2, -1, 0 }. */ - TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + TNL::Matrices::TridiagonalMatrix< double, Device > matrix ( 5, // number of matrix columns - { -2, -1, 0 }, // diagonals offsets - { { 0, 0, 1 }, // matrix elements - { 0, 2, 1 }, - { 3, 2, 1 }, - { 3, 2, 1 }, - { 3, 2, 1 } } ); + { { 0, 1, 3 }, // matrix elements + { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 } } ); auto view = matrix.getView(); /*** @@ -72,10 +70,10 @@ void allRowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - allRowsReduction< TNL::Devices::Host >(); + rowsReduction< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); + rowsReduction< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cpp index 143aa864c..bd889e1af 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cpp @@ -1,27 +1,24 @@ #include -#include +#include #include #include template< typename Device > -void forAllRowsExample() +void forRowsExample() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): * - * 0 0 / 1 . . . . \ -> { 0, 0, 1 } - * 0 | 2 1 . . . | -> { 0, 2, 1 } - * | 3 2 1 . . | -> { 3, 2, 1 } - * | . 3 2 1 . | -> { 3, 2, 1 } - * \ . . 3 2 1 / -> { 3, 2, 1 } - * - * The diagonals offsets are { -2, -1, 0 }. + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } */ - TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( - 5, // number of matrix rows - 5, // number of matrix columns - { -2, -1, 0 } ); // matrix diagonals offsets + TNL::Matrices::TridiagonalMatrix< double, Device > matrix( + 5, // number of matrix rows + 5 ); // number of matrix columns auto view = matrix.getView(); auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) { @@ -30,13 +27,13 @@ void forAllRowsExample() * and so we do not need to check anything. The element value can be expressed * by the 'localIdx' variable, see the following figure: * - * 0 1 2 <- localIdx values - * ------- - * 0 0 / 1 . . . . \ -> { 0, 0, 1 } - * 0 | 2 1 . . . | -> { 0, 2, 1 } - * | 3 2 1 . . | -> { 3, 2, 1 } - * | . 3 2 1 . | -> { 3, 2, 1 } - * \ . . 3 2 1 / -> { 3, 2, 1 } + * 0 1 2 <- localIdx values + * ------- + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } * */ value = 3 - localIdx; @@ -48,10 +45,10 @@ void forAllRowsExample() int main( int argc, char* argv[] ) { std::cout << "Creating matrix on host: " << std::endl; - forAllRowsExample< TNL::Devices::Host >(); + forRowsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Creating matrix on CUDA device: " << std::endl; - forAllRowsExample< TNL::Devices::Cuda >(); + forRowsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp index 23aa06753..24fe78f7f 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include @@ -10,18 +10,15 @@ void forRowsExample() * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): * - * 0 0 / 1 . . . . \ -> { 0, 0, 1 } - * 0 | 2 1 . . . | -> { 0, 2, 1 } - * | 3 2 1 . . | -> { 3, 2, 1 } - * | . 3 2 1 . | -> { 3, 2, 1 } - * \ . . 3 2 1 / -> { 3, 2, 1 } - * - * The diagonals offsets are { -2, -1, 0 }. + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } */ - TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( - 5, // number of matrix rows - 5, // number of matrix columns - { -2, -1, 0 } ); // matrix diagonals offsets + TNL::Matrices::TridiagonalMatrix< double, Device > matrix( + 5, // number of matrix rows + 5 ); // number of matrix columns auto view = matrix.getView(); auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) { @@ -30,13 +27,13 @@ void forRowsExample() * and so we do not need to check anything. The element value can be expressed * by the 'localIdx' variable, see the following figure: * - * 0 1 2 <- localIdx values - * ------- - * 0 0 / 1 . . . . \ -> { 0, 0, 1 } - * 0 | 2 1 . . . | -> { 0, 2, 1 } - * | 3 2 1 . . | -> { 3, 2, 1 } - * | . 3 2 1 . | -> { 3, 2, 1 } - * \ . . 3 2 1 / -> { 3, 2, 1 } + * 0 1 2 <- localIdx values + * ------- + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } * */ value = 3 - localIdx; diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cpp index 2b366ab3d..f20811ea6 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -8,32 +8,22 @@ template< typename Device > void laplaceOperatorMatrix() { - const int gridSize( 4 ); - const int matrixSize = gridSize * gridSize; - TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( - matrixSize, // number of rows - matrixSize, // number of columns - { - gridSize, -1, 0, 1, gridSize } // diagonals offsets + const int gridSize( 6 ); + const int matrixSize = gridSize; + TNL::Matrices::TridiagonalMatrix< double, Device > matrix( + matrixSize, // number of rows + matrixSize // number of columns ); matrix.setElements( { - { 0.0, 0.0, 1.0 }, // set matrix elements corresponding to boundary grid nodes - { 0.0, 0.0, 1.0 }, // and Dirichlet boundary conditions, i.e. 1 on the main diagonal - { 0.0, 0.0, 1.0 }, // which is the third one - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, // set matrix elements corresponding to inner grid nodes, i.e. 4 on the main diagonal - { -1.0, -1.0, 4.0, -1.0, -1.0 }, // (the third one) and -1 to the other sub-diagonals - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, - { -1.0, -1.0, 4.0, -1.0, -1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0 } + { 0.0, 1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { -1.0, 2.0, -1.0 }, + { 0.0, 1.0 } } ); auto view = matrix.getView(); + TNL::Containers::Vector< int, Device > rowLengths; view.getCompressedRowLengths( rowLengths ); std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cpp index 748c95665..8e5f20793 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include @@ -9,16 +9,14 @@ template< typename Device > void getRowExample() { const int matrixSize = 5; - auto diagonalsOffsets = { -2, -1, 0 }; - using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + using MatrixType = TNL::Matrices::TridiagonalMatrix< double, Device >; MatrixType matrix ( matrixSize, // number of matrix columns - diagonalsOffsets, - { { 0.0, 0.0, 1.0 }, // matrix elements + { { 0.0, 2.0, 1.0 }, // matrix elements { 0.0, 2.0, 1.0 }, { 3.0, 2.0, 1.0 }, { 3.0, 2.0, 1.0 }, - { 3.0, 2.0, 1.0 } } ); + { 0.0, 2.0, 1.0 } } ); auto view = matrix.getView(); /*** @@ -32,7 +30,7 @@ void getRowExample() /*** * Compute the matrix trace. */ - int trace = TNL::Algorithms::Reduction< Device >::reduce( matrix.getRows(), std::plus<>{}, fetch, 0 ); + int trace = TNL::Algorithms::Reduction< Device >::reduce( view.getRows(), std::plus<>{}, fetch, 0 ); std::cout << "Matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Matrix trace is: " << trace << "." << std::endl; } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cpp index bf1226662..5be0bd761 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cpp @@ -1,15 +1,14 @@ #include #include -#include +#include #include template< typename Device > void getElements() { const int matrixSize( 5 ); - TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + TNL::Matrices::TridiagonalMatrix< double, Device > matrix ( matrixSize, // number of matrix columns - { -1, 0, 1 }, // matrix diagonals offsets { // matrix elements definition { 0.0, 2.0, -1.0 }, { -1.0, 2.0, -1.0 }, diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp index ac322f9aa..c40fa4885 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -8,18 +8,20 @@ template< typename Device > void getRowExample() { const int matrixSize( 5 ); - auto diagonalsOffsets = { -1, 0, 1 }; // Variadic templates in SharedPointer - // constructor do not recognize initializer - // list so we give it a hint. - using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + using MatrixType = TNL::Matrices::TridiagonalMatrix< double, Device >; MatrixType matrix( matrixSize, // number of matrix rows - matrixSize, // number of matrix columns - diagonalsOffsets ); + matrixSize // number of matrix columns + ); auto view = matrix.getView(); auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { + //auto row = matrix->getRow( rowIdx ); + // For some reason the previous line of code is not accepted by nvcc 10.1 + // so we replace it with the following two lines. + //auto ref = matrix.modifyData(); auto row = view.getRow( rowIdx ); + if( rowIdx > 0 ) row.setElement( 0, -1.0 ); // elements below the diagonal row.setElement( 1, 2.0 ); // elements on the diagonal @@ -30,7 +32,7 @@ void getRowExample() /*** * Set the matrix elements. */ - TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix.getRows(), f ); + TNL::Algorithms::ParallelFor< Device >::exec( 0, view.getRows(), f ); std::cout << std::endl << matrix << std::endl; } @@ -40,6 +42,9 @@ int main( int argc, char* argv[] ) getRowExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA + // It seems that nvcc 10.1 does not handle lambda functions properly. + // It is hard to make nvcc to compile this example and it does not work + // properly. We will try it with later version of CUDA. std::cout << "Getting matrix rows on CUDA device: " << std::endl; getRowExample< TNL::Devices::Cuda >(); #endif diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp index 6793d8bb2..fa345292c 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include template< typename Device > @@ -11,22 +11,20 @@ void rowsReduction() * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): * - * 0 0 / 1 . . . . \ -> { 0, 0, 1 } - * 0 | 2 1 . . . | -> { 0, 2, 1 } - * | 3 2 1 . . | -> { 3, 2, 1 } - * | . 3 2 1 . | -> { 3, 2, 1 } - * \ . . 3 2 1 / -> { 3, 2, 1 } + * 0 / 1 3 . . . \ -> { 0, 1, 3 } + * | 2 1 3 . . | -> { 2, 1, 3 } + * | . 2 1 3 . | -> { 2, 1, 3 } + * | . . 2 1 3 | -> { 2, 1, 3 } + * \ . . . 2 1 / 0 -> { 2, 1, 0 } * - * The diagonals offsets are { -2, -1, 0 }. */ - TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( + TNL::Matrices::TridiagonalMatrix< double, Device > matrix ( 5, // number of matrix columns - { -2, -1, 0 }, // diagonals offsets - { { 0, 0, 1 }, // matrix elements - { 0, 2, 1 }, - { 3, 2, 1 }, - { 3, 2, 1 }, - { 3, 2, 1 } } ); + { { 0, 1, 3 }, // matrix elements + { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 } } ); auto view = matrix.getView(); /*** @@ -63,7 +61,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - view.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + view.rowsReduction( 0, view.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp index 41f936670..90cc05b3f 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp @@ -1,20 +1,16 @@ #include #include -#include +#include #include #include -#include -#include template< typename Device > void setElements() { const int matrixSize( 5 ); - auto diagonalsOffsets = { -1, 0, 1 }; // offsets of tridiagonal matrix - using Matrix = TNL::Matrices::MultidiagonalMatrix< double, Device >; - Matrix matrix( matrixSize, matrixSize, diagonalsOffsets ); + using Matrix = TNL::Matrices::TridiagonalMatrix< double, Device >; + Matrix matrix( matrixSize, matrixSize ); auto view = matrix.getView(); - for( int i = 0; i < 5; i++ ) view.setElement( i, i, i ); @@ -41,6 +37,9 @@ int main( int argc, char* argv[] ) setElements< TNL::Devices::Host >(); #ifdef HAVE_CUDA + // It seems that nvcc 10.1 does not handle lambda functions properly. + // It is hard to make nvcc to compile this example and it does not work + // properly. We will try it with later version of CUDA. std::cout << "Set elements on CUDA device:" << std::endl; setElements< TNL::Devices::Cuda >(); #endif diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.h b/src/TNL/Matrices/MultidiagonalMatrixView.h index 38eff8218..231957025 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixView.h @@ -20,7 +20,7 @@ namespace TNL { namespace Matrices { /** - * \brief Implementation of sparse multi-diagonal matrix. + * \brief Implementation of sparse multidiagonal matrix. * * It serves as an accessor to \ref SparseMatrix for example when passing the * matrix to lambda functions. SparseMatrix view can be also created in CUDA kernels. @@ -182,6 +182,14 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > [[deprecated]] IndexType getRowLength( const IndexType row ) const; + /** + * \brief Returns number of non-zero matrix elements. + * + * This method really counts the non-zero matrix elements and so + * it returns zero for matrix having all allocated elements set to zero. + * + * \return number of non-zero matrix elements. + */ IndexType getNonzeroElementsCount() const; /** @@ -191,12 +199,13 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \tparam Device_ is \e Device type of the source matrix. * \tparam Index_ is \e Index type of the source matrix. * \tparam Organization_ is \e Organization of the source matrix. - * \tparam RealAllocator_ is \e RealAllocator of the source matrix. - * \tparam IndexAllocator_ is \e IndexAllocator of the source matrix. * * \return \e true if both matrices are identical and \e false otherwise. */ - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_ > + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_ > bool operator == ( const MultidiagonalMatrixView< Real_, Device_, Index_, Organization_ >& matrix ) const; /** @@ -206,14 +215,15 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \tparam Device_ is \e Device type of the source matrix. * \tparam Index_ is \e Index type of the source matrix. * \tparam Organization_ is \e Organization of the source matrix. - * \tparam RealAllocator_ is \e RealAllocator of the source matrix. - * \tparam IndexAllocator_ is \e IndexAllocator of the source matrix. * * \param matrix is the source matrix. * * \return \e true if both matrices are NOT identical and \e false otherwise. */ - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_ > + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_ > bool operator != ( const MultidiagonalMatrixView< Real_, Device_, Index_, Organization_ >& matrix ) const; /** @@ -620,10 +630,6 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > protected: - //__cuda_callable__ - //IndexType getElementIndex( const IndexType row, - // const IndexType localIdx ) const; - DiagonalsOffsetsView diagonalsOffsets; HostDiagonalsOffsetsView hostDiagonalsOffsets; diff --git a/src/TNL/Matrices/TridiagonalMatrix.h b/src/TNL/Matrices/TridiagonalMatrix.h index 436bf6272..afd139238 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.h +++ b/src/TNL/Matrices/TridiagonalMatrix.h @@ -274,8 +274,8 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename Vector > void getCompressedRowLengths( Vector& rowLengths ) const; - [[deprecated]] - IndexType getRowLength( const IndexType row ) const; + //[[deprecated]] + //IndexType getRowLength( const IndexType row ) const; //IndexType getMaxRowLength() const; diff --git a/src/TNL/Matrices/TridiagonalMatrix.hpp b/src/TNL/Matrices/TridiagonalMatrix.hpp index 78fffdeab..1d7d5e8ef 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.hpp +++ b/src/TNL/Matrices/TridiagonalMatrix.hpp @@ -182,18 +182,6 @@ getCompressedRowLengths( Vector& rowLengths ) const return this->view.getCompressedRowLengths( rowLengths ); } -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization, - typename RealAllocator > -Index -TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -getRowLength( const IndexType row ) const -{ - return this->view.getRowLength( row ); -} - template< typename Real, typename Device, typename Index, @@ -216,7 +204,7 @@ Index TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getNonzeroElementsCount() const { - return this->view.getNumberOfNonzeroMatrixElements(); + return this->view.getNonzeroElementsCount(); } template< typename Real, @@ -720,8 +708,6 @@ TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: getElementIndex( const IndexType row, const IndexType column ) const { IndexType localIdx = column - row + 1; - //if( row > 0 ) - // localIdx++; TNL_ASSERT_GE( localIdx, 0, "" ); TNL_ASSERT_LT( localIdx, 3, "" ); diff --git a/src/TNL/Matrices/TridiagonalMatrixView.h b/src/TNL/Matrices/TridiagonalMatrixView.h index d558d8c34..0f7f37a4a 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.h +++ b/src/TNL/Matrices/TridiagonalMatrixView.h @@ -19,6 +19,20 @@ namespace TNL { namespace Matrices { +/** + * \brief Implementation of sparse tridiagonal matrix. + * + * It serves as an accessor to \ref SparseMatrix for example when passing the + * matrix to lambda functions. SparseMatrix view can be also created in CUDA kernels. + * + * See \ref TridiagonalMatrix for more details. + * + * \tparam Real is a type of matrix elements. + * \tparam Device is a device where the matrix is allocated. + * \tparam Index is a type for indexing of the matrix elements. + * \tparam Organization tells the ordering of matrix elements. It is either RowMajorOrder + * or ColumnMajorOrder. + */ template< typename Real = double, typename Device = Devices::Host, typename Index = int, @@ -26,104 +40,499 @@ template< typename Real = double, class TridiagonalMatrixView : public MatrixView< Real, Device, Index > { public: + + + // Supporting types - they are not important for the user + using BaseType = MatrixView< Real, Device, Index >; + using ValuesViewType = typename BaseType::ValuesView; + using IndexerType = details::TridiagonalMatrixIndexer< Index, Organization >; + + /** + * \brief The type of matrix elements. + */ using RealType = Real; + + /** + * \brief The device where the matrix is allocated. + */ using DeviceType = Device; + + /** + * \brief The type used for matrix elements indexing. + */ using IndexType = Index; - using BaseType = MatrixView< Real, Device, Index >; - using IndexerType = details::TridiagonalMatrixIndexer< IndexType, Organization >; - using ValuesViewType = typename BaseType::ValuesView; + + /** + * \brief Type of related matrix view. + */ using ViewType = TridiagonalMatrixView< Real, Device, Index, Organization >; + + /** + * \brief Matrix view type for constant instances. + */ using ConstViewType = TridiagonalMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; - using RowView = TridiagonalMatrixRowView< ValuesViewType, IndexerType >; - // TODO: remove this - it is here only for compatibility with original matrix implementation - typedef Containers::Vector< IndexType, DeviceType, IndexType > CompressedRowLengthsVector; - typedef Containers::VectorView< IndexType, DeviceType, IndexType > CompressedRowLengthsVectorView; - typedef typename CompressedRowLengthsVectorView::ConstViewType ConstCompressedRowLengthsVectorView; + /** + * \brief Type for accessing matrix rows. + */ + using RowView = TridiagonalMatrixRowView< ValuesViewType, IndexerType >; + /** + * \brief Helper type for getting self type or its modifications. + */ template< typename _Real = Real, typename _Device = Device, typename _Index = Index, ElementsOrganization Organization_ = Containers::Segments::DefaultElementsOrganization< Device >::getOrganization() > using Self = TridiagonalMatrixView< _Real, _Device, _Index, Organization_ >; + /** + * \brief Constructor with no parameters. + */ + __cuda_callable__ TridiagonalMatrixView(); + /** + * \brief Constructor with all necessary data and views. + * + * \param values is a vector view with matrix elements values + * \param indexer is an indexer of matrix elements + */ + __cuda_callable__ TridiagonalMatrixView( const ValuesViewType& values, const IndexerType& indexer ); + /** + * \brief Copy constructor. + * + * \param matrix is an input tridiagonal matrix view. + */ + __cuda_callable__ + TridiagonalMatrixView( const TridiagonalMatrixView& view ) = default; + + /** + * \brief Move constructor. + * + * \param matrix is an input tridiagonal matrix view. + */ + __cuda_callable__ + TridiagonalMatrixView( TridiagonalMatrixView&& view ) = default; + + /** + * \brief Returns a modifiable view of the tridiagonal matrix. + * + * \return tridiagonal matrix view. + */ ViewType getView(); + /** + * \brief Returns a non-modifiable view of the tridiagonal matrix. + * + * \return tridiagonal matrix view. + */ ConstViewType getConstView() const; + /** + * \brief Returns string with serialization type. + * + * The string has a form `Matrices::TridiagonalMatrix< RealType, [any_device], IndexType, Organization, [any_allocator] >`. + * + * See \ref TridiagonalMatrix::getSerializationType. + * + * \return \ref String with the serialization type. + */ static String getSerializationType(); + /** + * \brief Returns string with serialization type. + * + * See \ref TridiagonalMatrix::getSerializationType. + * + * \return \ref String with the serialization type. + */ virtual String getSerializationTypeVirtual() const; + /** + * \brief Computes number of non-zeros in each row. + * + * \param rowLengths is a vector into which the number of non-zeros in each row + * will be stored. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getCompressedRowLengths.cpp + * \par Output + * \include TridiagonalMatrixViewExample_getCompressedRowLengths.out + */ template< typename Vector > void getCompressedRowLengths( Vector& rowLengths ) const; - [[deprecated]] - IndexType getRowLength( const IndexType row ) const; - - IndexType getMaxRowLength() const; - - IndexType getNumberOfNonzeroMatrixElements() const; - - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_ > + //[[deprecated]] + //IndexType getRowLength( const IndexType row ) const; + + //IndexType getMaxRowLength() const; + + /** + * \brief Returns number of non-zero matrix elements. + * + * This method really counts the non-zero matrix elements and so + * it returns zero for matrix having all allocated elements set to zero. + * + * \return number of non-zero matrix elements. + */ + IndexType getNonzeroElementsCount() const; + + /** + * \brief Comparison operator with another tridiagonal matrix. + * + * \tparam Real_ is \e Real type of the source matrix. + * \tparam Device_ is \e Device type of the source matrix. + * \tparam Index_ is \e Index type of the source matrix. + * \tparam Organization_ is \e Organization of the source matrix. + * + * \return \e true if both matrices are identical and \e false otherwise. + */ + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_ > bool operator == ( const TridiagonalMatrixView< Real_, Device_, Index_, Organization_ >& matrix ) const; - template< typename Real_, typename Device_, typename Index_, ElementsOrganization Organization_ > + /** + * \brief Comparison operator with another multidiagonal matrix. + * + * \tparam Real_ is \e Real type of the source matrix. + * \tparam Device_ is \e Device type of the source matrix. + * \tparam Index_ is \e Index type of the source matrix. + * \tparam Organization_ is \e Organization of the source matrix. + * + * \param matrix is the source matrix. + * + * \return \e true if both matrices are NOT identical and \e false otherwise. + */ + template< typename Real_, + typename Device_, + typename Index_, + ElementsOrganization Organization_ > bool operator != ( const TridiagonalMatrixView< Real_, Device_, Index_, Organization_ >& matrix ) 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/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp + * \par Output + * \include TridiagonalMatrixViewExample_getRow.out + * + * See \ref TridiagonalMatrixRowView. + */ __cuda_callable__ RowView getRow( const IndexType& rowIdx ); + /** + * \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/TridiagonalMatrix/TridiagonalMatrixViewExample_getConstRow.cpp + * \par Output + * \include TridiagonalMatrixViewExample_getConstRow.out + * + * See \ref TridiagonalMatrixRowView. + */ __cuda_callable__ const RowView getRow( const IndexType& rowIdx ) const; + /** + * \brief Set all matrix elements to given value. + * + * \param value is the new value of all matrix elements. + */ void setValue( const RealType& v ); + /** + * \brief Sets element at given \e row and \e column to given \e 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. \ref TridiagonalMatrix::getRow + * or \ref TridiagonalMatrix::forRows and \ref TridiagonalMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp + * \par Output + * \include TridiagonalMatrixViewExample_setElement.out + */ void setElement( const IndexType row, const IndexType column, const RealType& value ); + /** + * \brief Add element at given \e row and \e column to given \e 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. \ref TridiagonalMatrix::getRow + * or \ref TridiagonalMatrix::forRows and \ref TridiagonalMatrix::forAllRows. + * The call may fail if the matrix row capacity is exhausted. + * + * \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. + * \param thisElementMultiplicator is multiplicator the original matrix element + * value is multiplied by before addition of given \e value. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_addElement.cpp + * \par Output + * \include TridiagonalMatrixViewExample_addElement.out + * + */ void addElement( const IndexType row, const IndexType column, const RealType& value, const RealType& thisElementMultiplicator = 1.0 ); + /** + * \brief 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. \ref TridiagonalMatrix::getRow + * or \ref TridiagonalMatrix::forRows and \ref TridiagonalMatrix::forAllRows. + * + * \param row is a row index of the matrix element. + * \param column i a column index of the matrix element. + * + * \return value of given matrix element. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getElement.cpp + * \par Output + * \include TridiagonalMatrixViewExample_getElement.out + * + */ RealType getElement( const IndexType row, const IndexType column ) const; + /** + * \brief Method for performing general reduction on matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp + * \par Output + * \include TridiagonalMatrixViewExample_rowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for performing general reduction on matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp + * \par Output + * \include TridiagonalMatrixViewExample_rowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + /** + * \brief Method for performing general reduction on all matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp + * \par Output + * \include TridiagonalMatrixViewExample_allRowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** + * \brief Method for performing general reduction on all matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType& columnIdx, RealType& elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp + * \par Output + * \include TridiagonalMatrixViewExample_allRowsReduction.out + */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + /** + * \brief Method for iteration over all matrix rows for constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixViewExample_forRows.out + */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ) const; + /** + * \brief Method for iteration over all matrix rows for non-constant instances. + * + * \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 + * be interrupted. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixViewExample_forRows.out + */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ); + /** + * \brief This method calls \e forRows for all matrix rows (for constant instances). + * + * See \ref TridiagonalMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cpp + * \par Output + * \include TridiagonalMatrixViewExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ) const; + /** + * \brief This method calls \e forRows for all matrix rows. + * + * See \ref TridiagonalMatrix::forRows. + * + * \tparam Function is a type of lambda function that will operate on matrix elements. + * \param function is an instance of the lambda function to be called in each row. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllRows.cpp + * \par Output + * \include TridiagonalMatrixViewExample_forAllRows.out + */ template< typename Function > void forAllRows( Function& function ); - template< typename Vector > - __cuda_callable__ - typename Vector::RealType rowVectorProduct( const IndexType row, - const Vector& vector ) const; - + /** + * \brief Computes product of matrix and vector. + * + * More precisely, it computes: + * + * `outVector = matrixMultiplicator * ( * this ) * inVector + outVectorMultiplicator * outVector` + * + * \tparam InVector is type of input vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * \tparam OutVector is type of output vector. It can be \ref Vector, + * \ref VectorView, \ref Array, \ref ArraView or similar container. + * + * \param inVector is input vector. + * \param outVector is output vector. + * \param matrixMultiplicator is a factor by which the matrix is multiplied. It is one by default. + * \param outVectorMultiplicator is a factor by which the outVector is multiplied before added + * to the result of matrix-vector product. It is zero by default. + * \param begin is the beginning of the rows range for which the vector product + * is computed. It is zero by default. + * \param end is the end of the rows range for which the vector product + * is computed. It is number if the matrix rows by default. + */ template< typename InVector, typename OutVector > void vectorProduct( const InVector& inVector, @@ -149,18 +558,58 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > Vector2& x, const RealType& omega = 1.0 ) const; + /** + * \brief Assignment of exactly the same matrix type. + * + * \param matrix is input matrix for the assignment. + * \return reference to this matrix. + */ + TridiagonalMatrixView& operator=( const TridiagonalMatrixView& view ); + + /** + * \brief Method for saving the matrix to a file. + * + * \param file is the output file. + */ void save( File& file ) const; + /** + * \brief Method for saving the matrix to the file with given filename. + * + * \param fileName is name of the file. + */ void save( const String& fileName ) const; + /** + * \brief Method for printing the matrix to output stream. + * + * \param str is the output stream. + */ void print( std::ostream& str ) const; + /** + * \brief This method returns matrix elements indexer used by this matrix. + * + * \return constant reference to the indexer. + */ __cuda_callable__ const IndexerType& getIndexer() const; + /** + * \brief This method returns matrix elements indexer used by this matrix. + * + * \return non-constant reference to the indexer. + */ __cuda_callable__ IndexerType& getIndexer(); + /** + * \brief Returns padding index denoting padding zero elements. + * + * These elements are used for efficient data alignment in memory. + * + * \return value of the padding index. + */ __cuda_callable__ IndexType getPaddingIndex() const; diff --git a/src/TNL/Matrices/TridiagonalMatrixView.hpp b/src/TNL/Matrices/TridiagonalMatrixView.hpp index 3e758be68..f24b8dd2f 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixView.hpp @@ -107,36 +107,13 @@ getCompressedRowLengths( Vector& rowLengths ) const this->allRowsReduction( fetch, reduce, keep, 0 ); } - -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization > -Index -TridiagonalMatrixView< Real, Device, Index, Organization >:: -getRowLength( const IndexType row ) const -{ - return this->indexer.getRowSize( row ); -} - -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization > -Index -TridiagonalMatrixView< Real, Device, Index, Organization >:: -getMaxRowLength() const -{ - return 3; -} - template< typename Real, typename Device, typename Index, ElementsOrganization Organization > Index TridiagonalMatrixView< Real, Device, Index, Organization >:: -getNumberOfNonzeroMatrixElements() const +getNonzeroElementsCount() const { const auto values_view = this->values.getConstView(); auto fetch = [=] __cuda_callable__ ( const IndexType i ) -> IndexType { @@ -479,18 +456,6 @@ forAllRows( Function& function ) this->forRows( 0, this->indexer.getNonemptyRowsCount(), function ); } -template< typename Real, - typename Device, - typename Index, - ElementsOrganization Organization > -template< typename Vector > -__cuda_callable__ -typename Vector::RealType -TridiagonalMatrixView< Real, Device, Index, Organization >:: -rowVectorProduct( const IndexType row, const Vector& vector ) const -{ -} - template< typename Real, typename Device, typename Index, @@ -531,6 +496,19 @@ vectorProduct( const InVector& inVector, this->rowsReduction( begin, end, fetch, reduction, keeper2, ( RealType ) 0.0 ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > +TridiagonalMatrixView< Real, Device, Index, Organization >& +TridiagonalMatrixView< Real, Device, Index, Organization >:: +operator=( const TridiagonalMatrixView& view ) +{ + MatrixView< Real, Device, Index >::operator=( view ); + this->indexer = view.indexer; + return *this; +} + template< typename Real, typename Device, typename Index, diff --git a/src/UnitTests/Matrices/TridiagonalMatrixTest.h b/src/UnitTests/Matrices/TridiagonalMatrixTest.h index 977e4b251..4c68db45a 100644 --- a/src/UnitTests/Matrices/TridiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/TridiagonalMatrixTest.h @@ -139,28 +139,6 @@ void test_GetCompressedRowLengths() EXPECT_EQ( rowLengths, correctRowLengths ); } -template< typename Matrix > -void test_GetRowLength() -{ - using RealType = typename Matrix::RealType; - using DeviceType = typename Matrix::DeviceType; - using IndexType = typename Matrix::IndexType; - - const IndexType rows = 8; - const IndexType cols = 7; - - Matrix m( rows, cols ); - - EXPECT_EQ( m.getRowLength( 0 ), 2 ); - EXPECT_EQ( m.getRowLength( 1 ), 3 ); - EXPECT_EQ( m.getRowLength( 2 ), 3 ); - EXPECT_EQ( m.getRowLength( 3 ), 3 ); - EXPECT_EQ( m.getRowLength( 4 ), 3 ); - EXPECT_EQ( m.getRowLength( 5 ), 3 ); - EXPECT_EQ( m.getRowLength( 6 ), 2 ); - EXPECT_EQ( m.getRowLength( 7 ), 1 ); -} - template< typename Matrix > void test_GetAllocatedElementsCount() { @@ -1401,13 +1379,6 @@ TYPED_TEST( MatrixTest, getCompressedRowLengthTest ) test_GetCompressedRowLengths< MatrixType >(); } -TYPED_TEST( MatrixTest, getRowLengthTest ) -{ - using MatrixType = typename TestFixture::MatrixType; - - test_GetRowLength< MatrixType >(); -} - TYPED_TEST( MatrixTest, getAllocatedElementsCountTest ) { using MatrixType = typename TestFixture::MatrixType; -- GitLab From 57761d0054dc4e4b56de4e799774c4f0f4813124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 11 Jun 2020 14:13:03 +0200 Subject: [PATCH 38/38] Fixes of tridiagonal matrix examples for CUDA. --- .../TridiagonalMatrixExample_setElement.cpp | 3 --- .../TridiagonalMatrixViewExample_getRow.cpp | 7 ------- .../TridiagonalMatrixViewExample_setElement.cpp | 3 --- src/TNL/Matrices/TridiagonalMatrix.hpp | 4 ++-- src/TNL/Matrices/TridiagonalMatrixView.h | 3 +++ src/TNL/Matrices/TridiagonalMatrixView.hpp | 14 +++++++++++--- 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp index 751aca3a5..03121d41b 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp @@ -44,9 +44,6 @@ int main( int argc, char* argv[] ) setElements< TNL::Devices::Host >(); #ifdef HAVE_CUDA - // It seems that nvcc 10.1 does not handle lambda functions properly. - // It is hard to make nvcc to compile this example and it does not work - // properly. We will try it with later version of CUDA. std::cout << "Set elements on CUDA device:" << std::endl; setElements< TNL::Devices::Cuda >(); #endif diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp index c40fa4885..641149e05 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp @@ -16,10 +16,6 @@ void getRowExample() auto view = matrix.getView(); auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { - //auto row = matrix->getRow( rowIdx ); - // For some reason the previous line of code is not accepted by nvcc 10.1 - // so we replace it with the following two lines. - //auto ref = matrix.modifyData(); auto row = view.getRow( rowIdx ); if( rowIdx > 0 ) @@ -42,9 +38,6 @@ int main( int argc, char* argv[] ) getRowExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA - // It seems that nvcc 10.1 does not handle lambda functions properly. - // It is hard to make nvcc to compile this example and it does not work - // properly. We will try it with later version of CUDA. std::cout << "Getting matrix rows on CUDA device: " << std::endl; getRowExample< TNL::Devices::Cuda >(); #endif diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp index 90cc05b3f..a42490414 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp @@ -37,9 +37,6 @@ int main( int argc, char* argv[] ) setElements< TNL::Devices::Host >(); #ifdef HAVE_CUDA - // It seems that nvcc 10.1 does not handle lambda functions properly. - // It is hard to make nvcc to compile this example and it does not work - // properly. We will try it with later version of CUDA. std::cout << "Set elements on CUDA device:" << std::endl; setElements< TNL::Devices::Cuda >(); #endif diff --git a/src/TNL/Matrices/TridiagonalMatrix.hpp b/src/TNL/Matrices/TridiagonalMatrix.hpp index 1d7d5e8ef..1d522e40d 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.hpp +++ b/src/TNL/Matrices/TridiagonalMatrix.hpp @@ -600,7 +600,7 @@ operator=( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealA if( std::is_same< Device, Device_ >::value ) { const auto matrix_view = matrix.getView(); - auto f = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value ) mutable { + auto f = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value, bool& compute ) mutable { value = matrix_view.getValues()[ matrix_view.getIndexer().getGlobalIndex( rowIdx, localIdx ) ]; }; this->forAllRows( f ); @@ -610,7 +610,7 @@ operator=( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealA TridiagonalMatrix< Real, Device, Index, Organization_ > auxMatrix; auxMatrix = matrix; const auto matrix_view = auxMatrix.getView(); - auto f = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value ) mutable { + auto f = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value, bool& compute ) mutable { value = matrix_view.getValues()[ matrix_view.getIndexer().getGlobalIndex( rowIdx, localIdx ) ]; }; this->forAllRows( f ); diff --git a/src/TNL/Matrices/TridiagonalMatrixView.h b/src/TNL/Matrices/TridiagonalMatrixView.h index 0f7f37a4a..321b0fab7 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.h +++ b/src/TNL/Matrices/TridiagonalMatrixView.h @@ -275,6 +275,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \par Output * \include TridiagonalMatrixViewExample_setElement.out */ + __cuda_callable__ void setElement( const IndexType row, const IndexType column, const RealType& value ); @@ -302,6 +303,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \include TridiagonalMatrixViewExample_addElement.out * */ + __cuda_callable__ void addElement( const IndexType row, const IndexType column, const RealType& value, @@ -328,6 +330,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \include TridiagonalMatrixViewExample_getElement.out * */ + __cuda_callable__ RealType getElement( const IndexType row, const IndexType column ) const; diff --git a/src/TNL/Matrices/TridiagonalMatrixView.hpp b/src/TNL/Matrices/TridiagonalMatrixView.hpp index f24b8dd2f..d63e6dd50 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixView.hpp @@ -190,7 +190,7 @@ template< typename Real, typename Device, typename Index, ElementsOrganization Organization > -void +__cuda_callable__ void TridiagonalMatrixView< Real, Device, Index, Organization >:: setElement( const IndexType row, const IndexType column, const RealType& value ) { @@ -200,9 +200,13 @@ setElement( const IndexType row, const IndexType column, const RealType& value ) TNL_ASSERT_LT( column, this->getColumns(), "" ); if( abs( row - column ) > 1 ) { +#ifdef __CUDA_ARCH__ + TNL_ASSERT_TRUE( false, "Wrong matrix element coordinates tridiagonal matrix." ); +#else std::stringstream msg; msg << "Wrong matrix element coordinates ( " << row << ", " << column << " ) in tridiagonal matrix."; throw std::logic_error( msg.str() ); +#endif } this->values.setElement( this->getElementIndex( row, column ), value ); } @@ -211,7 +215,7 @@ template< typename Real, typename Device, typename Index, ElementsOrganization Organization > -void +__cuda_callable__ void TridiagonalMatrixView< Real, Device, Index, Organization >:: addElement( const IndexType row, const IndexType column, @@ -224,9 +228,13 @@ addElement( const IndexType row, TNL_ASSERT_LT( column, this->getColumns(), "" ); if( abs( row - column ) > 1 ) { +#ifdef __CUDA_ARCH__ + TNL_ASSERT_TRUE( false, "Wrong matrix element coordinates tridiagonal matrix." ); +#else std::stringstream msg; msg << "Wrong matrix element coordinates ( " << row << ", " << column << " ) in tridiagonal matrix."; throw std::logic_error( msg.str() ); +#endif } const Index i = this->getElementIndex( row, column ); this->values.setElement( i, thisElementMultiplicator * this->values.getElement( i ) + value ); @@ -236,7 +244,7 @@ template< typename Real, typename Device, typename Index, ElementsOrganization Organization > -Real +__cuda_callable__ Real TridiagonalMatrixView< Real, Device, Index, Organization >:: getElement( const IndexType row, const IndexType column ) const { -- GitLab