Struct MemoryPool

Source
pub struct MemoryPool<F: Float + Debug> { /* private fields */ }
Expand description

Memory pool for efficient tensor allocation and reuse

Implementations§

Source§

impl<F: Float + Debug + Clone + 'static> MemoryPool<F>

Source

pub fn new(max_pool_size_mb: usize) -> Self

Create a new memory pool

Examples found in repository?
examples/memory_efficient_example.rs (line 47)
43fn demo_memory_pool() -> Result<()> {
44    println!("\n🔄 Memory Pool Demo");
45    println!("------------------");
46
47    let mut pool = MemoryPool::<f32>::new(50); // 50MB max pool size
48
49    // Allocate several tensors
50    println!("Allocating tensors...");
51    let tensor1 = pool.allocate(&[1000, 500]); // ~2MB
52    let tensor2 = pool.allocate(&[500, 200]); // ~400KB
53    let tensor3 = pool.allocate(&[100, 100]); // ~40KB
54
55    let stats = pool.get_pool_stats();
56    println!("Pool stats after allocation:");
57    print_pool_stats(&stats);
58
59    // Return tensors to pool
60    println!("Returning tensors to pool...");
61    pool.deallocate(tensor1);
62    pool.deallocate(tensor2);
63    pool.deallocate(tensor3);
64
65    let stats = pool.get_pool_stats();
66    println!("Pool stats after deallocation:");
67    print_pool_stats(&stats);
68
69    // Reuse tensors
70    println!("Reusing tensors (should be faster)...");
71    let start = Instant::now();
72    let _reused1 = pool.allocate(&[1000, 500]);
73    let _reused2 = pool.allocate(&[500, 200]);
74    let reuse_time = start.elapsed();
75    println!("Reuse time: {:?}", reuse_time);
76
77    let stats = pool.get_pool_stats();
78    println!("Final pool stats:");
79    print_pool_stats(&stats);
80
81    Ok(())
82}
Source

pub fn allocate(&mut self, shape: &[usize]) -> ArrayD<F>

Allocate or reuse a tensor with the given shape

Examples found in repository?
examples/memory_efficient_example.rs (line 51)
43fn demo_memory_pool() -> Result<()> {
44    println!("\n🔄 Memory Pool Demo");
45    println!("------------------");
46
47    let mut pool = MemoryPool::<f32>::new(50); // 50MB max pool size
48
49    // Allocate several tensors
50    println!("Allocating tensors...");
51    let tensor1 = pool.allocate(&[1000, 500]); // ~2MB
52    let tensor2 = pool.allocate(&[500, 200]); // ~400KB
53    let tensor3 = pool.allocate(&[100, 100]); // ~40KB
54
55    let stats = pool.get_pool_stats();
56    println!("Pool stats after allocation:");
57    print_pool_stats(&stats);
58
59    // Return tensors to pool
60    println!("Returning tensors to pool...");
61    pool.deallocate(tensor1);
62    pool.deallocate(tensor2);
63    pool.deallocate(tensor3);
64
65    let stats = pool.get_pool_stats();
66    println!("Pool stats after deallocation:");
67    print_pool_stats(&stats);
68
69    // Reuse tensors
70    println!("Reusing tensors (should be faster)...");
71    let start = Instant::now();
72    let _reused1 = pool.allocate(&[1000, 500]);
73    let _reused2 = pool.allocate(&[500, 200]);
74    let reuse_time = start.elapsed();
75    println!("Reuse time: {:?}", reuse_time);
76
77    let stats = pool.get_pool_stats();
78    println!("Final pool stats:");
79    print_pool_stats(&stats);
80
81    Ok(())
82}
Source

pub fn deallocate(&mut self, tensor: ArrayD<F>)

Return a tensor to the pool for reuse

Examples found in repository?
examples/memory_efficient_example.rs (line 61)
43fn demo_memory_pool() -> Result<()> {
44    println!("\n🔄 Memory Pool Demo");
45    println!("------------------");
46
47    let mut pool = MemoryPool::<f32>::new(50); // 50MB max pool size
48
49    // Allocate several tensors
50    println!("Allocating tensors...");
51    let tensor1 = pool.allocate(&[1000, 500]); // ~2MB
52    let tensor2 = pool.allocate(&[500, 200]); // ~400KB
53    let tensor3 = pool.allocate(&[100, 100]); // ~40KB
54
55    let stats = pool.get_pool_stats();
56    println!("Pool stats after allocation:");
57    print_pool_stats(&stats);
58
59    // Return tensors to pool
60    println!("Returning tensors to pool...");
61    pool.deallocate(tensor1);
62    pool.deallocate(tensor2);
63    pool.deallocate(tensor3);
64
65    let stats = pool.get_pool_stats();
66    println!("Pool stats after deallocation:");
67    print_pool_stats(&stats);
68
69    // Reuse tensors
70    println!("Reusing tensors (should be faster)...");
71    let start = Instant::now();
72    let _reused1 = pool.allocate(&[1000, 500]);
73    let _reused2 = pool.allocate(&[500, 200]);
74    let reuse_time = start.elapsed();
75    println!("Reuse time: {:?}", reuse_time);
76
77    let stats = pool.get_pool_stats();
78    println!("Final pool stats:");
79    print_pool_stats(&stats);
80
81    Ok(())
82}
Source

pub fn get_usage(&self) -> MemoryUsage

Get current memory usage

Source

pub fn clear(&mut self)

Clear the memory pool

Source

pub fn get_pool_stats(&self) -> PoolStatistics

Get pool statistics

Examples found in repository?
examples/memory_efficient_example.rs (line 55)
43fn demo_memory_pool() -> Result<()> {
44    println!("\n🔄 Memory Pool Demo");
45    println!("------------------");
46
47    let mut pool = MemoryPool::<f32>::new(50); // 50MB max pool size
48
49    // Allocate several tensors
50    println!("Allocating tensors...");
51    let tensor1 = pool.allocate(&[1000, 500]); // ~2MB
52    let tensor2 = pool.allocate(&[500, 200]); // ~400KB
53    let tensor3 = pool.allocate(&[100, 100]); // ~40KB
54
55    let stats = pool.get_pool_stats();
56    println!("Pool stats after allocation:");
57    print_pool_stats(&stats);
58
59    // Return tensors to pool
60    println!("Returning tensors to pool...");
61    pool.deallocate(tensor1);
62    pool.deallocate(tensor2);
63    pool.deallocate(tensor3);
64
65    let stats = pool.get_pool_stats();
66    println!("Pool stats after deallocation:");
67    print_pool_stats(&stats);
68
69    // Reuse tensors
70    println!("Reusing tensors (should be faster)...");
71    let start = Instant::now();
72    let _reused1 = pool.allocate(&[1000, 500]);
73    let _reused2 = pool.allocate(&[500, 200]);
74    let reuse_time = start.elapsed();
75    println!("Reuse time: {:?}", reuse_time);
76
77    let stats = pool.get_pool_stats();
78    println!("Final pool stats:");
79    print_pool_stats(&stats);
80
81    Ok(())
82}

Auto Trait Implementations§

§

impl<F> Freeze for MemoryPool<F>

§

impl<F> RefUnwindSafe for MemoryPool<F>
where F: RefUnwindSafe,

§

impl<F> Send for MemoryPool<F>
where F: Send,

§

impl<F> Sync for MemoryPool<F>
where F: Sync,

§

impl<F> Unpin for MemoryPool<F>

§

impl<F> UnwindSafe for MemoryPool<F>
where F: RefUnwindSafe,

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, 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> IntoEither for T

Source§

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

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V