Template Numerical Library  version develop:5fd0ca953
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 <Vector.h>

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

Public Types

using AllocatorType = Allocator
 Allocator type used for allocating this vector. More...
 
using ConstViewType = VectorView< std::add_const_t< Real >, Device, Index >
 Compatible constant VectorView type.
 
using DeviceType = Device
 Device where the vector is allocated. More...
 
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 = typename Allocators::Default< Devices::Host >::template Allocator< double >
 Allocator type used for allocating this array. More...
 
using ConstViewType = ArrayView< std::add_const_t< double >, Devices::Host, int >
 Compatible constant ArrayView type.
 
using DeviceType = Devices::Host
 Device where the array is allocated. More...
 
using IndexType = int
 Type being used for the array elements indexing.
 
using Self = Array< _Value, _Device, _Index, _Allocator >
 A template which allows to quickly obtain an Array type with changed template parameters.
 
using ValueType = double
 Type of elements stored in this array.
 
using ViewType = ArrayView< double, Devices::Host, int >
 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. More...
 
 Vector (Vector &&)=default
 Default move constructor.
 
ConstViewType getConstView (IndexType begin=0, IndexType end=0) const
 Returns a non-modifiable view of the vector. More...
 
ViewType getView (IndexType begin=0, IndexType end=0)
 Returns a modifiable view of the vector. More...
 
 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< Real, Device, Index, Allocator > & operator*= (const VectorExpression &expression)
 
template<typename VectorExpression >
Vectoroperator*= (const VectorExpression &expression)
 Multiplies elements of this vector and a vector expression and stores the result in this vector. More...
 
template<typename VectorExpression >
Vector< Real, Device, Index, Allocator > & operator+= (const VectorExpression &expression)
 
template<typename VectorExpression >
Vectoroperator+= (const VectorExpression &expression)
 Adds elements of this vector and a vector expression and stores the result in this vector. More...
 
template<typename VectorExpression >
Vector< Real, Device, Index, Allocator > & operator-= (const VectorExpression &expression)
 
template<typename VectorExpression >
Vectoroperator-= (const VectorExpression &expression)
 Subtracts elements of this vector and a vector expression and stores the result in this vector. More...
 
template<typename VectorExpression >
Vector< Real, Device, Index, Allocator > & operator/= (const VectorExpression &expression)
 
template<typename VectorExpression >
Vectoroperator/= (const VectorExpression &expression)
 Divides elements of this vector and a vector expression and stores the result in this vector. More...
 
template<typename T , typename... , typename = std::enable_if_t< std::is_convertible< T, Real >::value || IsArrayType< T >::value >>
Array< Real, Device, Index, Allocator > & operator= (const T &data)
 Assigns a value or an array - same as Array::operator=. More...
 
Vectoroperator= (const Vector &)=default
 Copy-assignment operator for copying data from another vector.
 
template<typename VectorExpression , typename... , typename >
Vector< Real, Device, Index, Allocator > & operator= (const VectorExpression &expression)
 
template<typename VectorExpression , typename... , typename = std::enable_if_t< Expressions::HasEnabledExpressionTemplates< VectorExpression >::value && ! IsArrayType< VectorExpression >::value >>
Vectoroperator= (const VectorExpression &expression)
 Assigns a vector expression to this vector. More...
 
Vectoroperator= (Vector &&)=default
 Move-assignment operator for acquiring data from rvalues.
 
template<Algorithms::ScanType Type = Algorithms::ScanType::Inclusive, typename VectorExpression >
void scan (const VectorExpression &expression, IndexType begin=0, IndexType end=0)
 Computes the scan (prefix sum) of the vector expression. More...
 
template<Algorithms::ScanType Type = Algorithms::ScanType::Inclusive>
void scan (IndexType begin=0, IndexType end=0)
 Computes the scan (prefix sum) of the vector elements. More...
 
template<Algorithms::ScanType Type = Algorithms::ScanType::Inclusive, typename VectorExpression , typename FlagsArray >
void segmentedScan (const VectorExpression &expression, FlagsArray &flags, IndexType begin=0, IndexType end=0)
 Computes the segmented scan (prefix sum) of a vector expression. More...
 
template<Algorithms::ScanType Type = Algorithms::ScanType::Inclusive, typename FlagsArray >
void segmentedScan (FlagsArray &flags, IndexType begin=0, IndexType end=0)
 Computes the segmented scan (prefix sum) of the vector elements. More...
 
- 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.
 
 Array (Array &&array)
 Move constructor for initialization from rvalues. More...
 
 Array (const AllocatorType &allocator)
 Constructs an empty array and sets the provided allocator. More...
 
 Array (const Array &array)
 Copy constructor (makes a deep copy). More...
 
 Array (const Array &array, const AllocatorType &allocator)
 Copy constructor with a specific allocator (makes a deep copy). More...
 
 Array (const Array &array, IndexType begin, IndexType size=0, const AllocatorType &allocator=AllocatorType())
 Copy constructor (makes a deep copy). More...
 
 Array (const Array< Value_, Device_, Index_, Allocator_ > &a)
 Copy constructor from array with different template parameters. More...
 
 Array (const IndexType &size, const AllocatorType &allocator=AllocatorType())
 Constructs an array with given size. More...
 
 Array (const IndexType &size, const double &value, const AllocatorType &allocator=AllocatorType())
 Constructs an array with given size and value. More...
 
 Array (const std::initializer_list< InValue > &list, const AllocatorType &allocator=AllocatorType())
 Constructor which initializes the array by copying elements from std::initializer_list, e.g. {...}. More...
 
 Array (const std::list< InValue > &list, const AllocatorType &allocator=AllocatorType())
 Constructor which initializes the array by copying elements from std::list. More...
 
 Array (const std::vector< InValue > &vector, const AllocatorType &allocator=AllocatorType())
 Constructor which initializes the array by copying elements from std::vector. More...
 
 Array (double *data, const IndexType &size, const AllocatorType &allocator=AllocatorType())
 Constructs an array with given size and copies data from given pointer. More...
 
virtual ~Array ()
 Destructor.
 
void bind (const ArrayT &array, const IndexType &begin=0, const IndexType &size=0)
 Binds this array with another array. More...
 
void bind (double *_data, const int _size)
 Binds _data with this array. More...
 
void bind (StaticArray< Size, double > &array)
 Binds this array with a static array of size Size. More...
 
bool containsOnlyValue (const ValueType &v, IndexType begin=0, IndexType end=0) const
 Checks if all elements have the same value v. More...
 
bool containsValue (const ValueType &v, IndexType begin=0, IndexType end=0) const
 Checks if there is an element with value v. More...
 
__cuda_callable__ bool empty () const
 Returns true if the current array size is zero. More...
 
void evaluate (const Function &f, IndexType begin=0, IndexType end=0)
 Sets the array elements using given lambda function. More...
 
AllocatorType getAllocator () const
 Returns the allocator associated with the array.
 
__cuda_callable__ double * getArrayData ()
 Returns a raw pointer to the data. More...
 
__cuda_callable__ const double * getArrayData () const
 Returns a const-qualified raw pointer to the data. More...
 
ConstViewType getConstView (IndexType begin=0, IndexType end=0) const
 Returns a non-modifiable view of the array. More...
 
__cuda_callable__ double * getData ()
 Returns a raw pointer to the data. More...
 
__cuda_callable__ const double * getData () const
 Returns a const-qualified raw pointer to the data. More...
 
__cuda_callable__ double getElement (const int &i) const
 Returns the value of the i-th element. More...
 
virtual String getSerializationTypeVirtual () const
 Returns a String representation of the array type in C++ style, with a placeholder in place of Device and Allocator.
 
__cuda_callable__ int getSize () const
 Returns the current array size. More...
 
ViewType getView (IndexType begin=0, IndexType end=0)
 Returns a modifiable view of the array. More...
 
void load (const String &fileName)
 Method for loading the array from a binary file fileName. More...
 
 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. More...
 
Arrayoperator= (Array &&array)
 Move-assignment operator for acquiring data from rvalues. More...
 
Arrayoperator= (const Array &array)
 Copy-assignment operator for copying data from another array. More...
 
Arrayoperator= (const std::list< InValue > &list)
 Copies elements from std::list to this array. More...
 
Array< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > > & operator= (const std::list< InValue > &list)
 
Arrayoperator= (const std::vector< InValue > &vector)
 Copies elements from std::vector to this array. More...
 
Array< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > > & operator= (const std::vector< InValue > &vector)
 
Arrayoperator= (const T &data)
 Assigns either array-like container or a single value. More...
 
Array< double, Devices::Host, int, typename Allocators::Default< Devices::Host >::template Allocator< double > > & operator= (const T &data)
 
bool operator== (const ArrayT &array) const
 Compares the array with another array-like container. More...
 
__cuda_callable__ double & operator[] (const int &i)
 Accesses the i-th element of the array. More...
 
__cuda_callable__ const double & operator[] (const int &i) const
 Accesses the i-th element of the array. More...
 
void reset ()
 Resets the array to the empty state. More...
 
void save (const String &fileName) const
 Method for saving the array to a binary file fileName. More...
 
__cuda_callable__ void setElement (const int &i, const double &v)
 Sets the value of the i-th element to v. More...
 
void setLike (const ArrayT &array)
 Sets the same size as the size of an existing array. More...
 
void setSize (int size)
 Method for setting the array size. More...
 
void setValue (const ValueType &v, IndexType begin=0, IndexType end=0)
 Sets elements of the array to given value. More...
 
void swap (Array &array)
 Swaps this array with another. More...
 

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 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 releaseData ()
 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 > >
double * allocationPointer
 Pointer to the originally allocated data. More...
 
typename Allocators::Default< Devices::Host >::template Allocator< double > allocator
 The internal allocator instance.
 
double * data
 Pointer to the data.
 
int * referenceCounter
 Counter of objects sharing this array or some parts of it. More...
 
int 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
RealAn arithmetic type for the vector values, e.g. float or double.
DeviceThe device to be used for the execution of vector operations.
IndexThe indexing type.
AllocatorThe 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>
using namespace TNL;
using namespace std;
int main()
{
vector1 = 0;
cout << "Does vector contain 1?" << vector1.containsValue( 1 ) << endl;
cout << "Does vector contain only zeros?" << vector1.containsOnlyValue( 0 ) << endl;
vector2 = 1;
vector2.swap( vector1 );
vector2.setElement( 2, 4 );
cout << "First vector:" << vector1.getData() << endl;
cout << "Second vector:" << vector2.getData() << endl;
vector2.reset();
cout << "Second vector after reset:" << vector2.getData() << endl;
Containers::Vector<int> vect = { 1, 2, -3, 3 };
cout << "The smallest element is:" << min( vect ) << endl;
cout << "The absolute biggest element is:" << max( abs( vect ) ) << endl;
cout << "Sum of all vector elements:" << sum( vect ) << endl;
vect *= 2.0;
cout << "Vector multiplied by 2:" << vect << endl;
}
Output

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 Allocators::Cuda, Allocators::CudaHost, Allocators::CudaManaged, Allocators::Host or Allocators:Default.

◆ 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 where the vector is allocated.

See Devices::Host or Devices::Cuda.

Constructor & Destructor Documentation

◆ Vector()

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

Constructor from expression template.

Parameters
expressioninput expression template

Member Function Documentation

◆ getConstView()

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

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
beginThe beginning of the vector sub-interval. It is 0 by default.
endThe end of the vector sub-interval. The default value is 0 which is, however, replaced with the array size.

◆ getView()

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

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
beginThe beginning of the vector sub-interval. It is 0 by default.
endThe 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)

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
expressionReference 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
expressionReference 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
expressionReference 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
expressionReference 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< T, Real >::value || 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
expressionThe vector expression to be evaluated and assigned to this vector.
Returns
Reference to this vector.

◆ scan() [1/2]

template<typename Real , typename Device , typename Index , typename Allocator >
template<Algorithms::ScanType Type, typename VectorExpression >
void TNL::Containers::Vector< Real, Device, Index, Allocator >::scan ( const VectorExpression &  expression,
IndexType  begin = 0,
IndexType  end = 0 
)

Computes the scan (prefix sum) of the vector expression.

By default, scan is computed for the whole vector. If begin or end is set to a non-zero value, only elements in the sub-interval [begin, end) are scanned.

Template Parameters
TypeThe scan type - either Inclusive or Exclusive.
Parameters
expressionA vector expression for which scan is computed and stored in this vector.
beginThe beginning of the vector sub-interval. It is 0 by default.
endThe end of the vector sub-interval. The default value is 0 which is, however, replaced with the array size.

◆ scan() [2/2]

template<typename Real , typename Device , typename Index , typename Allocator >
template<Algorithms::ScanType Type>
void TNL::Containers::Vector< Real, Device, Index, Allocator >::scan ( IndexType  begin = 0,
IndexType  end = 0 
)

Computes the scan (prefix sum) of the vector elements.

By default, scan is computed for the whole vector. If begin or end is set to a non-zero value, only elements in the sub-interval [begin, end) are scanned.

Template Parameters
TypeThe scan type - either Inclusive or Exclusive.
Parameters
beginThe beginning of the vector sub-interval. It is 0 by default.
endThe end of the vector sub-interval. The default value is 0 which is, however, replaced with the array size.

◆ segmentedScan() [1/2]

template<typename Real , typename Device , typename Index , typename Allocator >
template<Algorithms::ScanType Type, typename VectorExpression , typename FlagsArray >
void TNL::Containers::Vector< Real, Device, Index, Allocator >::segmentedScan ( const VectorExpression &  expression,
FlagsArray &  flags,
IndexType  begin = 0,
IndexType  end = 0 
)

Computes the segmented scan (prefix sum) of a vector expression.

By default, segmented scan is computed for the whole vector. If begin or end is set to a non-zero value, only elements in the sub-interval [begin, end) are scanned.

Template Parameters
TypeThe scan type - either Inclusive or Exclusive.
Parameters
expressionA vector expression for which scan is computed and stored in this vector.
flagsA binary array where ones indicate the beginning of each segment.
beginThe beginning of the vector sub-interval. It is 0 by default.
endThe end of the vector sub-interval. The default value is 0 which is, however, replaced with the array size.

◆ segmentedScan() [2/2]

template<typename Real , typename Device , typename Index , typename Allocator >
template<Algorithms::ScanType Type, typename FlagsArray >
void TNL::Containers::Vector< Real, Device, Index, Allocator >::segmentedScan ( FlagsArray &  flags,
IndexType  begin = 0,
IndexType  end = 0 
)

Computes the segmented scan (prefix sum) of the vector elements.

By default, segmented scan is computed for the whole vector. If begin or end is set to a non-zero value, only elements in the sub-interval [begin, end) are scanned.

Template Parameters
TypeThe scan type - either Inclusive or Exclusive.
Parameters
flagsA binary array where ones indicate the beginning of each segment.
beginThe beginning of the vector sub-interval. It is 0 by default.
endThe end of the vector sub-interval. The default value is 0 which is, however, replaced with the array size.

The documentation for this class was generated from the following files:
TNL
The main TNL namespace.
Definition: AtomicOperations.h:22
TNL::max
__cuda_callable__ ResultType max(const T1 &a, const T2 &b)
This function returns maximum of two numbers.
Definition: Math.h:70
std::cout
TNL::Containers::Vector
Vector extends Array with algebraic operations.
Definition: Vector.h:57
std::endl
T endl(T... args)
std
STL namespace.
TNL::abs
__cuda_callable__ T abs(const T &n)
This function returns absolute value of given number n.
Definition: Math.h:90
TNL::min
__cuda_callable__ ResultType min(const T1 &a, const T2 &b)
This function returns minimum of two numbers.
Definition: Math.h:45