pub struct SharedBytesAllocationController { /* private fields */ }Expand description
Allocation controller backed by bytes::Bytes for zero-copy access.
This enables zero-copy tensor loading by referencing data directly from the source buffer without copying into heap-allocated memory.
§Notes
- Read access via
memory()is zero-copy as long as no mutable access has been requested. - Mutable access via
memory_mut()triggers a copy of the data into heap memory (copy-on-write semantics), behind aOnceso the copy happens exactly once even under concurrent access. - The underlying
bytes::Bytesremains valid for the lifetime of this controller. This is ensured bybytes::Bytesusing reference counting internally.
Implementations§
Sourcepub fn new(
bytes: Bytes,
property: AllocationProperty,
) -> SharedBytesAllocationController
pub fn new( bytes: Bytes, property: AllocationProperty, ) -> SharedBytesAllocationController
Create a new controller from a bytes::Bytes buffer with a specific
allocation property.
The allocation property is used by GPU backends to optimize data transfers:
AllocationProperty::File: Uses pinned memory staging buffers for faster DMA transfers (useful for memory-mapped files)AllocationProperty::Native: Data is in heap memoryAllocationProperty::Other: Unknown backing storage
§Example
use cubecl_common::bytes::{SharedBytesAllocationController, AllocationProperty};
// From static data (zero-copy, no heap allocation)
let static_bytes = bytes::Bytes::from_static(&[1, 2, 3, 4]);
let controller = SharedBytesAllocationController::new(static_bytes, AllocationProperty::Other);
// Memory-mapped file data - use File property for optimized GPU transfers
let mmap_bytes = bytes::Bytes::from_static(&[1, 2, 3, 4]); // pretend this is mmap
let controller = SharedBytesAllocationController::new(mmap_bytes, AllocationProperty::File);Trait Implementations§
Source§fn alloc_align(&self) -> usize
fn alloc_align(&self) -> usize
The alignment this allocation was created with.
Source§fn property(&self) -> AllocationProperty
fn property(&self) -> AllocationProperty
Returns memory property for the current allocation.
Source§fn capacity(&self) -> usize
fn capacity(&self) -> usize
The total byte capacity of the allocation, WITHOUT materializing lazy storage. Read more
Source§fn memory(
&self,
policy: AccessPolicy,
) -> Result<&[MaybeUninit<u8>], AccessError>
fn memory( &self, policy: AccessPolicy, ) -> Result<&[MaybeUninit<u8>], AccessError>
Returns a view of the memory of the whole allocation, subject to
policy. Read moreSource§unsafe fn memory_mut(
&mut self,
policy: AccessPolicy,
) -> Result<&mut [MaybeUninit<u8>], AccessError>
unsafe fn memory_mut( &mut self, policy: AccessPolicy, ) -> Result<&mut [MaybeUninit<u8>], AccessError>
Returns a mutable view of the memory of the whole allocation, subject to
policy. Read moreSource§fn split(
&mut self,
offset: usize,
) -> Result<(Box<dyn AllocationController>, Box<dyn AllocationController>), SplitError>
fn split( &mut self, offset: usize, ) -> Result<(Box<dyn AllocationController>, Box<dyn AllocationController>), SplitError>
Splits the current allocation in multiple separate allocations.
Source§fn view(
&self,
start: usize,
end: usize,
) -> Option<Box<dyn AllocationController>>
fn view( &self, start: usize, end: usize, ) -> Option<Box<dyn AllocationController>>
Returns a zero-copy controller over the sub-range
[start, end) of this
allocation when the backend supports cheap sub-views (files and shared
buffers), borrowing &self. Returns None otherwise.Source§fn duplicate(&self) -> Option<Box<dyn AllocationController>>
fn duplicate(&self) -> Option<Box<dyn AllocationController>>
Duplicates the current allocation with a clone on write strategy if the allocation
controller supports it.
Source§unsafe fn copy_into(&self, buf: &mut [u8])
unsafe fn copy_into(&self, buf: &mut [u8])
Reads the data from the current allocation controller and copy its content into the provided
buffer. Read more
Auto Trait Implementations§
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more