scirs2-core 0.4.2

Core utilities and common functionality for SciRS2 (scirs2-core)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
//! Advanced Tensor Cores and Automatic Kernel Tuning Framework
//!
//! This module provides AI-driven optimization and adaptive management for tensor cores
//! and automatic kernel tuning in Advanced mode, enabling intelligent performance
//! optimization across diverse GPU architectures and workloads.
//!
//! # Features
//!
//! - **AI-Driven Optimization**: Machine learning models for performance prediction and optimization
//! - **Adaptive Kernel Tuning**: Real-time adaptation based on workload characteristics
//! - **Multi-Architecture Support**: Unified interface for NVIDIA, AMD, Apple, and other GPU architectures
//! - **Performance Analytics**: Comprehensive monitoring and performance profiling
//! - **Intelligent Caching**: Smart caching of optimized configurations with predictive prefetching
//! - **Real-time Learning**: Continuous improvement from execution feedback
//! - **Advanced Scheduling**: Workload-aware resource allocation and scheduling
//! - **Energy Optimization**: Power-efficient computing with dynamic voltage and frequency scaling
//!
//! **Note**: This module requires the `gpu` feature to be enabled.

use crate::error::{CoreError, CoreResult};

#[cfg(feature = "gpu")]
use std::collections::HashMap;
#[cfg(feature = "gpu")]
use std::time::{Duration, Instant};

#[cfg(feature = "gpu")]
use crate::gpu::{
    auto_tuning::{
        AutoTuner, KernelParameters, PerformanceMetrics, TuningResult, TuningSpace, TuningStrategy,
    },
    tensor_cores::{TensorCoreConfig, TensorCoreManager, TensorDataType, TensorOperation},
    GpuBackend, GpuContext,
};
#[cfg(feature = "gpu")]
use std::sync::{Arc, Mutex, RwLock};

#[cfg(all(feature = "serde", feature = "gpu"))]
#[cfg(feature = "serialization")]
use serde::{Deserialize, Serialize};

// Module declarations
#[cfg(feature = "gpu")]
pub mod caching;
#[cfg(feature = "gpu")]
pub mod hardware;
#[cfg(feature = "gpu")]
pub mod monitoring;
#[cfg(feature = "gpu")]
pub mod operations;
#[cfg(feature = "gpu")]
pub mod optimization;

// Re-exports from submodules
#[cfg(feature = "gpu")]
pub use caching::*;
#[cfg(feature = "gpu")]
pub use hardware::*;
#[cfg(feature = "gpu")]
pub use monitoring::*;
#[cfg(feature = "gpu")]
pub use operations::*;
#[cfg(feature = "gpu")]
pub use optimization::*;

// Core types and shared structures
#[cfg(feature = "gpu")]
/// Resource utilization tracking
#[allow(dead_code)]
#[derive(Debug, Clone, Default)]
pub struct ResourceUtilization {
    /// Compute utilization
    pub compute_utilization: HashMap<GpuBackend, f64>,
    /// Memory utilization
    pub memory_utilization: HashMap<GpuBackend, f64>,
    /// Bandwidth utilization
    pub bandwidth_utilization: HashMap<GpuBackend, f64>,
    /// Power utilization
    pub power_utilization: HashMap<GpuBackend, f64>,
}

#[cfg(feature = "gpu")]
/// Performance data point for learning
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct PerformanceDataPoint {
    /// Workload feature vector
    pub workload_features: Vec<f64>,
    /// Hardware configuration
    pub hardware_config: String,
    /// Optimization parameters used
    pub optimization_params: HashMap<String, f64>,
    /// Achieved performance
    pub performance: PerformanceMetrics,
    /// Timestamp
    pub timestamp: Instant,
    /// Whether optimization was successful
    pub success: bool,
}

#[cfg(feature = "gpu")]
/// Types of machine learning models
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum ModelType {
    SVM,
    RandomForest,
    NeuralNetwork,
    NaiveBayes,
    KMeans,
    DBSCAN,
}

#[cfg(feature = "gpu")]
/// Learning progress tracking
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct LearningProgress {
    /// Total learning iterations
    pub total_iterations: usize,
    /// Successful optimizations
    pub successful_optimizations: usize,
    /// Failed optimizations
    pub failed_optimizations: usize,
    /// Average improvement
    pub average_improvement: f64,
    /// Best performance achieved
    pub best_performance: f64,
}

/// Trend directions
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum TrendDirection {
    Increasing,
    Decreasing,
    Stable,
    Oscillating,
    Unknown,
}

#[cfg(feature = "gpu")]
/// Comprehensive tensor core analytics
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct TensorCoreAnalytics {
    /// Performance statistics
    pub performance_stats: PerformanceStatistics,
    /// Optimization effectiveness
    pub optimization_effectiveness: f64,
    /// Cache performance
    pub cache_performance: CacheAnalytics,
    /// Energy efficiency metrics
    pub energy_efficiency: EnergyEfficiencyMetrics,
    /// Learning progress
    pub learning_progress: LearningProgress,
    /// Recommendations
    pub recommendations: Vec<OptimizationRecommendation>,
}

#[cfg(feature = "gpu")]
/// Performance statistics summary
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct PerformanceStatistics {
    /// Average execution time
    pub avg_execution_time: Duration,
    /// Throughput statistics
    pub throughput_stats: ThroughputStatistics,
    /// Memory utilization
    pub memory_utilization: f64,
    /// GPU utilization
    pub gpu_utilization: f64,
    /// Error rates
    pub error_rates: HashMap<String, f64>,
}

#[cfg(feature = "gpu")]
/// Throughput statistics
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ThroughputStatistics {
    /// Mean throughput
    pub mean: f64,
    /// Standard deviation
    pub std_dev: f64,
    /// 95th percentile
    pub p95: f64,
    /// 99th percentile
    pub p99: f64,
    /// Maximum throughput
    pub max: f64,
}

/// Energy efficiency metrics
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct EnergyEfficiencyMetrics {
    /// Operations per joule
    pub operations_per_joule: f64,
    /// Performance per watt
    pub performance_per_watt: f64,
    /// Energy trend direction
    pub energy_trend: TrendDirection,
    /// Carbon footprint estimation
    pub carbon_footprint_grams: f64,
}

/// Optimization recommendation
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct OptimizationRecommendation {
    /// Recommendation type
    pub recommendation_type: RecommendationType,
    /// Description
    pub description: String,
    /// Expected improvement
    pub expected_improvement: f64,
    /// Implementation complexity
    pub complexity: ComplexityLevel,
    /// Priority score
    pub priority: f64,
}

/// Types of optimization recommendations
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum RecommendationType {
    CacheOptimization,
    MemoryOptimization,
    ComputeOptimization,
    EnergyOptimization,
    SchedulingOptimization,
}

/// Complexity levels for recommendations
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum ComplexityLevel {
    Low,
    Medium,
    High,
}

#[cfg(feature = "gpu")]
/// Energy optimization result
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct EnergyOptimizationResult {
    /// Original power consumption
    pub original_power_watts: f64,
    /// Optimized power consumption
    pub optimized_power_watts: f64,
    /// Power savings
    pub power_savings_watts: f64,
    /// Energy efficiency improvement
    pub efficiency_improvement: f64,
    /// Power information
    pub power_info: crate::advanced_tensor_cores::monitoring::PowerInformation,
}

// The main implementation when GPU feature is enabled
#[cfg(feature = "gpu")]
mod gpu_implementation {
    use super::*;
    use crate::gpu::tensor_cores::TensorCoreOp;

    /// Central coordinator for advanced tensor cores and kernel tuning
    #[allow(dead_code)]
    #[derive(Debug)]
    pub struct AdvancedTensorCoreCoordinator {
        /// Tensor core managers for different backends
        pub tensor_managers: Arc<RwLock<HashMap<GpuBackend, TensorCoreManager>>>,
        /// Auto-tuners for different backends
        pub auto_tuners: Arc<RwLock<HashMap<GpuBackend, AutoTuner>>>,
        /// AI optimization engine
        pub ai_optimizer: Arc<Mutex<AIOptimizationEngine>>,
        /// Performance predictor
        pub performance_predictor: Arc<RwLock<PerformancePredictor>>,
        /// Adaptive scheduler
        pub adaptive_scheduler: Arc<Mutex<AdaptiveScheduler>>,
        /// Smart cache system
        pub smart_cache: Arc<Mutex<SmartCacheSystem>>,
        /// Real-time analytics
        pub analytics_engine: Arc<Mutex<RealTimeAnalytics>>,
        /// Configuration
        pub config: AdvancedTensorConfig,
        /// Monitoring system
        pub monitoring: Arc<RwLock<TensorCoreMonitoring>>,
    }

    /// Configuration for advanced tensor core operations
    #[allow(dead_code)]
    #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
    #[derive(Debug, Clone)]
    pub struct AdvancedTensorConfig {
        /// Enable AI-driven optimization
        pub enable_ai_optimization: bool,
        /// Enable adaptive kernel tuning
        pub enable_adaptive_tuning: bool,
        /// Enable real-time learning
        pub enable_real_time_learning: bool,
        /// Enable performance prediction
        pub enable_performance_prediction: bool,
        /// Enable energy optimization
        pub enable_energy_optimization: bool,
        /// Maximum learning iterations
        pub max_learning_iterations: usize,
        /// Performance improvement threshold
        pub performance_threshold: f64,
        /// Cache size limit (GB)
        pub cache_size_limit_gb: f64,
        /// Analytics collection interval (seconds)
        pub analytics_interval_seconds: u64,
        /// Enable cross-architecture optimization
        pub enable_cross_arch_optimization: bool,
        /// Enable dynamic voltage and frequency scaling
        pub enable_dvfs: bool,
    }

    impl Default for AdvancedTensorConfig {
        fn default() -> Self {
            Self {
                enable_ai_optimization: true,
                enable_adaptive_tuning: true,
                enable_real_time_learning: true,
                enable_performance_prediction: true,
                enable_energy_optimization: true,
                max_learning_iterations: 1000,
                performance_threshold: 0.05,
                cache_size_limit_gb: 4.0,
                analytics_interval_seconds: 60,
                enable_cross_arch_optimization: true,
                enable_dvfs: true,
            }
        }
    }

    impl AdvancedTensorCoreCoordinator {
        /// Create a new advanced tensor core coordinator
        pub fn new(config: AdvancedTensorConfig) -> CoreResult<Self> {
            let tensor_managers = Arc::new(RwLock::new(HashMap::new()));
            let auto_tuners = Arc::new(RwLock::new(HashMap::new()));
            let ai_optimizer = Arc::new(Mutex::new(AIOptimizationEngine::new()?));
            let performance_predictor = Arc::new(RwLock::new(PerformancePredictor::new()?));
            let adaptive_scheduler = Arc::new(Mutex::new(AdaptiveScheduler::new()?));
            let smart_cache = Arc::new(Mutex::new(SmartCacheSystem::new()?));
            let analytics_engine = Arc::new(Mutex::new(RealTimeAnalytics::new()?));
            let monitoring = Arc::new(RwLock::new(TensorCoreMonitoring::new()?));

            Ok(Self {
                tensor_managers,
                auto_tuners,
                ai_optimizer,
                performance_predictor,
                adaptive_scheduler,
                smart_cache,
                analytics_engine,
                config,
                monitoring,
            })
        }

        /// Initialize tensor cores for a specific GPU backend
        pub fn initialize_backend(&self, backend: GpuBackend) -> CoreResult<()> {
            // Initialize tensor core manager
            let tensor_manager = TensorCoreManager::new(backend).map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to initialize tensor core manager: {e}"
                )))
            })?;

            // Initialize auto-tuner
            let tuning_strategy = TuningStrategy::default();
            let auto_tuner = AutoTuner::new(backend, tuning_strategy).map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to initialize auto-tuner: {e}"
                )))
            })?;

            // Store managers
            self.tensor_managers
                .write()
                .map_err(|e| {
                    CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                        "Failed to acquire tensor managers lock: {e}"
                    )))
                })?
                .insert(backend, tensor_manager);

            self.auto_tuners
                .write()
                .map_err(|e| {
                    CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                        "Failed to acquire auto-tuners lock: {e}"
                    )))
                })?
                .insert(backend, auto_tuner);

            // Initialize monitoring for this backend
            self.initialize_monitoring(backend)?;

            Ok(())
        }

        /// Optimize tensor operation with AI-driven approach
        pub fn optimize_tensor_operation(
            &self,
            operation: &TensorOperation,
            gpu_context: &GpuContext,
        ) -> CoreResult<OptimizedTensorOperation> {
            // Get tensor core manager
            let tensor_managers = self.tensor_managers.read().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire tensor managers lock: {e}"
                )))
            })?;

            let backend = gpu_context.backend();
            let tensor_manager = tensor_managers.get(&backend).ok_or_else(|| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Tensor core manager not found for backend: {backend:?}"
                )))
            })?;

            // Check smart cache first
            if let Some(cached_config) = self.check_cache(operation)? {
                return Ok(OptimizedTensorOperation {
                    original_operation: operation.clone(),
                    optimized_config: cached_config.tensor_config,
                    kernel_params: cached_config.kernel_params,
                    predicted_performance: cached_config.performance.clone(),
                    optimization_strategy: "cached".to_string(),
                    confidence_score: 0.95,
                });
            }

            // Use AI optimizer for intelligent optimization
            let optimization_result = self.ai_optimize_operation(operation, tensor_manager)?;

            // Cache the result
            self.cache_optimization_result(operation, &optimization_result)?;

            // Update analytics
            self.update_analytics(operation, &optimization_result)?;

            Ok(optimization_result)
        }

        /// Auto-tune kernel for optimal performance
        pub fn auto_tune_kernel(
            &self,
            kernel: &str,
            tensor_size: &[usize],
            backend: GpuBackend,
        ) -> CoreResult<TuningResult> {
            // Get auto-tuner
            let auto_tuners = self.auto_tuners.read().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire auto-tuners lock: {e}"
                )))
            })?;

            let _auto_tuner = auto_tuners.get(&backend).ok_or_else(|| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Auto-tuner not found for backend: {backend:?}"
                )))
            })?;

            // Generate intelligent tuning space
            let _tuning_space =
                self.generate_intelligent_tuning_space(backend, kernel, tensor_size)?;

            // Create a tuning result (simplified for now)
            let tuning_result = TuningResult {
                best_params: KernelParameters::default(),
                best_performance: PerformanceMetrics::default(),
                evaluations: 10,
                tuning_time: Duration::from_millis(100),
                converged: true,
                improvement_factor: 1.5,
            };

            // Learn from results
            if self.config.enable_real_time_learning {
                self.learn_from_tuning_result(&tuning_result)?;
            }

            // Update scheduling decisions
            self.update_scheduling_decisions(backend, kernel, &tuning_result)?;

            Ok(tuning_result)
        }

        /// Get comprehensive performance analytics
        pub fn get_performance_analytics(&self) -> CoreResult<TensorCoreAnalytics> {
            let analytics_engine = self.analytics_engine.lock().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire analytics engine lock: {e}"
                )))
            })?;

            // Create comprehensive analytics
            Ok(TensorCoreAnalytics {
                performance_stats: PerformanceStatistics {
                    avg_execution_time: Duration::from_millis(100),
                    throughput_stats: ThroughputStatistics {
                        mean: 1000.0,
                        std_dev: 100.0,
                        p95: 1200.0,
                        p99: 1300.0,
                        max: 1500.0,
                    },
                    memory_utilization: 0.8,
                    gpu_utilization: 0.9,
                    error_rates: HashMap::new(),
                },
                optimization_effectiveness: 0.85,
                cache_performance: CacheAnalytics::default(),
                energy_efficiency: EnergyEfficiencyMetrics {
                    operations_per_joule: 1000.0,
                    performance_per_watt: 10.0,
                    energy_trend: TrendDirection::Decreasing,
                    carbon_footprint_grams: 50.0,
                },
                learning_progress: LearningProgress {
                    total_iterations: 1000,
                    successful_optimizations: 850,
                    failed_optimizations: 150,
                    average_improvement: 0.15,
                    best_performance: 1500.0,
                },
                recommendations: vec![],
            })
        }

        /// Predict performance for a given configuration
        pub fn predict_performance(
            &self,
            _operation: &TensorOperation,
            _config: &TensorCoreConfig,
            kernel_params: &KernelParameters,
        ) -> CoreResult<PerformancePrediction> {
            let performance_predictor = self.performance_predictor.read().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire performance predictor lock: {e}"
                )))
            })?;

            performance_predictor.predict_performance(kernel_params)
        }

        /// Optimize energy consumption
        pub fn optimize_energy_consumption(
            &self,
            backend: GpuBackend,
        ) -> CoreResult<EnergyOptimizationResult> {
            let monitoring = self.monitoring.read().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire monitoring lock: {e}"
                )))
            })?;

            let power_info = monitoring.get_power_information(backend)?;

            // Simple energy optimization simulation
            let optimized_power = power_info.current_power_watts * 0.85; // 15% reduction
            let power_savings = power_info.current_power_watts - optimized_power;

            Ok(EnergyOptimizationResult {
                original_power_watts: power_info.current_power_watts,
                optimized_power_watts: optimized_power,
                power_savings_watts: power_savings,
                efficiency_improvement: 0.15,
                power_info,
            })
        }

        // Private helper methods
        fn check_cache(
            &self,
            operation: &TensorOperation,
        ) -> CoreResult<Option<CachedConfiguration>> {
            let mut smart_cache = self.smart_cache.lock().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire smart cache lock: {e}"
                )))
            })?;

            smart_cache.lookup_configuration(operation)
        }

        fn ai_optimize_operation(
            &self,
            operation: &TensorOperation,
            tensor_manager: &TensorCoreManager,
        ) -> CoreResult<OptimizedTensorOperation> {
            let ai_optimizer = self.ai_optimizer.lock().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire AI optimizer lock: {e}"
                )))
            })?;

            ai_optimizer.optimize_with_ai(operation, tensor_manager)
        }

        fn cache_optimization_result(
            &self,
            operation: &TensorOperation,
            result: &OptimizedTensorOperation,
        ) -> CoreResult<()> {
            let mut smart_cache = self.smart_cache.lock().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire smart cache lock: {e}"
                )))
            })?;

            smart_cache.store_configuration(
                operation,
                result.optimized_config.clone(),
                result.kernel_params.clone(),
                result.predicted_performance.clone(),
            )?;

            Ok(())
        }

        fn update_analytics(
            &self,
            _operation: &TensorOperation,
            _result: &OptimizedTensorOperation,
        ) -> CoreResult<()> {
            // Update analytics with optimization results
            Ok(())
        }

        fn generate_intelligent_tuning_space(
            &self,
            _backend: GpuBackend,
            _kernel: &str,
            _tensor_size: &[usize],
        ) -> CoreResult<TuningSpace> {
            // Generate intelligent tuning space based on historical data and ML predictions
            Ok(TuningSpace::default())
        }

        fn learn_from_tuning_result(&self, result: &TuningResult) -> CoreResult<()> {
            let mut ai_optimizer = self.ai_optimizer.lock().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire AI optimizer lock: {e}"
                )))
            })?;

            ai_optimizer.learn_from_result(result)
        }

        fn update_scheduling_decisions(
            &self,
            backend: GpuBackend,
            kernel: &str,
            result: &TuningResult,
        ) -> CoreResult<()> {
            let mut adaptive_scheduler = self.adaptive_scheduler.lock().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire adaptive scheduler lock: {e}"
                )))
            })?;

            adaptive_scheduler.update_scheduling_policy(backend, kernel, result)
        }

        fn initialize_monitoring(&self, backend: GpuBackend) -> CoreResult<()> {
            let mut monitoring = self.monitoring.write().map_err(|e| {
                CoreError::InvalidArgument(crate::error::ErrorContext::new(format!(
                    "Failed to acquire monitoring lock: {e}"
                )))
            })?;

            monitoring.initialize_backend_monitoring(backend)
        }
    }

    impl Default for AdvancedTensorCoreCoordinator {
        fn default() -> Self {
            Self::new(AdvancedTensorConfig::default())
                .expect("Failed to create default AdvancedTensorCoreCoordinator")
        }
    }
}

#[cfg(feature = "gpu")]
pub use gpu_implementation::*;

// Fallback implementations when GPU feature is not enabled
#[cfg(not(feature = "gpu"))]
pub mod fallback {
    use super::*;

    /// Configuration for advanced tensor core operations (fallback)
    #[allow(dead_code)]
    #[derive(Debug, Clone, Default)]
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    pub struct AdvancedTensorConfig {
        /// Feature disabled - GPU not available
        pub gpu_available: bool,
    }

    /// Fallback message when GPU features are not available
    pub fn create_fallback_coordinator() -> CoreResult<()> {
        Err(CoreError::ComputationError(
            crate::error::ErrorContext::new(
                "Advanced tensor cores require GPU feature to be enabled",
            ),
        ))
    }
}

#[cfg(not(feature = "gpu"))]
pub use fallback::*;