Skip to main content

xlog_core/
config.rs

1//! Configuration types for XLOG runtime
2
3/// GPU memory budget configuration.
4///
5/// Use [`MemoryBudget::default()`] or the builder methods ([`from_device_memory`],
6/// [`with_limit`], [`with_ooc`]) to construct.
7#[derive(Debug, Clone)]
8#[non_exhaustive]
9pub struct MemoryBudget {
10    /// Maximum device memory to use in bytes
11    pub device_bytes: u64,
12    /// Allow out-of-core execution (spill to host)
13    pub allow_ooc: bool,
14    /// Abort on memory budget exceeded (vs try to continue)
15    pub abort_on_exceed: bool,
16}
17
18impl Default for MemoryBudget {
19    fn default() -> Self {
20        Self {
21            device_bytes: 0, // Will be set from device query
22            allow_ooc: false,
23            abort_on_exceed: true,
24        }
25    }
26}
27
28impl MemoryBudget {
29    /// Create a budget using 80% of available device memory
30    pub fn from_device_memory(total_bytes: u64) -> Self {
31        Self {
32            device_bytes: (total_bytes as f64 * 0.8) as u64,
33            allow_ooc: false,
34            abort_on_exceed: true,
35        }
36    }
37
38    /// Create a budget with explicit byte limit
39    pub fn with_limit(device_bytes: u64) -> Self {
40        Self {
41            device_bytes,
42            allow_ooc: false,
43            abort_on_exceed: true,
44        }
45    }
46
47    /// Enable out-of-core mode
48    pub fn with_ooc(mut self) -> Self {
49        self.allow_ooc = true;
50        self
51    }
52}
53
54/// Runtime configuration for XLOG execution.
55///
56/// Use [`RuntimeConfig::default()`] and the builder methods to construct.
57#[derive(Debug, Clone)]
58pub struct RuntimeConfig {
59    /// Memory budget settings
60    pub memory: MemoryBudget,
61    /// Use deterministic execution (may be slower)
62    pub deterministic: bool,
63    /// Enable profiling (row counts, memory tracking)
64    pub profile: bool,
65    /// Maximum fixpoint iterations before abort
66    pub max_iterations: u32,
67}
68
69impl Default for RuntimeConfig {
70    fn default() -> Self {
71        Self {
72            memory: MemoryBudget::default(),
73            deterministic: true,
74            profile: false,
75            max_iterations: 1_000_000,
76        }
77    }
78}
79
80impl RuntimeConfig {
81    /// Enable profiling
82    pub fn with_profiling(mut self) -> Self {
83        self.profile = true;
84        self
85    }
86
87    /// Set memory budget
88    pub fn with_memory(mut self, memory: MemoryBudget) -> Self {
89        self.memory = memory;
90        self
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn test_memory_budget_default() {
100        let budget = MemoryBudget::default();
101        assert!(!budget.allow_ooc);
102        assert!(budget.abort_on_exceed);
103    }
104
105    #[test]
106    fn test_runtime_config_default() {
107        let config = RuntimeConfig::default();
108        assert!(config.deterministic);
109        assert!(!config.profile);
110    }
111
112    #[test]
113    fn test_memory_budget_from_device() {
114        let budget = MemoryBudget::from_device_memory(10_000_000_000);
115        assert_eq!(budget.device_bytes, 8_000_000_000);
116    }
117}