Commit 114ea7d1 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Added documentation and examples for SparseMatrix[,View]::forRows.

parent b57c3b73
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ set( COMMON_EXAMPLES
   SparseMatrixExample_allRowsReduction
   SparseMatrixExample_forElements
   SparseMatrixExample_forEachElement
   SparseMatrixExample_forRows
   SparseMatrixViewExample_getSerializationType
   SparseMatrixViewExample_getCompressedRowLengths
   SparseMatrixViewExample_getConstRow
@@ -27,6 +28,7 @@ set( COMMON_EXAMPLES
   SparseMatrixViewExample_rowsReduction
   SparseMatrixViewExample_allRowsReduction
   SparseMatrixViewExample_forElements
   SparseMatrixViewExample_forRows
   SparseMatrixViewExample_forEachElement
)

+37 −0
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Matrices/SparseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>

template< typename Device >
void forRowsExample()
{
   using MatrixType = TNL::Matrices::SparseMatrix< double, Device >;
   using RowViewType = typename MatrixType::RowViewType;
   MatrixType matrix( { 1, 2, 3, 4, 5, }, 5  );

   auto f = [] __cuda_callable__ ( RowViewType& row ) mutable {
      for( int localIdx = 0;
           localIdx <= row.getRowIndex(); // This is important, some matrix formats may allocate more matrix elements
           localIdx++ )                   // than we requested. These padding elements are processed here as well.
                                          // and so we cannot use row.getSize()
      {
         row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 );
         row.setColumnIndex( localIdx, localIdx );
      }
   };
   matrix.forEachRow( 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
}
+1 −0
Original line number Diff line number Diff line
SparseMatrixExample_forRows.cpp
 No newline at end of file
+38 −0
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Matrices/SparseMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>

template< typename Device >
void forRowsExample()
{
   using MatrixType = TNL::Matrices::SparseMatrix< double, Device >;
   using RowViewType = typename MatrixType::ViewType::RowViewType;
   MatrixType matrix( { 1, 2, 3, 4, 5, }, 5  );
   auto view = matrix.getView();

   auto f = [] __cuda_callable__ ( RowViewType& row ) mutable {
      for( int localIdx = 0;
           localIdx <= row.getRowIndex(); // This is important, some matrix formats may allocate more matrix elements
           localIdx++ )                   // than we requested. These padding elements are processed here as well.
                                          // and so we cannot use row.getSize()
      {
         row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 );
         row.setColumnIndex( localIdx, localIdx );
      }
   };
   view.forEachRow( 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
}
+1 −0
Original line number Diff line number Diff line
SparseMatrixViewExample_forRows.cpp
 No newline at end of file
Loading