pub trait Array:
Any
+ Send
+ Sync {
// Required methods
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
fn create(&self, shape: &[usize], fill_value: MtsArray) -> Box<dyn Array>;
fn copy(&self, device: DLDevice) -> Box<dyn Array>;
fn shape(&self) -> Vec<usize>;
fn reshape(&mut self, shape: &[usize]);
fn swap_axes(&mut self, axis_1: usize, axis_2: usize);
fn move_data(
&mut self,
input: &dyn Array,
movements: &[mts_data_movement_t],
);
fn device(&self) -> DLDevice;
fn dtype(&self) -> DLDataType;
fn as_dlpack(
&self,
device: DLDevice,
stream: Option<i64>,
max_version: DLPackVersion,
) -> Result<DLPackTensor, Error>;
fn from_dlpack(
&self,
dl_tensor: DLPackTensor,
) -> Result<Box<dyn Array>, Error>;
}Expand description
The Array trait is used by metatensor to manage different kind of data array
with a single API. Metatensor only knows about Box<dyn Array>, and
manipulate the data through the functions on this trait.
This corresponds to the mts_array_t struct in metatensor-core.
Required Methods§
Sourcefn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
Get the array as a mutable Any reference
Sourcefn create(&self, shape: &[usize], fill_value: MtsArray) -> Box<dyn Array>
fn create(&self, shape: &[usize], fill_value: MtsArray) -> Box<dyn Array>
Create a new array with the same array origin, data type, and device as
the current one, but with the requested shape.
The new array should be filled with the scalar value from fill_value,
which must be an MtsArray with shape (1,) and the same dtype as this
array.
Sourcefn copy(&self, device: DLDevice) -> Box<dyn Array>
fn copy(&self, device: DLDevice) -> Box<dyn Array>
Make a copy of this array
The new array is expected to have the same array origin and data type, but live on the given device.
Sourcefn shape(&self) -> Vec<usize>
fn shape(&self) -> Vec<usize>
Get the shape of the array. This can be empty if the array has no shape (e.g. a scalar).
Sourcefn swap_axes(&mut self, axis_1: usize, axis_2: usize)
fn swap_axes(&mut self, axis_1: usize, axis_2: usize)
Swap the axes axis_1 and axis_2 in this array
Sourcefn move_data(&mut self, input: &dyn Array, movements: &[mts_data_movement_t])
fn move_data(&mut self, input: &dyn Array, movements: &[mts_data_movement_t])
Set entries in self taking data from the input array.
The output array is guaranteed to be created by calling
mts_array_t::create with one of the arrays in the same block or tensor
map as the input.
The movements indicate where the data should be moved from input to
output.
This function should copy data from input[movements[i].sample_in, ..., movements[i].properties_start_in + x] to
array[movements[i].sample_out, ..., movements[i].properties_start_out + x] for i up to movements_count and x up to
movements[i].properties_length. All indexes are 0-based.
Sourcefn device(&self) -> DLDevice
fn device(&self) -> DLDevice
Get the device where this array’s data resides.
For CPU arrays this should return DLDevice::cpu().
Sourcefn dtype(&self) -> DLDataType
fn dtype(&self) -> DLDataType
Get the data type of this array.
This populates the dtype vtable slot for fast dtype queries.
Implementations should return the appropriate DLDataType for their
element type (e.g. float64 = DLDataType { code: kDLFloat, bits: 64, lanes: 1 }).
Sourcefn as_dlpack(
&self,
device: DLDevice,
stream: Option<i64>,
max_version: DLPackVersion,
) -> Result<DLPackTensor, Error>
fn as_dlpack( &self, device: DLDevice, stream: Option<i64>, max_version: DLPackVersion, ) -> Result<DLPackTensor, Error>
Convert the array to a DLPack tensor.
The returned pointer is owned by the caller (and cleaned up via its deleter).
Sourcefn from_dlpack(&self, dl_tensor: DLPackTensor) -> Result<Box<dyn Array>, Error>
fn from_dlpack(&self, dl_tensor: DLPackTensor) -> Result<Box<dyn Array>, Error>
Create a new array from a DLPack tensor, taking ownership of the
tensor’s data.