array_view.h

Class

template<typename T, int Dim>
class ArrayView

Public Functions

template<typename Indexable1, typename Indexable2>
inline ArrayView(T *ptr, const Indexable1 &shape, const Indexable2 &strides)

Construct from any types with subscript operator, i.e. vector/array

host only due to cuda/stl incompatibility

Parameters:
  • ptr[in] pointer to data

  • shape[in] arbitrary container of shape dimensions

  • strides[in] arbitrary container of stride offsets

template<typename ContainerT>
inline ArrayView(T *ptr, const ContainerT &shape)

Construct from any container type, deducing strides

host only due to cuda/stl incompatibility

Parameters:
  • ptr[in] pointer to data

  • shape[in] arbitrary container of shape dimensions

inline OSDK_FN ArrayView(T *ptr, const int (&shape)[Dim], const int (&strides)[Dim])

Construct from C arrays

device, host

Parameters:
  • ptr[in] pointer to data

  • shape[in] C array of shape dimensions

  • strides[in] C array of stride offsets

template<typename IntT>
inline OSDK_FN ArrayView(T *ptr, std::initializer_list<IntT> shape)

Construct from initializer list, deducing strides

device, host

Parameters:
  • ptr[in] pointer to data

  • shape[in] initializer_list of shape dimensions

inline OSDK_FN ArrayView(T *ptr, const int (&shape)[Dim])

Construct from C array, deducing strides

device, host

Parameters:
  • ptr[in] pointer to data

  • shape[in] C array of shape dimensions

template<typename OtherT, typename = std::enable_if_t<std::is_same<T, const OtherT>::value, void>>
inline OSDK_FN ArrayView(const ArrayView<OtherT, Dim> &other)

Below allows conversion from ArrayView to ConstArrayView but not the other way around

/code ArrayView4<int> a; ConstArrayView4<int> b = a; // compiles ArrayView4<int> c = b; // does not compile /endcode

Parameters:

other[in] ArrayView

template<typename... Args> inline OSDK_FN const T & operator() (Args &&... idx) const

Retrieve reference to value with subscript operator

Parameters:

idx[in] indices

Returns:

reference to value

template<typename... Args> inline OSDK_FN T & operator() (Args &&... idx)

Retrieve reference to value with subscript operator

Parameters:

idx[in] indices

Returns:

reference to value

template<typename... Args> inline OSDK_FN ArrayView< T, subview_dim< Args... >()> subview (Args... idx) const

Get a subview

Operates similarly to numpy ndarray bracket operator, returning a sliced, potentially sparse subview

The following two snippets are functionally equivalent

std::vector<int> data(100*100*100);
ArrayView3<int> view{data.data(), {100,100,100};
// get a slice of all elements in the first dimension with second
// and third pinned to 10 and 20 respectively
ArrayView1<int> subview = view.subview(keep(), 10, 20);

import numpy as np
arr = np.ndarray(shape=(100,100,100), dtype=np.int32)
subview = arr[:,10,20]
Parameters:

idx[in] pack of int indices or idx_range (keep())

Returns:

subview with different dimensions

template<typename ...Args>
inline ArrayView<T, sizeof...(Args)> reshape(Args... dims) const

Reshape array view to a different set of dimensions

Throws:
  • std::invalid_argument – on trying to reshape a sparse ArrayView

  • std::invalid_argument – on flattened dimension size not matching the original view size

Parameters:

dims[in] new dimensions

Returns:

reshaped ArrayView with new dimensions

inline OSDK_FN bool sparse () const

Check if the ArrayView is not contiguous

Returns:

true if sparse

inline const T *begin() const

Iterator functions below return basic pointers to data, which only works if the current array is not sparse.

Because we have no way of throwing if the array is sparse, these are explicitly host side at the moment, until we get to implementing sparse iterators

Returns:

pointer to data

inline const T *data() const

Get the underlying data pointer.

Returns:

pointer to data

Public Members

const int32_t strides[Dim]

C array of stride offsets

const int32_t shape[Dim]

C array of shape dimensions

Public Static Functions

template<typename... Args> static inline OSDK_FN constexpr int subview_dim ()

Templated utility to calculate subview dimensions at compile time

Template Parameters:

Args – parameter pack of indices or idx_range

Returns:

subview dimension