diff --git a/Documentation/Tutorials/ForLoops/StaticForExample-2.cpp b/Documentation/Tutorials/ForLoops/StaticForExample-2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ee4afd72c42e2bf0fd8db28bd3f5e7c3c47cc0f --- /dev/null +++ b/Documentation/Tutorials/ForLoops/StaticForExample-2.cpp @@ -0,0 +1,4 @@ +for( int i = 0; i < Size; i++ ) +{ + a[ i ] = b[ i ] + c; sum += a[ i ]; +}; diff --git a/Documentation/Tutorials/ForLoops/StaticForExample.cpp b/Documentation/Tutorials/ForLoops/StaticForExample.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47757458d71505fa585f3624575bcdaa55869602 --- /dev/null +++ b/Documentation/Tutorials/ForLoops/StaticForExample.cpp @@ -0,0 +1,28 @@ +#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; +} + diff --git a/Documentation/Tutorials/ForLoops/tutorial_04_ForLoops.md b/Documentation/Tutorials/ForLoops/tutorial_04_ForLoops.md index e8d220aa7074e7c0062043650d84c551f68ec1c8..e389329a8e093f0d75b5020daa6de87b3ccda06b 100644 --- a/Documentation/Tutorials/ForLoops/tutorial_04_ForLoops.md +++ b/Documentation/Tutorials/ForLoops/tutorial_04_ForLoops.md @@ -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>