MU Library
mu::NDArray< T > Class Template Reference

NDArray is a class that can be used in both host and device code. NDArray is automatically divided into multiple NDArrayViews for each task. More...

#include "mu/ndarray/ndarray.hpp"

Public Types

using ShapeArray = std::array< std::int64_t, MAX_DIMS >
 
using StridesArray = std::array< std::int64_t, MAX_DIMS >
 

Public Member Functions

 NDArray ()=default
 
 NDArray (T *data, std::initializer_list< int64_t > shape)
 
 NDArray (T *data, const ShapeArray &shape)
 
 NDArray (void *data, const ShapeArray &shape, std::int64_t elemSize)
 
 NDArray (void *data, const ShapeArray &shape, std::int64_t elemSize, std::int64_t dims)
 
 ~NDArray ()=default
 
T * data ()
 
const T * data () const
 
void setData (void *data)
 
const ShapeArray & shape () const
 
const StridesArray & strides () const
 
std::int64_t dims () const
 
std::int64_t elemSize () const
 
std::int64_t size () const
 
std::int64_t numElements () const
 
template<typename... Idxs>
T & operator() (Idxs... idxs)
 
template<typename... Idxs>
const T & operator() (Idxs... idxs) const
 
NDArray< T > operator[] (std::int64_t idx)
 
NDArray< const T > operator[] (std::int64_t idx) const
 
template<typename U = T>
 operator typename std::enable_if_t< std::is_same_v< U, std::remove_const_t< T >>, T & > ()
 
 operator const T & () const
 
T & scalar ()
 
const T & scalar () const
 
template<typename... Idxs>
T & at (Idxs... idxs)
 
template<typename... Idxs>
const T & at (Idxs... idxs) const
 

Static Public Attributes

static constexpr std::int64_t MAX_DIMS = 10
 

Detailed Description

template<typename T>
class mu::NDArray< T >

NDArray is a class that can be used in both host and device code. NDArray is automatically divided into multiple NDArrayViews for each task.

Definition at line 22 of file ndarray.hpp.

Member Typedef Documentation

◆ ShapeArray

template<typename T >
using mu::NDArray< T >::ShapeArray = std::array<std::int64_t, MAX_DIMS>

Definition at line 26 of file ndarray.hpp.

◆ StridesArray

template<typename T >
using mu::NDArray< T >::StridesArray = std::array<std::int64_t, MAX_DIMS>

Definition at line 27 of file ndarray.hpp.

Constructor & Destructor Documentation

◆ NDArray() [1/5]

template<typename T >
mu::NDArray< T >::NDArray ( )
default

◆ NDArray() [2/5]

template<typename T >
mu::NDArray< T >::NDArray ( T *  data,
std::initializer_list< int64_t >  shape 
)
inline

Definition at line 30 of file ndarray.hpp.

31  : data_(const_cast<void*>(reinterpret_cast<const void*>(data))),
32  dims_(std::min<std::int64_t>(static_cast<std::int64_t>(shape.size()), MAX_DIMS)),
33  elemSize_(sizeof(T))
34  {
35  std::fill(shape_.begin(), shape_.end(), 0);
36  std::fill(strides_.begin(), strides_.end(), 0);
37  // Entries past MAX_DIMS are dropped, since device code cannot throw.
38  auto last = shape.size() > static_cast<std::size_t>(MAX_DIMS) ? shape.begin() + MAX_DIMS : shape.end();
39  std::copy(shape.begin(), last, shape_.begin());
40  calculateSizeAndStrides();
41  }
T * data()
Definition: ndarray.hpp:73
const ShapeArray & shape() const
Definition: ndarray.hpp:87
static constexpr std::int64_t MAX_DIMS
Definition: ndarray.hpp:25

◆ NDArray() [3/5]

template<typename T >
mu::NDArray< T >::NDArray ( T *  data,
const ShapeArray &  shape 
)
inline

Definition at line 47 of file ndarray.hpp.

48  : NDArray(const_cast<void*>(static_cast<const void*>(data)), shape, sizeof(T), dimsOf(shape))
49  {
50  }
NDArray()=default

◆ NDArray() [4/5]

template<typename T >
mu::NDArray< T >::NDArray ( void *  data,
const ShapeArray &  shape,
std::int64_t  elemSize 
)
inline

Definition at line 52 of file ndarray.hpp.

53  : NDArray(data, shape, elemSize, dimsOf(shape))
54  {
55  }
std::int64_t elemSize() const
Definition: ndarray.hpp:99

◆ NDArray() [5/5]

template<typename T >
mu::NDArray< T >::NDArray ( void *  data,
const ShapeArray &  shape,
std::int64_t  elemSize,
std::int64_t  dims 
)
inline

Definition at line 60 of file ndarray.hpp.

61  : data_(data),
62  dims_(std::clamp<std::int64_t>(dims, 0, MAX_DIMS)),
63  elemSize_(elemSize)
64  {
65  std::fill(shape_.begin(), shape_.end(), 0);
66  std::fill(strides_.begin(), strides_.end(), 0);
67  std::copy(shape.begin(), shape.begin() + dims_, shape_.begin());
68  calculateSizeAndStrides();
69  }
std::int64_t dims() const
Definition: ndarray.hpp:95

◆ ~NDArray()

template<typename T >
mu::NDArray< T >::~NDArray ( )
default

Member Function Documentation

◆ at() [1/2]

template<typename T >
template<typename... Idxs>
T& mu::NDArray< T >::at ( Idxs...  idxs)
inline

Definition at line 190 of file ndarray.hpp.

191  {
192  return const_cast<T&>(static_cast<const NDArray&>(*this).at(idxs...));
193  }

◆ at() [2/2]

template<typename T >
template<typename... Idxs>
const T& mu::NDArray< T >::at ( Idxs...  idxs) const
inline

Definition at line 196 of file ndarray.hpp.

197  {
198  std::array<std::int64_t, MAX_DIMS> indices = {idxs...};
199  if (sizeof...(Idxs) != static_cast<std::size_t>(dims_))
200  {
201  // Return reference to first element as fallback
202  return *reinterpret_cast<const T*>(data_);
203  }
204  for (std::size_t i = 0; i < indices.size(); ++i)
205  {
206  if (indices[i] < 0 || indices[i] >= shape_[i])
207  {
208  // Return reference to first element as fallback
209  return *reinterpret_cast<const T*>(data_);
210  }
211  }
212  return operator()(idxs...);
213  }
T & operator()(Idxs... idxs)
Definition: ndarray.hpp:113

◆ data() [1/2]

template<typename T >
T* mu::NDArray< T >::data ( )
inline

Definition at line 73 of file ndarray.hpp.

74  {
75  return reinterpret_cast<T*>(data_);
76  }

◆ data() [2/2]

template<typename T >
const T* mu::NDArray< T >::data ( ) const
inline

Definition at line 77 of file ndarray.hpp.

78  {
79  return reinterpret_cast<const T*>(data_);
80  }

◆ dims()

template<typename T >
std::int64_t mu::NDArray< T >::dims ( ) const
inline

Definition at line 95 of file ndarray.hpp.

96  {
97  return dims_;
98  }

◆ elemSize()

template<typename T >
std::int64_t mu::NDArray< T >::elemSize ( ) const
inline

Definition at line 99 of file ndarray.hpp.

100  {
101  return elemSize_;
102  }

◆ numElements()

template<typename T >
std::int64_t mu::NDArray< T >::numElements ( ) const
inline

Definition at line 107 of file ndarray.hpp.

108  {
109  return numElements_;
110  }

◆ operator const T &()

template<typename T >
mu::NDArray< T >::operator const T & ( ) const
inline

Definition at line 162 of file ndarray.hpp.

163  {
164  if (dims_ != 0)
165  {
166  // Return reference to first element as fallback
167  return *reinterpret_cast<const T*>(data_);
168  }
169  return *reinterpret_cast<const T*>(data_);
170  }

◆ operator typename std::enable_if_t< std::is_same_v< U, std::remove_const_t< T >>, T & >()

template<typename T >
template<typename U = T>
mu::NDArray< T >::operator typename std::enable_if_t< std::is_same_v< U, std::remove_const_t< T >>, T & > ( )
inline

Definition at line 152 of file ndarray.hpp.

153  {
154  if (dims_ != 0)
155  {
156  // Return reference to first element as fallback
157  return *reinterpret_cast<T*>(data_);
158  }
159  return *reinterpret_cast<T*>(data_);
160  }

◆ operator()() [1/2]

template<typename T >
template<typename... Idxs>
T& mu::NDArray< T >::operator() ( Idxs...  idxs)
inline

Definition at line 113 of file ndarray.hpp.

114  {
115  return const_cast<T&>(static_cast<const NDArray&>(*this)(idxs...));
116  }

◆ operator()() [2/2]

template<typename T >
template<typename... Idxs>
const T& mu::NDArray< T >::operator() ( Idxs...  idxs) const
inline

Definition at line 119 of file ndarray.hpp.

120  {
121  return reinterpret_cast<const T*>(data_)[computeOffset({idxs...})];
122  }

◆ operator[]() [1/2]

template<typename T >
NDArray<T> mu::NDArray< T >::operator[] ( std::int64_t  idx)
inline

Definition at line 126 of file ndarray.hpp.

127  {
128  auto const_result = static_cast<const NDArray&>(*this)[idx];
129  return NDArray<T>(
130  const_cast<void*>(static_cast<const void*>(const_result.data())),
131  const_result.shape(),
132  const_result.elemSize(),
133  const_result.dims());
134  }

◆ operator[]() [2/2]

template<typename T >
NDArray<const T> mu::NDArray< T >::operator[] ( std::int64_t  idx) const
inline

Definition at line 136 of file ndarray.hpp.

137  {
138  ShapeArray new_shape;
139  std::fill(new_shape.begin(), new_shape.end(), 0);
140  std::copy(shape_.begin() + 1, shape_.end(), new_shape.begin());
141 
142  const T* subData = reinterpret_cast<const T*>(data_) + idx * strides_[0];
143  return NDArray<const T>(
144  const_cast<void*>(static_cast<const void*>(subData)),
145  new_shape,
146  sizeof(T),
147  dims_ > 0 ? dims_ - 1 : 0);
148  }
std::array< std::int64_t, MAX_DIMS > ShapeArray
Definition: ndarray.hpp:26

◆ scalar() [1/2]

template<typename T >
T& mu::NDArray< T >::scalar ( )
inline

Definition at line 173 of file ndarray.hpp.

174  {
175  return const_cast<T&>(static_cast<const NDArray&>(*this).scalar());
176  }

◆ scalar() [2/2]

template<typename T >
const T& mu::NDArray< T >::scalar ( ) const
inline

Definition at line 178 of file ndarray.hpp.

179  {
180  if (dims_ != 0)
181  {
182  // Return reference to first element as fallback
183  return *reinterpret_cast<const T*>(data_);
184  }
185  return *reinterpret_cast<const T*>(data_);
186  }

◆ setData()

template<typename T >
void mu::NDArray< T >::setData ( void *  data)
inline

Definition at line 82 of file ndarray.hpp.

83  {
84  data_ = data;
85  }

◆ shape()

template<typename T >
const ShapeArray& mu::NDArray< T >::shape ( ) const
inline

Definition at line 87 of file ndarray.hpp.

88  {
89  return shape_;
90  }

◆ size()

template<typename T >
std::int64_t mu::NDArray< T >::size ( ) const
inline

Definition at line 103 of file ndarray.hpp.

104  {
105  return size_;
106  }

◆ strides()

template<typename T >
const StridesArray& mu::NDArray< T >::strides ( ) const
inline

Definition at line 91 of file ndarray.hpp.

92  {
93  return strides_;
94  }

Member Data Documentation

◆ MAX_DIMS

template<typename T >
constexpr std::int64_t mu::NDArray< T >::MAX_DIMS = 10
staticconstexpr

Definition at line 25 of file ndarray.hpp.


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