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

Writting documentation on MultidiagonalMatrix.

parent f5108926
Loading
Loading
Loading
Loading
+45 −45
Original line number Diff line number Diff line
@@ -24,10 +24,10 @@ IF( BUILD_CUDA )
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out
                       OUTPUT MultidiagonalMatrixExample_setElements.out )

#   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths_cuda MultidiagonalMatrixExample_getCompressedRowLengths.cu )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths_cuda >
#                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out
#                       OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out )
   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths_cuda MultidiagonalMatrixExample_getCompressedRowLengths.cu )
   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths_cuda >
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out
                       OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out )

#   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getElementsCount_cuda MultidiagonalMatrixExample_getElementsCount.cu )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElementsCount_cuda >
@@ -59,25 +59,25 @@ IF( BUILD_CUDA )
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out
                       OUTPUT MultidiagonalMatrixExample_getElement.out )

#   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction_cuda MultidiagonalMatrixExample_rowsReduction.cu )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction_cuda >
#                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out
#                       OUTPUT MultidiagonalMatrixExample_rowsReduction.out )
   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction_cuda MultidiagonalMatrixExample_rowsReduction.cu )
   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction_cuda >
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out
                       OUTPUT MultidiagonalMatrixExample_rowsReduction.out )

#   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction_cuda MultidiagonalMatrixExample_allRowsReduction.cu )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction_cuda >
#                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out
#                       OUTPUT MultidiagonalMatrixExample_allRowsReduction.out )
   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction_cuda MultidiagonalMatrixExample_allRowsReduction.cu )
   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction_cuda >
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out
                       OUTPUT MultidiagonalMatrixExample_allRowsReduction.out )

#   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forRows_cuda MultidiagonalMatrixExample_forRows.cu )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forRows_cuda >
#                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forRows.out
#                       OUTPUT MultidiagonalMatrixExample_forRows.out )
   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forRows_cuda MultidiagonalMatrixExample_forRows.cu )
   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forRows_cuda >
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forRows.out
                       OUTPUT MultidiagonalMatrixExample_forRows.out )

#   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllRows_cuda MultidiagonalMatrixExample_forAllRows.cu )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllRows_cuda >
#                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out
#                       OUTPUT MultidiagonalMatrixExample_forAllRows.out )
   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllRows_cuda MultidiagonalMatrixExample_forAllRows.cu )
   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllRows_cuda >
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out
                       OUTPUT MultidiagonalMatrixExample_forAllRows.out )

#   CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_constructor_cuda MultidiagonalMatrixViewExample_constructor.cu )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_constructor_cuda >
@@ -165,10 +165,10 @@ ELSE()
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out
                       OUTPUT MultidiagonalMatrixExample_setElements.out )

#   ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths MultidiagonalMatrixExample_getCompressedRowLengths.cpp )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths >
#                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out
#                       OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out )
   ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths MultidiagonalMatrixExample_getCompressedRowLengths.cpp )
   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths >
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out
                       OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out )

#   ADD_EXECUTABLE( MultidiagonalMatrixExample_getElementsCount MultidiagonalMatrixExample_getElementsCount.cpp )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElementsCount >
@@ -200,25 +200,25 @@ ELSE()
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out
                       OUTPUT MultidiagonalMatrixExample_getElement.out )

#   ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction MultidiagonalMatrixExample_rowsReduction.cpp )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction >
#                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out
#                       OUTPUT MultidiagonalMatrixExample_rowsReduction.out )
   ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction MultidiagonalMatrixExample_rowsReduction.cpp )
   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction >
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out
                       OUTPUT MultidiagonalMatrixExample_rowsReduction.out )

#   ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction MultidiagonalMatrixExample_allRowsReduction.cpp )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction >
#                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out
#                       OUTPUT MultidiagonalMatrixExample_allRowsReduction.out )
   ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction MultidiagonalMatrixExample_allRowsReduction.cpp )
   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction >
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out
                       OUTPUT MultidiagonalMatrixExample_allRowsReduction.out )

#   ADD_EXECUTABLE( MultidiagonalMatrixExample_forRows MultidiagonalMatrixExample_forRows.cpp )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forRows >
#                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forRows.out
#                       OUTPUT MultidiagonalMatrixExample_forRows.out )
   ADD_EXECUTABLE( MultidiagonalMatrixExample_forRows MultidiagonalMatrixExample_forRows.cpp )
   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forRows >
                       ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forRows.out
                       OUTPUT MultidiagonalMatrixExample_forRows.out )

#   ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllRows MultidiagonalMatrixExample_forAllRows.cpp )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllRows >
#                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out
#                       OUTPUT MultidiagonalMatrixExample_forAllRows.out )
   ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllRows MultidiagonalMatrixExample_forAllRows.cpp )
   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllRows >
                        ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllRows.out
                       OUTPUT MultidiagonalMatrixExample_forAllRows.out )

#   ADD_EXECUTABLE( MultidiagonalMatrixViewExample_constructor MultidiagonalMatrixViewExample_constructor.cpp )
#   ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_constructor >
@@ -288,17 +288,17 @@ ADD_CUSTOM_TARGET( RunMatricesExamples ALL DEPENDS
   MultidiagonalMatrixExample_Constructor_init_list_2.out
   MultidiagonalMatrixExample_getSerializationType.out
   MultidiagonalMatrixExample_setElements.out
#   MultidiagonalMatrixExample_getCompressedRowLengths.out
   MultidiagonalMatrixExample_getCompressedRowLengths.out
#   MultidiagonalMatrixExample_getElementsCount.out
   MultidiagonalMatrixExample_getConstRow.out
   MultidiagonalMatrixExample_getRow.out
   MultidiagonalMatrixExample_setElement.out
   MultidiagonalMatrixExample_addElement.out
   MultidiagonalMatrixExample_getElement.out
#   MultidiagonalMatrixExample_rowsReduction.out
#   MultidiagonalMatrixExample_allRowsReduction.out
#   MultidiagonalMatrixExample_forRows.out
#   MultidiagonalMatrixExample_forAllRows.out
   MultidiagonalMatrixExample_rowsReduction.out
   MultidiagonalMatrixExample_allRowsReduction.out
   MultidiagonalMatrixExample_forRows.out
   MultidiagonalMatrixExample_forAllRows.out
#   MultidiagonalMatrixViewExample_constructor.out
#   MultidiagonalMatrixViewExample_getCompressedRowLengths.out
#   MultidiagonalMatrixViewExample_getElementsCount.out
+40 −25
Original line number Diff line number Diff line
@@ -6,39 +6,54 @@


template< typename Device >
void laplaceOperatorMatrix()
void createMultidiagonalMatrix()
{
   const int gridSize( 4 );
   const int matrixSize = gridSize * gridSize;
   const int matrixSize = 6;

   /***
    * Setup the following matrix (dots represent zeros):
    * 
    * /  4 -1 .  -1  .  . \
    * | -1  4 -1  . -1  . |
    * |  . -1  4 -1  . -1 |
    * | -1  . -1  4 -1  . |
    * |  . -1  . -1  4 -1 |
    * \  .  .  1  . -1  4 /
    * 
    * The diagonals offsets are { -3, -1, 0, 1, 3 }.
    */
   TNL::Matrices::MultidiagonalMatrix< double, Device > matrix( 
      matrixSize, { - gridSize, -1, 0, 1, gridSize }, {
         {  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 }
      matrixSize, { -3, -1, 0, 1, 3 }, {
   /***
    * To set the matrix elements we first extend the diagonals to their full
    * lengths even outside the matrix (dots represent zeros and zeros are
    * artificial zeros used for memory alignment):
    * 
    * 0  .  0 /  4 -1 .  -1  .  . \              -> {  0,  0,  4, -1, -1 }
    * .  0  . | -1  4 -1  . -1  . | .            -> {  0, -1,  4, -1, -1 }
    * .  .  0 |  . -1  4 -1  . -1 | .  .         -> {  0, -1,  4, -1, -1 }
    *    .  . | -1  . -1  4 -1  . | 0  .  .      -> { -1, -1,  4, -1,  0 }
    *       . |  . -1  . -1  4 -1 | .  0  .  .   -> { -1, -1,  4, -1,  0 }
    *         \  .  .  1  . -1  4 / 0  .  0  . . -> { -1, -1,  4,  0,  0 }
    * 
    */
      {  0,  0,  4, -1, -1 },
      {  0, -1,  4, -1, -1 },
      {  0, -1,  4, -1, -1 },
      { -1, -1,  4, -1,  0 },
      { -1, -1,  4, -1,  0 },
      { -1, -1,  4,  0,  0 }
      } );
   std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl;
   std::cout << "The matrix reads as: " << std::endl << matrix << std::endl;
}

int main( int argc, char* argv[] )
{
   std::cout << "Creating Laplace operator matrix on CPU ... " << std::endl;
   laplaceOperatorMatrix< TNL::Devices::Host >();
   std::cout << "Create multidiagonal matrix on CPU ... " << std::endl;
   createMultidiagonalMatrix< TNL::Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Creating Laplace operator matrix on CUDA GPU ... " << std::endl;
   laplaceOperatorMatrix< TNL::Devices::Cuda >();
   std::cout << "Creating multidiagonal matrix on CUDA GPU ... " << std::endl;
   createMultidiagonalMatrix< TNL::Devices::Cuda >();
#endif
}
+80 −0
Original line number Diff line number Diff line
#include <iostream>
#include <iomanip>
#include <functional>
#include <TNL/Matrices/MultidiagonalMatrix.h>
#include <TNL/Devices/Host.h>

template< typename Device >
void allRowsReduction()
{
   /***
    * 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 }.
    */
   TNL::Matrices::MultidiagonalMatrix< 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 } } );

   /***
    * Find largest element in each row.
    */
   TNL::Containers::Vector< double, Device > rowMax( matrix.getRows() );

   /***
    * Prepare vector 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 << "Rows reduction on host:" << std::endl;
   allRowsReduction< TNL::Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Rows reduction on CUDA device:" << std::endl;
   allRowsReduction< TNL::Devices::Cuda >();
#endif
}
+1 −0
Original line number Diff line number Diff line
MultidiagonalMatrixExample_allRowsReduction.cpp
 No newline at end of file
+56 −0
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Matrices/MultidiagonalMatrix.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>

template< typename Device >
void forAllRowsExample()
{
   /***
    * 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 }.
    */
   TNL::Matrices::MultidiagonalMatrix< double, Device > matrix(
      5,               // number of matrix rows
      5,               // number of matrix columns
      { -2, -1, 0 } ); // matrix diagonals offsets

   auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, double& value, bool& compute ) {
      /***
       * 'forRows' method iterates only over matrix elements lying on given subdiagonals
       * and so we do not need to check anything. The element value can be expressed
       * by the 'localIdx' variable, see the following figure:
       * 
       *                              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 } 
       * 
       */
      value = 3 - localIdx;
   };
   matrix.forAllRows( f );
   std::cout << matrix << std::endl;
}

int main( int argc, char* argv[] )
{
   std::cout << "Creating matrix on host: " << std::endl;
   forAllRowsExample< TNL::Devices::Host >();

#ifdef HAVE_CUDA
   std::cout << "Creating matrix on CUDA device: " << std::endl;
   forAllRowsExample< TNL::Devices::Cuda >();
#endif
}
Loading