Commit 94531169 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Merge branch 'JK/loops' into 'develop'

Loops

See merge request !94
parents 2ccb0785 25ff5a6b
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -6,12 +6,14 @@ ELSE()
   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_EXECUTABLE(staticForExample staticForExample.cpp)
ADD_CUSTOM_COMMAND( COMMAND staticForExample > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/staticForExample.out OUTPUT staticForExample.out )

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

ADD_CUSTOM_TARGET( RunAlgorithmsExamples ALL DEPENDS
   ParallelForExample.out
   unrolledForExample.out
   staticForExample.out
)
ENDIF()
 No newline at end of file
+6 −6
Original line number Diff line number Diff line
#include <iostream>
#include <cstdlib>
#include <TNL/Containers/Vector.h>
#include <TNL/Algorithms/ParallelFor.h>

@@ -13,10 +12,12 @@ void initMeshFunction( const int xSize,
                       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 );
   auto view = v.getView();
   auto init = [=] __cuda_callable__ ( int i, int j ) mutable
   {
      view[ j * xSize + i ] = c;
   };
   ParallelFor2D< Device >::exec( 0, 0, xSize, ySize, init );
}

int main( int argc, char* argv[] )
@@ -42,4 +43,3 @@ int main( int argc, char* argv[] )
#endif
   return EXIT_SUCCESS;
}
+7 −7
Original line number Diff line number Diff line
#include <iostream>
#include <cstdlib>
#include <TNL/Containers/Vector.h>
#include <TNL/Algorithms/ParallelFor.h>

@@ -14,16 +13,18 @@ void initMeshFunction( const int xSize,
                       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 );
   auto view = v.getView();
   auto init = [=] __cuda_callable__ ( int i, int j, int k ) mutable
   {
      view[ ( k * ySize + j ) * xSize + i ] = c;
   };
   ParallelFor3D< Device >::exec( 0, 0, 0, xSize, ySize, zSize, init );
}

int main( int argc, char* argv[] )
{
   /***
    * Define dimensions of 2D mesh function.
    * Define dimensions of a 3D mesh function.
    */
   const int xSize( 10 ), ySize( 10 ), zSize( 10 );
   const int size = xSize * ySize * zSize;
@@ -43,4 +44,3 @@ int main( int argc, char* argv[] )
#endif
   return EXIT_SUCCESS;
}
+6 −6
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;

/****
 * Set all elements of the vector v to the constant c.
@@ -14,10 +14,11 @@ void initVector( Vector< double, Device >& v,
                 const double& c )
{
   auto view = v.getView();
   auto init = [=] __cuda_callable__  ( int i, const double c ) mutable {
      view[ i ] = c; };

   Algorithms::ParallelFor< Device >::exec( 0, v.getSize(), init, c );
   auto init = [=] __cuda_callable__ ( int i ) mutable
   {
      view[ i ] = c;
   };
   ParallelFor< Device >::exec( 0, v.getSize(), init );
}

int main( int argc, char* argv[] )
@@ -39,4 +40,3 @@ int main( int argc, char* argv[] )
#endif
   return EXIT_SUCCESS;
}
+0 −31
Original line number Diff line number Diff line
#include <iostream>
#include <cstdlib>
#include <TNL/Containers/StaticVector.h>
#include <TNL/Algorithms/TemplateStaticFor.h>

using namespace TNL;
using namespace TNL::Containers;

const int Size( 5 );

template< int I >
struct LoopBody
{
   static void exec( const StaticVector< Size, double >& v ) {
      std::cout << "v[ " << I << " ] = " << v[ I ] << std::endl;
   }
};

int main( int argc, char* argv[] )
{
   /****
    * Initiate static vector
    */
   StaticVector< Size, double > v{ 1.0, 2.0, 3.0, 4.0, 5.0 };

   /****
    * Print out the vector using template parameters for indexing.
    */
   Algorithms::TemplateStaticFor< 0, Size, LoopBody >::exec( v );
}
Loading