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

Writting documentation on tridiagonal matrix view.

parent 76d2f76d
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Matrices/MultidiagonalMatrix.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>

template< typename Device >
void addElements()
{
   const int matrixSize( 5 );
   TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
   TNL::Matrices::TridiagonalMatrix< double, Device > matrix(
      matrixSize,    // number of rows
      matrixSize,     // number of columns
      { -1, 0, 1 } ); // diagonals offsets
      matrixSize     // number of columns
   );
   auto view = matrix.getView();

   for( int i = 0; i < matrixSize; i++ )
      view.setElement( i, i, i );

+15 −17
Original line number Diff line number Diff line
#include <iostream>
#include <iomanip>
#include <functional>
#include <TNL/Matrices/MultidiagonalMatrix.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>

template< typename Device >
void allRowsReduction()
void rowsReduction()
{
   /***
    * Set the following matrix (dots represent zero matrix elements and zeros are
    * padding zeros for memory alignment):
    * 
    * 0  0 / 1  .  .  .  . \  -> { 0, 0, 1 }
    *    0 | 2  1  .  .  . |  -> { 0, 2, 1 }
    *      | 3  2  1  .  . |  -> { 3, 2, 1 }
    *      | .  3  2  1  . |  -> { 3, 2, 1 }
    *      \ .  .  3  2  1 /  -> { 3, 2, 1 } 
    *  0 / 1  3  .  .  . \   -> { 0, 1, 3 }
    *    | 2  1  3  .  . |   -> { 2, 1, 3 }
    *    | .  2  1  3  . |   -> { 2, 1, 3 }
    *    | .  .  2  1  3 |   -> { 2, 1, 3 }
    *    \ .  .  .  2  1 / 0 -> { 2, 1, 0 } 
    * 
    * The diagonals offsets are { -2, -1, 0 }.
    */
   TNL::Matrices::MultidiagonalMatrix< double, Device > matrix (
   TNL::Matrices::TridiagonalMatrix< double, Device > matrix (
      5,              // number of matrix columns
      { -2, -1, 0 },  // diagonals offsets
      { { 0, 0, 1 },  // matrix elements
        { 0, 2, 1 }, 
        { 3, 2, 1 }, 
        { 3, 2, 1 },
        { 3, 2, 1 } } );
      { { 0, 1, 3 },  // matrix elements
        { 2, 1, 3 }, 
        { 2, 1, 3 }, 
        { 2, 1, 3 },
        { 2, 1, 3 } } );
   auto view = matrix.getView();

   /***
@@ -72,10 +70,10 @@ void allRowsReduction()
int main( int argc, char* argv[] )
{
   std::cout << "Rows reduction on host:" << std::endl;
   allRowsReduction< TNL::Devices::Host >();
   rowsReduction< TNL::Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Rows reduction on CUDA device:" << std::endl;
   allRowsReduction< TNL::Devices::Cuda >();
   rowsReduction< TNL::Devices::Cuda >();
#endif
}
+19 −22
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Matrices/MultidiagonalMatrix.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>

template< typename Device >
void forAllRowsExample()
void forRowsExample()
{
   /***
    * Set the following matrix (dots represent zero matrix elements and zeros are
    * padding zeros for memory alignment):
    * 
    * 0  0 / 1  .  .  .  . \  -> { 0, 0, 1 }
    *    0 | 2  1  .  .  . |  -> { 0, 2, 1 }
    *      | 3  2  1  .  . |  -> { 3, 2, 1 }
    *      | .  3  2  1  . |  -> { 3, 2, 1 }
    *      \ .  .  3  2  1 /  -> { 3, 2, 1 } 
    * 
    * The diagonals offsets are { -2, -1, 0 }.
    * 0 / 1  3  .  .  . \   -> { 0, 1, 3 }
    *   | 2  1  3  .  . |   -> { 2, 1, 3 }
    *   | .  2  1  3  . |   -> { 2, 1, 3 }
    *   | .  .  2  1  3 |   -> { 2, 1, 3 }
    *   \ .  .  .  2  1 / 0 -> { 2, 1, 0 } 
    */
   TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
   TNL::Matrices::TridiagonalMatrix< double, Device > matrix(
      5,      // number of matrix rows
      5,               // number of matrix columns
      { -2, -1, 0 } ); // matrix diagonals offsets
      5 );    // number of matrix columns
   auto view = matrix.getView();

   auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) {
@@ -32,11 +29,11 @@ void forAllRowsExample()
       * 
       *                           0  1  2  <- localIdx values
       *                           -------
       * 0  0 / 1  .  .  .  . \  -> { 0, 0, 1 }
       *    0 | 2  1  .  .  . |  -> { 0, 2, 1 }
       *      | 3  2  1  .  . |  -> { 3, 2, 1 }
       *      | .  3  2  1  . |  -> { 3, 2, 1 }
       *      \ .  .  3  2  1 /  -> { 3, 2, 1 } 
       * 0 / 1  3  .  .  . \   -> { 0, 1, 3 }
       *   | 2  1  3  .  . |   -> { 2, 1, 3 }
       *   | .  2  1  3  . |   -> { 2, 1, 3 }
       *   | .  .  2  1  3 |   -> { 2, 1, 3 }
       *   \ .  .  .  2  1 / 0 -> { 2, 1, 0 } 
       * 
       */
      value = 3 - localIdx;
@@ -48,10 +45,10 @@ void forAllRowsExample()
int main( int argc, char* argv[] )
{
   std::cout << "Creating matrix on host: " << std::endl;
   forAllRowsExample< TNL::Devices::Host >();
   forRowsExample< TNL::Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Creating matrix on CUDA device: " << std::endl;
   forAllRowsExample< TNL::Devices::Cuda >();
   forRowsExample< TNL::Devices::Cuda >();
#endif
}
+16 −19
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Matrices/MultidiagonalMatrix.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>

@@ -10,18 +10,15 @@ void forRowsExample()
    * Set the following matrix (dots represent zero matrix elements and zeros are
    * padding zeros for memory alignment):
    * 
    * 0  0 / 1  .  .  .  . \  -> { 0, 0, 1 }
    *    0 | 2  1  .  .  . |  -> { 0, 2, 1 }
    *      | 3  2  1  .  . |  -> { 3, 2, 1 }
    *      | .  3  2  1  . |  -> { 3, 2, 1 }
    *      \ .  .  3  2  1 /  -> { 3, 2, 1 } 
    * 
    * The diagonals offsets are { -2, -1, 0 }.
    * 0 / 1  3  .  .  . \   -> { 0, 1, 3 }
    *   | 2  1  3  .  . |   -> { 2, 1, 3 }
    *   | .  2  1  3  . |   -> { 2, 1, 3 }
    *   | .  .  2  1  3 |   -> { 2, 1, 3 }
    *   \ .  .  .  2  1 / 0 -> { 2, 1, 0 } 
    */
   TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
   TNL::Matrices::TridiagonalMatrix< double, Device > matrix(
      5,      // number of matrix rows
      5,               // number of matrix columns
      { -2, -1, 0 } ); // matrix diagonals offsets
      5 );    // number of matrix columns
   auto view = matrix.getView();

   auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) {
@@ -32,11 +29,11 @@ void forRowsExample()
       * 
       *                           0  1  2  <- localIdx values
       *                           -------
       * 0  0 / 1  .  .  .  . \  -> { 0, 0, 1 }
       *    0 | 2  1  .  .  . |  -> { 0, 2, 1 }
       *      | 3  2  1  .  . |  -> { 3, 2, 1 }
       *      | .  3  2  1  . |  -> { 3, 2, 1 }
       *      \ .  .  3  2  1 /  -> { 3, 2, 1 } 
       * 0 / 1  3  .  .  . \   -> { 0, 1, 3 }
       *   | 2  1  3  .  . |   -> { 2, 1, 3 }
       *   | .  2  1  3  . |   -> { 2, 1, 3 }
       *   | .  .  2  1  3 |   -> { 2, 1, 3 }
       *   \ .  .  .  2  1 / 0 -> { 2, 1, 0 } 
       * 
       */
      value = 3 - localIdx;
+13 −23
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Matrices/MultidiagonalMatrix.h>
#include <TNL/Matrices/TridiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>

@@ -8,32 +8,22 @@
template< typename Device >
void laplaceOperatorMatrix()
{
   const int gridSize( 4 );
   const int matrixSize = gridSize * gridSize;
   TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( 
   const int gridSize( 6 );
   const int matrixSize = gridSize;
   TNL::Matrices::TridiagonalMatrix< double, Device > matrix( 
      matrixSize, // number of rows
      matrixSize,                     // number of columns
   { - gridSize, -1, 0, 1, gridSize } // diagonals offsets
      matrixSize  // number of columns
   );
   matrix.setElements( {
         {  0.0,  0.0, 1.0 },  // set matrix elements corresponding to boundary grid nodes
         {  0.0,  0.0, 1.0 },  // and Dirichlet boundary conditions, i.e. 1 on the main diagonal
         {  0.0,  0.0, 1.0 },  // which is the third one
         {  0.0,  0.0, 1.0 },
         {  0.0,  0.0, 1.0 },
         { -1.0, -1.0, 4.0, -1.0, -1.0 }, // set matrix elements corresponding to inner grid nodes, i.e. 4 on the main diagonal
         { -1.0, -1.0, 4.0, -1.0, -1.0 }, //  (the third one) and -1 to the other sub-diagonals
         {  0.0,  0.0, 1.0 },
         {  0.0,  0.0, 1.0 },
         { -1.0, -1.0, 4.0, -1.0, -1.0 },
         { -1.0, -1.0, 4.0, -1.0, -1.0 },
         {  0.0,  0.0, 1.0 },
         {  0.0,  0.0, 1.0 },
         {  0.0,  0.0, 1.0 },
         {  0.0,  0.0, 1.0 },
         {  0.0,  0.0, 1.0 }
         {  0.0, 1.0 },
         { -1.0, 2.0, -1.0 },
         { -1.0, 2.0, -1.0 },
         { -1.0, 2.0, -1.0 },
         { -1.0, 2.0, -1.0 },
         {  0.0, 1.0 }
      } );
   auto view = matrix.getView();

   TNL::Containers::Vector< int, Device > rowLengths;
   view.getCompressedRowLengths( rowLengths );
   std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl;
Loading