Template Numerical Library: TNL::Containers::ArrayView< Value, Device, Index > Class Template Reference

Template Numerical Library version\ main:0b2c40fb1

Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Attributes | List of all members
TNL::Containers::ArrayView< Value, Device, Index > Class Template Reference

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>`

Inheritance diagram for TNL::Containers::ArrayView< Value, Device, Index >:
[legend]

## 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 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) noexcept
| Swaps this array view with another.

## Protected Attributes
ValueType * | data = nullptr
| Pointer to the data.
IndexType | size = 0
| Array view size.

## Detailed Description

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>
  using namespace TNL;
  /***
  * The following works for any device (CPU, GPU ...).
  */
  template< typename Device >
  void
  arrayViewExample()
  {
  const int size = 10;
  using ArrayType = Containers::Array< int, Device >;
  using IndexType = typename ArrayType::IndexType;
  using ViewType = Containers::ArrayView< int, Device >;
  ArrayType a1( size );
  ArrayType a2( size );
  ViewType a1_view = a1.getView();
  ViewType a2_view = a2.getView();
  /***
  * You may initiate the array view using setElement
  */
  for( int i = 0; i < size; i++ )
  a1_view.setElement( i, i );
  /***
  * You may also assign value to all array view elements ...
  */
  a2_view = 0;
  /***
  * More efficient way of array view elements manipulation is with the lambda functions
  */
  ArrayType a3( size );
  ViewType a3_view = a3.getView();
  auto f1 = [] __cuda_callable__( IndexType i, int& value )
  {
  value = 2 * i;
  };
  a3_view.forAllElements( f1 );
  for( int i = 0; i < size; i++ )
  if( a3_view.getElement( i ) != 2 * i )
  std::cerr << "Something is wrong!!!\n";
  /***
  * You may swap array view data with the swap method.
  */
  a1_view.swap( a3_view );
  /***
  * You may save it to file and load again
  */
  File( "a1_view.tnl", std::ios_base::out ) << a1_view;
  File( "a1_view.tnl", std::ios_base::in ) >> a2_view;
  std::remove( "a1_view.tnl" );
  if( a2_view != a1_view )
  std::cerr << "Something is wrong!!!\n";
  std::cout << "a2_view = " << a2_view << '\n';
  }
  int
  main()
  {
  std::cout << "The first test runs on CPU ...\n";
  arrayViewExample< Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "The second test runs on GPU ...\n";
  arrayViewExample< Devices::Cuda >();
  #endif
  }
  __cuda_callable__
  #define __cuda_callable__
  Definition Macros.h:49
  std::cerr
  TNL::Containers::ArrayView
  ArrayView is a simple data structure which provides a non-owning encapsulation of array data....
  Definition ArrayView.h:55
  TNL::Containers::ArrayView::size
  IndexType size
  Array view size.
  Definition ArrayView.h:583
  TNL::Containers::ArrayView::forAllElements
  void forAllElements(Function &&f)
  Process the lambda function f for each array element.
  TNL::Containers::ArrayView::setElement
  __cuda_callable__ void setElement(IndexType i, ValueType value)
  Sets the value of the i-th element to v.
  TNL::Containers::ArrayView::getElement
  __cuda_callable__ ValueType getElement(IndexType i) const
  Returns the value of the i-th element.
  TNL::Containers::ArrayView::ViewType
  ArrayView< Value, Device, Index > ViewType
  Compatible ArrayView type.
  Definition ArrayView.h:77
  TNL::Containers::ArrayView::IndexType
  Index IndexType
  Type being used for the array elements indexing.
  Definition ArrayView.h:72
  TNL::Containers::ArrayView::swap
  __cuda_callable__ void swap(ArrayView &view) noexcept
  Swaps this array view with another.
  TNL::Containers::Array
  Array is responsible for memory management, access to array elements, and general array operations.
  Definition Array.h:65
  TNL::File
  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
  TNL
  The main TNL namespace.
  Definition AtomicOperations.h:15
  std::remove
  T remove(T... args)

Output
  The first test runs on CPU ...
  a2_view = [ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 ]

## Member Typedef Documentation

## ◆ DeviceType

template<typename Value, typename Device = Devices::Host, typename Index = int>

using TNL::Containers::ArrayView< Value, Device, Index >::DeviceType = Device

Device used to run operations on the array.

See TNL::Devices for the available options.

## Constructor & Destructor Documentation

## ◆ ArrayView() [1/5]

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ TNL::Containers::ArrayView< Value, Device, Index >::ArrayView | ( |  | ) |
| default

Constructs an empty array view.

This method can be called from device kernels.

## ◆ ArrayView() [2/5]

template<typename Value, typename Device = Devices::Host, typename Index = int>

__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.

Parameters

  data | The data pointer to be bound.
  size | The number of elements in the array view.

## ◆ ArrayView() [3/5]

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ TNL::Containers::ArrayView< Value, Device, Index >::ArrayView | ( | const ArrayView< Value, Device, Index > & | view | ) |
| default

Shallow copy constructor.

This method can be called from device kernels.

Parameters

  view | The array view to be copied.

## ◆ ArrayView() [4/5]

template<typename Value, typename Device = Devices::Host, typename Index = int>
template<typename Value_>

__cuda_callable__ TNL::Containers::ArrayView< Value, Device, Index >::ArrayView | ( | const ArrayView< Value_, Device, Index > & | view | ) |
| inline

"Templated" shallow copy constructor.

This method can be called from device kernels.

Template Parameters

  Value_ | The template parameter can be any cv-qualified variant of ValueType.

Parameters

  view | The array view to be copied.

## ◆ ArrayView() [5/5]

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ TNL::Containers::ArrayView< Value, Device, Index >::ArrayView | ( | ArrayView< Value, Device, Index > && | view | ) |
| defaultnoexcept

Move constructor for initialization from rvalues.

This method can be called from device kernels.

Parameters

  view | The array view to be moved.

## Member Function Documentation

## ◆ bind() [1/2]

template<typename Value, typename Device = Devices::Host, typename Index = int>

__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.

Parameters

  view | The array view to be bound.

## ◆ bind() [2/2]

template<typename Value, typename Device = Devices::Host, typename Index = int>

__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.

Parameters

  data | The data pointer to be bound to the array view.
  size | The number of elements in the array view.

## ◆ empty()

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ bool TNL::Containers::ArrayView< Value, Device, Index >::empty | ( |  | ) | const
| nodiscard

Returns true if the current array view size is zero.

This method can be called from device kernels.

## ◆ forAllElements() [1/2]

template<typename Value, typename Device = Devices::Host, typename Index = int>
template<typename Function>

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

f( IndexType elementIdx, ValueType& elementValue )
TNL::Containers::ArrayView::ValueType
Value ValueType
Type of elements stored in this array.
Definition ArrayView.h:60

where

- elementIdx is an index of the array element being currently processed
- elementValue is a value of the array element being currently processed

This is performed at the same place where the array is allocated, i.e. it is efficient even on GPU.

Parameters

  f | The lambda function to be processed.

Example
  #include <iostream>
  #include <TNL/Containers/Array.h>
  #include <TNL/Containers/ArrayView.h>
  using namespace TNL;
  template< typename Device >
  void
  forElementsExample()
  {
  /****
  * Create new arrays
  */
  const int size = 10;
  Containers::Array< float, Device > a( size );
  Containers::Array< float, Device > b( size );
  b = 0;
  /****
  * Create an ArrayView and use it for initiation of elements of array `a`
  */
  auto a_view = a.getView();
  a_view.forAllElements(
  [] __cuda_callable__( int i, float& value )
  {
  value = i;
  } );
  /****
  * Initiate elements of array `b` with indexes 0-4 using `a_view`
  */
  b.getView().forElements(
  0,
  5,
  [ = ] __cuda_callable__( int i, float& value )
  {
  value = a_view[ i ] + 4.0;
  } );
  /****
  * Print the results
  */
  std::cout << " a = " << a << '\n';
  std::cout << " b = " << b << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Running example on the host system:\n";
  forElementsExample< Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Running example on the CUDA device:\n";
  forElementsExample< Devices::Cuda >();
  #endif
  }

Output
  Running example on the host system:
  a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  b = [ 4, 5, 6, 7, 8, 0, 0, 0, 0, 0 ]

## ◆ forAllElements() [2/2]

template<typename Value, typename Device = Devices::Host, typename Index = int>
template<typename Function>

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

f( IndexType elementIdx, const ValueType& elementValue )

where

- elementIdx is an index of the array element being currently processed
- elementValue is a value of the array element being currently processed

This is performed at the same place where the array is allocated, i.e. it is efficient even on GPU.

Parameters

  f | The lambda function to be processed.

Example
  #include <iostream>
  #include <TNL/Containers/Array.h>
  #include <TNL/Containers/ArrayView.h>
  using namespace TNL;
  template< typename Device >
  void
  forElementsExample()
  {
  /****
  * Create new arrays
  */
  const int size = 10;
  Containers::Array< float, Device > a( size );
  Containers::Array< float, Device > b( size );
  b = 0;
  /****
  * Create an ArrayView and use it for initiation of elements of array `a`
  */
  auto a_view = a.getView();
  a_view.forAllElements(
  [] __cuda_callable__( int i, float& value )
  {
  value = i;
  } );
  /****
  * Initiate elements of array `b` with indexes 0-4 using `a_view`
  */
  b.getView().forElements(
  0,
  5,
  [ = ] __cuda_callable__( int i, float& value )
  {
  value = a_view[ i ] + 4.0;
  } );
  /****
  * Print the results
  */
  std::cout << " a = " << a << '\n';
  std::cout << " b = " << b << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Running example on the host system:\n";
  forElementsExample< Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Running example on the CUDA device:\n";
  forElementsExample< Devices::Cuda >();
  #endif
  }

Output
  Running example on the host system:
  a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  b = [ 4, 5, 6, 7, 8, 0, 0, 0, 0, 0 ]

## ◆ forElements() [1/2]

template<typename Value, typename Device = Devices::Host, typename Index = int>
template<typename Function>

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

f( IndexType elementIdx, ValueType& elementValue )

where

- elementIdx is an index of the array element being currently processed
- elementValue is a value of the array element being currently processed

This is performed at the same place where the array is allocated, i.e. it is efficient even on GPU.

Parameters

  begin | The beginning of the array elements interval.
  end | The end of the array elements interval.
  f | The lambda function to be processed.

Example
  #include <iostream>
  #include <TNL/Containers/Array.h>
  #include <TNL/Containers/ArrayView.h>
  using namespace TNL;
  template< typename Device >
  void
  forElementsExample()
  {
  /****
  * Create new arrays
  */
  const int size = 10;
  Containers::Array< float, Device > a( size );
  Containers::Array< float, Device > b( size );
  b = 0;
  /****
  * Create an ArrayView and use it for initiation of elements of array `a`
  */
  auto a_view = a.getView();
  a_view.forAllElements(
  [] __cuda_callable__( int i, float& value )
  {
  value = i;
  } );
  /****
  * Initiate elements of array `b` with indexes 0-4 using `a_view`
  */
  b.getView().forElements(
  0,
  5,
  [ = ] __cuda_callable__( int i, float& value )
  {
  value = a_view[ i ] + 4.0;
  } );
  /****
  * Print the results
  */
  std::cout << " a = " << a << '\n';
  std::cout << " b = " << b << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Running example on the host system:\n";
  forElementsExample< Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Running example on the CUDA device:\n";
  forElementsExample< Devices::Cuda >();
  #endif
  }

Output
  Running example on the host system:
  a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  b = [ 4, 5, 6, 7, 8, 0, 0, 0, 0, 0 ]

## ◆ forElements() [2/2]

template<typename Value, typename Device = Devices::Host, typename Index = int>
template<typename Function>

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

f( IndexType elementIdx, const ValueType& elementValue )

where

- elementIdx is an index of the array element being currently processed
- elementValue is a value of the array element being currently processed

This is performed at the same place where the array is allocated, i.e. it is efficient even on GPU.

Parameters

  begin | The beginning of the array elements interval.
  end | The end of the array elements interval.
  f | The lambda function to be processed.

Example
  #include <iostream>
  #include <TNL/Containers/Array.h>
  #include <TNL/Containers/ArrayView.h>
  using namespace TNL;
  template< typename Device >
  void
  forElementsExample()
  {
  /****
  * Create new arrays
  */
  const int size = 10;
  Containers::Array< float, Device > a( size );
  Containers::Array< float, Device > b( size );
  b = 0;
  /****
  * Create an ArrayView and use it for initiation of elements of array `a`
  */
  auto a_view = a.getView();
  a_view.forAllElements(
  [] __cuda_callable__( int i, float& value )
  {
  value = i;
  } );
  /****
  * Initiate elements of array `b` with indexes 0-4 using `a_view`
  */
  b.getView().forElements(
  0,
  5,
  [ = ] __cuda_callable__( int i, float& value )
  {
  value = a_view[ i ] + 4.0;
  } );
  /****
  * Print the results
  */
  std::cout << " a = " << a << '\n';
  std::cout << " b = " << b << '\n';
  }
  int
  main( int argc, char* argv[] )
  {
  std::cout << "Running example on the host system:\n";
  forElementsExample< Devices::Host >();
  #ifdef __CUDACC__
  std::cout << "Running example on the CUDA device:\n";
  forElementsExample< Devices::Cuda >();
  #endif
  }

Output
  Running example on the host system:
  a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  b = [ 4, 5, 6, 7, 8, 0, 0, 0, 0, 0 ]

## ◆ getArrayData() [1/2]

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ ValueType * TNL::Containers::ArrayView< Value, Device, Index >::getArrayData | ( |  | ) |
| 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.

## ◆ getArrayData() [2/2]

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ const ValueType * TNL::Containers::ArrayView< Value, Device, Index >::getArrayData | ( |  | ) | const
| 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.

## ◆ getConstView()

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ ConstViewType TNL::Containers::ArrayView< Value, Device, Index >::getConstView | ( | IndexType | begin = 0,
|  | IndexType | end = 0 ) const
| 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.

Parameters

  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.

## ◆ getData() [1/2]

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ ValueType * TNL::Containers::ArrayView< Value, Device, Index >::getData | ( |  | ) |
| nodiscard

Returns a raw pointer to the data.

This method can be called from device kernels.

## ◆ getData() [2/2]

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ const ValueType * TNL::Containers::ArrayView< Value, Device, Index >::getData | ( |  | ) | const
| nodiscard

Returns a const-qualified raw pointer to the data.

This method can be called from device kernels.

## ◆ getElement()

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ ValueType TNL::Containers::ArrayView< Value, Device, Index >::getElement | ( | IndexType | i | ) | const
| 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.

Parameters

  i | The index of the element to be returned.

## ◆ getSize()

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ IndexType TNL::Containers::ArrayView< Value, Device, Index >::getSize | ( |  | ) | const
| nodiscard

Returns the current size of the array view.

This method can be called from device kernels.

## ◆ getView()

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ ViewType TNL::Containers::ArrayView< Value, Device, Index >::getView | ( | IndexType | begin = 0,
|  | IndexType | end = 0 )
| 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.

Parameters

  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.

## ◆ load()

template<typename Value, typename Device = Devices::Host, typename Index = int>

void TNL::Containers::ArrayView< Value, Device, Index >::load | ( | const std::string & | fileName | ) |

Method for loading the data from a binary file fileName.

Parameters

  fileName | The input file name.

## ◆ operator!=()

template<typename Value, typename Device = Devices::Host, typename Index = int>
template<typename ArrayT>

bool TNL::Containers::ArrayView< Value, Device, Index >::operator!= | ( | const ArrayT & | array | ) | const
| nodiscard

Compares the array view with another array-like container.

Template Parameters

  ArrayT | The type of the parameter can be any array-like container, e.g. Array, ArrayView, Vector, VectorView, etc.

Parameters

  array | Reference to the array-like container.

Returns
  The negated result of operator==.

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

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ Value & TNL::Containers::ArrayView< Value, Device, Index >::operator() | ( | IndexType | i | ) |
| nodiscard

Accesses the i-th element of the array.

Equivalent to operator[], with the same notes and caveats.

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

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ const Value & TNL::Containers::ArrayView< Value, Device, Index >::operator() | ( | IndexType | i | ) | const
| nodiscard

Accesses the i-th element of the array.

Equivalent to operator[], with the same notes and caveats.

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

template<typename Value, typename Device = Devices::Host, typename Index = int>

ArrayView & TNL::Containers::ArrayView< Value, Device, Index >::operator= | ( | const ArrayView< Value, Device, Index > & | view | ) |

Deep copy assignment operator for copying data from another array view.

Parameters

  view | Reference to the source array view.

Returns
  Reference to this array view.

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

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 >>

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.

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.

## ◆ operator==()

template<typename Value, typename Device = Devices::Host, typename Index = int>
template<typename ArrayT>

bool TNL::Containers::ArrayView< Value, Device, Index >::operator== | ( | const ArrayT & | array | ) | const
| nodiscard

Compares the array view with another array-like container.

Template Parameters

  ArrayT | The type of the parameter can be any array-like container, e.g. Array, ArrayView, Vector, VectorView, etc.

Parameters

  array | Reference to the array-like container.

Returns
  true if the array view is element-wise equal to the array-like container and false otherwise.

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

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ Value & TNL::Containers::ArrayView< Value, Device, Index >::operator[] | ( | IndexType | i | ) |
| 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.

Parameters

  i | The index of the element to be accessed.

Returns
  Reference to the i-th element.

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

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ const Value & TNL::Containers::ArrayView< Value, Device, Index >::operator[] | ( | IndexType | i | ) | const
| 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.

Parameters

  i | The index of the element to be accessed.

Returns
  Constant reference to the i-th element.

## ◆ save()

template<typename Value, typename Device = Devices::Host, typename Index = int>

void TNL::Containers::ArrayView< Value, Device, Index >::save | ( | const std::string & | fileName | ) | const

Method for saving the data to a binary file fileName.

Parameters

  fileName | The output file name.

## ◆ setElement()

template<typename Value, typename Device = Devices::Host, typename Index = int>

__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.

Parameters

  i | The index of the element to be set.
  value | The new value of the element.

## ◆ setValue()

template<typename Value, typename Device = Devices::Host, typename Index = int>

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.

Parameters

  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.

## ◆ swap()

template<typename Value, typename Device = Devices::Host, typename Index = int>

__cuda_callable__ void TNL::Containers::ArrayView< Value, Device, Index >::swap | ( | ArrayView< Value, Device, Index > & | view | ) |
| noexcept

Swaps this array view with another.

Swapping is done in a shallow way, i.e. only pointers and sizes are swapped.

Parameters

  view | The array view to be swapped with this array view.

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

- src/TNL/Containers/ArrayView.h