Loading Documentation/Examples/CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ ADD_SUBDIRECTORY( Algorithms ) ADD_SUBDIRECTORY( Containers ) ADD_SUBDIRECTORY( Pointers ) ADD_SUBDIRECTORY( Matrices ) ADD_SUBDIRECTORY( Solvers ) set( COMMON_EXAMPLES ) Loading Documentation/Examples/Solvers/CMakeLists.txt 0 → 100644 +1 −0 Original line number Diff line number Diff line add_subdirectory( Linear ) Documentation/Examples/Solvers/Linear/CMakeLists.txt 0 → 100644 +28 −0 Original line number Diff line number Diff line set( COMMON_EXAMPLES IterativeLinearSolverExample IterativeLinearSolverWithMonitorExample IterativeLinearSolverWithTimerExample IterativeLinearSolverWithPreconditionerExample IterativeLinearSolverWithRuntimeTypesExample ) if( BUILD_CUDA ) foreach( target IN ITEMS ${COMMON_EXAMPLES} ) cuda_add_executable( ${target}-cuda ${target}.cu OPTIONS ) add_custom_command( COMMAND ${target}-cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) endforeach() else() foreach( target IN ITEMS ${COMMON_EXAMPLES} ) add_executable( ${target} ${target}.cpp ) add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) set( HOST_OUTPUTS ${HOST_OUTPUTS} ${target}.out ) endforeach() endif() IF( BUILD_CUDA ) ADD_CUSTOM_TARGET( RunLinearSolversExamples-cuda ALL DEPENDS ${CUDA_OUTPUTS} ) ELSE() ADD_CUSTOM_TARGET( RunLinearSolversExamples ALL DEPENDS ${HOST_OUTPUTS} ) ENDIF() Documentation/Examples/Solvers/Linear/IterativeLinearSolverExample.cpp 0 → 100644 +82 −0 Original line number Diff line number Diff line #include <iostream> #include <memory> #include <TNL/Algorithms/ParallelFor.h> #include <TNL/Matrices/SparseMatrix.h> #include <TNL/Devices/Host.h> #include <TNL/Devices/Cuda.h> #include <TNL/Solvers/Linear/TFQMR.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; /*** * Solve the linear system. */ using LinearSolver = TNL::Solvers::Linear::TFQMR< MatrixType >; LinearSolver solver; solver.setMatrix( matrix_ptr ); solver.solve( b, x ); 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 } Documentation/Examples/Solvers/Linear/IterativeLinearSolverExample.cu 0 → 120000 +1 −0 Original line number Diff line number Diff line IterativeLinearSolverExample.cpp No newline at end of file Loading
Documentation/Examples/CMakeLists.txt +1 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ ADD_SUBDIRECTORY( Algorithms ) ADD_SUBDIRECTORY( Containers ) ADD_SUBDIRECTORY( Pointers ) ADD_SUBDIRECTORY( Matrices ) ADD_SUBDIRECTORY( Solvers ) set( COMMON_EXAMPLES ) Loading
Documentation/Examples/Solvers/CMakeLists.txt 0 → 100644 +1 −0 Original line number Diff line number Diff line add_subdirectory( Linear )
Documentation/Examples/Solvers/Linear/CMakeLists.txt 0 → 100644 +28 −0 Original line number Diff line number Diff line set( COMMON_EXAMPLES IterativeLinearSolverExample IterativeLinearSolverWithMonitorExample IterativeLinearSolverWithTimerExample IterativeLinearSolverWithPreconditionerExample IterativeLinearSolverWithRuntimeTypesExample ) if( BUILD_CUDA ) foreach( target IN ITEMS ${COMMON_EXAMPLES} ) cuda_add_executable( ${target}-cuda ${target}.cu OPTIONS ) add_custom_command( COMMAND ${target}-cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) endforeach() else() foreach( target IN ITEMS ${COMMON_EXAMPLES} ) add_executable( ${target} ${target}.cpp ) add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) set( HOST_OUTPUTS ${HOST_OUTPUTS} ${target}.out ) endforeach() endif() IF( BUILD_CUDA ) ADD_CUSTOM_TARGET( RunLinearSolversExamples-cuda ALL DEPENDS ${CUDA_OUTPUTS} ) ELSE() ADD_CUSTOM_TARGET( RunLinearSolversExamples ALL DEPENDS ${HOST_OUTPUTS} ) ENDIF()
Documentation/Examples/Solvers/Linear/IterativeLinearSolverExample.cpp 0 → 100644 +82 −0 Original line number Diff line number Diff line #include <iostream> #include <memory> #include <TNL/Algorithms/ParallelFor.h> #include <TNL/Matrices/SparseMatrix.h> #include <TNL/Devices/Host.h> #include <TNL/Devices/Cuda.h> #include <TNL/Solvers/Linear/TFQMR.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; /*** * Solve the linear system. */ using LinearSolver = TNL::Solvers::Linear::TFQMR< MatrixType >; LinearSolver solver; solver.setMatrix( matrix_ptr ); solver.solve( b, x ); 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 }
Documentation/Examples/Solvers/Linear/IterativeLinearSolverExample.cu 0 → 120000 +1 −0 Original line number Diff line number Diff line IterativeLinearSolverExample.cpp No newline at end of file