pub struct Buffer { /* private fields */ }Expand description
A device buffer that may be a view into another buffer.
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn new(
allocator: Arc<dyn Allocator>,
dtype: DType,
shape: Vec<usize>,
options: BufferOptions,
) -> Self
pub fn new( allocator: Arc<dyn Allocator>, dtype: DType, shape: Vec<usize>, options: BufferOptions, ) -> Self
Create a new buffer with lazy allocation.
Sourcepub fn allocate(
allocator: Arc<dyn Allocator>,
dtype: DType,
shape: Vec<usize>,
options: BufferOptions,
) -> Result<Self>
pub fn allocate( allocator: Arc<dyn Allocator>, dtype: DType, shape: Vec<usize>, options: BufferOptions, ) -> Result<Self>
Create a new buffer with immediate allocation.
Sourcepub fn ensure_allocated(&self) -> Result<()>
pub fn ensure_allocated(&self) -> Result<()>
Ensure the underlying buffer is allocated.
Sourcepub fn is_allocated(&self) -> bool
pub fn is_allocated(&self) -> bool
Check if the buffer is allocated.
Sourcepub fn as_host_bytes(&self) -> Result<&[u8]>
pub fn as_host_bytes(&self) -> Result<&[u8]>
Get a byte slice of the buffer data (CPU-accessible buffers only).
Zero-copy. For realized tensors after realize(), this is safe because
the scheduler guarantees no concurrent kernel writes.
§Errors
NotAllocatedif buffer hasn’t been allocatedNotCpuAccessiblefor CUDA device buffers (usecopyoutinstead)
Sourcepub fn as_host_bytes_mut(&self) -> Result<&mut [u8]>
pub fn as_host_bytes_mut(&self) -> Result<&mut [u8]>
Sourcepub fn as_array<T: HasDType>(&self) -> Result<ArrayViewD<'_, T>>
pub fn as_array<T: HasDType>(&self) -> Result<ArrayViewD<'_, T>>
Typed immutable view over CPU-accessible buffer memory.
Returns an ndarray view shaped according to the buffer’s concrete dimensions. Only works for CPU-accessible buffers — fails for device-only CUDA memory.
§Errors
TypeMismatchifT::DTYPEdoesn’t match buffer dtypeNotCpuAccessiblefor non-CPU-accessible buffersNotAllocatedif buffer hasn’t been allocated
Sourcepub fn as_array_mut<T: HasDType>(&self) -> Result<ArrayViewMutD<'_, T>>
pub fn as_array_mut<T: HasDType>(&self) -> Result<ArrayViewMutD<'_, T>>
Typed mutable view over CPU-accessible buffer memory.
Same as [as_array] but allows writes. Caller must ensure no kernels
are executing concurrently (safety is the caller’s responsibility).
§Errors
Same as [as_array].
Sourcepub fn as_slice<T: HasDType>(&self) -> Result<&[T]>
pub fn as_slice<T: HasDType>(&self) -> Result<&[T]>
Zero-copy typed slice view (CPU-accessible only).
Sourcepub fn item<T: HasDType + Copy>(&self) -> Result<T>
pub fn item<T: HasDType + Copy>(&self) -> Result<T>
Read a single scalar value from the buffer (CPU-accessible only).
Panics if the buffer contains more than one element.
Sourcepub fn id(&self) -> BufferId
pub fn id(&self) -> BufferId
Get the unique identifier for this buffer’s underlying allocation.
Views into the same buffer share the same ID. This is used for dependency tracking in parallel execution.
Sourcepub fn copyin(&mut self, src: &[u8]) -> Result<()>
pub fn copyin(&mut self, src: &[u8]) -> Result<()>
Copy data from host memory into this buffer.
Sourcepub fn copy_from(&mut self, src: &Buffer) -> Result<()>
pub fn copy_from(&mut self, src: &Buffer) -> Result<()>
Copy data from another buffer to this buffer.
Sourcepub fn synchronize(&self) -> Result<()>
pub fn synchronize(&self) -> Result<()>
Synchronize the device (wait for all operations to complete).
Sourcepub unsafe fn as_raw_ptr(&self) -> *mut u8
pub unsafe fn as_raw_ptr(&self) -> *mut u8
Get a raw pointer to the buffer data for kernel execution.
§Safety
The returned pointer is only valid while the buffer is allocated. The caller must ensure:
- Buffer remains allocated during pointer lifetime
- No conflicting accesses occur during kernel execution
- Pointer is not used after buffer is freed
§Panics
Panics if the buffer is not yet allocated.