GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing tnl-project.gitlab.io — Template Numerical Library: Vectors
Toggle main menu visibility
Loading...
Searching...
No Matches
Vectors Table of ContentsIntroductionThis chapter introduces vectors in TNL. Vector, in addition to Array, offers also basic operations from linear algebra. The reader will mainly learn how to do BLAS level 1 operations in TNL. Thanks to the design of TNL, it is easier to implement, independent of the hardware architecture, and in some cases even faster then BLAS or cuBLAS implementation. Dynamic vectorsVector is a class template similar to Array, which is defined in the TNL::Containers namespace and has four template parameters:
Unlike Array, Vector requires that the Real type is numeric or a type for which basic algebraic operations are defined. What kind of algebraic operations are required depends on what vector operations the user will call. Vector is derived from Array so it inherits all its methods. In the same way the Array has its counterpart ArraView, Vector has VectorView which is derived from ArrayView. See Arrays for more details. In addition to the comparison operators == and != defined for Array and ArrayView, the comparison operators <, <=, >, and >= are available for Vector and VectorView. The comparison follows the lexicographic order and it is performed by an algorithm equivalent to std::lexicographical_compare. Horizontal operationsBy horizontal operations we mean vector expressions where we have one or more vectors as an input and a vector as an output. Horizontal operations in TNL are performed by the expression templates. It makes algebraic operations with vectors easy to do and very efficient at the same time. In some cases, one gets code even more efficient compared to BLAS and cuBLAS. See the following example. 1#include <iostream>
2#include <TNL/Containers/Vector.h>
3#include <TNL/Containers/VectorView.h>
4
7
8template< typename Device >
9void
10expressions()
11{
12 using RealType = float;
14
15 /****
16 * Create vectors
17 */
18 const int size = 11;
19 VectorType a( size );
20 VectorType b( size );
21 VectorType c( size );
22 a.forAllElements(
24 {
25 value = 3.14 * ( i - 5.0 ) / 5.0;
26 } );
27 b = a * a;
34}
35
36int
37main( int argc, char* argv[] )
38{
39 /****
40 * Perform test on CPU
41 */
43 expressions< Devices::Host >();
44
45 /****
46 * Perform test on GPU
47 */
48#if defined( __CUDACC__ ) || defined( __HIP__ )
51 expressions< Devices::GPU >();
52#endif
53}
#define __cuda_callable__ This macro serves for annotating functions which are supposed to be called even from the GPU device. Definition Macros.h:50 __cuda_callable__ T abs(const T &n) This function returns absolute value of given number n. Definition Math.h:75 constexpr T sign(const T &value) This function represents the signum function. Definition Math.h:540 __cuda_callable__ auto sin(const T &value) -> decltype(std::sin(value)) This function returns sine of the given value. Definition Math.h:285 Output is: Expressions on CPU ...
a = [ -3.14, -2.512, -1.884, -1.256, -0.628, 0, 0.628, 1.256, 1.884, 2.512, 3.14 ]
sin( a ) = [ -0.00159255, -0.588816, -0.951351, -0.950859, -0.587528, 0, 0.587528, 0.950859, 0.951351, 0.588816, 0.00159255 ]
abs( sin( a ) ) = [ 0.00159255, 0.588816, 0.951351, 0.950859, 0.587528, 0, 0.587528, 0.950859, 0.951351, 0.588816, 0.00159255 ]
b = [ 9.8596, 6.31014, 3.54946, 1.57754, 0.394384, 0, 0.394384, 1.57754, 3.54946, 6.31014, 9.8596 ]
c = [ -9.41841, -6.94718, -4.70065, -2.81714, -1.29647, 0, 2.47153, 4.71886, 6.60335, 8.12482, 9.42159 ]
Expressions on GPU ...
a = [ -3.14, -2.512, -1.884, -1.256, -0.628, 0, 0.628, 1.256, 1.884, 2.512, 3.14 ]
sin( a ) = [ -0.00159255, -0.588816, -0.951351, -0.950859, -0.587528, 0, 0.587528, 0.950859, 0.951351, 0.588816, 0.00159255 ]
abs( sin( a ) ) = [ 0.00159255, 0.588816, 0.951351, 0.950859, 0.587528, 0, 0.587528, 0.950859, 0.951351, 0.588816, 0.00159255 ]
b = [ 9.8596, 6.31014, 3.54946, 1.57754, 0.394384, 0, 0.394384, 1.57754, 3.54946, 6.31014, 9.8596 ]
c = [ -9.41841, -6.94718, -4.70065, -2.81714, -1.29647, 0, 2.47153, 4.71886, 6.60335, 8.12482, 9.42159 ]
The expression is evaluated on the same device where the vectors are allocated, this is done automatically. One cannot, however, mix vectors from different devices in one expression. Vector expressions consist of operands, operators, and functions. Operands may be vectors, vector views, or other vector expressions. Operators available for vector expressions are listed in the following table, where v is a result vector and expr, expr1, expr2 are vector expressions:
Additionally, vector expressions may contain any function listed in the following table, where v is a result vector and expr, expr1, expr2 are vector expressions:
Vertical operationsBy vertical operations we mean (parallel) reduction based operations where we have one vector expressions as an input and one value as an output. For example computing scalar product, vector norm or finding minimum or maximum of vector elements is based on reduction. See the following example. 1#include <iostream>
2#include <TNL/Containers/Vector.h>
3#include <TNL/Containers/VectorView.h>
4
7
8template< typename Device >
9void
10expressions()
11{
12 using RealType = float;
15
16 /****
17 * Create vectors
18 */
19 const int size = 11;
20 VectorType a_v( size );
21 VectorType b_v( size );
22 VectorType c_v( size );
23 ViewType a = a_v.getView();
24 ViewType b = b_v.getView();
25 ViewType c = c_v.getView();
26 a.forAllElements(
28 {
29 value = i;
30 } );
31 b.forAllElements(
33 {
34 value = i - 5.0;
35 } );
36 c = -5;
37
55 std::cout << "all( lessEqual( abs( a + b ), abs( a ) + abs( b ) ) ) == " << ( cmp ? "true" : "false" ) << '\n';
56 auto [ equal_val, equal_pos ] = argAny( equalTo( a, 5 ) );
57 if( equal_val )
59 else
61}
62
63int
64main( int argc, char* argv[] )
65{
66 /****
67 * Perform test on CPU
68 */
70 expressions< Devices::Host >();
71
72 /****
73 * Perform test on GPU
74 */
75#if defined( __CUDACC__ ) || defined( __HIP__ )
78 expressions< Devices::GPU >();
79#endif
80}
VectorView extends ArrayView with algebraic operations. Definition VectorView.h:24 constexpr ResultType argMin(const T1 &a, const T2 &b) This function returns argument of minimum of two numbers. Definition Math.h:103 constexpr ResultType min(const T1 &a, const T2 &b) This function returns minimum of two numbers. Definition Math.h:24 constexpr ResultType argMax(const T1 &a, const T2 &b) This function returns argument of maximum of two numbers. Definition Math.h:113 Output is: Expressions on CPU ...
a == [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
b == [ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 ]
c == [ -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5 ]
min( a ) == 0 at 0
max( a ) == 10 at 10
min( b ) == -5 at 0
max( b ) == 5 at 10
min( abs( b ) ) == 0
sum( b ) == 0
sum( abs( b ) ) == 30
Scalar product: ( a, b ) == 110
Scalar product: ( a + 3, abs( b ) / 2 ) == 120
all( lessEqual( abs( a + b ), abs( a ) + abs( b ) ) ) == true
5-th element of a is equal to 5
Expressions on GPU ...
a == [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
b == [ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 ]
c == [ -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5 ]
min( a ) == 0 at 0
max( a ) == 10 at 10
min( b ) == -5 at 0
max( b ) == 5 at 10
min( abs( b ) ) == 0
sum( b ) == 0
sum( abs( b ) ) == 30
Scalar product: ( a, b ) == 110
Scalar product: ( a + 3, abs( b ) / 2 ) == 120
all( lessEqual( abs( a + b ), abs( a ) + abs( b ) ) ) == true
5-th element of a is equal to 5
The following table shows vertical operations that can be used on vector expressions:
Static vectorsStatic 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 and given by a template parameter. The StaticVector class template is defined in the TNL::Containers namespace and has two template parameters:
The interface of StaticVector is similar to Vector. StaticVector also supports expression templates, which make the use of static vectors simple and efficient at the same time, and lexicographic comparison by operators <, <=, > and >=. Example: #include <iostream>
#include <TNL/Containers/StaticVector.h>
using namespace TNL;
using namespace TNL::Containers;
int
main( int argc, char* argv[] )
{
Vector v1( 1.0 );
Vector v2( 1.0, 2.0, 3.0 );
Vector v3( v1 - v2 / 2.0 );
Vector v4( 0.0 );
Vector v5( 1.0 );
v4 += v2;
v5 *= v3;
}
The output looks as: v1 = [ 1, 1, 1 ]
v2 = [ 1, 2, 3 ]
v3 = [ 0.5, 0, -0.5 ]
v4 = [ 1, 2, 3 ]
v5 = [ 0.5, 0, -0.5 ]
abs( v3 - 2.0 ) = [ 1.5, 2, 2.5 ]
v2 * v2 = [ 1, 4, 9 ]
Distributed vectorsDistributedVector extends DistributedArray with algebraic operations. The functionality is similar to how Vector extends Array. DistributedVector also supports expression templates and other operations present in Vector. Example: #include <iostream>
#include <TNL/Containers/BlockPartitioning.h>
#include <TNL/Containers/DistributedVector.h>
#include <TNL/MPI/ScopedInitializer.h>
/***
* The following works for any device (CPU, GPU ...).
*/
template< typename Device >
void
distributedVectorExample()
{
using VectorType = TNL::Containers::DistributedVector< int, Device >;
using IndexType = typename VectorType::IndexType;
using LocalRangeType = typename VectorType::LocalRangeType;
const TNL::MPI::Comm communicator = MPI_COMM_WORLD;
// We set the global vector size to a prime number to force non-uniform distribution.
const int size = 97;
const LocalRangeType localRange = TNL::Containers::splitRange< IndexType >( size, communicator );
VectorType v( localRange, ghosts, size, communicator );
v.forElements(
0,
size,
{
value = idx;
} );
const int sum = TNL::sum( v );
if( communicator.rank() == 0 )
}
int
main( int argc, char* argv[] )
{
TNL::MPI::ScopedInitializer mpi( argc, argv );
if( TNL::MPI::GetRank() == 0 )
std::cout << "The first test runs on CPU ...\n";
distributedVectorExample< TNL::Devices::Host >();
#if defined( __CUDACC__ ) || defined( __HIP__ )
TNL::MPI::Barrier();
if( TNL::MPI::GetRank() == 0 )
std::cout << "The second test runs on GPU ...\n";
distributedVectorExample< TNL::Devices::GPU >();
#endif
}
DistributedVector extends DistributedArray with algebraic operations. Definition DistributedVector.h:25 int size() const Returns the size of the group associated with a communicator. Definition Comm.h:228 int rank() const Determines the rank of the calling process in the communicator. Definition Comm.h:221 Subrange< Index > splitRange(Index rangeBegin, Index rangeEnd, int rank, int num_subintervals) A helper function which splits a one-dimensional range. Definition BlockPartitioning.h:27 Definition ScopedInitializer.h:62 The output looks as: Rank 0: rank on node is 0, using GPU id 0 of 2
Environment:
CUDA_VISIBLE_DEVICES=
Rank 2: rank on node is 2, using GPU id 0 of 2
Environment:
CUDA_VISIBLE_DEVICES=
Rank 3: rank on node is 3, using GPU id 1 of 2
Environment:
CUDA_VISIBLE_DEVICES=
Rank 1: rank on node is 1, using GPU id 1 of 2
Environment:
CUDA_VISIBLE_DEVICES=
Rank 3 has subrange TNL::Containers::Subrange<int>( 73, 97 )
Rank 1 has subrange TNL::Containers::Subrange<int>( 25, 49 )
Rank 2 has subrange TNL::Containers::Subrange<int>( 49, 73 )
The first test runs on CPU ...
Rank 0 has subrange TNL::Containers::Subrange<int>( 0, 25 )
Global sum is 4656
The second test runs on GPU ...
Rank 3 has subrange TNL::Containers::Subrange<int>( 73, 97 )
Rank 1 has subrange TNL::Containers::Subrange<int>( 25, 49 )
Rank 2 has subrange TNL::Containers::Subrange<int>( 49, 73 )
Rank 0 has subrange TNL::Containers::Subrange<int>( 0, 25 )
Global sum is 4656
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |