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

Merge branch 'tutorials' into 'develop'

Tutorials

See merge request !44
parents b1055115 91166cb2
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
IF( BUILD_CUDA )
   CUDA_ADD_EXECUTABLE(ParallelForExampleCuda ParallelForExample.cu)
   ADD_CUSTOM_COMMAND( COMMAND ParallelForExampleCuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/ParallelForExample.out OUTPUT ParallelForExample.out )
ELSE()
   ADD_EXECUTABLE(ParallelForExample ParallelForExample.cpp)
   ADD_CUSTOM_COMMAND( COMMAND ParallelForExample > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/ParallelForExample.out OUTPUT ParallelForExample.out )
ENDIF()

IF( BUILD_CUDA )
ADD_CUSTOM_TARGET( RunAlgorithmsExamples-cuda ALL DEPENDS
   ParallelForExample.out
 )
ELSE()
ADD_CUSTOM_TARGET( RunAlgorithmsExamples ALL DEPENDS
   ParallelForExample.out
 )
ENDIF()
 No newline at end of file
+45 −0
Original line number Diff line number Diff line
#include <iostream>
#include <cstdlib>
#include <TNL/Containers/Vector.h>
#include <TNL/Algorithms/ParallelFor.h>

using namespace TNL;
using namespace TNL::Containers;
using namespace TNL::Algorithms;

template< typename Device >
void initMeshFunction( const int xSize,
                       const int ySize,
                       Vector< double, Device >& v,
                       const double& c )
{
   auto view = v1.getConstView();
   auto init = [=] __cuda_callable__  ( int i, int j, const int xSize, const double c ) mutable {
      view[ j * xSize + i ] =  c; };
   ParallelFor2D< Device >::exec( 0, 0, xSize, ySize, init, xSize, c );
}

int main( int argc, char* argv[] )
{
   /***
    * Define dimensions of 2D mesh function.
    */
   const int xSize( 10 ), ySize( 10 );
   const int size = xSize * ySize;

   /***
    * Firstly, test the mesh function initiation on CPU.
    */
   Vector< double, Devices::Host > host_v;
   initMeshFunction( xSize, ySize, host_v, 1.0 );

   /***
    * And then also on GPU.
    */
#ifdef HAVE_CUDA
   Vector< double, Devices::Cuda > cuda_v( size );
   initMeshFunction( xSize, ySize, cuda_v, 1.0 );
#endif
   return EXIT_SUCCESS;
}
+1 −0
Original line number Diff line number Diff line
ParallelForExample-2D.cpp
 No newline at end of file
+46 −0
Original line number Diff line number Diff line
#include <iostream>
#include <cstdlib>
#include <TNL/Containers/Vector.h>
#include <TNL/Algorithms/ParallelFor.h>

using namespace TNL;
using namespace TNL::Containers;
using namespace TNL::Algorithms;

template< typename Device >
void initMeshFunction( const int xSize,
                       const int ySize,
                       const int zSize,
                       Vector< double, Device >& v,
                       const double& c )
{
   auto view = v1.getConstView();
   auto init = [=] __cuda_callable__  ( int i, int j, int k, const int xSize, const int ySize, const double c ) mutable {
      view[ ( k * ySize + j ) * xSize + i ] =  c; };
   ParallelFor3D< Device >::exec( 0, 0, xSize, ySize, init, xSize, ySize, c );
}

int main( int argc, char* argv[] )
{
   /***
    * Define dimensions of 2D mesh function.
    */
   const int xSize( 10 ), ySize( 10 ), zSize( 10 );
   const int size = xSize * ySize * zSize;

   /***
    * Firstly, test the mesh function initiation on CPU.
    */
   Vector< double, Devices::Host > host_v;
   initMeshFunction( xSize, ySize, zSize, host_v, 1.0 );

   /***
    * And then also on GPU.
    */
#ifdef HAVE_CUDA
   Vector< double, Devices::Cuda > cuda_v( size );
   initMeshFunction( xSize, ySize, cuda_v, 1.0 );
#endif
   return EXIT_SUCCESS;
}
+1 −0
Original line number Diff line number Diff line
ParallelForExample-3D.cpp
 No newline at end of file
Loading