Data arrays¶
-
class DataArrayBase¶
DataArrayBasemanages n-dimensional arrays used as data in a block or tensor map. The array itself if 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.WARNING: all function implementations MUST be thread-safe, and can be called from multiple threads at the same time. The
DataArrayBaseitself might be moved from one thread to another.Subclassed by metatensor::EmptyDataArray, metatensor::SimpleDataArray< T, typename >, metatensor_torch::TorchDataArray
Public Functions
-
DataArrayBase(const DataArrayBase&) = default¶
DataArrayBase can be copy-constructed.
-
DataArrayBase &operator=(const DataArrayBase&) = default¶
DataArrayBase can be copy-assigned.
-
DataArrayBase(DataArrayBase&&) noexcept = default¶
DataArrayBase can be move-constructed.
-
DataArrayBase &operator=(DataArrayBase&&) noexcept = default¶
DataArrayBase can be move-assigned.
-
virtual mts_data_origin_t origin() const = 0¶
Get “data origin” for this array in.
Users of
DataArrayBaseshould register a single data origin withmts_register_data_origin, and use it for all compatible arrays.
-
virtual DLDevice device() const = 0¶
Get the device where this array’s data resides.
-
virtual DLDataType dtype() const = 0¶
Get the data type of this array.
-
virtual DLManagedTensorVersioned *as_dlpack(DLDevice device, const int64_t *stream, DLPackVersion max_version) = 0¶
Get a DLPack representation of this array
The returned pointer is owned by the caller and should be freed using its deleter function when no longer needed.
See the documentation of
mts_array_t::as_dlpackfor more details about the parameters.
-
virtual std::unique_ptr<DataArrayBase> copy() const = 0¶
Make a copy of this DataArrayBase and return the new array. The new array is expected to have the same data origin and parameters (data type, data location, etc.)
-
virtual std::unique_ptr<DataArrayBase> create(std::vector<uintptr_t> shape, MtsArray fill_value) const = 0¶
Create a new array with the same options as the current one (data type, data location, etc.) and the requested
shape.The new array should be filled with the scalar value from
fill_value, which must be anMtsArraycontaining a single element with the same dtype as this array.
-
virtual const std::vector<uintptr_t> &shape() const & = 0¶
Get the shape of this array.
-
virtual void reshape(const std::vector<uintptr_t> &shape) = 0¶
Set the shape of this array to the given
shape
-
virtual void swap_axes(uintptr_t axis_1, uintptr_t axis_2) = 0¶
Swap the axes
axis_1andaxis_2in thisarray.
-
virtual void move_data(const DataArrayBase &input, std::vector<mts_data_movement_t> moves) = 0¶
Set entries in the current array taking data from the
inputarray.This array is guaranteed to be created by calling
mts_array_t::createwith one of the arrays in the same block or tensor map as theinput.The
movesindicate where the data should be moved frominputto the current DataArrayBase.This function should copy data from
input[move.sample_in, ..., move.properties_start_in + i]toarray[move.sample_out, ..., move.properties_start_out + i]for eachmoveinmovesandiup tomove.properties_length. All indexes are 0-based.
Public Static Functions
-
static inline MtsArray to_mts_array(std::unique_ptr<DataArrayBase> data)¶
Convert a concrete
DataArrayBaseto a C-compatiblemts_array_tThe
mts_array_ttakes ownership of the data, which should be released withmts_array_t::destroy.
-
DataArrayBase(const DataArrayBase&) = default¶
-
class MtsArray¶
RAII wrapper around
mts_array_tthat takes ownership of the underlying data and calls the appropriate destroy function when it goes out of scope.This class also provides some convenience functions to call the function pointers in
mts_array_t.Public Functions
-
inline explicit MtsArray(mts_array_t array)¶
Create an
MtsArraythat takes ownership of the givenmts_array_t.
-
inline mts_array_t release() &&¶
Release ownership of the
mts_array_tand return it. After this call, theMtsArrayis empty and does not own any data.
-
inline mts_array_t as_mts_array_t() const¶
Get a non-owning view of this
MtsArrayas anmts_array_t.
-
inline mts_data_origin_t origin() const¶
Get the data origin for this array.
-
inline DLDevice device() const¶
Get the device where this array’s data resides.
-
inline DLDataType dtype() const¶
Get the data type of this array.
-
inline DLManagedTensorVersioned *as_dlpack(DLDevice device, const int64_t *stream, DLPackVersion max_version)¶
Get a DLPack representation of this array
The returned pointer is owned by the caller and should be freed using its deleter function when no longer needed.
See the documentation of
mts_array_t::as_dlpackfor more details about the parameters.
-
template<typename T>
inline DLPackArray<T> as_dlpack_array(DLDevice device, const int64_t *stream, DLPackVersion max_version)¶ Get a DLPackArray corresponding to this array
See the documentation of
mts_array_t::as_dlpackfor more details about the parameters.
-
inline MtsArray create(const std::vector<uintptr_t> &shape, MtsArray fill_value) const¶
Create a new array with the same options as the current one (data type, data location, etc.) and the requested
shape.The new array should be filled with the scalar value from
fill_value, which must be anmts_array_twith shape(1,)and the same dtype as this array. This function should callfill_value.destroyif the function pointer is not null whenfill_valueis no longer needed.
-
inline std::vector<uintptr_t> shape() const¶
Get the shape of this array.
-
inline void reshape(const std::vector<uintptr_t> &shape)¶
Set the shape of this array to the given
shape
-
inline void swap_axes(uintptr_t axis_1, uintptr_t axis_2)¶
Swap the axes
axis_1andaxis_2in thisarray.
-
inline void move_data(const MtsArray &input, std::vector<mts_data_movement_t> moves)¶
Set entries in the current array taking data from the
inputarray.This array is guaranteed to be created by calling
mts_array_t::createwith one of the arrays in the same block or tensor map as theinput.The
movesindicate where the data should be moved frominputto the current DataArrayBase.This function copy data from
input[move.sample_in, ..., move.properties_start_in + i]toarray[move.sample_out, ..., move.properties_start_out + i]for eachmoveinmovesandiup tomove.properties_length. All indexes are 0-based.
-
inline explicit MtsArray(mts_array_t array)¶
-
template<typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
class SimpleDataArray : public metatensor::DataArrayBase¶ Very basic implementation of DataArrayBase in C++.
This is included as an example implementation of DataArrayBase, and to make metatensor usable without additional dependencies. For other uses cases, it might be better to implement DataArrayBase on your data, using functionalities from
Eigen,Boost.Array, etc.Public Functions
-
inline SimpleDataArray(std::vector<uintptr_t> shape, T value = T{})¶
Create a SimpleDataArray with the given
shape, and all elements set tovalue
-
inline SimpleDataArray(std::vector<uintptr_t> shape, std::vector<T> data)¶
Create a SimpleDataArray with the given
shapeanddata.The data is interpreted as a row-major n-dimensional array.
-
SimpleDataArray(const SimpleDataArray&) = default¶
SimpleDataArray can be copy-constructed.
-
SimpleDataArray &operator=(const SimpleDataArray&) = default¶
SimpleDataArray can be copy-assigned.
-
SimpleDataArray(SimpleDataArray&&) noexcept = default¶
SimpleDataArray can be move-constructed.
-
SimpleDataArray &operator=(SimpleDataArray&&) noexcept = default¶
SimpleDataArray can be move-assigned.
-
inline NDArray<T> view() const¶
Get a const view of the data managed by this SimpleDataArray.
-
inline NDArray<T> view()¶
Get a mutable view of the data managed by this SimpleDataArray.
Public Static Functions
-
static inline SimpleDataArray &from_mts_array(MtsArray &array)¶
Extract a reference to SimpleDataArray out of an
mts_array_t.This function fails if the
mts_array_tdoes not contain a SimpleDataArray.
-
static inline const SimpleDataArray &from_mts_array(const MtsArray &array)¶
Extract a const reference to SimpleDataArray out of an
mts_array_t.This function fails if the
mts_array_tdoes not contain a SimpleDataArray.
-
inline SimpleDataArray(std::vector<uintptr_t> shape, T value = T{})¶
-
class EmptyDataArray : public metatensor::DataArrayBase¶
An implementation of
DataArrayBasecontaining no data.This class only tracks it’s shape, and can be used when only the metadata of a
TensorBlockis important, leaving the data unspecified.Public Functions
-
inline EmptyDataArray(std::vector<uintptr_t> shape)¶
Create ae
EmptyDataArraywith the givenshape
-
EmptyDataArray(const EmptyDataArray&) = default¶
EmptyDataArray can be copy-constructed.
-
EmptyDataArray &operator=(const EmptyDataArray&) = default¶
EmptyDataArray can be copy-assigned.
-
EmptyDataArray(EmptyDataArray&&) noexcept = default¶
EmptyDataArray can be move-constructed.
-
EmptyDataArray &operator=(EmptyDataArray&&) noexcept = default¶
EmptyDataArray can be move-assigned.
-
inline EmptyDataArray(std::vector<uintptr_t> shape)¶