|
__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.
|
|
template<typename Value, typename Device = Devices::Host, typename Index = int>
class TNL::Containers::ArrayView< Value, Device, Index >
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.
- Template Parameters
-
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.
- Example
#include <iostream>
#include <TNL/Containers/Array.h>
#include <TNL/Containers/ArrayView.h>
template< typename Device >
void
arrayViewExample()
{
const int size = 10;
using IndexType =
typename ArrayType::IndexType;
ArrayType a1( size ), a2( size );
for( int i = 0; i < size; i++ )
a2_view = 0;
ArrayType a3( size );
{
value = 2 * i;
};
for( int i = 0; i < size; i++ )
File(
"a1_view.tnl", std::ios_base::out ) << a1_view;
File(
"a1_view.tnl", std::ios_base::in ) >> a2_view;
if( a2_view != a1_view )
}
int
main()
{
arrayViewExample< Devices::Host >();
#ifdef __CUDACC__
arrayViewExample< Devices::Cuda >();
#endif
}
#define __cuda_callable__
Definition Macros.h:49
ArrayView is a simple data structure which provides a non-owning encapsulation of array data....
Definition ArrayView.h:55
__cuda_callable__ void swap(ArrayView &view)
Swaps this array view with another.
Definition ArrayView.hpp:111
void forAllElements(Function &&f)
Process the lambda function f for each array element.
Definition ArrayView.hpp:312
__cuda_callable__ void setElement(IndexType i, ValueType value)
Sets the value of the i-th element to v.
Definition ArrayView.hpp:177
__cuda_callable__ ViewType getView(IndexType begin=0, IndexType end=0)
Returns a modifiable view of the array view.
Definition ArrayView.hpp:58
__cuda_callable__ ValueType getElement(IndexType i) const
Returns the value of the i-th element.
Definition ArrayView.hpp:187
Index IndexType
Type being used for the array elements indexing.
Definition ArrayView.h:72
Array is responsible for memory management, access to array elements, and general array operations.
Definition Array.h:64
This class serves for binary IO. It allows to do IO even for data allocated on GPU together with on-t...
Definition File.h:25
The main TNL namespace.
Definition AtomicOperations.h:9
- Output
The first test runs on CPU ...
a2_view = [ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 ]
The second test runs on GPU ...
a2_view = [ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 ]
template<typename Value , typename Device = Devices::Host, typename Index = int>
template<typename T , typename... , typename = std::enable_if_t< std::is_convertible_v< T, ValueType > || IsArrayType< T >::value >>
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.
- Template Parameters
-
T | The type of the source array or value. |
- Parameters
-
data | Reference to the source array or value. |
- Returns
- Reference to this array view.