Commit 4f594e30 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Update of matrix forRows examples.

parent 47f892d6
Loading
Loading
Loading
Loading
+21 −4
Original line number Diff line number Diff line
@@ -9,17 +9,34 @@ void forRowsExample()
{
   using MatrixType = TNL::Matrices::DenseMatrix< double, Device >;
   using RowView = typename MatrixType::RowView;
   MatrixType matrix( 5, 5 );
   const int size = 5;
   MatrixType matrix( size, size );

   /***
    * Set the matrix elements.
    */
   auto f = [=] __cuda_callable__ ( RowView& row ) mutable {
      const int& rowIdx = row.getRowIndex();
      row.setValue( rowIdx, 10 * ( rowIdx + 1 ) );
      if( rowIdx > 0 )
         row.setValue( rowIdx - 1, -1.0 );
      row.setValue( rowIdx, rowIdx + 1.0 );
      if( rowIdx < size - 1 )
         row.setValue( rowIdx + 1, -1.0 );
   };
   matrix.forAllRows( f );
   std::cout << matrix << std::endl;

   /***
    * Set the matrix elements.
    * Now divide each matrix row by its largest element with use of iterators.
    */
   matrix.forAllRows( f );
   matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
      double largest = std::numeric_limits< double >::lowest();
      for( auto element : row )
         largest = TNL::max( largest, element.value() );
      for( auto element : row )
         element.value() /= largest;
   } );
   std::cout << "Divide each matrix row by its largest element... " << std::endl;
   std::cout << matrix << std::endl;
}

+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ void forElementsExample()
   auto matrixView = matrix.getView();

   auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) {
      if( rowIdx < columnIdx )
      if( columnIdx > rowIdx )
         compute = false;
      else
         value = rowIdx + columnIdx;
+21 −4
Original line number Diff line number Diff line
@@ -9,18 +9,35 @@ void forRowsExample()
{
   using MatrixType = TNL::Matrices::DenseMatrix< double, Device >;
   using RowView = typename MatrixType::RowView;
   MatrixType matrix( 5, 5 );
   const int size = 5;
   MatrixType matrix( size, size );
   auto view = matrix.getView();

   /***
    * Set the matrix elements.
    */
   auto f = [=] __cuda_callable__ ( RowView& row ) mutable {
      const int& rowIdx = row.getRowIndex();
      row.setValue( rowIdx, 10 * ( rowIdx + 1 ) );
      if( rowIdx > 0 )
         row.setValue( rowIdx - 1, -1.0 );
      row.setValue( rowIdx, rowIdx + 1.0 );
      if( rowIdx < size - 1 )
         row.setValue( rowIdx + 1, -1.0 );
   };
   view.forAllRows( f );
   std::cout << matrix << std::endl;

   /***
    * Set the matrix elements.
    * Now divide each matrix row by its largest element - with the use of iterators.
    */
   view.forAllRows( f );
   view.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable {
      double largest = std::numeric_limits< double >::lowest();
      for( auto element : row )
         largest = TNL::max( largest, element.value() );
      for( auto element : row )
         element.value() /= largest;
   } );
   std::cout << "Divide each matrix row by its largest element... " << std::endl;
   std::cout << matrix << std::endl;
}

+7 −2
Original line number Diff line number Diff line
@@ -7,7 +7,8 @@
template< typename Device >
void getRowExample()
{
   TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 );
   const int size = 5;
   TNL::Matrices::DenseMatrix< double, Device > matrix( size, size );

   /***
    * Create dense matrix view which can be captured by the following lambda
@@ -17,7 +18,11 @@ void getRowExample()

   auto f = [=] __cuda_callable__ ( int rowIdx ) mutable {
      auto row = matrixView.getRow( rowIdx );
      row.setValue( rowIdx, 10 * ( rowIdx + 1 ) );
      if( rowIdx > 0 )
         row.setValue( rowIdx - 1, -1.0 );
      row.setValue( rowIdx, rowIdx + 1.0 );
      if( rowIdx < size - 1 )
         row.setValue( rowIdx + 1, -1.0 );
   };

   /***
+16 −1
Original line number Diff line number Diff line
@@ -62,11 +62,12 @@ void forRowsExample()
   auto matrix = TNL::Matrices::LambdaMatrixFactory< double, Device, int >::create(
      matrixSize, matrixSize, matrixElements, rowLengths );
   using MatrixType = decltype( matrix );
   using RowView = typename MatrixType::RowView;

   TNL::Matrices::DenseMatrix< double, Device > denseMatrix( matrixSize, matrixSize );
   denseMatrix.setValue( 0.0 );
   auto dense_view = denseMatrix.getView();
   auto f = [=] __cuda_callable__ ( const typename MatrixType::RowView& row ) mutable {
   auto f = [=] __cuda_callable__ ( const RowView& row ) mutable {
      auto dense_row = dense_view.getRow( row.getRowIndex() );
      for( int localIdx = 0; localIdx < row.getSize(); localIdx++ )
         dense_row.setValue( row.getColumnIndex( localIdx ), row.getValue( localIdx ) );
@@ -75,6 +76,20 @@ void forRowsExample()

   std::cout << "Laplace operator lambda matrix: " << std::endl << matrix << std::endl;
   std::cout << "Laplace operator dense matrix: " << std::endl << denseMatrix << std::endl;

   /***
    * Compute sum of elements in each row and store it into a vector.
    */
   TNL::Containers::Vector< double, Device > sum_vector( matrixSize );
   auto sum_view = sum_vector.getView();
   matrix.forAllRows( [=] __cuda_callable__ ( const RowView& row ) mutable {
      double sum( 0.0 );
      for( auto element : row )
         sum += TNL::abs( element.value() );
      sum_view[ row.getRowIndex() ] = sum;
   } );

   std::cout << "Sums in matrix rows = " << sum_vector << std::endl;
}

int main( int argc, char* argv[] )
Loading