pub struct ManagedBuffer<T: DeviceRepr> { /* private fields */ }Expand description
Owned allocation of unified (managed) memory — a single pointer that
is accessible from both the host and the GPU, with on-demand migration
handled by the driver. Compare with DeviceBuffer, which is
device-only and requires explicit memcpys.
On a discrete GPU, accessing the buffer from host code after a kernel
finishes (and vice versa) requires a stream synchronize in the
unified-memory model; ManagedBuffer::as_host_slice assumes the
caller has done that.
Implementations§
Source§impl<T: DeviceRepr> ManagedBuffer<T>
impl<T: DeviceRepr> ManagedBuffer<T>
Sourcepub fn new(context: &Context, len: usize) -> Result<Self>
pub fn new(context: &Context, len: usize) -> Result<Self>
Allocate len elements of unified memory with the default
(ManagedAttach::Global) attach mode.
Sourcepub fn new_with_flags(
context: &Context,
len: usize,
attach: ManagedAttach,
) -> Result<Self>
pub fn new_with_flags( context: &Context, len: usize, attach: ManagedAttach, ) -> Result<Self>
Allocate with an explicit ManagedAttach mode.
Sourcepub fn advise(&self, advice: MemAdvise, device: &Device) -> Result<()>
pub fn advise(&self, advice: MemAdvise, device: &Device) -> Result<()>
Provide a hint to the Unified-Memory subsystem about how this range
will be accessed. device is the ordinal this advice targets (e.g.
the compute device for SET_ACCESSED_BY); pass the current device’s
ordinal when in doubt.
Sourcepub fn prefetch_async(&self, device: &Device, stream: &Stream) -> Result<()>
pub fn prefetch_async(&self, device: &Device, stream: &Stream) -> Result<()>
Asynchronously prefetch this range to device on stream.
Sourcepub unsafe fn as_host_slice(&self) -> &[T]
pub unsafe fn as_host_slice(&self) -> &[T]
Access the buffer as a host slice. Safe to call on integrated GPUs or after a synchronize on discrete GPUs; otherwise you’ll see stale data.
§Safety
The caller must ensure:
- No concurrent kernel is writing to this buffer.
- On discrete GPUs, a relevant synchronize has been issued since the last device-side write.
Sourcepub unsafe fn as_host_slice_mut(&mut self) -> &mut [T]
pub unsafe fn as_host_slice_mut(&mut self) -> &mut [T]
Mutable host view. Same safety rules as as_host_slice.
§Safety
The caller must ensure no concurrent device or host access.
Sourcepub fn as_raw(&self) -> CUdeviceptr
pub fn as_raw(&self) -> CUdeviceptr
Raw device pointer — the same value as the host pointer under UVM.