Template Numerical Library version main:1655e92
Static Public Member Functions | List of all members
TNL::Algorithms::ParallelFor3D< Device, Mode > Struct Template Reference

Parallel for loop for three dimensional domain of indices. More...

#include <TNL/Algorithms/ParallelFor.h>

Static Public Member Functions

template<typename Index , typename Function , typename... FunctionArgs>
static void exec (Index startX, Index startY, Index startZ, Index endX, Index endY, Index endZ, Function f, FunctionArgs... args)
 Static method for the execution of the loop. More...
 

Detailed Description

template<typename Device = Devices::Sequential, ParallelForMode Mode = SynchronousMode>
struct TNL::Algorithms::ParallelFor3D< Device, Mode >

Parallel for loop for three dimensional domain of indices.

Template Parameters
Devicespecifies the device where the for-loop will be executed. It can be TNL::Devices::Host, TNL::Devices::Cuda or TNL::Devices::Sequential.
Modedefines synchronous/asynchronous mode on parallel devices.

Member Function Documentation

◆ exec()

template<typename Device = Devices::Sequential, ParallelForMode Mode = SynchronousMode>
template<typename Index , typename Function , typename... FunctionArgs>
static void TNL::Algorithms::ParallelFor3D< Device, Mode >::exec ( Index  startX,
Index  startY,
Index  startZ,
Index  endX,
Index  endY,
Index  endZ,
Function  f,
FunctionArgs...  args 
)
inlinestatic

Static method for the execution of the loop.

Template Parameters
Indexis the type of the loop indices.
Functionis the type of the functor to be called in each iteration (it is usually deduced from the argument used in the function call).
FunctionArgsis a variadic pack of types for additional parameters that are forwarded to the functor in every iteration.
Parameters
startXthe for-loop iterates over index domain [startX,endX) x [startY,endY) x [startZ,endZ).
startYthe for-loop iterates over index domain [startX,endX) x [startY,endY) x [startZ,endZ).
startZthe for-loop iterates over index domain [startX,endX) x [startY,endY) x [startZ,endZ).
endXthe for-loop iterates over index domain [startX,endX) x [startY,endY) x [startZ,endZ).
endYthe for-loop iterates over index domain [startX,endX) x [startY,endY) x [startZ,endZ).
endZthe for-loop iterates over index domain [startX,endX) x [startY,endY) x [startZ,endZ).
fis the function to be called in each iteration
argsare additional parameters to be passed to the function f.

The function f is called for each iteration as

f( i, j, k, args... )

where the first parameter is changing the most often.

Example
#include <iostream>
#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 = 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 a 3D 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( size );
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, zSize, cuda_v, 1.0 );
#endif
return EXIT_SUCCESS;
}
#define __cuda_callable__
Definition: CudaCallable.h:22
Namespace for fundamental TNL algorithms.
Definition: AtomicOperations.h:14
Namespace for TNL containers.
Definition: Array.h:21
The main TNL namespace.
Definition: AtomicOperations.h:13
static void exec(Index startX, Index startY, Index startZ, Index endX, Index endY, Index endZ, Function f, FunctionArgs... args)
Static method for the execution of the loop.
Definition: ParallelFor.h:190
Output

The documentation for this struct was generated from the following file: