PXL
pxl::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 NDArray slices for each task. More...

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
 
template<typename U >
 NDArray (T *data, std::initializer_list< U > 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 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 pxl::NDArray< T >

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

Definition at line 24 of file ndarray.hpp.

Member Typedef Documentation

◆ ShapeArray

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

Definition at line 28 of file ndarray.hpp.

◆ StridesArray

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

Definition at line 29 of file ndarray.hpp.

Constructor & Destructor Documentation

◆ NDArray() [1/4]

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

◆ NDArray() [2/4]

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

Definition at line 33 of file ndarray.hpp.

34  : data_(const_cast<void*>(static_cast<const void*>(data))),
35  dims_(static_cast<std::int64_t>(shape.size())),
36  elemSize_(sizeof(T))
37  {
38  std::fill(shape_.begin(), shape_.end(), 0);
39  std::fill(strides_.begin(), strides_.end(), 0);
40  for (size_t i = 0; i < shape.size(); ++i)
41  {
42  shape_[i] = static_cast<std::int64_t>(*(shape.begin() + i));
43  }
44  calculateSizeAndStrides();
45  }
T * data()
Definition: ndarray.hpp:84
const ShapeArray & shape() const
Definition: ndarray.hpp:98

◆ NDArray() [3/4]

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

Definition at line 46 of file ndarray.hpp.

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

◆ NDArray() [4/4]

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

Definition at line 64 of file ndarray.hpp.

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

◆ ~NDArray()

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

Member Function Documentation

◆ at() [1/2]

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

Definition at line 214 of file ndarray.hpp.

215  {
216  return const_cast<T&>(static_cast<const NDArray&>(*this).at(idxs...));
217  }
NDArray()=default

◆ at() [2/2]

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

Definition at line 220 of file ndarray.hpp.

221  {
222  std::vector<std::int64_t> indices = {idxs...};
223  if (indices.size() != static_cast<size_t>(dims_))
224  {
225  throw std::out_of_range("Number of indices does not match ndarray dimensions");
226  }
227  for (size_t i = 0; i < indices.size(); ++i)
228  {
229  if (indices[i] < 0 || indices[i] >= shape_[i])
230  {
231  throw std::out_of_range("Index out of bounds");
232  }
233  }
234  return operator()(idxs...);
235  }
T & operator()(Idxs... idxs)
Definition: ndarray.hpp:124

◆ data() [1/2]

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

Definition at line 84 of file ndarray.hpp.

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

◆ data() [2/2]

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

Definition at line 88 of file ndarray.hpp.

89  {
90  return reinterpret_cast<const T*>(data_);
91  }

◆ dims()

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

Definition at line 106 of file ndarray.hpp.

107  {
108  return dims_;
109  }

◆ elemSize()

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

Definition at line 110 of file ndarray.hpp.

111  {
112  return elemSize_;
113  }

◆ numElements()

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

Definition at line 118 of file ndarray.hpp.

119  {
120  return numElements_;
121  }

◆ operator const T &()

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

Definition at line 188 of file ndarray.hpp.

189  {
190  if (dims_ != 0)
191  {
192  throw std::logic_error("Scalar conversion only available for 0-dimensional ndarrays");
193  }
194  return *reinterpret_cast<const T*>(data_);
195  }

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

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

Definition at line 179 of file ndarray.hpp.

180  {
181  if (dims_ != 0)
182  {
183  throw std::logic_error("Scalar conversion only available for 0-dimensional ndarrays");
184  }
185  return *reinterpret_cast<T*>(data_);
186  }

◆ operator()() [1/2]

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

Definition at line 124 of file ndarray.hpp.

125  {
126  return const_cast<T&>(static_cast<const NDArray&>(*this)(idxs...));
127  }

◆ operator()() [2/2]

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

Definition at line 130 of file ndarray.hpp.

131  {
132 #ifndef NDEBUG
133  // Debug mode: perform bounds checking
134  std::vector<std::int64_t> indices = {idxs...};
135  if (indices.size() != static_cast<size_t>(dims_))
136  {
137  throw std::out_of_range("Number of indices does not match ndarray dimensions");
138  }
139  for (size_t i = 0; i < indices.size(); ++i)
140  {
141  if (indices[i] < 0 || indices[i] >= shape_[i])
142  {
143  throw std::out_of_range("Index out of bounds");
144  }
145  }
146 #endif
147  return reinterpret_cast<const T*>(data_)[computeOffset({idxs...})];
148  }

◆ operator[]() [1/2]

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

Definition at line 151 of file ndarray.hpp.

152  {
153  auto const_result = static_cast<const NDArray&>(*this)[idx];
154  return NDArray<T>(
155  const_cast<T*>(const_result.data()),
156  const_result.shape());
157  }

◆ operator[]() [2/2]

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

Definition at line 159 of file ndarray.hpp.

160  {
161 #ifndef NDEBUG
162  // Debug mode: perform bounds checking
163  if (idx < 0 || idx >= shape_[0])
164  {
165  throw std::out_of_range("Index out of bounds for first dimension");
166  }
167 #endif
168  ShapeArray new_shape;
169  std::fill(new_shape.begin(), new_shape.end(), 0);
170  std::copy(shape_.begin() + 1, shape_.end(), new_shape.begin());
171 
172  return NDArray<const T>(
173  reinterpret_cast<const T*>(data_) + idx * strides_[0],
174  new_shape);
175  }
std::array< std::int64_t, MAX_DIMS > ShapeArray
Definition: ndarray.hpp:28

◆ scalar() [1/2]

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

Definition at line 198 of file ndarray.hpp.

199  {
200  return const_cast<T&>(static_cast<const NDArray&>(*this).scalar());
201  }

◆ scalar() [2/2]

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

Definition at line 203 of file ndarray.hpp.

204  {
205  if (dims_ != 0)
206  {
207  throw std::logic_error("scalar() only available for 0-dimensional ndarrays");
208  }
209  return *reinterpret_cast<const T*>(data_);
210  }

◆ setData()

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

Definition at line 93 of file ndarray.hpp.

94  {
95  data_ = data;
96  }

◆ shape()

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

Definition at line 98 of file ndarray.hpp.

99  {
100  return shape_;
101  }

◆ size()

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

Definition at line 114 of file ndarray.hpp.

115  {
116  return size_;
117  }

◆ strides()

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

Definition at line 102 of file ndarray.hpp.

103  {
104  return strides_;
105  }

Member Data Documentation

◆ MAX_DIMS

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

Definition at line 27 of file ndarray.hpp.


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