Skip to main content

Array

Trait Array 

Source
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§

Source

fn as_any(&self) -> &dyn Any

Get the array as a Any reference

Source

fn as_any_mut(&mut self) -> &mut dyn Any

Get the array as a mutable Any reference

Source

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.

Source

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.

Source

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).

Source

fn reshape(&mut self, shape: &[usize])

Change the shape of the array to the given shape

Source

fn swap_axes(&mut self, axis_1: usize, axis_2: usize)

Swap the axes axis_1 and axis_2 in this array

Source

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.

Source

fn device(&self) -> DLDevice

Get the device where this array’s data resides.

For CPU arrays this should return DLDevice::cpu().

Source

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 }).

Source

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).

Source

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.

Implementations on Foreign Types§

Source§

impl<T> Array for Arc<RwLock<ArrayD<T>>>
where T: 'static + Send + Sync + Clone + Default + GetDLPackDataType + DLPackPointerCast,

Source§

fn as_any(&self) -> &dyn Any

Source§

fn as_any_mut(&mut self) -> &mut dyn Any

Source§

fn create(&self, shape: &[usize], fill_value: MtsArray) -> Box<dyn Array>

Source§

fn copy(&self, device: DLDevice) -> Box<dyn Array>

Source§

fn shape(&self) -> Vec<usize>

Source§

fn reshape(&mut self, shape: &[usize])

Source§

fn swap_axes(&mut self, axis_1: usize, axis_2: usize)

Source§

fn move_data(&mut self, input: &dyn Array, movements: &[mts_data_movement_t])

Source§

fn device(&self) -> DLDevice

Source§

fn dtype(&self) -> DLDataType

Source§

fn as_dlpack( &self, device: DLDevice, stream: Option<i64>, max_version: DLPackVersion, ) -> Result<DLPackTensor, Error>

Source§

fn from_dlpack( &self, dlpack_tensor: DLPackTensor, ) -> Result<Box<dyn Array>, Error>

Implementors§