Skip to main content

tenflowers_core/eager_execution/
config.rs

1//! Configuration structs and enums for Ultra-Performance Eager Execution
2
3use std::time::Duration;
4
5#[cfg(feature = "serialize")]
6use serde::{Deserialize, Serialize};
7
8/// Ultra-Performance Eager Execution Configuration
9///
10/// Comprehensive configuration for achieving sub-millisecond overhead with
11/// advanced optimization strategies across SIMD, parallel, memory, and adaptive systems.
12#[derive(Debug, Clone)]
13pub struct EagerExecutionConfig {
14    /// Enable operation caching
15    pub enable_op_cache: bool,
16    /// Enable memory pool
17    pub enable_memory_pool: bool,
18    /// Enable async execution where possible
19    pub enable_async_execution: bool,
20    /// Maximum cache size (number of operations)
21    pub max_cache_size: usize,
22    /// Memory pool size in bytes
23    pub memory_pool_size: usize,
24    /// Target overhead threshold (nanoseconds)
25    pub target_overhead_ns: u64,
26    /// Enable context switching optimization
27    pub enable_context_optimization: bool,
28    /// Enable kernel fusion for compatible operations
29    pub enable_kernel_fusion: bool,
30
31    // Ultra-Performance Configuration Sections
32    /// SIMD acceleration configuration
33    pub simd_config: SimdExecutionConfig,
34    /// Parallel execution optimization
35    pub parallel_config: ParallelExecutionConfig,
36    /// Memory optimization strategies
37    pub memory_config: MemoryOptimizationConfig,
38    /// Performance monitoring and analytics
39    pub monitoring_config: PerformanceMonitoringConfig,
40    /// Adaptive tuning system
41    pub adaptive_config: AdaptiveTuningConfig,
42    /// GPU acceleration configuration
43    pub gpu_config: GpuAccelerationConfig,
44    /// Ultra-low latency optimizations
45    pub ultra_latency_config: UltraLatencyConfig,
46}
47
48/// SIMD Acceleration Configuration for Ultra-Performance
49#[derive(Debug, Clone)]
50pub struct SimdExecutionConfig {
51    /// Enable SIMD vectorization
52    pub enable_simd: bool,
53    /// Preferred SIMD width (128, 256, 512 bits)
54    pub simd_width: u32,
55    /// Enable auto-vectorization hints
56    pub enable_auto_vectorization: bool,
57    /// Use target-specific SIMD instructions (AVX2, AVX512, NEON)
58    pub enable_target_features: bool,
59    /// Minimum array size for SIMD activation
60    pub simd_threshold: usize,
61    /// Enable SIMD for element-wise operations
62    pub simd_elementwise: bool,
63    /// Enable SIMD for matrix operations
64    pub simd_matrix_ops: bool,
65    /// SIMD alignment requirements (bytes)
66    pub memory_alignment: usize,
67}
68
69/// Parallel Execution Configuration for Multi-Core Optimization
70#[derive(Debug, Clone)]
71pub struct ParallelExecutionConfig {
72    /// Enable parallel execution
73    pub enable_parallel: bool,
74    /// Number of worker threads (0 = auto-detect)
75    pub num_threads: usize,
76    /// Minimum problem size for parallelization
77    pub parallel_threshold: usize,
78    /// Thread pool strategy
79    pub thread_strategy: ThreadPoolStrategy,
80    /// Enable work-stealing for load balancing
81    pub enable_work_stealing: bool,
82    /// Chunk size strategy for parallel operations
83    pub chunk_strategy: ChunkStrategy,
84    /// Enable NUMA-aware thread placement
85    pub numa_aware: bool,
86    /// CPU affinity optimization
87    pub cpu_affinity: bool,
88}
89
90/// Thread pool strategies for different workload patterns
91#[derive(Debug, Clone)]
92pub enum ThreadPoolStrategy {
93    /// Global shared thread pool
94    Global,
95    /// Per-device dedicated thread pools
96    PerDevice,
97    /// Adaptive pool that scales with workload
98    Adaptive,
99    /// Custom thread pool configuration
100    Custom {
101        core_threads: usize,
102        max_threads: usize,
103    },
104}
105
106/// Chunking strategies for parallel data processing
107#[derive(Debug, Clone)]
108pub enum ChunkStrategy {
109    /// Fixed chunk size
110    Fixed(usize),
111    /// Adaptive based on data size and thread count
112    Adaptive,
113    /// Work-stealing with dynamic chunk sizes
114    WorkStealing,
115    /// Cache-aware chunking for memory hierarchy optimization
116    CacheAware,
117}
118
119/// Memory Optimization Configuration for Efficient Resource Usage
120#[derive(Debug, Clone)]
121pub struct MemoryOptimizationConfig {
122    /// Enable memory pool optimization
123    pub enable_pooling: bool,
124    /// Pool block size strategy
125    pub pool_strategy: PoolStrategy,
126    /// Enable memory mapping for large arrays
127    pub enable_memory_mapping: bool,
128    /// Memory mapping threshold (bytes)
129    pub mmap_threshold: usize,
130    /// Enable adaptive chunking for large datasets
131    pub enable_adaptive_chunking: bool,
132    /// Chunk size for adaptive processing
133    pub adaptive_chunk_size: usize,
134    /// Enable zero-copy operations where possible
135    pub enable_zero_copy: bool,
136    /// Memory bandwidth optimization
137    pub bandwidth_optimization: bool,
138    /// Cache-friendly memory layouts
139    pub cache_optimization: bool,
140    /// Memory pre-allocation strategy
141    pub preallocation_strategy: PreallocationStrategy,
142}
143
144/// Memory pool strategies for different allocation patterns
145#[derive(Debug, Clone)]
146pub enum PoolStrategy {
147    /// Fixed-size blocks
148    FixedSize {
149        block_size: usize,
150        num_blocks: usize,
151    },
152    /// Multiple pool sizes for different allocation sizes
153    MultiSize { sizes: Vec<usize> },
154    /// Adaptive pool that grows based on usage patterns
155    Adaptive {
156        initial_size: usize,
157        growth_factor: f64,
158    },
159    /// Segregated pools for different data types
160    Segregated,
161}
162
163/// Memory pre-allocation strategies
164#[derive(Debug, Clone)]
165pub enum PreallocationStrategy {
166    /// No pre-allocation
167    None,
168    /// Pre-allocate based on historical usage
169    Historical,
170    /// Pre-allocate fixed amount per device
171    Fixed(usize),
172    /// Adaptive pre-allocation based on workload prediction
173    Adaptive,
174}
175
176/// Performance Monitoring Configuration for Real-Time Analytics
177#[derive(Debug, Clone)]
178pub struct PerformanceMonitoringConfig {
179    /// Enable performance monitoring
180    pub enable_monitoring: bool,
181    /// Metrics collection frequency
182    pub collection_frequency: Duration,
183    /// Enable real-time profiling
184    pub enable_profiling: bool,
185    /// Benchmark execution periodically
186    pub enable_benchmarking: bool,
187    /// Benchmark frequency
188    pub benchmark_frequency: Duration,
189    /// Enable memory usage tracking
190    pub track_memory_usage: bool,
191    /// Enable operation timing
192    pub enable_timing: bool,
193    /// Enable cache hit/miss tracking
194    pub track_cache_performance: bool,
195    /// Enable hardware performance counters
196    pub enable_hardware_counters: bool,
197    /// Performance history retention (number of samples)
198    pub history_retention: usize,
199    /// Enable performance alerts
200    pub enable_alerts: bool,
201    /// Performance threshold for alerts
202    pub alert_threshold: Duration,
203}
204
205/// Adaptive Tuning Configuration for Self-Optimization
206#[derive(Debug, Clone)]
207pub struct AdaptiveTuningConfig {
208    /// Enable adaptive optimization
209    pub enable_adaptive: bool,
210    /// Learning rate for adaptive algorithms
211    pub learning_rate: f64,
212    /// Adaptation frequency
213    pub adaptation_frequency: Duration,
214    /// Enable workload prediction
215    pub enable_prediction: bool,
216    /// Prediction algorithm
217    pub prediction_algorithm: PredictionAlgorithm,
218    /// Enable auto-tuning of SIMD parameters
219    pub tune_simd: bool,
220    /// Enable auto-tuning of parallel parameters
221    pub tune_parallel: bool,
222    /// Enable auto-tuning of memory parameters
223    pub tune_memory: bool,
224    /// Minimum confidence for parameter changes
225    pub min_confidence: f64,
226    /// Enable A/B testing for optimizations
227    pub enable_ab_testing: bool,
228    /// Sample size for statistical significance
229    pub sample_size: usize,
230}
231
232/// Prediction algorithms for workload forecasting
233#[derive(Debug, Clone)]
234pub enum PredictionAlgorithm {
235    /// Moving average prediction
236    MovingAverage { window_size: usize },
237    /// Exponential smoothing
238    ExponentialSmoothing { alpha: f64 },
239    /// Linear regression on historical data
240    LinearRegression,
241    /// Machine learning-based prediction
242    MachineLearning { model_complexity: ModelComplexity },
243}
244
245/// ML model complexity for prediction algorithms
246#[derive(Debug, Clone)]
247pub enum ModelComplexity {
248    /// Simple linear models
249    Simple,
250    /// Polynomial features
251    Polynomial { degree: u32 },
252    /// Neural network-based
253    NeuralNetwork { hidden_layers: Vec<usize> },
254}
255
256/// GPU Acceleration Configuration for Hardware Optimization
257#[derive(Debug, Clone)]
258pub struct GpuAccelerationConfig {
259    /// Enable GPU acceleration
260    pub enable_gpu: bool,
261    /// GPU memory pool size (bytes)
262    pub gpu_memory_pool: usize,
263    /// Enable async GPU operations
264    pub enable_async_gpu: bool,
265    /// GPU kernel fusion optimization
266    pub enable_kernel_fusion: bool,
267    /// Tensor core utilization for mixed precision
268    pub enable_tensor_cores: bool,
269    /// Mixed precision computation
270    pub mixed_precision: bool,
271    /// GPU memory transfer optimization
272    pub optimize_transfers: bool,
273    /// Enable multi-GPU coordination
274    pub enable_multi_gpu: bool,
275    /// GPU scheduling strategy
276    pub scheduling_strategy: GpuSchedulingStrategy,
277}
278
279/// GPU scheduling strategies for workload distribution
280#[derive(Debug, Clone)]
281pub enum GpuSchedulingStrategy {
282    /// Round-robin across available GPUs
283    RoundRobin,
284    /// Load-based scheduling
285    LoadBased,
286    /// Memory-aware scheduling
287    MemoryAware,
288    /// Latency-optimized scheduling
289    LatencyOptimized,
290}
291
292/// Ultra-Low Latency Configuration for Critical Performance
293#[derive(Debug, Clone)]
294pub struct UltraLatencyConfig {
295    /// Enable ultra-low latency mode
296    pub enable_ultra_latency: bool,
297    /// CPU isolation for critical threads
298    pub cpu_isolation: bool,
299    /// Real-time scheduling priority
300    pub realtime_priority: bool,
301    /// Disable CPU frequency scaling
302    pub disable_cpu_scaling: bool,
303    /// Pre-fault memory pages
304    pub prefault_memory: bool,
305    /// Disable swap for critical processes
306    pub disable_swap: bool,
307    /// Enable lock-free data structures
308    pub enable_lockfree: bool,
309    /// Optimize for L1/L2 cache residency
310    pub optimize_cache_residency: bool,
311    /// Branch prediction optimization
312    pub optimize_branch_prediction: bool,
313}
314
315impl Default for EagerExecutionConfig {
316    fn default() -> Self {
317        Self {
318            enable_op_cache: true,
319            enable_memory_pool: true,
320            enable_async_execution: true,
321            max_cache_size: 1000,
322            memory_pool_size: 128 * 1024 * 1024, // 128MB
323            target_overhead_ns: 1_000_000,       // 1ms in nanoseconds
324            enable_context_optimization: true,
325            enable_kernel_fusion: true,
326
327            // Ultra-Performance Configuration Defaults
328            simd_config: SimdExecutionConfig::default(),
329            parallel_config: ParallelExecutionConfig::default(),
330            memory_config: MemoryOptimizationConfig::default(),
331            monitoring_config: PerformanceMonitoringConfig::default(),
332            adaptive_config: AdaptiveTuningConfig::default(),
333            gpu_config: GpuAccelerationConfig::default(),
334            ultra_latency_config: UltraLatencyConfig::default(),
335        }
336    }
337}
338
339impl Default for SimdExecutionConfig {
340    fn default() -> Self {
341        Self {
342            enable_simd: true,
343            simd_width: 256, // AVX2 default
344            enable_auto_vectorization: true,
345            enable_target_features: true,
346            simd_threshold: 1024, // Minimum 1K elements for SIMD
347            simd_elementwise: true,
348            simd_matrix_ops: true,
349            memory_alignment: 32, // 32-byte alignment for AVX2
350        }
351    }
352}
353
354impl Default for ParallelExecutionConfig {
355    fn default() -> Self {
356        Self {
357            enable_parallel: true,
358            num_threads: 0,             // Auto-detect
359            parallel_threshold: 10_000, // Minimum 10K elements for parallelization
360            thread_strategy: ThreadPoolStrategy::Adaptive,
361            enable_work_stealing: true,
362            chunk_strategy: ChunkStrategy::Adaptive,
363            numa_aware: true,
364            cpu_affinity: false, // Conservative default
365        }
366    }
367}
368
369impl Default for MemoryOptimizationConfig {
370    fn default() -> Self {
371        Self {
372            enable_pooling: true,
373            pool_strategy: PoolStrategy::Adaptive {
374                initial_size: 64 * 1024 * 1024, // 64MB initial
375                growth_factor: 1.5,
376            },
377            enable_memory_mapping: true,
378            mmap_threshold: 100 * 1024 * 1024, // 100MB threshold
379            enable_adaptive_chunking: true,
380            adaptive_chunk_size: 1024 * 1024, // 1MB chunks
381            enable_zero_copy: true,
382            bandwidth_optimization: true,
383            cache_optimization: true,
384            preallocation_strategy: PreallocationStrategy::Historical,
385        }
386    }
387}
388
389impl Default for PerformanceMonitoringConfig {
390    fn default() -> Self {
391        Self {
392            enable_monitoring: true,
393            collection_frequency: Duration::from_millis(100), // 100ms
394            enable_profiling: true,
395            enable_benchmarking: false, // Disabled by default to reduce overhead
396            benchmark_frequency: Duration::from_secs(60), // 1 minute
397            track_memory_usage: true,
398            enable_timing: true,
399            track_cache_performance: true,
400            enable_hardware_counters: false, // Requires elevated privileges
401            history_retention: 1000,         // Keep 1000 samples
402            enable_alerts: true,
403            alert_threshold: Duration::from_millis(5), // 5ms alert threshold
404        }
405    }
406}
407
408impl Default for AdaptiveTuningConfig {
409    fn default() -> Self {
410        Self {
411            enable_adaptive: true,
412            learning_rate: 0.01, // Conservative learning rate
413            adaptation_frequency: Duration::from_secs(30), // 30 seconds
414            enable_prediction: true,
415            prediction_algorithm: PredictionAlgorithm::ExponentialSmoothing { alpha: 0.3 },
416            tune_simd: true,
417            tune_parallel: true,
418            tune_memory: true,
419            min_confidence: 0.8,      // 80% confidence threshold
420            enable_ab_testing: false, // Disabled by default
421            sample_size: 100,         // 100 operations per test
422        }
423    }
424}
425
426impl Default for GpuAccelerationConfig {
427    fn default() -> Self {
428        Self {
429            enable_gpu: true,
430            gpu_memory_pool: 512 * 1024 * 1024, // 512MB GPU memory pool
431            enable_async_gpu: true,
432            enable_kernel_fusion: true,
433            enable_tensor_cores: true,
434            mixed_precision: true,
435            optimize_transfers: true,
436            enable_multi_gpu: true,
437            scheduling_strategy: GpuSchedulingStrategy::LoadBased,
438        }
439    }
440}
441
442impl Default for UltraLatencyConfig {
443    fn default() -> Self {
444        Self {
445            enable_ultra_latency: false, // Disabled by default - requires system configuration
446            cpu_isolation: false,
447            realtime_priority: false,
448            disable_cpu_scaling: false,
449            prefault_memory: true, // Safe to enable
450            disable_swap: false,
451            enable_lockfree: true, // Safe optimization
452            optimize_cache_residency: true,
453            optimize_branch_prediction: true,
454        }
455    }
456}
457
458/// Operation signature for caching
459#[derive(Debug, Clone, Hash, PartialEq, Eq)]
460pub struct OpSignature {
461    pub operation: String,
462    pub input_shapes: Vec<Vec<usize>>,
463    pub dtype: crate::DType,
464    pub device: crate::Device,
465    pub params: Vec<(String, String)>, // Serialized parameters
466}
467
468/// Cached operation result
469#[derive(Debug, Clone)]
470pub struct CachedOperation {
471    pub signature: OpSignature,
472    pub result_shape: Vec<usize>,
473    pub execution_time: std::time::Duration,
474    pub memory_usage: usize,
475    pub created_at: std::time::Instant,
476    pub last_used: std::time::Instant,
477    pub use_count: usize,
478}
479
480/// Execution metrics for overhead tracking
481#[derive(Debug, Clone)]
482#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
483pub struct ExecutionMetrics {
484    pub operation: String,
485    pub device: crate::Device,
486    pub setup_time: std::time::Duration,
487    pub execution_time: std::time::Duration,
488    pub teardown_time: std::time::Duration,
489    pub total_overhead: std::time::Duration,
490    pub memory_allocation_time: std::time::Duration,
491    pub cache_hit: bool,
492    pub meets_target: bool,
493}