Data arrays¶
-
class DataArrayBase¶
DataArrayBasemanages 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.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< T, typename >, metatensor::KokkosDataArray< T, MemorySpace >, 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> from_dlpack(DLManagedTensorVersioned *dl_tensor) const = 0¶
Create a new
DataArrayBasefrom a DLPack tensor. The returnedDataArrayBasetakes ownership of the DLPack tensor and will call its deleter when it goes out of scope.
-
virtual std::unique_ptr<DataArrayBase> copy(DLDevice device) 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 dtype, but live on the given
device.
-
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.
-
inline MtsArray from_dlpack(DLManagedTensorVersioned *dl_tensor)¶
Create a new
MtsArrayfrom a DLPack tensor. The returnedMtsArraytakes ownership of the DLPack tensor and will call its deleter when it goes out of scope.
-
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 copies 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 use 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<storage_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<storage_t> view() const¶
Get a const view of the data managed by this SimpleDataArray.
-
inline NDArray<storage_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{})¶
-
template<typename T = double, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
class EmptyDataArray : public metatensor::DataArrayBase¶ An implementation of
DataArrayBasecontaining no data.This class only tracks its 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 an
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)¶
Kokkos arrays¶
metatensor::KokkosDataArray stores data in a Kokkos view, on CPU
or GPU, using the CUDA (NVIDIA GPUs), HIP (AMD GPUs) or SYCL (Intel GPUs) Kokkos
backends. Using the SYCL backend on NVIDIA or AMD GPUs is not supported, please
use the CUDA or HIP backend instead. This class is defined in a separate header,
which is not included by <metatensor.hpp>. Metatensor does not distribute
Kokkos, so you need to make Kokkos (version 4.0 or later) available in the
include path yourself, for example by linking to the Kokkos::kokkos CMake
target:
find_package(Kokkos REQUIRED)
find_package(metatensor REQUIRED)
target_link_libraries(your-target metatensor Kokkos::kokkos)
#include <metatensor.hpp>
#include <metatensor/kokkos.hpp>
auto view = Kokkos::View<double**>("values", n_samples, n_properties);
// ... fill the view
auto block = metatensor::TensorBlock(
std::make_unique<metatensor::KokkosDataArray<double>>(view),
samples, {}, properties
);
All Kokkos views must be released before calling Kokkos::finalize(). This
includes the views stored in KokkosDataArray, and thus any
TensorBlock/TensorMap containing them.
-
template<typename T, typename MemorySpace = typename Kokkos::DefaultExecutionSpace::memory_space>
class KokkosDataArray : public metatensor::DataArrayBase¶ Implementation of
DataArrayBaseusing Kokkos to store the data.Tis the type of the array elements, andMemorySpacethe Kokkos memory space where the data is allocated (defaults to the memory space ofKokkos::DefaultExecutionSpace).The data can have any memory layout: the array stores a pointer to the first element, together with the shape and the strides (in number of elements) of the array. This allows to wrap any
Kokkos::View(withKokkos::LayoutRight,Kokkos::LayoutLeft,Kokkos::LayoutStride, …) without copying the data.This class is only available when including
<metatensor/kokkos.hpp>, and requires Kokkos to be available in the include path.WARNING: Kokkos requires all views to be destroyed before calling
Kokkos::finalize(). This includes the views stored insideKokkosDataArray, and thus allTensorBlock/TensorMapcontainingKokkosDataArray, as well as any DLPack tensors created from them.Public Functions
-
template<typename DataType, typename ...Properties>
inline explicit KokkosDataArray(Kokkos::View<DataType, Properties...> view)¶ Create a
KokkosDataArrayfrom an existing n-dimensionalKokkos::View, with any layout. The shape and strides of the array are taken from the view. The data is shared with the view (no copy is made), and the view is kept alive by thisKokkosDataArray.Warning: while the
KokkosDataArraykeeps the view alive, the view itself might not keep its data alive. This is the case when usingMemoryTraits<Unmanaged>. In this case, the caller is responsible for ensuring that the data is not destroyed while theKokkosDataArrayis still in use.If the view has a
constvalue type (e.g.Kokkos::View<const double**>), the array is read-only (seeis_read_only()).
-
inline KokkosDataArray(std::vector<uintptr_t> shape, T value = T{})¶
Create a
KokkosDataArraywith the givenshape, and all elements set tovalue. The data is allocated contiguously, in row-major order.
-
inline KokkosDataArray(std::vector<uintptr_t> shape, flat_view_t data)¶
Create a
KokkosDataArraywith the givenshape, usingdataas storage. The data is interpreted as a contiguous row-major array, and is shared with the caller (no copy is made).
-
KokkosDataArray(const KokkosDataArray&) = default¶
KokkosDataArray can be copy-constructed. The data is shared between the copies.
-
KokkosDataArray &operator=(const KokkosDataArray&) = default¶
KokkosDataArray can be copy-assigned. The data is shared between the copies.
-
KokkosDataArray(KokkosDataArray&&) noexcept = default¶
KokkosDataArray can be move-constructed.
-
KokkosDataArray &operator=(KokkosDataArray&&) noexcept = default¶
KokkosDataArray can be move-assigned.
-
inline bool is_read_only() const¶
Check if this array is read-only, i.e. if it was created from a const
Kokkos::Viewor from a DLPack tensor marked as read-only. Functions giving write access to the data throw an error for read-only arrays.
-
inline const T *data() const¶
Get a pointer to the first element of this array. The pointer is in
MemorySpace, and the data should be accessed usingshape()andstrides().Use
view()with a non-const data type to modify the data.
-
inline const std::vector<int64_t> &strides() const¶
Get the strides of this array, in number of elements.
-
inline bool is_contiguous() const¶
Check if this array is contiguous in row-major order.
-
template<typename DataType, typename Layout = Kokkos::LayoutStride>
inline Kokkos::View<DataType, Layout, MemorySpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>> view() const¶ Get an unmanaged n-dimensional Kokkos view of the data in this array.
DataTypeshould be the Kokkos data type of the view, e.g.double**for a 2-dimensional array ofdouble. The returned view is only valid as long as thisKokkosDataArrayis alive.Layoutcan beKokkos::LayoutStride(the default), which works for all arrays; orKokkos::LayoutRight/Kokkos::LayoutLeft, which only work if the data is contiguous in row-major/column-major order.This function throws an error if the rank or the static extents of
DataTypedo not match the shape of this array, or if the strides of this array are not compatible withLayout. For read-only arrays,DataTypemust be const (e.g.const double**).
Public Static Functions
-
static inline KokkosDataArray &from_mts_array(MtsArray &array)¶
Extract a reference to
KokkosDataArrayout of anMtsArray.This function fails if the
MtsArraydoes not contain aKokkosDataArraywith the sameTandMemorySpace.
-
static inline const KokkosDataArray &from_mts_array(const MtsArray &array)¶
Extract a const reference to
KokkosDataArrayout of anMtsArray.This function fails if the
MtsArraydoes not contain aKokkosDataArraywith the sameTandMemorySpace.
Private Functions
Create a
KokkosDataArrayfrom raw data, with the givenshapeandstrides.keepaliveshould keep the data alive.
-
template<typename DataType, typename ...Properties>