Commit 117283d0 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Jakub Klinkovský
Browse files

Added functions for matrix wrapping.

parent e61d838b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ set( COMMON_EXAMPLES
    DenseMatrixViewExample_forElements
    DenseMatrixViewExample_forRows
    DenseMatrixViewExample_forAllElements
    DenseMatrixViewExample_wrap
)

if( BUILD_CUDA )
+34 −0
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Matrices/MatrixWrapping.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>

template< typename Device >
void wrapMatrixView()
{
   const int rows( 3 ), columns( 4 );
   TNL::Containers::Vector< double, Device > valuesVector {
      1,  2,  3,  4,
      5,  6,  7,  8,
      9, 10, 11, 12 };
   double* values = valuesVector.getData();

   /***
    * Wrap the array `values` to dense matrix view
    */
   auto matrix = TNL::Matrices::wrapDenseMatrix< Device >( rows, columns, values );
   std::cout << "Matrix reads as: " << std::endl << matrix << std::endl;
}

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

#ifdef HAVE_CUDA
   std::cout << "Wraping matrix view on CUDA device: " << std::endl;
   wrapMatrixView< TNL::Devices::Cuda >();
#endif
}
+1 −0
Original line number Diff line number Diff line
DenseMatrixViewExample_wrap.cpp
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ set( COMMON_EXAMPLES
   SparseMatrixViewExample_forElements
   SparseMatrixViewExample_forRows
   SparseMatrixViewExample_forAllElements
   SparseMatrixViewExample_wrapCSR
   SparseMatrixViewExample_wrapEllpack
)

if( BUILD_CUDA )
+45 −0
Original line number Diff line number Diff line
#include <iostream>
#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Matrices/DenseMatrix.h>
#include <TNL/Matrices/MatrixWrapping.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>

template< typename Device >
void wrapMatrixView()
{
   /***
    * Encode the following matrix to CSR format...
    *
    * /  1  2  0  0 \.
    * |  0  6  0  0 |
    * |  9  0  0  0 |
    * \  0  0 15 16 /
    */
   const int rows( 4 ), columns( 4 );
   TNL::Containers::Vector< double, Device > valuesVector     { 1, 2, 6, 9, 15, 16 };
   TNL::Containers::Vector< int, Device > columnIndexesVector { 0, 1, 1, 0,  2,  3 };
   TNL::Containers::Vector< int, Device > rowPointersVector   { 0, 2, 3, 4, 6 };

   double* values = valuesVector.getData();
   int* columnIndexes = columnIndexesVector.getData();
   int* rowPointers = rowPointersVector.getData();

   /***
    * Wrap the arrays `rowPointers, `values` and `columnIndexes` to sparse matrix view
    */
   auto matrix = TNL::Matrices::wrapCSRMatrix< Device >( rows, columns, rowPointers, values, columnIndexes );

   std::cout << "Matrix reads as: " << std::endl << matrix << std::endl;
}

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

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