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

Writting tutorial on StaticVector.

parent 2e04963f
No related branches found
No related tags found
1 merge request!41Tutorials
......@@ -3,10 +3,13 @@ IF( BUILD_CUDA )
ADD_CUSTOM_COMMAND( COMMAND Expressions > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/Expressions.out OUTPUT Expressions.out )
CUDA_ADD_EXECUTABLE( Reduction Reduction.cu )
ADD_CUSTOM_COMMAND( COMMAND Reduction > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/Reduction.out OUTPUT Reduction.out )
ADD_EXECUTABLE( StaticVectorExample StaticVectorExample.cpp )
ADD_CUSTOM_COMMAND( COMMAND StaticVectorExample > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/StaticVectorExample.out OUTPUT StaticVectorExample.out )
ENDIF()
IF( BUILD_CUDA )
ADD_CUSTOM_TARGET( TutorialsVectors-cuda ALL DEPENDS
Expressions.out
Reduction.out )
Reduction.out
StaticVectorExample.out )
ENDIF()
#include <iostream>
#include <TNL/Containers/StaticVector.h>
using namespace TNL;
using namespace TNL::Containers;
int main( int argc, char* argv[] )
{
using Vector = StaticVector< 3, double >;
Vector v1( 1.0 ), v2( 1.0, 2.0, 3.0 ), v3( v1 - v2 / 2.0 );
Vector v4( 0.0 ), v5( 1.0 );
v4 += v2;
v5 *= v3;
std::cout << "v1 = " << v1 << std::endl;
std::cout << "v2 = " << v2 << std::endl;
std::cout << "v3 = " << v3 << std::endl;
std::cout << "v4 = " << v4 << std::endl;
std::cout << "v5 = " << v5 << std::endl;
std::cout << "abs( v3 - 2.0 ) = " << abs( v3 - 2.0 ) << std::endl;
std::cout << "v2 * v2 = " << v2 * v2 << std::endl;
}
......@@ -44,4 +44,18 @@ Output is:
## Static vectors <a name="static_vectors"></a>
## Distributed vectors <a name="distributed_vectors"></a>
\ No newline at end of file
Static vectors are derived from static arrays and so they are allocated on the stack and can be created in CUDA kernels as well. Their size is fixed as well and it is given by a template parameter. Static vector is a templated class defined in namespace `TNL::Containers` having two template parameters:
* `Size` is the array size.
* `Real` is type of numbers stored in the array.
The interface of StaticVectors is smillar to Vector. Probably the most important methods are those related with static vector expressions which are handled by expression templates. They make the use of static vectors simpel and efficient at the same time. See the following simple demonstration:
\include StaticVectorExample.cpp
The output looks as:
\include StaticVectorExample.out
## Distributed vectors <a name="distributed_vectors"></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