Data arrays¶
-
struct mts_array_t¶
mts_array_tmanages n-dimensional arrays used as data in a block or tensor map. The array itself is opaque to this library and can come from multiple sources: Rust program, a C/C++ program, a Fortran program, Python with numpy or torch. The data does not have to live on CPU, or even on the same machine where this code is executed.This struct contains a C-compatible manual implementation of a virtual table (vtable, i.e. trait in Rust, pure virtual class in C++); allowing manipulation of the array in an opaque way.
WARNING: all function implementations MUST be thread-safe, and can be called from multiple threads at the same time. The
mts_array_titself might be moved from one thread to another.Public Members
-
void *ptr¶
User-provided data should be stored here, it will be passed as the first parameter to all function pointers below.
-
void (*destroy)(void *array)¶
Remove this array and free the associated memory. This function can be set to
NULLif there is no memory management to do.
-
mts_status_t (*origin)(const void *array, mts_data_origin_t *origin)¶
This function needs to store the “data origin” for this array in
origin. Users ofmts_array_tshould register a single data origin withmts_register_data_origin, and use it for all compatible arrays.This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
mts_status_t (*device)(const void *array, DLDevice *device)¶
Query the device where this array’s data resides without exporting via DLPack.
The implementation must store the device information in
*device.This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
mts_status_t (*dtype)(const void *array, DLDataType *dtype)¶
Query the data type of this array without a full DLPack export.
The implementation must store the data type in
*dtype.This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
mts_status_t (*as_dlpack)(void *array, DLManagedTensorVersioned **dl_managed_tensor, DLDevice device, const int64_t *stream, DLPackVersion max_version)¶
Get a DLPack representation of the underlying data.
This function exports the array as a
DLManagedTensorVersionedstruct into*dl_managed_tensor, following the DLPack data interchange standard.The
deviceparameter specifies the desired DLPack device type. If this differs from the array’s current device, the implementation should attempt to make the data accessible on the requested device (e.g., by copying).The
streamparameter is a pointer to an integer representing a device-specific stream or queue. If this isNULL, the implementation should use the default stream for the specified device. If this is-1, no synchronization should be performed. Some devices have specific stream values:For CUDA devices,
1represents the legacy default stream,2the per-thread default stream. Any value above2indicates the stream number.0is not allowed as it could mean the same asNULL,1or2.For ROCm devices,
0represents the default stream, any value above2indicates the stream number.1and2are not allowed.
See also the documentation of
__dlpack__for more information about streams: https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__dlpack__.htmlmax_versionspecifies the maximum DLPack API version the caller supports. The implementation should try to return a tensor compatible with this version, but this is not guaranteed, and the caller should check the returned tensor’s version.The returned
DLManagedTensorVersionedis owned by the caller, who is responsible for calling itsdeleterfunction when the tensor is no longer needed. The lifetime of theDLManagedTensorVersionedmust not exceed the lifetime of themts_array_tit was created from.This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
mts_status_t (*shape)(const void *array, const uintptr_t **shape, uintptr_t *shape_count)¶
Get the shape of the array managed by this
mts_array_tin the*shapepointer, and the number of dimension (size of the*shapearray) in*shape_count. If the array is a single scalar,shape_countshould be set to 0, and the shape pointer toNULL.This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
mts_status_t (*reshape)(void *array, const uintptr_t *shape, uintptr_t shape_count)¶
Change the shape of the array managed by this
mts_array_tto the givenshape.shape_countmust contain the number of elements in theshapearray.This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
mts_status_t (*swap_axes)(void *array, uintptr_t axis_1, uintptr_t axis_2)¶
Swap the axes
axis_1andaxis_2in thisarray.This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
mts_status_t (*create)(const void *array, const uintptr_t *shape, uintptr_t shape_count, struct mts_array_t fill_value, struct mts_array_t *new_array)¶
Create a new array with the same options as the current one (data type, data location, etc.) and the requested
shape; and store it innew_array. The number of elements in theshapearray should be given inshape_count.The new array should be filled with the scalar value from
fill_value, which must be anmts_array_tcontaining a single scalar (empty shape) with the same dtype as this array. This function should callfill_value.destroyif the function pointer is notNULLwhenfill_valueis no longer needed.This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
mts_status_t (*copy)(const void *array, struct mts_array_t *new_array)¶
Make a copy of this
arrayand return the new array innew_array.The new array is expected to have the same data origin and parameters (data type, data location, etc.)
This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
mts_status_t (*move_data)(void *output, const void *input, const struct mts_data_movement_t *movements, uintptr_t movements_count)¶
Set entries in the
outputarray (the current array) taking data from theinputarray. Theoutputarray is guaranteed to be created by callingmts_array_t::createwith one of the arrays in the same block or tensor map as theinput.The
movementsarray of sizemovements_countindicate where the data should be moved frominputtooutput.This function should copy data from
input[movements[i].sample_in, ..., movements[i].properties_start_in + x]toarray[movements[i].sample_out, ..., movements[i].properties_start_out + x]foriup tomovements_countandxup tomovements[i].properties_length. All indexes are 0-based.This function should return
MTS_SUCCESSon success, orMTS_CALLBACK_ERRORon failure. In case of failure, the implementation should callmts_set_last_errorwith an appropriate error message before returning.
-
void *ptr¶
-
mts_status_t mts_register_data_origin(const char *name, mts_data_origin_t *origin)¶
Register a new data origin with the given
name. Calling this function multiple times with the same name will give the samemts_data_origin_t.- Parameters:
name – name of the data origin as an UTF-8 encoded NULL-terminated string
origin – pointer to an
mts_data_origin_twhere the origin will be stored
- Returns:
The status code of this operation. If the status is not
MTS_SUCCESS, you can usemts_last_error()to get the full error message.
-
mts_status_t mts_get_data_origin(mts_data_origin_t origin, char *buffer, uintptr_t buffer_size)¶
Get the name used to register a given data
originin the givenbuffer- Parameters:
origin – pre-registered data origin
buffer – buffer to be filled with the data origin name. The origin name will be written as an UTF-8 encoded, NULL-terminated string
buffer_size – size of the buffer
- Returns:
The status code of this operation. If the status is not
MTS_SUCCESS, you can usemts_last_error()to get the full error message.