Skip to main content

cubecl_runtime/memory_management/
mod.rs

1pub(crate) mod memory_pool;
2
3mod base;
4
5/// Export utilities to keep track of CPU buffers when performing async data copies.
6pub mod drop_queue;
7
8pub use base::*;
9
10/// Dynamic memory management strategy.
11mod memory_manage;
12pub use memory_manage::*;
13
14use alloc::vec::Vec;
15
16/// The type of memory pool to use.
17#[derive(Debug, Clone)]
18pub enum PoolType {
19    /// Use a memory where every allocation is a separate page.
20    ExclusivePages {
21        /// The minimum number of bytes to allocate in this pool.
22        max_alloc_size: u64,
23    },
24    /// Use a memory where each allocation is a slice of a bigger allocation.
25    SlicedPages {
26        /// The page size to allocate.
27        page_size: u64,
28        /// The maximum size of a slice to allocate in the pool.
29        max_slice_size: u64,
30        /// Hard cap on the total bytes of pages this pool may hold.
31        ///
32        /// The effective cap is `floor(max_pool_size / page_size)` whole pages.
33        /// If `max_pool_size < page_size`, the page size is shrunk to the
34        /// (alignment-rounded) cap so the budget is honored with a single page.
35        /// When the cap is reached and no free slice fits after coalescing,
36        /// reserving returns [`IoError`](crate::server::IoError)
37        /// `PoolCapacityExceeded` instead of silently growing. `None` (the
38        /// previous behavior) keeps unbounded growth.
39        ///
40        /// Note: runtimes that create one memory management per stream (CUDA,
41        /// HIP) apply the cap per stream.
42        max_pool_size: Option<u64>,
43    },
44}
45
46/// Options to create a memory pool.
47#[derive(Debug, Clone)]
48pub struct MemoryPoolOptions {
49    /// What kind of pool to use.
50    pub pool_type: PoolType,
51    /// Period after which allocations are deemed unused and deallocated.
52    ///
53    /// This period is measured in the number of allocations in the parent allocator. If a page
54    /// in the pool was unused for the entire period, it will be deallocated. This period is
55    /// approximmate, as checks are only done occasionally.
56    pub dealloc_period: Option<u64>,
57}
58
59/// High level configuration of memory management.
60#[derive(Clone, Debug)]
61pub enum MemoryConfiguration {
62    /// The default preset, which uses pools that allocate sub slices.
63    #[cfg(not(exclusive_memory_only))]
64    SubSlices,
65    /// Default preset for using exclusive pages.
66    /// This can be necessary for backends don't support sub-slices.
67    ExclusivePages,
68    /// Custom settings.
69    Custom {
70        /// Options for each pool to construct. When allocating, the first
71        /// possible pool will be picked for an allocation.
72        pool_options: Vec<MemoryPoolOptions>,
73    },
74}
75
76#[allow(clippy::derivable_impls)]
77impl Default for MemoryConfiguration {
78    fn default() -> Self {
79        #[cfg(exclusive_memory_only)]
80        {
81            MemoryConfiguration::ExclusivePages
82        }
83        #[cfg(not(exclusive_memory_only))]
84        {
85            MemoryConfiguration::SubSlices
86        }
87    }
88}