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 (void *data, const ShapeArray &shape, std::int64_t elemSize, std::int64_t dims)
 Uses shape[0..dims) as given, zeros included; entries past dims are ignored. More...
 
 ~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 26 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 30 of file ndarray.hpp.

◆ StridesArray

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

Definition at line 31 of file ndarray.hpp.

Constructor & Destructor Documentation

◆ NDArray() [1/5]

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

◆ NDArray() [2/5]

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

Definition at line 35 of file ndarray.hpp.

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

◆ NDArray() [3/5]

template<typename T >
pxl::NDArray< T >::NDArray ( T *  data,
const ShapeArray &  shape 
)
inline
Warning
A ShapeArray has no length, so dims ends at the last nonzero entry. Zeros before it are zero-length dimensions, but a trailing one is dropped: {4, 3, 0} becomes a 2-D array of 12 elements. Use the constructor that takes dims for such a shape.

Definition at line 55 of file ndarray.hpp.

56  : NDArray(const_cast<void*>(static_cast<const void*>(data)), shape, sizeof(T), dimsOf(shape))
57  {
58  }
NDArray()=default

◆ NDArray() [4/5]

template<typename T >
pxl::NDArray< T >::NDArray ( void *  data,
const ShapeArray &  shape,
std::int64_t  elemSize 
)
inline
Warning
Drops a trailing zero-length dimension, as NDArray(T*, const ShapeArray&) does.

Definition at line 63 of file ndarray.hpp.

64  : NDArray(data, shape, elemSize, dimsOf(shape))
65  {
66  }
std::int64_t elemSize() const
Definition: ndarray.hpp:119

◆ NDArray() [5/5]

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

Uses shape[0..dims) as given, zeros included; entries past dims are ignored.

Exceptions
std::invalid_argumentif dims is outside [0, MAX_DIMS] or elemSize is not positive.

Definition at line 72 of file ndarray.hpp.

73  : data_(data),
74  dims_(dims),
75  elemSize_(elemSize)
76  {
77  if (dims < 0 || dims > MAX_DIMS)
78  {
79  throw std::invalid_argument("NDArray dims must be between 0 and MAX_DIMS");
80  }
81  if (elemSize <= 0)
82  {
83  throw std::invalid_argument("NDArray elemSize must be positive");
84  }
85  std::fill(shape_.begin(), shape_.end(), 0);
86  std::fill(strides_.begin(), strides_.end(), 0);
87  std::copy(shape.begin(), shape.begin() + dims, shape_.begin());
88  calculateSizeAndStrides();
89  }
static constexpr std::int64_t MAX_DIMS
Definition: ndarray.hpp:29
std::int64_t dims() const
Definition: ndarray.hpp:115

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

230  {
231  return const_cast<T&>(static_cast<const NDArray&>(*this).at(idxs...));
232  }

◆ at() [2/2]

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

Definition at line 235 of file ndarray.hpp.

236  {
237  std::vector<std::int64_t> indices = {idxs...};
238  if (indices.size() != static_cast<size_t>(dims_))
239  {
240  throw std::out_of_range("Number of indices does not match ndarray dimensions");
241  }
242  for (size_t i = 0; i < indices.size(); ++i)
243  {
244  if (indices[i] < 0 || indices[i] >= shape_[i])
245  {
246  throw std::out_of_range("Index out of bounds");
247  }
248  }
249  return operator()(idxs...);
250  }
T & operator()(Idxs... idxs)
Definition: ndarray.hpp:133

◆ data() [1/2]

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

Definition at line 93 of file ndarray.hpp.

94  {
95  return reinterpret_cast<T*>(data_);
96  }

◆ data() [2/2]

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

Definition at line 97 of file ndarray.hpp.

98  {
99  return reinterpret_cast<const T*>(data_);
100  }

◆ dims()

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

Definition at line 115 of file ndarray.hpp.

116  {
117  return dims_;
118  }

◆ elemSize()

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

Definition at line 119 of file ndarray.hpp.

120  {
121  return elemSize_;
122  }

◆ numElements()

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

Definition at line 127 of file ndarray.hpp.

128  {
129  return numElements_;
130  }

◆ operator const T &()

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

Definition at line 203 of file ndarray.hpp.

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

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

195  {
196  if (dims_ != 0)
197  {
198  throw std::logic_error("Scalar conversion only available for 0-dimensional ndarrays");
199  }
200  return *reinterpret_cast<T*>(data_);
201  }

◆ operator()() [1/2]

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

Definition at line 133 of file ndarray.hpp.

134  {
135  return const_cast<T&>(static_cast<const NDArray&>(*this)(idxs...));
136  }

◆ operator()() [2/2]

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

Definition at line 139 of file ndarray.hpp.

140  {
141 #ifndef NDEBUG
142  // Debug mode: perform bounds checking
143  std::vector<std::int64_t> indices = {idxs...};
144  if (indices.size() != static_cast<size_t>(dims_))
145  {
146  throw std::out_of_range("Number of indices does not match ndarray dimensions");
147  }
148  for (size_t i = 0; i < indices.size(); ++i)
149  {
150  if (indices[i] < 0 || indices[i] >= shape_[i])
151  {
152  throw std::out_of_range("Index out of bounds");
153  }
154  }
155 #endif
156  return reinterpret_cast<const T*>(data_)[computeOffset({idxs...})];
157  }

◆ operator[]() [1/2]

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

Definition at line 161 of file ndarray.hpp.

162  {
163  auto const_result = static_cast<const NDArray&>(*this)[idx];
164  return NDArray<T>(
165  const_cast<void*>(static_cast<const void*>(const_result.data())),
166  const_result.shape(),
167  const_result.elemSize(),
168  const_result.dims());
169  }

◆ operator[]() [2/2]

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

Definition at line 171 of file ndarray.hpp.

172  {
173 #ifndef NDEBUG
174  // Debug mode: perform bounds checking
175  if (idx < 0 || idx >= shape_[0])
176  {
177  throw std::out_of_range("Index out of bounds for first dimension");
178  }
179 #endif
180  ShapeArray new_shape;
181  std::fill(new_shape.begin(), new_shape.end(), 0);
182  std::copy(shape_.begin() + 1, shape_.end(), new_shape.begin());
183 
184  const T* subData = reinterpret_cast<const T*>(data_) + idx * strides_[0];
185  return NDArray<const T>(
186  const_cast<void*>(static_cast<const void*>(subData)),
187  new_shape,
188  sizeof(T),
189  dims_ > 0 ? dims_ - 1 : 0);
190  }
std::array< std::int64_t, MAX_DIMS > ShapeArray
Definition: ndarray.hpp:30

◆ scalar() [1/2]

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

Definition at line 213 of file ndarray.hpp.

214  {
215  return const_cast<T&>(static_cast<const NDArray&>(*this).scalar());
216  }

◆ scalar() [2/2]

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

Definition at line 218 of file ndarray.hpp.

219  {
220  if (dims_ != 0)
221  {
222  throw std::logic_error("scalar() only available for 0-dimensional ndarrays");
223  }
224  return *reinterpret_cast<const T*>(data_);
225  }

◆ setData()

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

Definition at line 102 of file ndarray.hpp.

103  {
104  data_ = data;
105  }

◆ shape()

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

Definition at line 107 of file ndarray.hpp.

108  {
109  return shape_;
110  }

◆ size()

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

Definition at line 123 of file ndarray.hpp.

124  {
125  return size_;
126  }

◆ strides()

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

Definition at line 111 of file ndarray.hpp.

112  {
113  return strides_;
114  }

Member Data Documentation

◆ MAX_DIMS

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

Definition at line 29 of file ndarray.hpp.


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