Skip to main content

ManagedMemory

Struct ManagedMemory 

Source
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>

Source

pub fn zeroed( context: &Arc<Context>, length: usize, attach_flags: MemoryAttachFlags, ) -> Result<Self>

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.

Source

pub fn filled( context: &Arc<Context>, length: usize, attach_flags: MemoryAttachFlags, value: u8, ) -> Result<Self>

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.

Source

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.

Source

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.

Source

pub fn context(&self) -> &Arc<Context> ⓘ

Returns the CUDA context retained by this allocation or pool.

Source

pub fn allocation(&self) -> &Arc<DeviceAllocation> ⓘ

Returns the allocation owner retained by submissions, graphs, and interop users.

Source

pub fn cast<U: AnyBitPatternDeviceRepr>( self, ) -> StdResult<ManagedMemory<U>, Self>

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.

Source

pub fn into_uninit(self) -> ManagedMemory<MaybeUninit<T>>

Erases initialization while retaining the managed allocation and its attachment flags.

Source

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.

Source

pub const fn len(&self) -> usize

Returns the number of elements in the logical allocation range.

Source

pub const fn is_empty(&self) -> bool

Returns whether the logical allocation range contains no elements.

Source

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.

Source

pub const fn attach_flags(&self) -> MemoryAttachFlags

Returns the attachment policy recorded when the managed allocation was created.

Source

pub const fn as_ptr(&self) -> *const T

Returns the allocation’s CUDA-accessible element pointer without synchronizing pending work.

Source

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.

Source

pub const fn as_device_ptr(&self) -> DevicePtr

Returns the allocation’s address as an untyped CUDA device pointer.

Source

pub fn view(&self) -> DeviceView<'_, T>

Borrows all initialized elements as a shared device view.

Source

pub fn view_mut(&mut self) -> DeviceViewMut<'_, T>

Borrows all initialized elements as an exclusive device view.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn set_zeroes(&mut self) -> Result<()>

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.

Source

pub fn set_value(&mut self, value: u8) -> Result<()>

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>>

Source

pub fn output(&mut self) -> DeviceOutputView<'_, T>

Borrows this managed allocation as a write-only output of T values.

Source

pub unsafe fn assume_init_prefix( self, length: usize, ) -> Result<ManagedMemory<T>>

Exposes an initialized prefix while retaining the full managed allocation.

§Safety

The prefix must contain valid initialized T values and all producing work must have completed.

§Errors

Returns an error if length exceeds the currently exposed element count.

Source

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>

Source

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.

Trait Implementations§

Source§

impl<T: Debug + DeviceRepr> Debug for ManagedMemory<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: DeviceRepr> DeviceOutput<T> for ManagedMemory<T>

Source§

fn as_mut_ptr(&mut self) -> *mut T

Returns a pointer to the writable device output range. Read more
Source§

impl<T: DeviceRepr> DeviceOutput<T> for ManagedMemory<MaybeUninit<T>>

Source§

fn as_mut_ptr(&mut self) -> *mut T

Returns a pointer to the writable device output range. Read more
Source§

impl<T: DeviceRepr> DeviceRange<T> for ManagedMemory<T>

Source§

fn as_device_ptr(&self) -> *const T

Returns the device input pointer without asserting that its values are initialized. Read more
Source§

fn len(&self) -> usize

Returns the number of input elements.
Source§

fn context(&self) -> &Arc<Context> ⓘ

Returns the context associated with the input allocation.
Source§

fn is_empty(&self) -> bool

Returns whether the input range contains no elements.
Source§

fn byte_len(&self) -> Result<usize>

Returns the input range length in bytes. Read more
Source§

impl<T: DeviceRepr> DeviceRange<T> for ManagedMemory<MaybeUninit<T>>

Source§

fn len(&self) -> usize

Returns the number of input elements.
Source§

fn context(&self) -> &Arc<Context> ⓘ

Returns the context associated with the input allocation.
Source§

fn as_device_ptr(&self) -> *const T

Returns the device input pointer without asserting that its values are initialized. Read more
Source§

fn is_empty(&self) -> bool

Returns whether the input range contains no elements.
Source§

fn byte_len(&self) -> Result<usize>

Returns the input range length in bytes. Read more
Source§

impl<T: DeviceRepr> DeviceRegionProvider for ManagedMemory<T>

Source§

fn region(&self) -> DeviceRegion<'_>

Borrows the exact allocation range described by this provider’s pointers.
Source§

impl<T: DeviceRepr> DeviceSlice<T> for ManagedMemory<T>

Source§

impl<T: DeviceRepr> DeviceSliceMut<T> for ManagedMemory<T>

Source§

impl<T: DeviceRepr> PushKernelArg for &ManagedMemory<T>

Source§

fn push_to(self, params: &mut KernelParameters<'_>)

Appends this value in the kernel ABI representation selected by its implementation.
Source§

impl<T: DeviceRepr> PushKernelArg for &mut ManagedMemory<T>

Source§

fn push_to(self, params: &mut KernelParameters<'_>)

Appends this value in the kernel ABI representation selected by its implementation.
Source§

impl<T: DeviceRepr + Send> Send for ManagedMemory<T>

Source§

impl<T: DeviceRepr + Sync> Sync for ManagedMemory<T>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.