Skip to main content

CudaMemPool

Struct CudaMemPool 

Source
pub struct CudaMemPool { /* private fields */ }
Expand description

Safe wrapper around a CUDA memory pool.

The pool amortizes allocation overhead by maintaining a reservoir of device memory. Allocations are fast sub-allocations from this reservoir, and frees return memory to the pool rather than the OS (until the release threshold is exceeded).

§Thread Safety

This type uses internal locking to serialize host-side calls to CUDA driver APIs. cuMemAllocFromPoolAsync is not host-thread reentrant, so concurrent calls from multiple threads must be serialized. The GPU-side operations remain asynchronous and stream-ordered.

Use CudaMemPoolBuilder for configurable pool creation with pre-allocation.

Implementations§

Source§

impl CudaMemPool

Source

pub fn builder( context: Arc<CudaContext>, reserve_size: usize, ) -> CudaMemPoolBuilder

Create a builder for a new CUDA memory pool.

§Arguments
  • context - CUDA context for the device
  • reserve_size - Number of bytes to pre-allocate to warm the pool
Source

pub fn alloc_async(&self, size: usize, stream: &CudaStream) -> Result<u64>

Allocate memory from the pool asynchronously.

This is the safe variant that takes a &CudaStream reference, ensuring the stream is valid for the duration of the call.

The allocation is stream-ordered; the memory is available for use after all preceding operations on the stream complete.

§Host Serialization

This method acquires an internal mutex because cuMemAllocFromPoolAsync is not host-thread reentrant. The allocation itself is stream-ordered on the GPU side.

§Arguments
  • size - Size in bytes to allocate
  • stream - CUDA stream for async ordering
§Returns

Device pointer to the allocated memory

Source

pub unsafe fn alloc_async_raw( &self, size: usize, stream: CUstream, ) -> Result<u64>

Allocate memory from the pool asynchronously (raw stream handle variant).

This is the unsafe variant for use when you have a raw CUstream handle from sources other than cudarc’s CudaStream.

§Host Serialization

This method acquires an internal mutex because cuMemAllocFromPoolAsync is not host-thread reentrant.

§Arguments
  • size - Size in bytes to allocate
  • stream - Raw CUDA stream handle for async ordering
§Returns

Device pointer to the allocated memory

§Safety

The caller must ensure that stream is a valid CUDA stream handle that will remain valid for the duration of this call.

Source

pub fn free_async(&self, ptr: u64, stream: &CudaStream) -> Result<()>

Free memory back to the pool asynchronously.

This is the safe variant that takes a &CudaStream reference.

The memory is returned to the pool’s reservoir (not the OS) and can be reused by subsequent allocations. The free is stream-ordered.

§Arguments
  • ptr - Device pointer previously allocated from this pool
  • stream - CUDA stream for async ordering
Source

pub unsafe fn free_async_raw(&self, ptr: u64, stream: CUstream) -> Result<()>

Free memory back to the pool asynchronously (raw stream handle variant).

This is the unsafe variant for use when you have a raw CUstream handle.

The memory is returned to the pool’s reservoir (not the OS) and can be reused by subsequent allocations. The free is stream-ordered.

§Arguments
  • ptr - Device pointer previously allocated from this pool
  • stream - Raw CUDA stream handle for async ordering
§Safety

The caller must ensure that:

  • ptr is a valid device pointer previously allocated from this pool
  • stream is a valid CUDA stream handle

Trait Implementations§

Source§

impl Drop for CudaMemPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for CudaMemPool

Source§

impl Sync for CudaMemPool

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more