NDArray< T > Class Template Reference

NDArray&lt; T &gt; Class Template Reference#

Runtime Library: mobilint::NDArray< T > Class Template Reference
Runtime Library v0.30
Mobilint SDK qb
mobilint::NDArray< T > Class Template Reference

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.
operator[] (size_t idx) const
 Accesses an element of the array (const version).

Detailed Description

template<typename T>
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
TThe type of the elements in the array.

Definition at line 77 of file ndarray.h.

Member Typedef Documentation

◆ value_type

template<typename T>
using mobilint::NDArray< T >::value_type = T

Definition at line 79 of file ndarray.h.

Constructor & Destructor Documentation

◆ NDArray() [1/4]

template<typename T>
mobilint::NDArray< T >::NDArray ( const std::vector< int64_t > & shape,
StatusCode & sc )
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]shapeThe shape (dimensions) of the array.
[out]scStatus code indicating success or failure of memory allocation.

Definition at line 95 of file ndarray.h.

◆ NDArray() [2/4]

template<typename T>
mobilint::NDArray< T >::NDArray ( T * ptr,
const std::vector< int64_t > & shape )
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]ptrA pointer to the data.
[in]shapeThe shape (dimensions) of the array.

Definition at line 109 of file ndarray.h.

◆ NDArray() [3/4]

template<typename T>
mobilint::NDArray< T >::NDArray ( T * ptr,
const std::vector< int64_t > & shape,
bool owner )
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]ptrA pointer to the data.
[in]shapeThe shape (dimensions) of the array.
ownerIgnored parameter (was intended to indicate ownership).

Definition at line 122 of file ndarray.h.

◆ NDArray() [4/4]

template<typename T>
mobilint::NDArray< T >::NDArray ( std::shared_ptr< void > data,
const std::vector< int64_t > & shape )
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:

size_t bytesize = sizeof(float) * 100; // Allocate memory for 100 floats
void* raw_ptr = std::malloc(bytesize);
if (!raw_ptr) {
// Handle memory allocation failure
}
auto buffer = std::shared_ptr<void>(raw_ptr, std::free);
NDArray<float> array(buffer, {10, 10}); // Create a 10x10 array
A class representing an N-dimensional array (NDArray).
Definition ndarray.h:77
Parameters
[in]dataA 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]shapeThe shape (dimensions) of the array.

Definition at line 149 of file ndarray.h.

Member Function Documentation

◆ data()

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

Returns a pointer to the underlying data.

Returns
A pointer to the array's data.

Definition at line 157 of file ndarray.h.

◆ shape()

template<typename T>
const std::vector< int64_t > & mobilint::NDArray< T >::shape ( ) const
inline

Returns the shape (dimensions) of the array.

Returns
A reference to the vector representing the shape.

Definition at line 164 of file ndarray.h.

◆ ndim()

template<typename T>
std::size_t mobilint::NDArray< T >::ndim ( ) const
inline

Returns the number of dimensions of the array.

Returns
The number of dimensions.

Definition at line 171 of file ndarray.h.

◆ size()

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

Returns the total number of elements in the array.

Returns
The total number of elements.

Definition at line 178 of file ndarray.h.

◆ begin() [1/2]

template<typename T>
T * mobilint::NDArray< T >::begin ( )
inline

Returns an iterator to the beginning of the data.

Returns
A pointer to the first element.

Definition at line 185 of file ndarray.h.

◆ end() [1/2]

template<typename T>
T * mobilint::NDArray< T >::end ( )
inline

Returns an iterator to the end of the data.

Returns
A pointer past the last element.

Definition at line 192 of file ndarray.h.

◆ begin() [2/2]

template<typename T>
const T * mobilint::NDArray< T >::begin ( ) const
inline

Returns a constant iterator to the beginning of the data.

Returns
A pointer to the first element.

Definition at line 199 of file ndarray.h.

◆ end() [2/2]

template<typename T>
const T * mobilint::NDArray< T >::end ( ) const
inline

Returns a constant iterator to the end of the data.

Returns
A pointer past the last element.

Definition at line 206 of file ndarray.h.

◆ operator[]() [1/2]

template<typename T>
T & mobilint::NDArray< T >::operator[] ( size_t idx)
inline

Accesses an element of the array.

Parameters
idxThe index of the element.
Returns
A reference to the element at the specified index.

Definition at line 214 of file ndarray.h.

◆ operator[]() [2/2]

template<typename T>
T mobilint::NDArray< T >::operator[] ( size_t idx) const
inline

Accesses an element of the array (const version).

Parameters
idxThe index of the element.
Returns
A copy of the element at the specified index.

Definition at line 222 of file ndarray.h.


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