pub struct VirtualMemoryPool { /* private fields */ }Expand description
Elastic memory pool with virtual memory backing.
This pool reserves a large virtual address space but only allocates physical
memory on-demand when allocate() is called. This enables:
- Large virtual capacity (e.g., 128GB) with minimal initial physical usage
- Dynamic allocation/deallocation based on workload
- Reduced memory waste for bursty workloads
§Example
use candle_cuda_vmm::VirtualMemoryPool;
use candle_core::Device;
let device = Device::new_cuda(0)?;
let mut pool = VirtualMemoryPool::new(
128 * 1024 * 1024 * 1024, // 128GB virtual capacity
2 * 1024 * 1024, // 2MB page size
device,
)?;
// Allocate 1GB of physical memory on-demand
let addr = pool.allocate(0, 1024 * 1024 * 1024)?;
println!("Physical usage: {} bytes", pool.physical_memory_usage());
// Deallocate when done
pool.deallocate(0, 1024 * 1024 * 1024)?;Implementations§
Source§impl VirtualMemoryPool
impl VirtualMemoryPool
Sourcepub fn new(capacity: usize, page_size: usize, device: Device) -> Result<Self>
pub fn new(capacity: usize, page_size: usize, device: Device) -> Result<Self>
Create a new virtual memory pool.
§Arguments
capacity- Maximum virtual address space (e.g., 128GB).page_size- Page granularity (e.g., 2MB for large pages).device- CUDA device.
§Returns
Pool with reserved virtual address space, no physical memory allocated.
§Errors
Returns error if:
- Device is not a CUDA device
- Page size is invalid (not power of 2 or < 64KB)
- Virtual address reservation fails
Sourcepub fn allocate(&mut self, offset: usize, size: usize) -> Result<usize>
pub fn allocate(&mut self, offset: usize, size: usize) -> Result<usize>
Allocate and map physical pages on-demand.
§Arguments
offset- Offset in virtual address space (bytes).size- Number of bytes to allocate.
§Returns
Base virtual address of allocated region.
§Errors
Returns error if:
- Offset/size out of bounds
- Region already allocated
- Physical memory allocation fails
Sourcepub fn physical_memory_usage(&self) -> usize
pub fn physical_memory_usage(&self) -> usize
Get current physical memory usage in bytes.
Sourcepub fn base_address(&self) -> usize
pub fn base_address(&self) -> usize
Get base virtual address.
Sourcepub fn compact(&mut self) -> Result<()>
pub fn compact(&mut self) -> Result<()>
Compact pool by coalescing free pages (no-op for now, future optimization).
Sourcepub fn stats(&self) -> MemoryStats
pub fn stats(&self) -> MemoryStats
Get memory statistics.
Auto Trait Implementations§
impl Freeze for VirtualMemoryPool
impl RefUnwindSafe for VirtualMemoryPool
impl Send for VirtualMemoryPool
impl Sync for VirtualMemoryPool
impl Unpin for VirtualMemoryPool
impl UnwindSafe for VirtualMemoryPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more