pub struct StreamMemoryPool { /* private fields */ }Expand description
A memory pool for stream-ordered allocations.
On platforms with a real CUDA driver (Linux, Windows), creating a pool
calls cuMemPoolCreate under the hood. On macOS (where there is no
NVIDIA driver), pool metadata is tracked locally but any operation that
would require the driver returns Err(CudaError::NotSupported).
§Allocation tracking
The pool tracks allocation counts and byte totals locally for diagnostics. These statistics are maintained even on macOS so that the API surface can be exercised in tests.
Implementations§
Source§impl StreamMemoryPool
impl StreamMemoryPool
Sourcepub fn new(config: StreamOrderedAllocConfig) -> CudaResult<Self>
pub fn new(config: StreamOrderedAllocConfig) -> CudaResult<Self>
Create a new memory pool for the given device.
The configuration is validated before the pool is created. On
platforms with a real CUDA driver, cuMemPoolCreate is invoked.
On macOS, a local-only pool is created for testing purposes.
§Errors
CudaError::InvalidValueif the config fails validation.CudaError::NotSupportedon macOS (pool metadata is still created so that tests can exercise the API).
Sourcepub fn alloc_async(
&mut self,
size: usize,
stream: u64,
) -> CudaResult<StreamAllocation>
pub fn alloc_async( &mut self, size: usize, stream: u64, ) -> CudaResult<StreamAllocation>
Allocate memory on a stream (stream-ordered).
The allocation becomes available when all prior work on the stream
has completed. The returned StreamAllocation tracks the pointer,
size, and ownership.
§Errors
CudaError::InvalidValueifsizeis zero.CudaError::OutOfMemoryifmax_pool_sizewould be exceeded.CudaError::NotSupportedon macOS.
Sourcepub fn free_async(&mut self, alloc: &mut StreamAllocation) -> CudaResult<()>
pub fn free_async(&mut self, alloc: &mut StreamAllocation) -> CudaResult<()>
Free memory on a stream (stream-ordered).
The memory is returned to the pool when all prior work on the stream has completed. The allocation is marked as freed and cannot be freed again.
§Errors
CudaError::InvalidValueif the allocation is already freed.CudaError::NotSupportedon macOS.
Sourcepub fn trim(&mut self, min_bytes_to_keep: usize) -> CudaResult<()>
pub fn trim(&mut self, min_bytes_to_keep: usize) -> CudaResult<()>
Trim the pool, releasing unused memory back to the OS.
At least min_bytes_to_keep bytes of reserved memory will remain
in the pool for future allocations.
§Errors
CudaError::NotSupportedon macOS.
Sourcepub fn stats(&self) -> PoolUsageStats
pub fn stats(&self) -> PoolUsageStats
Get pool usage statistics.
The returned PoolUsageStats combines locally tracked allocation
counts with byte-level information. On macOS, the reserved/used
byte fields mirror the local bookkeeping since no driver is available.
Sourcepub fn set_attribute(&mut self, attr: PoolAttribute) -> CudaResult<()>
pub fn set_attribute(&mut self, attr: PoolAttribute) -> CudaResult<()>
Set a pool attribute.
Only attributes that carry a value (e.g. PoolAttribute::ReleaseThreshold)
modify pool state. Read-only attributes (e.g. ReservedMemCurrent)
return CudaError::InvalidValue.
§Errors
CudaError::InvalidValuefor read-only attributes.CudaError::NotSupportedon macOS.
Sourcepub fn enable_peer_access(&self, peer_device: i32) -> CudaResult<()>
pub fn enable_peer_access(&self, peer_device: i32) -> CudaResult<()>
Enable peer access from another device to allocations in this pool.
After this call, kernels running on peer_device can access memory
allocated from this pool.
§Errors
CudaError::InvalidDeviceifpeer_deviceequals this pool’s device.CudaError::NotSupportedon macOS.
Sourcepub fn disable_peer_access(&self, peer_device: i32) -> CudaResult<()>
pub fn disable_peer_access(&self, peer_device: i32) -> CudaResult<()>
Disable peer access from another device to allocations in this pool.
§Errors
CudaError::InvalidDeviceifpeer_deviceequals this pool’s device.CudaError::NotSupportedon macOS.
Sourcepub fn reset_peak_stats(&mut self)
pub fn reset_peak_stats(&mut self)
Reset peak statistics (peak allocated bytes and peak allocation count).
Sourcepub fn default_pool(device: i32) -> CudaResult<Self>
pub fn default_pool(device: i32) -> CudaResult<Self>
Get the default memory pool for a device.
CUDA provides a default pool per device. On macOS, this returns a local-only pool with default configuration.
§Errors
CudaError::InvalidValueifdeviceis negative.
Sourcepub fn config(&self) -> &StreamOrderedAllocConfig
pub fn config(&self) -> &StreamOrderedAllocConfig
Returns the pool configuration.