Loading Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cpp +4 −0 Original line number Diff line number Diff line Loading @@ -10,6 +10,10 @@ template< typename Device > void setElements() { TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 1, 1, 1, 1 }, 5 ); /**** * Get the matrix view. */ auto view = matrix.getView(); for( int i = 0; i < 5; i++ ) view.setElement( i, i, i ); Loading Documentation/Tutorials/Matrices/CMakeLists.txt +12 −0 Original line number Diff line number Diff line Loading @@ -34,6 +34,11 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_setElement.out OUTPUT DenseMatrixViewExample_setElement.out ) CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_data_encapsulation DenseMatrixViewExample_data_encapsulation.cu ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_data_encapsulation > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_data_encapsulation.out OUTPUT DenseMatrixViewExample_data_encapsulation.out ) CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_2 SparseMatrixExample_Constructor_init_list_2.cu ) ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_2 > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_2.out Loading Loading @@ -84,6 +89,11 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction_vectorProduct.out OUTPUT SparseMatrixExample_rowsReduction_vectorProduct.out ) CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cu ) ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out OUTPUT SparseMatrixViewExample_setElement.out ) ELSE() # ADD_EXECUTABLE( UniquePointerExample UniquePointerExample.cpp ) # ADD_CUSTOM_COMMAND( COMMAND UniquePointerExample > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/UniquePointerExample.out OUTPUT UniquePointerExample.out ) Loading @@ -102,6 +112,7 @@ ADD_CUSTOM_TARGET( TutorialsMatricesCuda ALL DEPENDS DenseMatrixExample_rowsReduction_vectorProduct.out DenseMatrixExample_rowsReduction_maxNorm.out DenseMatrixViewExample_setElement.out DenseMatrixViewExample_data_encapsulation.out SparseMatrixExample_Constructor_init_list_2.out SparseMatrixExample_setRowCapacities.out SparseMatrixExample_Constructor_std_map.out Loading @@ -110,6 +121,7 @@ ADD_CUSTOM_TARGET( TutorialsMatricesCuda ALL DEPENDS SparseMatrixExample_setElement.out SparseMatrixExample_forRows.out SparseMatrixExample_rowsReduction_vectorProduct.out SparseMatrixViewExample_setElement.out ) ENDIF() # Loading Documentation/Tutorials/Matrices/DenseMatrixViewExample_data_encapsulation.cpp 0 → 100644 +64 −0 Original line number Diff line number Diff line #include <iostream> #ifdef HAVE_CUDA #include <cuda.h> #endif #include <TNL/Containers/VectorView.h> #include <TNL/Algorithms/ParallelFor.h> #include <TNL/Matrices/DenseMatrix.h> #include <TNL/Devices/Host.h> template< typename Device > void encapsulation() { const int size = 5; /*** * Allocate the dense matrix with no use of TNL */ double* host_data = new double[ size * size ]; for( int row = 0; row < size; row++ ) for( int column = 0; column < size; column++ ) host_data[ row * size + column ] = row * size + column + 1; double* data = nullptr; if( std::is_same< Device, TNL::Devices::Host >::value ) { data = new double[ size * size ]; memcpy( data, host_data, sizeof( double ) * size * size ); } #ifdef HAVE_CUDA else if( std::is_same< Device, TNL::Devices::Cuda >::value ) { cudaMalloc( ( void**) &data, sizeof( double ) * size * size ); cudaMemcpy( data, host_data, sizeof( double ) * size * size, cudaMemcpyHostToDevice ); } #endif /*** * Encapsulate the matrix into DenseMatrixView. */ TNL::Containers::VectorView< double, Device > dataView( data, size * size ); TNL::Matrices::DenseMatrixView< double, Device, int, TNL::Algorithms::Segments::RowMajorOrder > matrix( 5, 5, dataView ); std::cout << "Dense matrix view reads as:" << std::endl; std::cout << matrix << std::endl; auto f = [=] __cuda_callable__ ( int i ) mutable { matrix.setElement( i, i, -i ); }; TNL::Algorithms::ParallelFor< Device >::exec( 0, 5, f ); std::cout << "Dense matrix view after elements manipulation:" << std::endl; std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { std::cout << "Dense matrix encapsulation on host:" << std::endl; encapsulation< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Dense matrix encapsulation on CUDA device:" << std::endl; encapsulation< TNL::Devices::Cuda >(); #endif } Documentation/Tutorials/Matrices/DenseMatrixViewExample_data_encapsulation.cu 0 → 120000 +1 −0 Original line number Diff line number Diff line DenseMatrixViewExample_data_encapsulation.cpp No newline at end of file Documentation/Tutorials/Matrices/SparseMatrixViewExample_setElement.cpp 0 → 120000 +1 −0 Original line number Diff line number Diff line ../../Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cpp No newline at end of file Loading
Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cpp +4 −0 Original line number Diff line number Diff line Loading @@ -10,6 +10,10 @@ template< typename Device > void setElements() { TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 1, 1, 1, 1 }, 5 ); /**** * Get the matrix view. */ auto view = matrix.getView(); for( int i = 0; i < 5; i++ ) view.setElement( i, i, i ); Loading
Documentation/Tutorials/Matrices/CMakeLists.txt +12 −0 Original line number Diff line number Diff line Loading @@ -34,6 +34,11 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_setElement.out OUTPUT DenseMatrixViewExample_setElement.out ) CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_data_encapsulation DenseMatrixViewExample_data_encapsulation.cu ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_data_encapsulation > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_data_encapsulation.out OUTPUT DenseMatrixViewExample_data_encapsulation.out ) CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_2 SparseMatrixExample_Constructor_init_list_2.cu ) ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_2 > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_2.out Loading Loading @@ -84,6 +89,11 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction_vectorProduct.out OUTPUT SparseMatrixExample_rowsReduction_vectorProduct.out ) CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cu ) ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out OUTPUT SparseMatrixViewExample_setElement.out ) ELSE() # ADD_EXECUTABLE( UniquePointerExample UniquePointerExample.cpp ) # ADD_CUSTOM_COMMAND( COMMAND UniquePointerExample > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/UniquePointerExample.out OUTPUT UniquePointerExample.out ) Loading @@ -102,6 +112,7 @@ ADD_CUSTOM_TARGET( TutorialsMatricesCuda ALL DEPENDS DenseMatrixExample_rowsReduction_vectorProduct.out DenseMatrixExample_rowsReduction_maxNorm.out DenseMatrixViewExample_setElement.out DenseMatrixViewExample_data_encapsulation.out SparseMatrixExample_Constructor_init_list_2.out SparseMatrixExample_setRowCapacities.out SparseMatrixExample_Constructor_std_map.out Loading @@ -110,6 +121,7 @@ ADD_CUSTOM_TARGET( TutorialsMatricesCuda ALL DEPENDS SparseMatrixExample_setElement.out SparseMatrixExample_forRows.out SparseMatrixExample_rowsReduction_vectorProduct.out SparseMatrixViewExample_setElement.out ) ENDIF() # Loading
Documentation/Tutorials/Matrices/DenseMatrixViewExample_data_encapsulation.cpp 0 → 100644 +64 −0 Original line number Diff line number Diff line #include <iostream> #ifdef HAVE_CUDA #include <cuda.h> #endif #include <TNL/Containers/VectorView.h> #include <TNL/Algorithms/ParallelFor.h> #include <TNL/Matrices/DenseMatrix.h> #include <TNL/Devices/Host.h> template< typename Device > void encapsulation() { const int size = 5; /*** * Allocate the dense matrix with no use of TNL */ double* host_data = new double[ size * size ]; for( int row = 0; row < size; row++ ) for( int column = 0; column < size; column++ ) host_data[ row * size + column ] = row * size + column + 1; double* data = nullptr; if( std::is_same< Device, TNL::Devices::Host >::value ) { data = new double[ size * size ]; memcpy( data, host_data, sizeof( double ) * size * size ); } #ifdef HAVE_CUDA else if( std::is_same< Device, TNL::Devices::Cuda >::value ) { cudaMalloc( ( void**) &data, sizeof( double ) * size * size ); cudaMemcpy( data, host_data, sizeof( double ) * size * size, cudaMemcpyHostToDevice ); } #endif /*** * Encapsulate the matrix into DenseMatrixView. */ TNL::Containers::VectorView< double, Device > dataView( data, size * size ); TNL::Matrices::DenseMatrixView< double, Device, int, TNL::Algorithms::Segments::RowMajorOrder > matrix( 5, 5, dataView ); std::cout << "Dense matrix view reads as:" << std::endl; std::cout << matrix << std::endl; auto f = [=] __cuda_callable__ ( int i ) mutable { matrix.setElement( i, i, -i ); }; TNL::Algorithms::ParallelFor< Device >::exec( 0, 5, f ); std::cout << "Dense matrix view after elements manipulation:" << std::endl; std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { std::cout << "Dense matrix encapsulation on host:" << std::endl; encapsulation< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Dense matrix encapsulation on CUDA device:" << std::endl; encapsulation< TNL::Devices::Cuda >(); #endif }
Documentation/Tutorials/Matrices/DenseMatrixViewExample_data_encapsulation.cu 0 → 120000 +1 −0 Original line number Diff line number Diff line DenseMatrixViewExample_data_encapsulation.cpp No newline at end of file
Documentation/Tutorials/Matrices/SparseMatrixViewExample_setElement.cpp 0 → 120000 +1 −0 Original line number Diff line number Diff line ../../Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cpp No newline at end of file