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>
impl<F: Float + Debug + Clone + 'static> MemoryPool<F>
Sourcepub fn new(max_pool_size_mb: usize) -> Self
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}
Sourcepub fn allocate(&mut self, shape: &[usize]) -> ArrayD<F>
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}
Sourcepub fn deallocate(&mut self, tensor: ArrayD<F>)
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}
Sourcepub fn get_usage(&self) -> MemoryUsage
pub fn get_usage(&self) -> MemoryUsage
Get current memory usage
Sourcepub fn get_pool_stats(&self) -> PoolStatistics
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> 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> 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