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...
|
| | 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 constexpr std::int64_t | MAX_DIMS = 10 |
| |
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.
◆ ShapeArray
◆ StridesArray
◆ NDArray() [1/5]
◆ NDArray() [2/5]
template<typename T >
template<typename U >
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())),
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)
44 shape_[i] =
static_cast<std::int64_t
>(*(
shape.begin() + i));
46 calculateSizeAndStrides();
const ShapeArray & shape() const
◆ NDArray() [3/5]
- 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.
◆ NDArray() [4/5]
◆ NDArray() [5/5]
Uses shape[0..dims) as given, zeros included; entries past dims are ignored.
- Exceptions
-
| std::invalid_argument | if dims is outside [0, MAX_DIMS] or elemSize is not positive. |
Definition at line 72 of file ndarray.hpp.
79 throw std::invalid_argument(
"NDArray dims must be between 0 and MAX_DIMS");
83 throw std::invalid_argument(
"NDArray elemSize must be positive");
85 std::fill(shape_.begin(), shape_.end(), 0);
86 std::fill(strides_.begin(), strides_.end(), 0);
88 calculateSizeAndStrides();
static constexpr std::int64_t MAX_DIMS
std::int64_t dims() const
◆ ~NDArray()
◆ at() [1/2]
template<typename T >
template<typename... Idxs>
Definition at line 229 of file ndarray.hpp.
231 return const_cast<T&
>(
static_cast<const NDArray&
>(*this).at(idxs...));
◆ at() [2/2]
template<typename T >
template<typename... Idxs>
Definition at line 235 of file ndarray.hpp.
237 std::vector<std::int64_t> indices = {idxs...};
238 if (indices.size() !=
static_cast<size_t>(dims_))
240 throw std::out_of_range(
"Number of indices does not match ndarray dimensions");
242 for (
size_t i = 0; i < indices.size(); ++i)
244 if (indices[i] < 0 || indices[i] >= shape_[i])
246 throw std::out_of_range(
"Index out of bounds");
T & operator()(Idxs... idxs)
◆ data() [1/2]
Definition at line 93 of file ndarray.hpp.
95 return reinterpret_cast<T*
>(data_);
◆ data() [2/2]
Definition at line 97 of file ndarray.hpp.
99 return reinterpret_cast<const T*
>(data_);
◆ dims()
◆ elemSize()
◆ numElements()
◆ operator const T &()
Definition at line 203 of file ndarray.hpp.
207 throw std::logic_error(
"Scalar conversion only available for 0-dimensional ndarrays");
209 return *
reinterpret_cast<const T*
>(data_);
◆ 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.
198 throw std::logic_error(
"Scalar conversion only available for 0-dimensional ndarrays");
200 return *
reinterpret_cast<T*
>(data_);
◆ operator()() [1/2]
template<typename T >
template<typename... Idxs>
Definition at line 133 of file ndarray.hpp.
135 return const_cast<T&
>(
static_cast<const NDArray&
>(*this)(idxs...));
◆ 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.
143 std::vector<std::int64_t> indices = {idxs...};
144 if (indices.size() !=
static_cast<size_t>(dims_))
146 throw std::out_of_range(
"Number of indices does not match ndarray dimensions");
148 for (
size_t i = 0; i < indices.size(); ++i)
150 if (indices[i] < 0 || indices[i] >= shape_[i])
152 throw std::out_of_range(
"Index out of bounds");
156 return reinterpret_cast<const T*
>(data_)[computeOffset({idxs...})];
◆ operator[]() [1/2]
Definition at line 161 of file ndarray.hpp.
163 auto const_result =
static_cast<const NDArray&
>(*this)[idx];
165 const_cast<void*
>(
static_cast<const void*
>(const_result.data())),
166 const_result.shape(),
167 const_result.elemSize(),
168 const_result.dims());
◆ operator[]() [2/2]
Definition at line 171 of file ndarray.hpp.
175 if (idx < 0 || idx >= shape_[0])
177 throw std::out_of_range(
"Index out of bounds for first dimension");
181 std::fill(new_shape.begin(), new_shape.end(), 0);
182 std::copy(shape_.begin() + 1, shape_.end(), new_shape.begin());
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)),
189 dims_ > 0 ? dims_ - 1 : 0);
std::array< std::int64_t, MAX_DIMS > ShapeArray
◆ scalar() [1/2]
Definition at line 213 of file ndarray.hpp.
215 return const_cast<T&
>(
static_cast<const NDArray&
>(*this).scalar());
◆ scalar() [2/2]
Definition at line 218 of file ndarray.hpp.
222 throw std::logic_error(
"scalar() only available for 0-dimensional ndarrays");
224 return *
reinterpret_cast<const T*
>(data_);
◆ setData()
◆ shape()
◆ size()
◆ strides()
◆ MAX_DIMS
The documentation for this class was generated from the following files: