DataBuffer

Struct DataBuffer 

Source
pub struct DataBuffer<T> { /* private fields */ }
Expand description

Data storage for tensor elements.

Abstracts over ownership: data may be Rust-owned (Vec<T>), externally-owned (e.g., imported via DLPack with a release callback), or GPU device memory. Shape and stride metadata are not stored here.

§Shared ownership

DataBuffer uses Arc, so cloning a tensor or buffer is shallow and shares storage. Deep copies happen only when a tensor is materialized into a new contiguous allocation.

§Examples

use tenferro_tensor::DataBuffer;

let buf = DataBuffer::<f64>::from_vec(vec![1.0, 2.0, 3.0]);
assert_eq!(buf.len(), 3);

Implementations§

Source§

impl<T> DataBuffer<T>

Source

pub fn from_vec(v: Vec<T>) -> Self

Create a buffer from an owned Vec<T>.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::from_vec(vec![1.0, 2.0, 3.0]);
assert!(buf.is_owned());
Source

pub unsafe fn from_external( ptr: *const T, len: usize, release: impl FnOnce() + Send + 'static, ) -> Self

Create a buffer from externally-owned data with a release callback.

§Safety
  • ptr must point to a valid, properly aligned allocation of at least len elements of T.
  • The allocation must remain valid until the release callback fires.
§Examples
use tenferro_tensor::DataBuffer;

let data = vec![1.0, 2.0, 3.0];
let ptr = data.as_ptr();
let len = data.len();
let buf = unsafe { DataBuffer::from_external(ptr, len, move || drop(data)) };
assert!(!buf.is_owned());
Source

pub fn as_slice(&self) -> Option<&[T]>

Returns the raw data as a slice for CPU-accessible buffers.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::from_vec(vec![1.0, 2.0, 3.0]);
assert_eq!(buf.as_slice(), Some(&[1.0, 2.0, 3.0][..]));
Source

pub fn as_mut_slice(&mut self) -> Option<&mut [T]>

Returns the raw data as a mutable slice, if uniquely owned.

§Examples
use tenferro_tensor::DataBuffer;

let mut buf = DataBuffer::from_vec(vec![1.0, 2.0]);
if let Some(slice) = buf.as_mut_slice() {
    slice[0] = 42.0;
}
assert_eq!(buf.as_slice().unwrap()[0], 42.0);
Source

pub fn len(&self) -> usize

Returns the number of elements in the buffer.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::from_vec(vec![1.0, 2.0, 3.0]);
assert_eq!(buf.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if the buffer has no elements.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::<f64>::from_vec(vec![]);
assert!(buf.is_empty());
Source

pub fn is_owned(&self) -> bool

Returns true if the buffer is Rust-owned.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::from_vec(vec![1.0f64]);
assert!(buf.is_owned());
Source

pub fn is_gpu(&self) -> bool

Returns true if the buffer resides on GPU device memory.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::from_vec(vec![1.0f64]);
assert!(!buf.is_gpu());
Source

pub fn is_unique(&self) -> bool

Returns true if this is the only reference to the underlying buffer.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::from_vec(vec![1.0f64]);
assert!(buf.is_unique());
let buf2 = buf.clone();
assert!(!buf.is_unique());
Source

pub fn try_into_vec(self) -> Option<Vec<T>>

Extract the inner Vec<T> if this is the sole owner of a CPU-owned buffer.

Source

pub fn as_ptr(&self) -> Option<*const T>

Returns a raw CPU pointer to the data, or None for GPU buffers.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::from_vec(vec![1.0f64]);
assert!(buf.as_ptr().is_some());
Source

pub fn as_device_ptr(&self) -> Option<*const T>

Returns the GPU device pointer, or None for CPU buffers.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::from_vec(vec![1.0f64]);
assert!(buf.as_device_ptr().is_none());
Source

pub fn gpu_memory_space(&self) -> Option<LogicalMemorySpace>

Returns the logical memory space of a GPU buffer, or None for CPU buffers.

§Examples
use tenferro_tensor::DataBuffer;

let buf = DataBuffer::from_vec(vec![1.0f64]);
assert!(buf.gpu_memory_space().is_none());
Source§

impl<T: Scalar> DataBuffer<T>

Source

pub fn zeros_on_device( n_elements: usize, memory_space: LogicalMemorySpace, ) -> Result<Self>

Allocate a zero-filled buffer on the specified device.

For CPU (MainMemory) this creates an owned Vec<T> filled with T::zero(). For CUDA GPU memory (when the cuda feature is enabled) this allocates directly on the device and zero-fills via host-to-device copy.

§Errors

Returns an error for unsupported memory spaces or if device allocation fails.

§Examples
use tenferro_tensor::DataBuffer;
use tenferro_device::LogicalMemorySpace;

let buf = DataBuffer::<f64>::zeros_on_device(4, LogicalMemorySpace::MainMemory).unwrap();
assert_eq!(buf.as_slice().unwrap(), &[0.0; 4]);
Source

pub fn allocate_uninit_on_device( n_elements: usize, memory_space: LogicalMemorySpace, ) -> Result<Self>

Allocate an uninitialized buffer on the specified device.

For CPU (MainMemory) this creates an owned Vec<T> with the requested capacity. The contents are unspecified (currently zero-filled for safety, but callers must not rely on that).

For CUDA GPU memory (when the cuda feature is enabled) this allocates device memory without initialization.

§Errors

Returns an error for unsupported memory spaces or if device allocation fails.

§Examples
use tenferro_tensor::DataBuffer;
use tenferro_device::LogicalMemorySpace;

let buf = DataBuffer::<f64>::allocate_uninit_on_device(4, LogicalMemorySpace::MainMemory).unwrap();
assert_eq!(buf.len(), 4);

Trait Implementations§

Source§

impl<T> Clone for DataBuffer<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Send> Send for DataBuffer<T>

Source§

impl<T: Sync> Sync for DataBuffer<T>

Auto Trait Implementations§

§

impl<T> Freeze for DataBuffer<T>

§

impl<T> !RefUnwindSafe for DataBuffer<T>

§

impl<T> Unpin for DataBuffer<T>

§

impl<T> !UnwindSafe for DataBuffer<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.