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>
impl<T> DataBuffer<T>
Sourcepub unsafe fn from_external(
ptr: *const T,
len: usize,
release: impl FnOnce() + Send + 'static,
) -> Self
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
ptrmust point to a valid, properly aligned allocation of at leastlenelements ofT.- 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());Sourcepub fn as_slice(&self) -> Option<&[T]>
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][..]));Sourcepub fn as_mut_slice(&mut self) -> Option<&mut [T]>
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);Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn is_owned(&self) -> bool
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());Sourcepub fn is_gpu(&self) -> bool
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());Sourcepub fn is_unique(&self) -> bool
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());Sourcepub fn try_into_vec(self) -> Option<Vec<T>>
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.
Sourcepub fn as_ptr(&self) -> Option<*const T>
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());Sourcepub fn as_device_ptr(&self) -> Option<*const T>
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());Sourcepub fn gpu_memory_space(&self) -> Option<LogicalMemorySpace>
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>
impl<T: Scalar> DataBuffer<T>
Sourcepub fn zeros_on_device(
n_elements: usize,
memory_space: LogicalMemorySpace,
) -> Result<Self>
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]);Sourcepub fn allocate_uninit_on_device(
n_elements: usize,
memory_space: LogicalMemorySpace,
) -> Result<Self>
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);