Template Numerical Library version\ main:be918e6f
|
ArrayView is a simple data structure which provides a non-owning encapsulation of array data. That is, ArrayView is like Array without memory management. More...
#include <TNL/Containers/ArrayView.h>
Public Types | |
using | ConstViewType = ArrayView< std::add_const_t< Value >, Device, Index > |
Compatible constant ArrayView type. | |
using | DeviceType = Device |
Device used to run operations on the array. | |
using | IndexType = Index |
Type being used for the array elements indexing. | |
template<typename _Value, typename _Device = Device, typename _Index = Index> | |
using | Self = ArrayView< _Value, _Device, _Index > |
A template which allows to quickly obtain an ArrayView type with changed template parameters. | |
using | ValueType = Value |
Type of elements stored in this array. | |
using | ViewType = ArrayView< Value, Device, Index > |
Compatible ArrayView type. | |
Public Member Functions | |
__cuda_callable__ | ArrayView ()=default |
Constructs an empty array view. | |
__cuda_callable__ | ArrayView (ArrayView &&view) noexcept=default |
Move constructor for initialization from rvalues. | |
__cuda_callable__ | ArrayView (const ArrayView &view)=default |
Shallow copy constructor. | |
template<typename Value_> | |
__cuda_callable__ | ArrayView (const ArrayView< Value_, Device, Index > &view) |
"Templated" shallow copy constructor. | |
__cuda_callable__ | ArrayView (ValueType *data, IndexType size) |
Constructs an array view by binding to the given data pointer and size. | |
__cuda_callable__ void | bind (ArrayView view) |
Method for rebinding (reinitialization) using another array view. | |
__cuda_callable__ void | bind (ValueType *data, IndexType size) |
Method for rebinding (reinitialization) using a raw pointer and size. | |
__cuda_callable__ bool | empty () const |
Returns true if the current array view size is zero. | |
template<typename Function> | |
void | forAllElements (Function &&f) |
Process the lambda function f for each array element. | |
template<typename Function> | |
void | forAllElements (Function &&f) const |
Process the lambda function f for each array element for constant instances. | |
template<typename Function> | |
void | forElements (IndexType begin, IndexType end, Function &&f) |
Process the lambda function f for each array element in interval [ begin, end). | |
template<typename Function> | |
void | forElements (IndexType begin, IndexType end, Function &&f) const |
Process the lambda function f for each array element in interval [ begin, end) for constant instances of the array. | |
__cuda_callable__ ValueType * | getArrayData () |
Returns a raw pointer to the data. | |
__cuda_callable__ const ValueType * | getArrayData () const |
Returns a const-qualified raw pointer to the data. | |
__cuda_callable__ ConstViewType | getConstView (IndexType begin=0, IndexType end=0) const |
Returns a non-modifiable view of the array view. | |
__cuda_callable__ ValueType * | getData () |
Returns a raw pointer to the data. | |
__cuda_callable__ const ValueType * | 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 size of the array view. | |
__cuda_callable__ ViewType | getView (IndexType begin=0, IndexType end=0) |
Returns a modifiable view of the array view. | |
void | load (const std::string &fileName) |
Method for loading the data from a binary file fileName. | |
template<typename ArrayT> | |
bool | operator!= (const ArrayT &array) const |
Compares the array view with another array-like container. | |
__cuda_callable__ Value & | operator() (IndexType i) |
Accesses the i-th element of the array. | |
__cuda_callable__ const Value & | operator() (IndexType i) const |
Accesses the i-th element of the array. | |
ArrayView & | operator= (const ArrayView &view) |
Deep copy assignment operator for copying data from another array view. | |
template<typename T, typename..., typename = std::enable_if_t< std::is_convertible_v< T, ValueType > || IsArrayType< T >::value >> | |
ArrayView & | operator= (const T &data) |
Assigns either array-like container or a single value. | |
template<typename T, typename..., typename> | |
ArrayView< Value, Device, Index > & | operator= (const T &data) |
template<typename ArrayT> | |
bool | operator== (const ArrayT &array) const |
Compares the array view with another array-like container. | |
__cuda_callable__ Value & | operator[] (IndexType i) |
Accesses the i-th element of the array view. | |
__cuda_callable__ const Value & | operator[] (IndexType i) const |
Accesses the i-th element of the array view. | |
__cuda_callable__ void | reset () |
void | save (const std::string &fileName) const |
Method for saving the data to a binary file fileName. | |
__cuda_callable__ void | setElement (IndexType i, ValueType value) |
Sets the value of the i-th element to v. | |
void | setValue (ValueType value, IndexType begin=0, IndexType end=0) |
Sets elements of the array view to given value. | |
__cuda_callable__ void | swap (ArrayView &view) |
Swaps this array view with another. | |
Protected Attributes | |
ValueType * | data = nullptr |
Pointer to the data. | |
IndexType | size = 0 |
Array view size. | |
ArrayView is a simple data structure which provides a non-owning encapsulation of array data. That is, ArrayView is like Array without memory management.
The meaning of the template parameters is the same as in Array.
Value | The type of array elements. |
Device | The device where the array is allocated. This ensures the compile-time checks of correct pointers manipulation. It can be any class defined in the TNL::Devices namespace. |
Index | The indexing type. |
ArrayView provides access to array elements and general array operations same as Array, but it does not manage memory. The construction of an ArrayView does not make a memory allocation, but it can be bound to an existing portion of memory specified by a raw pointer, another ArrayView, or an Array. Similarly, ArrayView is not resizable, so there is no setSize method. Note that ArrayView does not own its data, so it does not deallocate it in the destructor.
Another important difference between ArrayView and Array is in the copy semantics. While the copy-constructor of Array makes a deep copy of the data, ArrayView makes only a shallow copy in the copy-constructor, i.e. it changes only its pointer and size. As a result, array views can be efficiently passed by value (even to device kernels) or captured by value in lambda functions (even in device lambda functions). Note that operator= in both ArrayView and Array still makes a deep copy of the data.
See also Array, Vector, VectorView.
using TNL::Containers::ArrayView< Value, Device, Index >::DeviceType = Device |
Device used to run operations on the array.
See TNL::Devices for the available options.
|
default |
Constructs an empty array view.
This method can be called from device kernels.
__cuda_callable__ TNL::Containers::ArrayView< Value, Device, Index >::ArrayView | ( | ValueType * | data, |
IndexType | size ) |
Constructs an array view by binding to the given data pointer and size.
This method can be called from device kernels.
data | The data pointer to be bound. |
size | The number of elements in the array view. |
|
default |
Shallow copy constructor.
This method can be called from device kernels.
view | The array view to be copied. |
|
inline |
"Templated" shallow copy constructor.
This method can be called from device kernels.
Value_ | The template parameter can be any cv-qualified variant of ValueType. |
view | The array view to be copied. |
|
defaultnoexcept |
Move constructor for initialization from rvalues.
This method can be called from device kernels.
view | The array view to be moved. |
__cuda_callable__ void TNL::Containers::ArrayView< Value, Device, Index >::bind | ( | ArrayView< Value, Device, Index > | view | ) |
Method for rebinding (reinitialization) using another array view.
Note that you can also bind directly to an Array instance and other objects whose type is implicitly convertible to ArrayView.
This method can be called from device kernels.
view | The array view to be bound. |
__cuda_callable__ void TNL::Containers::ArrayView< Value, Device, Index >::bind | ( | ValueType * | data, |
IndexType | size ) |
Method for rebinding (reinitialization) using a raw pointer and size.
This method can be called from device kernels.
data | The data pointer to be bound to the array view. |
size | The number of elements in the array view. |
|
nodiscard |
Returns true if the current array view size is zero.
This method can be called from device kernels.
void TNL::Containers::ArrayView< Value, Device, Index >::forAllElements | ( | Function && | f | ) |
Process the lambda function f for each array element.
The lambda function is supposed to be declared as
where
This is performed at the same place where the array is allocated, i.e. it is efficient even on GPU.
f | The lambda function to be processed. |
void TNL::Containers::ArrayView< Value, Device, Index >::forAllElements | ( | Function && | f | ) | const |
Process the lambda function f for each array element for constant instances.
The lambda function is supposed to be declared as
where
This is performed at the same place where the array is allocated, i.e. it is efficient even on GPU.
f | The lambda function to be processed. |
void TNL::Containers::ArrayView< Value, Device, Index >::forElements | ( | IndexType | begin, |
IndexType | end, | ||
Function && | f ) |
Process the lambda function f for each array element in interval [ begin, end).
The lambda function is supposed to be declared as
where
This is performed at the same place where the array is allocated, i.e. it is efficient even on GPU.
begin | The beginning of the array elements interval. |
end | The end of the array elements interval. |
f | The lambda function to be processed. |
void TNL::Containers::ArrayView< Value, Device, Index >::forElements | ( | IndexType | begin, |
IndexType | end, | ||
Function && | f ) const |
Process the lambda function f for each array element in interval [ begin, end) for constant instances of the array.
The lambda function is supposed to be declared as
where
This is performed at the same place where the array is allocated, i.e. it is efficient even on GPU.
begin | The beginning of the array elements interval. |
end | The end of the array elements interval. |
f | The lambda function to be processed. |
|
nodiscard |
Returns a raw pointer to the data.
Use this method in algorithms where you want to emphasize that C-style array pointer is required.
This method can be called from device kernels.
|
nodiscard |
Returns a const-qualified raw pointer to the data.
Use this method in algorithms where you want to emphasize that C-style array pointer is required.
This method can be called from device kernels.
|
nodiscard |
Returns a non-modifiable view of the array view.
By default, a view for the whole array is returned. If begin or end is set to a non-zero value, a view only for the sub-interval [begin, end)
is returned.
begin | The beginning of the array view sub-interval. It is 0 by default. |
end | The end of the array view sub-interval. The default value is 0 which is, however, replaced with the array size. |
|
nodiscard |
Returns a raw pointer to the data.
This method can be called from device kernels.
|
nodiscard |
Returns a const-qualified raw pointer to the data.
This method can be called from device kernels.
|
nodiscard |
Returns the value of the i-th element.
This method can be called from both the host system and the device where the array is allocated.
i | The index of the element to be returned. |
|
nodiscard |
Returns the current size of the array view.
This method can be called from device kernels.
|
nodiscard |
Returns a modifiable view of the array view.
By default, a view for the whole array is returned. If begin or end is set to a non-zero value, a view only for the sub-interval [begin, end)
is returned.
begin | The beginning of the array view sub-interval. It is 0 by default. |
end | The end of the array view sub-interval. The default value is 0 which is, however, replaced with the array size. |
void TNL::Containers::ArrayView< Value, Device, Index >::load | ( | const std::string & | fileName | ) |
Method for loading the data from a binary file fileName.
fileName | The input file name. |
|
nodiscard |
Compares the array view with another array-like container.
ArrayT | The type of the parameter can be any array-like container, e.g. Array, ArrayView, Vector, VectorView, etc. |
array | Reference to the array-like container. |
|
nodiscard |
Accesses the i-th element of the array.
Equivalent to operator[], with the same notes and caveats.
|
nodiscard |
Accesses the i-th element of the array.
Equivalent to operator[], with the same notes and caveats.
ArrayView< Value, Device, Index > & TNL::Containers::ArrayView< Value, Device, Index >::operator= | ( | const ArrayView< Value, Device, Index > & | view | ) |
Deep copy assignment operator for copying data from another array view.
view | Reference to the source array view. |
ArrayView & TNL::Containers::ArrayView< Value, Device, Index >::operator= | ( | const T & | data | ) |
Assigns either array-like container or a single value.
If T is an array type, e.g. Array, ArrayView, StaticArray, Vector, VectorView, or StaticVector, the elements from data are copied into this array view. Otherwise, if it is a type convertible to ValueType, all array elements are set to the value data.
T | The type of the source array or value. |
data | Reference to the source array or value. |
|
nodiscard |
Compares the array view with another array-like container.
ArrayT | The type of the parameter can be any array-like container, e.g. Array, ArrayView, Vector, VectorView, etc. |
array | Reference to the array-like container. |
|
nodiscard |
Accesses the i-th element of the array view.
This method can be called only from the device which has direct access to the memory space where the data was allocated. For example, if the data was allocated in the host memory, it can be called only from host, and if the data was allocated in the device memory, it can be called only from device kernels. If NDEBUG is not defined, assertions inside this methods performs runtime checks for cross-device memory accesses which lead to segmentation fault. If you need to do just a pointer arithmetics use getData instead.
i | The index of the element to be accessed. |
|
nodiscard |
Accesses the i-th element of the array view.
This method can be called only from the device which has direct access to the memory space where the data was allocated. For example, if the data was allocated in the host memory, it can be called only from host, and if the data was allocated in the device memory, it can be called only from device kernels. If NDEBUG is not defined, assertions inside this methods performs runtime checks for cross-device memory accesses which lead to segmentation fault. If you need to do just a pointer arithmetics use getData instead.
i | The index of the element to be accessed. |
void TNL::Containers::ArrayView< Value, Device, Index >::save | ( | const std::string & | fileName | ) | const |
Method for saving the data to a binary file fileName.
fileName | The output file name. |
__cuda_callable__ void TNL::Containers::ArrayView< Value, Device, Index >::setElement | ( | IndexType | i, |
ValueType | value ) |
Sets the value of the i-th element to v.
This method can be called from both the host system and the device where the array is allocated.
i | The index of the element to be set. |
value | The new value of the element. |
void TNL::Containers::ArrayView< Value, Device, Index >::setValue | ( | ValueType | value, |
IndexType | begin = 0, | ||
IndexType | end = 0 ) |
Sets elements of the array view to given value.
By default, all array view elements are set to the given value. If begin or end is set to a non-zero value, only elements in the sub-interval [begin, end)
are set.
value | The new value for the array view elements. |
begin | The beginning of the array view sub-interval. It is 0 by default. |
end | The end of the array view sub-interval. The default value is 0 which is, however, replaced with the array view size. |
__cuda_callable__ void TNL::Containers::ArrayView< Value, Device, Index >::swap | ( | ArrayView< Value, Device, Index > & | view | ) |
Swaps this array view with another.
Swapping is done in a shallow way, i.e. only pointers and sizes are swapped.
view | The array view to be swapped with this array view. |