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 ()=default
 
T * data ()
 
const T * data () const
 
void setData (void *data)
 
const ShapeArrayshape () const
 
const StridesArraystrides () 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/4]

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

◆ NDArray() [2/4]

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_(shape.size()),
33  elemSize_(sizeof(T))
34  {
35  std::fill(shape_.begin(), shape_.end(), 0);
36  std::fill(strides_.begin(), strides_.end(), 0);
37  std::copy(shape.begin(), shape.end(), shape_.begin());
38  calculateSizeAndStrides();
39  }
T * data()
Definition: ndarray.hpp:79
const ShapeArray & shape() const
Definition: ndarray.hpp:93

◆ NDArray() [3/4]

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

Definition at line 41 of file ndarray.hpp.

42  : data_(const_cast<void*>(reinterpret_cast<const void*>(data))),
43  dims_(0),
44  elemSize_(sizeof(T))
45  {
46  std::fill(shape_.begin(), shape_.end(), 0);
47  std::fill(strides_.begin(), strides_.end(), 0);
48  for (std::size_t i = 0; i < MAX_DIMS; ++i)
49  {
50  if (shape[i] != 0)
51  {
52  shape_[i] = shape[i];
53  dims_++;
54  }
55  }
56  calculateSizeAndStrides();
57  }
static constexpr std::int64_t MAX_DIMS
Definition: ndarray.hpp:25

◆ NDArray() [4/4]

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

Definition at line 59 of file ndarray.hpp.

60  : data_(data),
61  dims_(0),
62  elemSize_(elemSize)
63  {
64  std::fill(shape_.begin(), shape_.end(), 0);
65  std::fill(strides_.begin(), strides_.end(), 0);
66  for (std::size_t i = 0; i < MAX_DIMS; ++i)
67  {
68  if (shape[i] != 0)
69  {
70  shape_[i] = shape[i];
71  dims_++;
72  }
73  }
74  calculateSizeAndStrides();
75  }
std::int64_t elemSize() const
Definition: ndarray.hpp:105

◆ ~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  }
NDArray()=default

◆ 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:119

◆ data() [1/2]

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

Definition at line 79 of file ndarray.hpp.

80  {
81  return reinterpret_cast<T*>(data_);
82  }

◆ data() [2/2]

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

Definition at line 83 of file ndarray.hpp.

84  {
85  return reinterpret_cast<const T*>(data_);
86  }

◆ dims()

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

Definition at line 101 of file ndarray.hpp.

102  {
103  return dims_;
104  }

◆ elemSize()

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

Definition at line 105 of file ndarray.hpp.

106  {
107  return elemSize_;
108  }

◆ numElements()

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

Definition at line 113 of file ndarray.hpp.

114  {
115  return numElements_;
116  }

◆ 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 119 of file ndarray.hpp.

120  {
121  return const_cast<T&>(static_cast<const NDArray&>(*this)(idxs...));
122  }

◆ operator()() [2/2]

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

Definition at line 125 of file ndarray.hpp.

126  {
127  return reinterpret_cast<const T*>(data_)[computeOffset({idxs...})];
128  }

◆ operator[]() [1/2]

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

Definition at line 131 of file ndarray.hpp.

132  {
133  auto const_result = static_cast<const NDArray&>(*this)[idx];
134  return NDArray<T>(
135  const_cast<T*>(const_result.data()),
136  const_result.shape());
137  }

◆ operator[]() [2/2]

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

Definition at line 139 of file ndarray.hpp.

140  {
141  ShapeArray new_shape;
142  std::fill(new_shape.begin(), new_shape.end(), 0);
143  std::copy(shape_.begin() + 1, shape_.end(), new_shape.begin());
144 
145  return NDArray<const T>(
146  reinterpret_cast<const T*>(data_) + idx * strides_[0],
147  new_shape);
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 88 of file ndarray.hpp.

89  {
90  data_ = data;
91  }

◆ shape()

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

Definition at line 93 of file ndarray.hpp.

94  {
95  return shape_;
96  }

◆ size()

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

Definition at line 109 of file ndarray.hpp.

110  {
111  return size_;
112  }

◆ strides()

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

Definition at line 97 of file ndarray.hpp.

98  {
99  return strides_;
100  }

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: