ndarray.h Source File

ndarray.h Source File#

Runtime Library: ndarray.h Source File
Runtime Library v0.30
Mobilint SDK qb
ndarray.h
Go to the documentation of this file.
1// Copyright ⓒ 2019- Mobilint Inc. All rights reserved.
5
6#ifndef MACCEL_NDARRAY_H_
7#define MACCEL_NDARRAY_H_
8
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12#include <memory>
13#include <numeric>
14#include <utility>
15#include <vector>
16
17#include "maccel/export.h"
18#include "maccel/status_code.h"
19#include "maccel/type.h"
20
21namespace mobilint {
26namespace internal {
27
28inline int64_t numel(const std::vector<int64_t>& shape) {
29 return std::accumulate(shape.begin(), shape.end(), INT64_C(1), std::multiplies<>());
30}
31
41class MACCEL_EXPORT NDArrayData {
42public:
43 NDArrayData() = default;
44 NDArrayData(int64_t bytesize, StatusCode& sc);
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} {};
48
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;
53
54 void* data() const { return mRawPtr; }
55
56private:
57 std::shared_ptr<void> mData = nullptr; // Owned data
58 void* mRawPtr = nullptr; // A raw pointer, which is not owned
59};
60
61} // namespace internal
62
76template <typename T>
77class NDArray {
78public:
79 using value_type = T;
80
84 NDArray() = default;
85
95 NDArray(const std::vector<int64_t>& shape, StatusCode& sc)
96 : mSize(internal::numel(shape)), mData(sizeof(T) * mSize, sc), mShape(shape) {}
97
109 NDArray(T* ptr, const std::vector<int64_t>& shape)
110 : mSize(internal::numel(shape)), mData(ptr), mShape(shape) {}
111
122 NDArray(T* ptr, const std::vector<int64_t>& shape, bool owner)
123 : NDArray(ptr, shape) {}
124
149 NDArray(std::shared_ptr<void> data, const std::vector<int64_t>& shape)
150 : mSize(internal::numel(shape)), mData(std::move(data)), mShape(shape) {}
151
157 T* data() const { return reinterpret_cast<T*>(mData.data()); }
158
164 const std::vector<int64_t>& shape() const { return mShape; }
165
171 std::size_t ndim() const { return mShape.size(); }
172
178 int64_t size() const { return mSize; }
179
185 T* begin() { return data(); }
186
192 T* end() { return data() + mSize; }
193
199 const T* begin() const { return data(); }
200
206 const T* end() const { return data() + mSize; }
207
214 T& operator[](size_t idx) { return data()[idx]; }
215
222 T operator[](size_t idx) const { return data()[idx]; }
223
224private:
225 int64_t mSize = 0;
227 std::vector<int64_t> mShape;
228};
229
231
232} // namespace mobilint
233
234#endif // MACCEL_NDARRAY_H_
T * end()
Returns an iterator to the end of the data.
Definition ndarray.h:192
T & operator[](size_t idx)
Accesses an element of the array.
Definition ndarray.h:214
NDArray(const std::vector< int64_t > &shape, StatusCode &sc)
Constructs an NDArray with the given shape, allocating memory for elements.
Definition ndarray.h:95
const T * begin() const
Returns a constant iterator to the beginning of the data.
Definition ndarray.h:199
const std::vector< int64_t > & shape() const
Returns the shape (dimensions) of the array.
Definition ndarray.h:164
NDArray(T *ptr, const std::vector< int64_t > &shape, bool owner)
Constructs an NDArray from a raw pointer without taking ownership.
Definition ndarray.h:122
T * data() const
Returns a pointer to the underlying data.
Definition ndarray.h:157
const T * end() const
Returns a constant iterator to the end of the data.
Definition ndarray.h:206
NDArray(T *ptr, const std::vector< int64_t > &shape)
Constructs an NDArray from a raw pointer without taking ownership.
Definition ndarray.h:109
T operator[](size_t idx) const
Accesses an element of the array (const version).
Definition ndarray.h:222
NDArray()=default
Default constructor. Creates an empty NDArray.
int64_t size() const
Returns the total number of elements in the array.
Definition ndarray.h:178
NDArray(std::shared_ptr< void > data, const std::vector< int64_t > &shape)
Constructs an NDArray from a shared memory block with shared ownership.
Definition ndarray.h:149
std::size_t ndim() const
Returns the number of dimensions of the array.
Definition ndarray.h:171
T * begin()
Returns an iterator to the beginning of the data.
Definition ndarray.h:185
A class representing the data underlying an NDArray.
Definition ndarray.h:41
StatusCode
Enumerates status codes for the maccel runtime.
Definition status_code.h:26