Commit 44dacdb0 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Finished documentation of sparse matrix.

parent 9578471a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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
+1 −1
Original line number Diff line number Diff line
@@ -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
+1 −2
Original line number Diff line number Diff line
@@ -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();

   /***
+39 −39
Original line number Diff line number Diff line
@@ -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
+67 −0
Original line number Diff line number Diff line
#include <iostream>
#include <iomanip>
#include <functional>
#include <TNL/Matrices/SparseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>

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
}
Loading