Template Numerical Library: TNL::Containers::Vector< Real, Device, Index, Allocator > Class Template Reference

Template Numerical Library version\ main:be7e16d88

Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
TNL::Containers::Vector< Real, Device, Index, Allocator > Class Template Reference

Vector extends Array with algebraic operations. More...

`#include <TNL/Containers/Vector.h>`

Inheritance diagram for TNL::Containers::Vector< Real, Device, Index, Allocator >:
[legend]
Collaboration diagram for TNL::Containers::Vector< Real, Device, Index, Allocator >:
[legend]

## Public Types
using | AllocatorType = Allocator
| Allocator type used for allocating this vector.
using | ConstViewType = VectorView< std::add_const_t< Real >, Device, Index >
| Compatible constant VectorView type.
using | DeviceType = Device
| Device used to run operations on the vector.
using | IndexType = Index
| Type being used for the vector elements indexing.
using | RealType = Real
| Type of elements stored in this vector.
template<typename _Real, typename _Device = Device, typename _Index = Index, typename _Allocator = typename Allocators::Default< _Device >::template Allocator< _Real >>
using | Self = Vector< _Real, _Device, _Index, _Allocator >
| A template which allows to quickly obtain a Vector type with changed template parameters.
using | ViewType = VectorView< Real, Device, Index >
| Compatible VectorView type.
Public Types inherited from TNL::Containers::Array< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >
using | AllocatorType
| Allocator type used for allocating this array.
using | ConstViewType
| Compatible constant ArrayView type.
using | DeviceType
| Device used to run operations on the array.
using | IndexType
| Type being used for the array elements indexing.
using | Self
| A template which allows to quickly obtain an Array type with changed template parameters.
using | ValueType
| Type of elements stored in this array.
using | ViewType
| Compatible ArrayView type.

## Public Member Functions
| Vector ()=default
| Constructs an empty array with zero size.
| Vector (const Vector &)=default
| Copy constructor (makes a deep copy).
| Vector (const Vector &vector, const AllocatorType &allocator)
| Copy constructor with a specific allocator (makes a deep copy).
template<typename VectorExpression, typename..., typename = std::enable_if_t< Expressions::HasEnabledExpressionTemplates< VectorExpression >::value && ! IsArrayType< VectorExpression >::value >>
| Vector (const VectorExpression &expression)
| Constructor from expression template.
| Vector (Vector &&) noexcept=default
| Default move constructor.
ConstViewType | getConstView (IndexType begin=0, IndexType end=0) const
| Returns a non-modifiable view of the vector.
ViewType | getView (IndexType begin=0, IndexType end=0)
| Returns a modifiable view of the vector.
| operator ConstViewType () const
| Conversion operator to a non-modifiable view of the vector.
| operator ViewType ()
| Conversion operator to a modifiable view of the vector.
template<typename VectorExpression>
Vector & | operator%= (const VectorExpression &expression)
| Modulo assignment operator for vector and a vector expression.
template<typename VectorExpression>
Vector & | operator*= (const VectorExpression &expression)
| Multiplies elements of this vector and a vector expression and stores the result in this vector.
template<typename VectorExpression>
Vector & | operator+= (const VectorExpression &expression)
| Adds elements of this vector and a vector expression and stores the result in this vector.
template<typename VectorExpression>
Vector & | operator-= (const VectorExpression &expression)
| Subtracts elements of this vector and a vector expression and stores the result in this vector.
template<typename VectorExpression>
Vector & | operator/= (const VectorExpression &expression)
| Divides elements of this vector and a vector expression and stores the result in this vector.
template<typename T, typename..., typename = std::enable_if_t< std::is_convertible_v< T, Real > || IsArrayType< T >::value >>
Array< Real, Device, Index, Allocator > & | operator= (const T &data)
| Assigns a value or an array - same as Array::operator=.
Vector & | operator= (const Vector &)=default
| Copy-assignment operator for copying data from another vector.
template<typename VectorExpression, typename..., typename = std::enable_if_t< Expressions::HasEnabledExpressionTemplates< VectorExpression >::value && ! IsArrayType< VectorExpression >::value >>
Vector & | operator= (const VectorExpression &expression)
| Assigns a vector expression to this vector.
Vector & | operator= (Vector &&) noexcept(false)=default
| Move-assignment operator for acquiring data from rvalues.
Public Member Functions inherited from TNL::Containers::Array< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >
| Array ()=default
| Constructs an empty array with zero size.
virtual | ~Array ()
| Destructor.
__cuda_callable__ bool | empty () const
| Returns true if the current array size is zero.
void | forAllElements (Function &&f)
| Process the lambda function f for each array element.
void | forElements (IndexType begin, IndexType end, Function &&f)
| Process the lambda function f for each array element in interval [ begin, end).
AllocatorType | getAllocator () const
| Returns the allocator associated with the array.
__cuda_callable__ const double * | getArrayData () const
| Returns a const-qualified raw pointer to the data.
ConstViewType | getConstView (IndexType begin=0, IndexType end=0) const
| Returns a non-modifiable view of the array.
__cuda_callable__ const double * | getData () const
| Returns a const-qualified raw pointer to the data.
__cuda_callable__ ValueType | getElement (IndexType i) const
| Returns the value of the i-th element.
__cuda_callable__ IndexType | getSize () const
| Returns the current array size.
ViewType | getView (IndexType begin=0, IndexType end=0)
| Returns a modifiable view of the array.
void | load (const String &fileName)
| Method for loading the array from a binary file fileName.
| operator ConstViewType () const
| Conversion operator to a non-modifiable view of the array.
| operator ViewType ()
| Conversion operator to a modifiable view of the array.
bool | operator!= (const ArrayT &array) const
| Compares the array with another array-like container.
__cuda_callable__ double & | operator() (IndexType i)
| Accesses the i-th element of the array.
Array & | operator= (const Array &array)
| Copy-assignment operator for copying data from another array.
bool | operator== (const ArrayT &array) const
| Compares the array with another array-like container.
__cuda_callable__ double & | operator[] (IndexType i)
| Accesses the i-th element of the array.
void | reset ()
| Resets the array to the empty state.
void | resize (IndexType size)
| Method for resizing the array.
void | save (const String &fileName) const
| Method for saving the array to a binary file fileName.
__cuda_callable__ void | setElement (IndexType i, ValueType value)
| Sets the value of the i-th element to v.
void | setLike (const ArrayT &array)
| Sets the same size as the size of an existing array.
void | setSize (IndexType size)
| Method for setting the array size.
void | setValue (ValueType value, IndexType begin=0, IndexType end=0)
| Sets elements of the array to given value.
void | swap (Array &array) noexcept
| Swaps this array with another.

## Additional Inherited Members
Static Public Member Functions inherited from TNL::Containers::Array< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >
static std::string | getSerializationType ()
| Returns a string representation of the array type in C++ style, with a placeholder in place of Device and Allocator.
Protected Member Functions inherited from TNL::Containers::Array< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >
void | reallocate (IndexType size)
| Internal method for reallocating array elements. Used only from the two overloads of resize.
void | releaseData ()
| Internal method for releasing (deallocating) array data.
Protected Attributes inherited from TNL::Containers::Array< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > >
Allocator | allocator
| The internal allocator instance.
double * | data
| Pointer to the data.
IndexType | size
| Number of elements in the array.

## Detailed Description

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>
class TNL::Containers::Vector< Real, Device, Index, Allocator >

Vector extends Array with algebraic operations.

The template parameters have the same meaning as in Array, with Real corresponding to Array's Value parameter.

Template Parameters

  Real | An arithmetic type for the vector values, e.g. float or double.
  Device | The device to be used for the execution of vector operations.
  Index | The indexing type.
  Allocator | The type of the allocator used for the allocation and deallocation of memory used by the array. By default, an appropriate allocator for the specified Device is selected with Allocators::Default.

Example
  #include <iostream>
  #include <TNL/Containers/Vector.h>
  #include <TNL/Containers/Array.h>
  #include <TNL/Devices/Host.h>
  #include <TNL/Devices/Cuda.h>
  using namespace TNL;
  template< typename Device >
  void
  VectorExample()
  {
  Containers::Vector< int, Device > vector1( 5 );
  vector1 = 0;
  Containers::Vector< int, Device > vector2( 3 );
  vector2 = 1;
  vector2.swap( vector1 );
  vector2.setElement( 2, 4 );
  std::cout << "First vector:" << vector1.getData() << '\n';
  std::cout << "Second vector:" << vector2.getData() << '\n';
  vector2.reset();
  std::cout << "Second vector after reset:" << vector2.getData() << '\n';
  Containers::Vector< int, Device > vect = { 1, 2, -3, 3 };
  std::cout << "The smallest element is:" << min( vect ) << '\n';
  std::cout << "The absolute biggest element is:" << max( abs( vect ) ) << '\n';
  std::cout << "Sum of all vector elements:" << sum( vect ) << '\n';
  vect *= 2.0;
  std::cout << "Vector multiplied by 2:" << vect << '\n';
  }
  int
  main()
  {
  std::cout << "Running vector example on the host system:\n";
  VectorExample< Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Running vector example on the CUDA device:\n";
  VectorExample< Devices::Cuda >();
  #endif
  }
  std::cout
  TNL::Containers::Vector
  Vector extends Array with algebraic operations.
  Definition Vector.h:36
  TNL
  The main TNL namespace.
  Definition AtomicOperations.h:9
  TNL::abs
  __cuda_callable__ T abs(const T &n)
  This function returns absolute value of given number n.
  Definition Math.h:75
  TNL::min
  constexpr ResultType min(const T1 &a, const T2 &b)
  This function returns minimum of two numbers.
  Definition Math.h:24
  TNL::max
  constexpr ResultType max(const T1 &a, const T2 &b)
  This function returns maximum of two numbers.
  Definition Math.h:49

Output
  Running vector example on the host system:
  First vector:0x565120c330e0
  Second vector:0x565120c330c0
  Second vector after reset:0
  The smallest element is:-3
  The absolute biggest element is:3
  Sum of all vector elements:3
  Vector multiplied by 2:[ 2, 4, -6, 6 ]

## Member Typedef Documentation

## ◆ AllocatorType

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>

using TNL::Containers::Vector< Real, Device, Index, Allocator >::AllocatorType = Allocator

Allocator type used for allocating this vector.

See TNL::Allocators.

## ◆ DeviceType

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>

using TNL::Containers::Vector< Real, Device, Index, Allocator >::DeviceType = Device

Device used to run operations on the vector.

See TNL::Devices for the available options.

## Constructor & Destructor Documentation

## ◆ Vector()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>
template<typename VectorExpression, typename..., typename = std::enable_if_t< Expressions::HasEnabledExpressionTemplates< VectorExpression >::value && ! IsArrayType< VectorExpression >::value >>

TNL::Containers::Vector< Real, Device, Index, Allocator >::Vector | ( | const VectorExpression & | expression | ) |
| explicit

Constructor from expression template.

Parameters

  expression | input expression template

## Member Function Documentation

## ◆ getConstView()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>

ConstViewType TNL::Containers::Vector< Real, Device, Index, Allocator >::getConstView | ( | IndexType | begin = 0,
|  | IndexType | end = 0 ) const
| nodiscard

Returns a non-modifiable view of the vector.

By default, a view for the whole vector is returned. If begin or end is set to a non-zero value, a view only for the sub-interval [begin, end) is returned.

Parameters

  begin | The beginning of the vector sub-interval. It is 0 by default.
  end | The end of the vector sub-interval. The default value is 0 which is, however, replaced with the array size.

## ◆ getView()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>

ViewType TNL::Containers::Vector< Real, Device, Index, Allocator >::getView | ( | IndexType | begin = 0,
|  | IndexType | end = 0 )
| nodiscard

Returns a modifiable view of the vector.

By default, a view for the whole vector is returned. If begin or end is set to a non-zero value, a view only for the sub-interval [begin, end) is returned.

Parameters

  begin | The beginning of the vector sub-interval. It is 0 by default.
  end | The end of the vector sub-interval. The default value is 0 which is, however, replaced with the array size.

## ◆ operator%=()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>
template<typename VectorExpression>

Vector & TNL::Containers::Vector< Real, Device, Index, Allocator >::operator%= | ( | const VectorExpression & | expression | ) |

Modulo assignment operator for vector and a vector expression.

The division is evaluated element-wise. The vector expression must either evaluate to a scalar or a vector of the same size as this vector.

Parameters

  expression | Reference to a vector expression.

Returns
  Reference to this vector.

## ◆ operator*=()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>
template<typename VectorExpression>

Vector & TNL::Containers::Vector< Real, Device, Index, Allocator >::operator*= | ( | const VectorExpression & | expression | ) |

Multiplies elements of this vector and a vector expression and stores the result in this vector.

The multiplication is evaluated element-wise. The vector expression must either evaluate to a scalar or a vector of the same size as this vector.

Parameters

  expression | Reference to a vector expression.

Returns
  Reference to this vector.

## ◆ operator+=()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>
template<typename VectorExpression>

Vector & TNL::Containers::Vector< Real, Device, Index, Allocator >::operator+= | ( | const VectorExpression & | expression | ) |

Adds elements of this vector and a vector expression and stores the result in this vector.

The addition is evaluated element-wise. The vector expression must either evaluate to a scalar or a vector of the same size as this vector.

Parameters

  expression | Reference to a vector expression.

Returns
  Reference to this vector.

## ◆ operator-=()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>
template<typename VectorExpression>

Vector & TNL::Containers::Vector< Real, Device, Index, Allocator >::operator-= | ( | const VectorExpression & | expression | ) |

Subtracts elements of this vector and a vector expression and stores the result in this vector.

The subtraction is evaluated element-wise. The vector expression must either evaluate to a scalar or a vector of the same size as this vector.

Parameters

  expression | Reference to a vector expression.

Returns
  Reference to this vector.

## ◆ operator/=()

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>
template<typename VectorExpression>

Vector & TNL::Containers::Vector< Real, Device, Index, Allocator >::operator/= | ( | const VectorExpression & | expression | ) |

Divides elements of this vector and a vector expression and stores the result in this vector.

The division is evaluated element-wise. The vector expression must either evaluate to a scalar or a vector of the same size as this vector.

Parameters

  expression | Reference to a vector expression.

Returns
  Reference to this vector.

## ◆ operator=() [1/2]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>
template<typename T, typename..., typename = std::enable_if_t< std::is_convertible_v< T, Real > || IsArrayType< T >::value >>

Array< Real, Device, Index, Allocator > & TNL::Containers::Vector< Real, Device, Index, Allocator >::operator= | ( | const T & | data | ) |
| inline

Assigns a value or an array - same as Array::operator=.

Returns
  Reference to this vector.

## ◆ operator=() [2/2]

template<typename Real = double, typename Device = Devices::Host, typename Index = int, typename Allocator = typename Allocators::Default< Device >::template Allocator< Real >>
template<typename VectorExpression, typename..., typename = std::enable_if_t< Expressions::HasEnabledExpressionTemplates< VectorExpression >::value && ! IsArrayType< VectorExpression >::value >>

Vector & TNL::Containers::Vector< Real, Device, Index, Allocator >::operator= | ( | const VectorExpression & | expression | ) |

Assigns a vector expression to this vector.

The assignment is evaluated element-wise. The vector expression must either evaluate to a scalar or a vector. If it evaluates to a vector with a different size than this vector, this vector is reallocated to match the size of the vector expression.

Parameters

  expression | The vector expression to be evaluated and assigned to this vector.

Returns
  Reference to this vector.

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

- src/TNL/Containers/Vector.h