NDArray< T > Class Template Reference#
|
Runtime Library v0.30
Mobilint SDK qb
|
A class representing an N-dimensional array (NDArray). More...
#include <ndarray.h>
Public Types | |
| using | value_type = T |
Public Member Functions | |
| NDArray ()=default | |
| Default constructor. Creates an empty NDArray. | |
| NDArray (const std::vector< int64_t > &shape, StatusCode &sc) | |
| Constructs an NDArray with the given shape, allocating memory for elements. | |
| NDArray (T *ptr, const std::vector< int64_t > &shape) | |
| Constructs an NDArray from a raw pointer without taking ownership. | |
| NDArray (T *ptr, const std::vector< int64_t > &shape, bool owner) | |
| Constructs an NDArray from a raw pointer without taking ownership. | |
| NDArray (std::shared_ptr< void > data, const std::vector< int64_t > &shape) | |
| Constructs an NDArray from a shared memory block with shared ownership. | |
| T * | data () const |
| Returns a pointer to the underlying data. | |
| const std::vector< int64_t > & | shape () const |
| Returns the shape (dimensions) of the array. | |
| std::size_t | ndim () const |
| Returns the number of dimensions of the array. | |
| int64_t | size () const |
| Returns the total number of elements in the array. | |
| T * | begin () |
| Returns an iterator to the beginning of the data. | |
| T * | end () |
| Returns an iterator to the end of the data. | |
| const T * | begin () const |
| Returns a constant iterator to the beginning of the data. | |
| const T * | end () const |
| Returns a constant iterator to the end of the data. | |
| T & | operator[] (size_t idx) |
| Accesses an element of the array. | |
| T | operator[] (size_t idx) const |
| Accesses an element of the array (const version). | |
Detailed Description
class mobilint::NDArray< T >
A class representing an N-dimensional array (NDArray).
The class automatically manages memory when it owns the data. If the NDArray is created from a raw pointer without taking ownership, it simply acts as a view of the existing data, and no memory management is performed by the NDArray.
NDArray is the recommended way for providing input and receiving output to/from model inferences. By using NDArray, unnecessary copies inside the maccel runtime are avoided, which helps improve performance during model inference operations.
- Template Parameters
-
T The type of the elements in the array.
Member Typedef Documentation
◆ value_type
| using mobilint::NDArray< T >::value_type = T |
Constructor & Destructor Documentation
◆ NDArray() [1/4]
|
inline |
Constructs an NDArray with the given shape, allocating memory for elements.
This constructor allocates memory for the array and takes ownership of the allocated data. The memory is automatically managed by the NDArray instance.
- Parameters
-
[in] shape The shape (dimensions) of the array. [out] sc Status code indicating success or failure of memory allocation.
◆ NDArray() [2/4]
|
inline |
Constructs an NDArray from a raw pointer without taking ownership.
This constructor creates an NDArray that acts as a view of the provided data. It does not manage the memory, and the memory is not owned by the NDArray instance. The user is responsible for ensuring that the data remains valid for the lifetime of the NDArray.
- Parameters
-
[in] ptr A pointer to the data. [in] shape The shape (dimensions) of the array.
◆ NDArray() [3/4]
|
inline |
Constructs an NDArray from a raw pointer without taking ownership.
- Deprecated
- This constructor is deprecated. Use NDArray(T*, const std::vector<int64_t>&) instead.
- Parameters
-
[in] ptr A pointer to the data. [in] shape The shape (dimensions) of the array. owner Ignored parameter (was intended to indicate ownership).
◆ NDArray() [4/4]
|
inline |
Constructs an NDArray from a shared memory block with shared ownership.
This constructor allows the NDArray to share ownership of an existing memory buffer, preventing unnecessary copies. The data parameter should be a std::shared_ptr<void> pointing to a valid memory block.
Example usage:
- Parameters
-
[in] data A shared pointer to the memory buffer. For example, it can be initialized using std::shared_ptr<void>(std::malloc(bytesize), std::free), but ensure that the allocation succeeds before passing it. [in] shape The shape (dimensions) of the array.
Member Function Documentation
◆ data()
|
inline |
◆ shape()
|
inline |
◆ ndim()
|
inline |
◆ size()
|
inline |
◆ begin() [1/2]
|
inline |
◆ end() [1/2]
|
inline |
◆ begin() [2/2]
|
inline |
◆ end() [2/2]
|
inline |
◆ operator[]() [1/2]
|
inline |
◆ operator[]() [2/2]
|
inline |
The documentation for this class was generated from the following file:
Generated by