diff --git a/Documentation/Examples/Solvers/Linear/CMakeLists.txt b/Documentation/Examples/Solvers/Linear/CMakeLists.txt index 6cc75f08eb651ed2f57ac7982563757ac44e308c..a07ba232a43e240f18caf81cc95a1ace6d70b02a 100644 --- a/Documentation/Examples/Solvers/Linear/CMakeLists.txt +++ b/Documentation/Examples/Solvers/Linear/CMakeLists.txt @@ -1,6 +1,7 @@ set( COMMON_EXAMPLES IterativeLinearSolverExample IterativeLinearSolverWithMonitorExample + IterativeLinearSolverWithTimerExample ) if( BUILD_CUDA ) diff --git a/Documentation/Examples/Solvers/Linear/IterativeLinearSolverWithTimerExample.cpp b/Documentation/Examples/Solvers/Linear/IterativeLinearSolverWithTimerExample.cpp new file mode 100644 index 0000000000000000000000000000000000000000..278ccbad0e85fcf103a2d4b24950ded7aa0358df --- /dev/null +++ b/Documentation/Examples/Solvers/Linear/IterativeLinearSolverWithTimerExample.cpp @@ -0,0 +1,101 @@ +#include <iostream> +#include <memory> +#include <chrono> +#include <thread> +#include <TNL/Timer.h> +#include <TNL/Algorithms/ParallelFor.h> +#include <TNL/Matrices/SparseMatrix.h> +#include <TNL/Devices/Sequential.h> +#include <TNL/Devices/Cuda.h> +#include <TNL/Solvers/Linear/Jacobi.h> + +template< typename Device > +void iterativeLinearSolverExample() +{ + /*** + * Set the following matrix (dots represent zero matrix elements): + * + * / 2.5 -1 . . . \ + * | -1 2.5 -1 . . | + * | . -1 2.5 -1. . | + * | . . -1 2.5 -1 | + * \ . . . -1 2.5 / + */ + using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; + using Vector = TNL::Containers::Vector< double, Device >; + const int size( 5 ); + auto matrix_ptr = std::make_shared< MatrixType >(); + matrix_ptr->setDimensions( size, size ); + matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) ); + + auto f = [=] __cuda_callable__ ( typename MatrixType::RowView& row ) mutable { + const int rowIdx = row.getRowIndex(); + if( rowIdx == 0 ) + { + row.setElement( 0, rowIdx, 2.5 ); // diagonal element + row.setElement( 1, rowIdx+1, -1 ); // element above the diagonal + } + else if( rowIdx == size - 1 ) + { + row.setElement( 0, rowIdx-1, -1.0 ); // element below the diagonal + row.setElement( 1, rowIdx, 2.5 ); // diagonal element + } + else + { + row.setElement( 0, rowIdx-1, -1.0 ); // element below the diagonal + row.setElement( 1, rowIdx, 2.5 ); // diagonal element + row.setElement( 2, rowIdx+1, -1.0 ); // element above the diagonal + } + }; + + /*** + * Set the matrix elements. + */ + matrix_ptr->forAllRows( f ); + std::cout << *matrix_ptr << std::endl; + + /*** + * Set the right-hand side vector + */ + Vector x( size, 1.0 ); + Vector b( size ); + matrix_ptr->vectorProduct( x, b ); + x = 0.0; + std::cout << "Vector b = " << b << std::endl; + + /*** + * Setup solver of the linear system + */ + using LinearSolver = TNL::Solvers::Linear::Jacobi< MatrixType >; + LinearSolver solver; + solver.setMatrix( matrix_ptr ); + solver.setOmega( 0.0005 ); + + /*** + * Setup monitor of the iterative solver + */ + using IterativeSolverMonitorType = TNL::Solvers::IterativeSolverMonitor< double, int >; + IterativeSolverMonitorType monitor; + TNL::Solvers::SolverMonitorThread mmonitorThread(monitor); + monitor.setRefreshRate(10); // refresh rate in milliseconds + monitor.setVerbose(1); + monitor.setStage( "Jacobi stage:" ); + TNL::Timer timer; + monitor.setTimer( timer ); + timer.start(); + solver.setSolverMonitor(monitor); + solver.solve( b, x ); + monitor.stopMainLoop(); + std::cout << "Vector x = " << x << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Solving linear system on host: " << std::endl; + iterativeLinearSolverExample< TNL::Devices::Sequential >(); + +#ifdef HAVE_CUDA + std::cout << "Solving linear system on CUDA device: " << std::endl; + iterativeLinearSolverExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Solvers/Linear/IterativeLinearSolverWithTimerExample.cu b/Documentation/Examples/Solvers/Linear/IterativeLinearSolverWithTimerExample.cu new file mode 120000 index 0000000000000000000000000000000000000000..4c497154d238d93a1ca7ff5c151bb51472e378e4 --- /dev/null +++ b/Documentation/Examples/Solvers/Linear/IterativeLinearSolverWithTimerExample.cu @@ -0,0 +1 @@ +IterativeLinearSolverWithTimerExample.cpp \ No newline at end of file