pub struct ManagedMemory<T: DeviceRepr> { /* private fields */ }Expand description
Owns a CUDA managed allocation and retains its context.
Host access through the guarded callback methods waits for tracked GPU work and excludes conflicting submissions for the duration of the callback.
Implementations§
Source§impl<T: DeviceRepr> ManagedMemory<T>
impl<T: DeviceRepr> ManagedMemory<T>
Sourcepub fn zeroed(
context: &Arc<Context>,
length: usize,
attach_flags: MemoryAttachFlags,
) -> Result<Self>where
T: ZeroableDeviceRepr,
pub fn zeroed(
context: &Arc<Context>,
length: usize,
attach_flags: MemoryAttachFlags,
) -> Result<Self>where
T: ZeroableDeviceRepr,
Allocates CUDA managed memory and completes zero initialization.
§Errors
Returns an error if the byte size overflows, the context cannot be bound, CUDA cannot allocate the memory, access tracking fails, or CUDA cannot fill or synchronize.
Sourcepub fn filled(
context: &Arc<Context>,
length: usize,
attach_flags: MemoryAttachFlags,
value: u8,
) -> Result<Self>where
T: AnyBitPatternDeviceRepr,
pub fn filled(
context: &Arc<Context>,
length: usize,
attach_flags: MemoryAttachFlags,
value: u8,
) -> Result<Self>where
T: AnyBitPatternDeviceRepr,
Allocates CUDA managed memory and completes its repeated byte fill.
§Errors
Returns an error if the byte size overflows, the context cannot be bound, CUDA cannot allocate the memory, access tracking fails, or CUDA cannot fill or synchronize.
Sourcepub unsafe fn from_raw(raw: RawManagedMemory<T>) -> Result<Self>
pub unsafe fn from_raw(raw: RawManagedMemory<T>) -> Result<Self>
Takes ownership of an existing managed allocation.
§Safety
raw.ptr must be null for an empty allocation or identify live managed storage for raw.len elements in raw.context, allocated with cudaMallocManaged.
The attachment flags must describe that allocation’s policy.
No other owner may free it, and each element must be initialized before it is read.
§Errors
Returns an error if the byte length overflows or a nonempty allocation has a null pointer.
Sourcepub fn into_raw(self) -> StdResult<RawManagedMemory<T>, Self>
pub fn into_raw(self) -> StdResult<RawManagedMemory<T>, Self>
Transfers the allocation when no submission, graph, or foreign owner retains it.
Returns the unchanged buffer if the allocation is shared. On success the caller must free the pointer and keep its returned context alive until then.
§Errors
Returns the wrapper unchanged as the error if another owner, submission, graph, or foreign handle retains the allocation.
Sourcepub fn context(&self) -> &Arc<Context> ⓘ
pub fn context(&self) -> &Arc<Context> ⓘ
Returns the CUDA context retained by this allocation or pool.
Sourcepub fn allocation(&self) -> &Arc<DeviceAllocation> ⓘ
pub fn allocation(&self) -> &Arc<DeviceAllocation> ⓘ
Returns the allocation owner retained by submissions, graphs, and interop users.
Sourcepub fn cast<U: AnyBitPatternDeviceRepr>(
self,
) -> StdResult<ManagedMemory<U>, Self>where
T: NoUninitDeviceRepr,
pub fn cast<U: AnyBitPatternDeviceRepr>(
self,
) -> StdResult<ManagedMemory<U>, Self>where
T: NoUninitDeviceRepr,
Reinterprets initialized elements while preserving managed allocation ownership and attachment flags.
§Errors
Returns the allocation unchanged as the error if U is zero-sized, its size does not divide the exposed byte length, or a nonempty range is not aligned for U.
Sourcepub fn into_uninit(self) -> ManagedMemory<MaybeUninit<T>>
pub fn into_uninit(self) -> ManagedMemory<MaybeUninit<T>>
Erases initialization while retaining the managed allocation and its attachment flags.
Sourcepub fn create_uninit(
context: &Arc<Context>,
length: usize,
attach_flags: MemoryAttachFlags,
) -> Result<ManagedMemory<MaybeUninit<T>>>
pub fn create_uninit( context: &Arc<Context>, length: usize, attach_flags: MemoryAttachFlags, ) -> Result<ManagedMemory<MaybeUninit<T>>>
Allocates CUDA managed memory without initializing its elements.
§Errors
Returns an error if the byte count overflows, a nonempty zero-sized allocation is requested, the attachment policy is unsupported, context binding fails, or CUDA cannot allocate managed memory.
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the number of elements in the logical allocation range.
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns whether the logical allocation range contains no elements.
Sourcepub fn byte_len(&self) -> usize
pub fn byte_len(&self) -> usize
Returns the size of the logical allocation range in bytes.
§Panics
Panics if the element count multiplied by the element size overflows usize.
Allocation construction validates this size, so this condition cannot occur for a valid allocation.
Sourcepub const fn attach_flags(&self) -> MemoryAttachFlags
pub const fn attach_flags(&self) -> MemoryAttachFlags
Returns the attachment policy recorded when the managed allocation was created.
Sourcepub const fn as_ptr(&self) -> *const T
pub const fn as_ptr(&self) -> *const T
Returns the allocation’s CUDA-accessible element pointer without synchronizing pending work.
Sourcepub const fn as_mut_ptr(&mut self) -> *mut T
pub const fn as_mut_ptr(&mut self) -> *mut T
Returns the allocation’s CUDA-accessible mutable element pointer.
The pointer does not establish execution ordering or permit conflicting accesses.
Sourcepub const fn as_device_ptr(&self) -> DevicePtr
pub const fn as_device_ptr(&self) -> DevicePtr
Returns the allocation’s address as an untyped CUDA device pointer.
Sourcepub fn view(&self) -> DeviceView<'_, T>
pub fn view(&self) -> DeviceView<'_, T>
Borrows all initialized elements as a shared device view.
Sourcepub fn view_mut(&mut self) -> DeviceViewMut<'_, T>
pub fn view_mut(&mut self) -> DeviceViewMut<'_, T>
Borrows all initialized elements as an exclusive device view.
Sourcepub fn with_host_slice<R>(&self, read: impl FnOnce(&[T]) -> R) -> Result<R>
pub fn with_host_slice<R>(&self, read: impl FnOnce(&[T]) -> R) -> Result<R>
Completes submitted accesses and excludes tracked GPU work while reading managed values. Requires concurrent managed access because unrelated streams may remain active.
§Errors
Returns Error::ManagedHostAccessUnsupported if the device lacks concurrent managed access.
Returns an error if the device attribute query, access tracking, or waiting for submitted work fails, before the callback runs.
Sourcepub fn with_host_slice_mut<R>(
&mut self,
write: impl FnOnce(&mut [T]) -> R,
) -> Result<R>
pub fn with_host_slice_mut<R>( &mut self, write: impl FnOnce(&mut [T]) -> R, ) -> Result<R>
Completes submitted accesses and excludes tracked GPU work while modifying managed values. Requires concurrent managed access support because unrelated streams may remain active.
§Errors
Returns Error::ManagedHostAccessUnsupported if the device lacks concurrent managed access.
Returns an error if the device attribute query, access tracking, or waiting for submitted work fails, before the callback runs.
Sourcepub unsafe fn as_host_slice(&self) -> &[T]
pub unsafe fn as_host_slice(&self) -> &[T]
Returns a host slice over this managed allocation.
§Safety
The caller must ensure no GPU work or other CPU reference can concurrently mutate the same memory for the returned lifetime, and that the allocation is accessible from the host at the point of access.
Sourcepub unsafe fn as_host_slice_mut(&mut self) -> &mut [T]
pub unsafe fn as_host_slice_mut(&mut self) -> &mut [T]
Returns a mutable host slice over this managed allocation.
§Safety
The caller must ensure no GPU work or other CPU reference can concurrently access the same memory for the returned lifetime, and that the allocation is accessible from the host at the point of access.
Sourcepub fn set_zeroes(&mut self) -> Result<()>where
T: ZeroableDeviceRepr,
pub fn set_zeroes(&mut self) -> Result<()>where
T: ZeroableDeviceRepr,
Fills every element with zero bytes and waits for completion.
§Errors
Returns an error if access tracking fails, the context cannot be bound, the byte size overflows, or CUDA cannot fill the memory or synchronize the default stream.
Sourcepub fn set_value(&mut self, value: u8) -> Result<()>where
T: AnyBitPatternDeviceRepr,
pub fn set_value(&mut self, value: u8) -> Result<()>where
T: AnyBitPatternDeviceRepr,
Fills every byte with value and waits for completion.
§Errors
Returns an error if access tracking fails, the context cannot be bound, the byte size overflows, or CUDA cannot fill the memory or synchronize the default stream.
Sourcepub fn prefetch_to(
&self,
location: MemoryLocation,
stream: &Stream,
) -> Result<()>
pub fn prefetch_to( &self, location: MemoryLocation, stream: &Stream, ) -> Result<()>
Enqueues a residency prefetch of the entire managed range to location.
The stream orders the request; completion is asynchronous.
§Errors
Returns an error if the stream belongs to another context, byte-size calculation overflows, context binding fails, or CUDA rejects the prefetch request or destination.
Sourcepub fn advise(
&self,
advice: MemoryAdvise,
location: MemoryLocation,
) -> Result<()>
pub fn advise( &self, advice: MemoryAdvise, location: MemoryLocation, ) -> Result<()>
Applies a managed-memory advice policy to this allocation.
§Errors
Returns an error if the context cannot be bound or CUDA rejects the advice or location.
Sourcepub fn set_read_mostly(&self) -> Result<()>
pub fn set_read_mostly(&self) -> Result<()>
Advises CUDA that this managed allocation is mostly read.
CUDA may create read-only copies of pages on processors that read this range. Writes remain coherent but can require additional migration or invalidation work.
§Errors
Returns an error if CUDA rejects the advice for this allocation.
Sourcepub fn unset_read_mostly(&self) -> Result<()>
pub fn unset_read_mostly(&self) -> Result<()>
Removes explicit read-mostly advice from this managed allocation.
§Errors
Returns an error if CUDA rejects the advice for this allocation.
Sourcepub fn set_preferred_location(&self, location: MemoryLocation) -> Result<()>
pub fn set_preferred_location(&self, location: MemoryLocation) -> Result<()>
Advises CUDA to prefer placing this managed allocation at location.
This guides migration policy but does not immediately migrate pages.
Use ManagedMemory::prefetch_to when an explicit migration is desired.
§Errors
Returns an error if CUDA rejects the location or advice for this allocation.
Sourcepub fn unset_preferred_location(&self) -> Result<()>
pub fn unset_preferred_location(&self) -> Result<()>
Clears the preferred-location advice for this managed allocation.
§Errors
Returns an error if CUDA rejects the advice for this allocation.
Sourcepub fn set_accessed_by(&self, location: MemoryLocation) -> Result<()>
pub fn set_accessed_by(&self, location: MemoryLocation) -> Result<()>
Advises CUDA that location will access this managed allocation.
This can establish or maintain mappings for the target processor without migrating the data there.
§Errors
Returns an error if CUDA rejects the location or advice for this allocation.
Sourcepub fn unset_accessed_by(&self, location: MemoryLocation) -> Result<()>
pub fn unset_accessed_by(&self, location: MemoryLocation) -> Result<()>
Clears accessed-by advice for location.
§Errors
Returns an error if CUDA rejects the location or advice for this allocation.
Sourcepub fn attach_to_stream(
&mut self,
stream: &Stream,
flags: MemoryAttachFlags,
) -> Result<()>
pub fn attach_to_stream( &mut self, stream: &Stream, flags: MemoryAttachFlags, ) -> Result<()>
Attaches this managed allocation to stream with the requested attach flags.
This enqueues the attachment on stream and updates the stored attach flags after CUDA accepts the request.
§Errors
Returns an error if CUDA cannot attach the allocation to stream, the flags are invalid for the allocation, or a previous asynchronous launch reports an error.
Source§impl<T: DeviceRepr> ManagedMemory<MaybeUninit<T>>
impl<T: DeviceRepr> ManagedMemory<MaybeUninit<T>>
Sourcepub fn output(&mut self) -> DeviceOutputView<'_, T>
pub fn output(&mut self) -> DeviceOutputView<'_, T>
Borrows this managed allocation as a write-only output of T values.
Sourcepub unsafe fn assume_init_prefix(
self,
length: usize,
) -> Result<ManagedMemory<T>>
pub unsafe fn assume_init_prefix( self, length: usize, ) -> Result<ManagedMemory<T>>
Sourcepub unsafe fn assume_init(self) -> ManagedMemory<T>
pub unsafe fn assume_init(self) -> ManagedMemory<T>
Marks every element initialized after its producer has completed.
§Safety
Every element must contain a valid T and all producer writes must have completed.
Source§impl<T: DeviceRepr> ManagedMemory<T>
impl<T: DeviceRepr> ManagedMemory<T>
Sourcepub fn location_info(
&self,
summary_granularity: usize,
sampling_granularity: usize,
) -> Result<Vec<MemoryLocation>>
pub fn location_info( &self, summary_granularity: usize, sampling_granularity: usize, ) -> Result<Vec<MemoryLocation>>
Returns the residency of this memory range, one location per summary region.
§Errors
Returns an error if a granularity is invalid, the byte size overflows, the context cannot be bound, the driver entry point is unavailable, or CUDA rejects the location query.