Skip to content
Snippets Groups Projects
Commit 06b0992e authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Writing tutorial on StaticFor.

parent f166b7ce
No related branches found
No related tags found
1 merge request!44Tutorials
for( int i = 0; i < Size; i++ )
{
a[ i ] = b[ i ] + c; sum += a[ i ];
};
#include <iostream>
#include <cstdlib>
#include <TNL/Containers/StaticVector.h>
#include <TNL/Algorithms/StaticFor.h>
using namespace TNL;
using namespace TNL::Containers;
int main( int argc, char* argv[] )
{
/****
* Create two static vectors
*/
const int Size( 3 );
StaticVector< Size, double > a, b;
a = 1.0;
b = 2.0;
double sum( 0.0 );
/****
* Compute an addition of a vector and a constant number.
*/
auto addition = [&]( int i, const double& c ) { a[ i ] = b[ i ] + c; sum += a[ i ]; };
Algorithms::StaticFor< 0, Size >::exec( addition, 3.14 );
std::cout << "a = " << a << std::endl;
std::cout << "sum = " << sum << std::endl;
}
......@@ -47,7 +47,23 @@ For the completness, we show modification of the previous example into 3D:
\include ParallelForExample-3D.cpp
## Static For<a name="static_for"></a>
Static for-loop is designed for short loops with constant (i.e. known at the compile time) number of iterations. It is often used with static arrays and vectors. An adventage of this kind of for loop is that it is explicitly unrolled when the loop is short (up to eight iterations). See the following example:
\include StaticForExample.cpp
Notice that the static for-loop works with a lambda function simillar to parallel for-loop. The bounds of the loop are passed as template parameters in the statement `Algorithms::StaticFor< 0, Size >`. The parameters of the static method `exec` are the lambda functions to be performed in each iteration and auxiliar data to be passed to the function. The function gets the loop index `i` first followed by the auxiliary data `sum` in this example.
The result looks as:
\include StaticForExample.out
The effect of `StaticFor` is really the same as usual for-loop. The following code does the same as the previous example:
\include StaticForExample-2.cpp
The benefit of `StaticFor` is mainly in the explicit unrolling of short loops which can improve the performance in some sitautions. `StaticFor` can be used also in CUDA kernels.
## Templated Static For<a name="templated_static_for"></a>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment