6#ifndef MACCEL_NDARRAY_H_
7#define MACCEL_NDARRAY_H_
17#include "maccel/export.h"
28inline int64_t numel(
const std::vector<int64_t>& shape) {
29 return std::accumulate(shape.begin(), shape.end(), INT64_C(1), std::multiplies<>());
41class MACCEL_EXPORT NDArrayData {
43 NDArrayData() =
default;
45 explicit NDArrayData(std::shared_ptr<void> data)
46 : mData(std::move(data)), mRawPtr(mData.get()) {}
47 explicit NDArrayData(
void* ptr) : mData{
nullptr}, mRawPtr{ptr} {};
49 NDArrayData(
const NDArrayData& other) =
default;
50 NDArrayData(NDArrayData&& other)
noexcept =
default;
51 NDArrayData& operator=(
const NDArrayData& other);
52 NDArrayData& operator=(NDArrayData&& other)
noexcept =
default;
54 void* data()
const {
return mRawPtr; }
57 std::shared_ptr<void> mData =
nullptr;
58 void* mRawPtr =
nullptr;
96 : mSize(internal::numel(
shape)), mData(sizeof(T) * mSize, sc), mShape(
shape) {}
110 : mSize(internal::numel(
shape)), mData(ptr), mShape(
shape) {}
150 : mSize(internal::numel(
shape)), mData(std::move(
data)), mShape(
shape) {}
157 T*
data()
const {
return reinterpret_cast<T*
>(mData.data()); }
164 const std::vector<int64_t>&
shape()
const {
return mShape; }
171 std::size_t
ndim()
const {
return mShape.size(); }
178 int64_t
size()
const {
return mSize; }
206 const T*
end()
const {
return data() + mSize; }
227 std::vector<int64_t> mShape;
T * end()
Returns an iterator to the end of the data.
T & operator[](size_t idx)
Accesses an element of the array.
NDArray(const std::vector< int64_t > &shape, StatusCode &sc)
Constructs an NDArray with the given shape, allocating memory for elements.
const T * begin() const
Returns a constant iterator to the beginning of the data.
const std::vector< int64_t > & shape() const
Returns the shape (dimensions) of the array.
NDArray(T *ptr, const std::vector< int64_t > &shape, bool owner)
Constructs an NDArray from a raw pointer without taking ownership.
T * data() const
Returns a pointer to the underlying data.
const T * end() const
Returns a constant iterator to the end of the data.
NDArray(T *ptr, const std::vector< int64_t > &shape)
Constructs an NDArray from a raw pointer without taking ownership.
T operator[](size_t idx) const
Accesses an element of the array (const version).
NDArray()=default
Default constructor. Creates an empty NDArray.
int64_t size() const
Returns the total number of elements in the array.
NDArray(std::shared_ptr< void > data, const std::vector< int64_t > &shape)
Constructs an NDArray from a shared memory block with shared ownership.
std::size_t ndim() const
Returns the number of dimensions of the array.
T * begin()
Returns an iterator to the beginning of the data.
A class representing the data underlying an NDArray.
StatusCode
Enumerates status codes for the maccel runtime.