Labels¶
-
typedef struct mts_labels_t mts_labels_t¶
Opaque type representing a set of labels used to carry metadata associated with an
mts_block_tor anmts_tensor_t.This is similar to a list of named tuples, but stored as a 2D array with a set of dimension names associated with the columns of this array. Each row/entry in this array is unique, and they are often (but not always) sorted in lexicographic order.
The following functions operate on mts_labels_t:
mts_labels(): create new labels from dimension names and a values arraymts_labels_assume_unique(): create new labels from a values array without verifying uniquenessmts_labels_clone(): increment the reference count of the labelsmts_labels_free(): decrement the reference count of the labelsmts_labels_dimensions(): get the dimension names of the labelsmts_labels_values(): get the values of the labels as an arraymts_labels_values_cpu(): get the values of the labels on CPUmts_labels_position(): get the position of an entry in the labelsmts_labels_union(): get the union of two labelsmts_labels_intersection(): get the intersection of two labelsmts_labels_difference(): get the set difference of two labelsmts_labels_select(): select entries in labels that match a selection
-
const struct mts_labels_t *mts_labels(const char *const *dimensions, uintptr_t dimensions_count, struct mts_array_t array)¶
Create a new set of Labels from the given dimension names and values array.
The array can be on any device. Data is moved to CPU internally to verify uniqueness of the labels entries.
This function allocates memory which must be released with
mts_labels_freewhen you don’t need it anymore.- Parameters:
dimensions – array of NULL-terminated UTF-8 strings containing the name of each dimensions of the new labels
dimensions_count – number of entries in the
dimensionsarrayarray – the values array (2D, i32, row-major). The labels take ownership of this array.
- Returns:
A pointer to the newly allocated labels, or a
NULLpointer in case of error. In case of error, you can usemts_last_error()to get the error message.
-
const struct mts_labels_t *mts_labels_assume_unique(const char *const *names, uintptr_t names_count, struct mts_array_t array)¶
Create a new set of Labels from the given dimension names and values array, without checking for uniqueness of the entries.
The array can be on any device (CPU or GPU). The caller must ensure that the labels entries are unique; passing non-unique entries is invalid and can lead to crashes or infinite loops.
This function allocates memory which must be released with
mts_labels_freewhen you don’t need it anymore.- Parameters:
names – array of NULL-terminated UTF-8 strings containing the names of each dimension
names_count – number of entries in the
namesarrayarray – the values array (2D, i32, row-major). The labels take ownership of this array.
- Returns:
A pointer to the newly allocated labels, or a
NULLpointer in case of error. In case of error, you can usemts_last_error()to get the error message.
-
const struct mts_labels_t *mts_labels_clone(const struct mts_labels_t *labels)¶
Make a copy of
labels, incrementing the internal reference count.Since
mts_labels_tare immutable, the copy is actually just a reference count increase, and as such should not be an expensive operation.mts_labels_freemust be used with the returned pointer to decrease the reference count and release the memory when you don’t need it anymore.- Parameters:
labels – pointer to an existing set of labels
- Returns:
A pointer to the newly allocated (cloned) labels, or a
NULLpointer in case of error. In case of error, you can usemts_last_error()to get the error message.
-
mts_status_t mts_labels_free(const struct mts_labels_t *labels)¶
Decrease the reference count of
labels, and release the corresponding memory once the reference count reaches 0.- Parameters:
labels – pointer to an existing set of labels, or NULL
- 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_labels_dimensions(const struct mts_labels_t *labels, const char *const **names, uintptr_t *count)¶
Get the dimension names for the given set of
labels.- Parameters:
labels – pointer to an existing set of labels
names – on output, will be set to a pointer to an array of NULL-terminated UTF-8 strings containing the names of each dimension. The array contains
*countelements.count – on output, will be set to the number of dimensions
- 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_labels_values(const struct mts_labels_t *labels, struct mts_array_t *array)¶
Get the values for the given set of
labelsas anmts_array_t.- Parameters:
labels – pointer to an existing set of labels
array – on output, will be set to the values array. This is a non-owning view (destroy is NULL) valid as long as the labels are alive.
- 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_labels_values_cpu(const struct mts_labels_t *labels, const int32_t **values, uintptr_t *count, uintptr_t *size)¶
Get the values for the given set of
labelsas anmts_array_t.- Parameters:
labels – pointer to an existing set of labels
values – on output, will be set to a pointer to the values array on CPU. The pointer is valid as long as the labels are alive, and points to the first element of a 2D array of shape (count, size) stored in row-major order.
count – on output, will be set to the number of entries (rows) in the labels
size – on output, will be set to the size of each entry in the labels (number of dimensions)
- 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_labels_position(const struct mts_labels_t *labels, const int32_t *values, uintptr_t values_count, int64_t *result)¶
Get the position of the entry defined by the
valuesarray in the given set oflabels.- Parameters:
labels – pointer to an existing set of labels
values – array containing the label to lookup
values_count – size of the values array
result – position of the values in the labels or -1 if the values were not found
- 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_labels_union(const struct mts_labels_t *first, const struct mts_labels_t *second, const struct mts_labels_t **result, int64_t *first_mapping, uintptr_t first_mapping_count, int64_t *second_mapping, uintptr_t second_mapping_count)¶
Take the union of two sets of labels.
If requested, this function can also give the positions in the union where each entry of the input labels ended up.
This function allocates memory for
*resultwhich must be released withmts_labels_freewhen you don’t need it anymore.- Parameters:
first – pointer to the first set of labels
second – pointer to the second set of labels
result – on output, will be set to a pointer to the newly allocated labels containing the union
first_mapping – if you want the mapping from the positions of entries in
firstto the positions inresult, this should be a pointer to an array containingfirst.countelements, to be filled by this function. Otherwise it should be aNULLpointer.first_mapping_count – number of elements in the
first_mappingarraysecond_mapping – if you want the mapping from the positions of entries in
secondto the positions inresult, this should be a pointer to an array containingsecond.countelements, to be filled by this function. Otherwise it should be aNULLpointer.second_mapping_count – number of elements in the
second_mappingarray
- 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_labels_intersection(const struct mts_labels_t *first, const struct mts_labels_t *second, const struct mts_labels_t **result, int64_t *first_mapping, uintptr_t first_mapping_count, int64_t *second_mapping, uintptr_t second_mapping_count)¶
Take the intersection of two sets of labels.
If requested, this function can also give the positions in the intersection where each entry of the input labels ended up.
This function allocates memory for
*resultwhich must be released withmts_labels_freewhen you don’t need it anymore.- Parameters:
first – pointer to the first set of labels
second – pointer to the second set of labels
result – on output, will be set to a pointer to the newly allocated labels containing the intersection
first_mapping – if you want the mapping from the positions of entries in
firstto the positions inresult, this should be a pointer to an array containingfirst.countelements, to be filled by this function. Otherwise it should be aNULLpointer. If an entry infirstis not used inresult, the mapping will be set to -1.first_mapping_count – number of elements in the
first_mappingarraysecond_mapping – if you want the mapping from the positions of entries in
secondto the positions inresult, this should be a pointer to an array containingsecond.countelements, to be filled by this function. Otherwise it should be aNULLpointer. If an entry insecondis not used inresult, the mapping will be set to -1.second_mapping_count – number of elements in the
second_mappingarray
- 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_labels_difference(const struct mts_labels_t *first, const struct mts_labels_t *second, const struct mts_labels_t **result, int64_t *first_mapping, uintptr_t first_mapping_count)¶
Take the difference of two sets of labels.
If requested, this function can also give the positions in the difference where each entry of the first set of labels ended up.
This function allocates memory for
*resultwhich must be released withmts_labels_freewhen you don’t need it anymore.- Parameters:
first – pointer to the first set of labels
second – pointer to the second set of labels
result – on output, will be set to a pointer to the newly allocated labels containing the difference
first_mapping – if you want the mapping from the positions of entries in
firstto the positions inresult, this should be a pointer to an array containingfirst.countelements, to be filled by this function. Otherwise it should be aNULLpointer. If an entry infirstis not used inresult, the mapping will be set to -1.first_mapping_count – number of elements in the
first_mappingarray
- 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_labels_select(const struct mts_labels_t *labels, const struct mts_labels_t *selection, int64_t *selected, uintptr_t *selected_count)¶
Select entries in the
labelsthat match theselection.The selection’s names must be a subset of the name of the
labelsnames.All entries in the
labelsthat match one of the entry in theselectionfor all the selection’s dimension will be picked. Any entry in theselectionbut not in thelabelswill be ignored.- Parameters:
labels – pointer to an existing set of labels
selection – pointer to labels defining the selection criteria. Multiple entries are interpreted as a logical
oroperation.selected – on input, a pointer to an array with space for
*selected_countentries. On output, the first*selected_countvalues will contain the index inlabelsof selected entries.selected_count – on input, size of the
selectedarray. On output, this will contain the number of selected entries.
- 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.