optirs-gpu 0.3.2

OptiRS GPU acceleration and multi-GPU optimization
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// Comprehensive GPU memory management system
//
// This module provides a complete GPU memory management solution including:
// - Advanced allocation strategies (buddy, slab, arena allocators)
// - Intelligent memory management (GC, prefetching, eviction, defragmentation)
// - Multi-vendor GPU support (NVIDIA CUDA, AMD ROCm, Intel OneAPI, Apple Metal)
//
// The system is designed to provide optimal memory utilization and performance
// across different GPU architectures and workloads.

pub mod allocation;
pub mod management;
pub mod vendors;

use std::collections::HashMap;
use std::ffi::c_void;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

// Re-export key types from submodules
pub use allocation::{
    AllocationStrategy, AllocationStrategyManager, AllocatorType, ArenaAllocator, BuddyAllocator,
    MemoryPool, SlabAllocator, UnifiedAllocator, UnifiedConfig,
};

use allocation::strategies::AllocationStats;

pub use management::{
    AccessType, DefragmentationEngine, EvictionEngine, GarbageCollectionEngine,
    IntegratedMemoryManager, ManagementStats, MemoryManagementConfig, MemoryManagementError,
    MemoryRegion, PrefetchingEngine,
};

use management::eviction_policies::{CacheObject, ObjectPriority, ObjectType, RegionType};

pub use vendors::{
    CudaConfig, CudaError, CudaMemoryBackend, CudaMemoryType, GpuBackendFactory, GpuVendor,
    MetalConfig, MetalError, MetalMemoryBackend, MetalMemoryType, OneApiConfig, OneApiError,
    OneApiMemoryBackend, OneApiMemoryType, RocmConfig, RocmError, RocmMemoryBackend,
    RocmMemoryType, UnifiedGpuBackend, UnifiedGpuError, UnifiedMemoryStats, VendorConfig,
};

/// Comprehensive GPU memory system configuration
#[derive(Debug, Clone)]
pub struct GpuMemorySystemConfig {
    /// Vendor-specific backend configuration
    pub vendor_config: VendorConfig,
    /// Memory allocation configuration
    pub allocation_config: UnifiedConfig,
    /// Memory management configuration
    pub management_config: MemoryManagementConfig,
    /// System-wide configuration
    pub system_config: SystemConfig,
}

/// System-wide configuration
#[derive(Debug, Clone)]
pub struct SystemConfig {
    /// Enable unified memory interface
    pub enable_unified_interface: bool,
    /// Enable cross-vendor memory sharing
    pub enable_cross_vendor_sharing: bool,
    /// Enable performance monitoring
    pub enable_performance_monitoring: bool,
    /// Monitoring interval
    pub monitoring_interval: Duration,
    /// Memory budget as fraction of total GPU memory
    pub memory_budget: f64,
    /// Enable automatic optimization
    pub enable_auto_optimization: bool,
    /// Optimization interval
    pub optimization_interval: Duration,
    /// Enable memory compression
    pub enable_memory_compression: bool,
    /// Thread pool size for memory operations
    pub thread_pool_size: usize,
}

impl Default for SystemConfig {
    fn default() -> Self {
        Self {
            enable_unified_interface: true,
            enable_cross_vendor_sharing: false,
            enable_performance_monitoring: true,
            monitoring_interval: Duration::from_millis(500),
            memory_budget: 0.9,
            enable_auto_optimization: true,
            optimization_interval: Duration::from_secs(60),
            enable_memory_compression: false,
            thread_pool_size: 4,
        }
    }
}

impl Default for GpuMemorySystemConfig {
    fn default() -> Self {
        let vendor = GpuBackendFactory::get_preferred_vendor();
        Self {
            vendor_config: GpuBackendFactory::create_default_config(vendor),
            allocation_config: UnifiedConfig::default(),
            management_config: MemoryManagementConfig::default(),
            system_config: SystemConfig::default(),
        }
    }
}

/// Unified GPU memory system
pub struct GpuMemorySystem {
    /// GPU backend
    gpu_backend: UnifiedGpuBackend,
    /// Allocation engine
    allocation_engine: UnifiedAllocator,
    /// Memory management system
    memory_manager: IntegratedMemoryManager,
    /// System configuration
    config: GpuMemorySystemConfig,
    /// System statistics
    stats: SystemStats,
    /// Memory regions tracking
    memory_regions: HashMap<*mut c_void, MemoryAllocation>,
    /// Background monitoring enabled
    monitoring_enabled: bool,
    /// Last optimization time
    last_optimization: Instant,
}

/// Memory allocation tracking
#[derive(Debug, Clone)]
pub struct MemoryAllocation {
    pub ptr: *mut c_void,
    pub size: usize,
    pub allocator_type: AllocatorType,
    pub vendor_memory_type: String,
    pub allocated_at: Instant,
    pub last_accessed: Option<Instant>,
    pub access_count: u64,
    pub ref_count: u32,
}

/// System-wide statistics
#[derive(Debug, Clone, Default)]
pub struct SystemStats {
    pub total_allocations: u64,
    pub total_deallocations: u64,
    pub bytes_allocated: u64,
    pub bytes_deallocated: u64,
    pub active_allocations: u64,
    pub peak_memory_usage: usize,
    pub fragmentation_ratio: f64,
    pub allocation_efficiency: f64,
    pub vendor_stats: UnifiedMemoryStats,
    pub allocation_stats: AllocationStats,
    pub management_stats: ManagementStats,
    pub uptime: Duration,
    pub optimization_cycles: u64,
}

impl GpuMemorySystem {
    /// Create new GPU memory system
    pub fn new(config: GpuMemorySystemConfig) -> Result<Self, GpuMemorySystemError> {
        // Initialize GPU backend
        let mut gpu_backend = UnifiedGpuBackend::new(config.vendor_config.clone())?;

        // Allocate memory pool from GPU backend
        let mut total_size =
            (config.system_config.memory_budget * gpu_backend.get_total_memory() as f64) as usize;

        // Round to nearest power of 2 if buddy allocator is enabled
        if config.allocation_config.enable_buddy {
            total_size = total_size.next_power_of_two();
        }

        let base_ptr = gpu_backend
            .allocate(total_size)
            .map_err(GpuMemorySystemError::BackendError)?;

        // Initialize allocation engine with GPU memory
        let allocation_engine = UnifiedAllocator::new(
            unsafe { NonNull::new_unchecked(base_ptr as *mut u8) },
            total_size,
            config.allocation_config.clone(),
        )
        .map_err(|e| GpuMemorySystemError::AllocationError(format!("{:?}", e)))?;

        // Initialize memory manager
        let memory_manager = IntegratedMemoryManager::new(config.management_config.clone());

        Ok(Self {
            gpu_backend,
            allocation_engine,
            memory_manager,
            config,
            stats: SystemStats::default(),
            memory_regions: HashMap::new(),
            monitoring_enabled: false,
            last_optimization: Instant::now(),
        })
    }

    /// Create system with auto-detected best configuration
    pub fn auto_create() -> Result<Self, GpuMemorySystemError> {
        let config = GpuMemorySystemConfig::default();
        Self::new(config)
    }

    /// Start the GPU memory system
    pub fn start(&mut self) -> Result<(), GpuMemorySystemError> {
        // Start background memory management
        if self.config.system_config.enable_performance_monitoring {
            self.memory_manager
                .start_background_management()
                .map_err(|e| GpuMemorySystemError::ManagementError(format!("{}", e)))?;
            self.monitoring_enabled = true;
        }

        // FEATURE STATUS (v1.0.0): UnifiedAllocator initialization pending
        //
        // The allocation engine is functional without explicit initialization.
        // Explicit initialization will be added in v1.1.0+ for:
        // - Pre-allocation of memory pools
        // - GPU device capability detection
        // - Optimal allocator strategy selection
        //
        // For v1.0.0, initialization happens lazily on first allocation.
        // PLANNED (v1.1.0+): self.allocation_engine.initialize()?;

        Ok(())
    }

    /// Allocate GPU memory with unified interface
    pub fn allocate(
        &mut self,
        size: usize,
        alignment: Option<usize>,
    ) -> Result<*mut c_void, GpuMemorySystemError> {
        let start_time = Instant::now();

        // Choose optimal allocator based on size and usage patterns
        let allocator_type = self.choose_allocator(size);

        // Allocate using allocation engine
        let ptr_nonnull =
            self.allocation_engine
                .allocate(size, allocator_type.clone(), alignment)?;

        // Convert NonNull<u8> to *mut c_void
        let ptr = ptr_nonnull.as_ptr() as *mut c_void;

        // Create allocation record
        let allocation = MemoryAllocation {
            ptr,
            size,
            allocator_type,
            vendor_memory_type: self.get_vendor_memory_type(),
            allocated_at: Instant::now(),
            last_accessed: Some(Instant::now()),
            access_count: 1,
            ref_count: 1,
        };

        // Track allocation
        self.memory_regions.insert(ptr, allocation);

        // Update statistics
        self.update_allocation_stats(size, start_time.elapsed());

        // Check for memory pressure and handle if needed
        self.handle_memory_pressure()?;

        Ok(ptr)
    }

    /// Free GPU memory
    pub fn free(&mut self, ptr: *mut c_void) -> Result<(), GpuMemorySystemError> {
        let start_time = Instant::now();

        // Get allocation info
        let allocation = self
            .memory_regions
            .remove(&ptr)
            .ok_or_else(|| GpuMemorySystemError::InvalidPointer("Pointer not found".to_string()))?;

        // Free using appropriate allocator
        self.allocation_engine
            .free(ptr, allocation.allocator_type)?;

        // Update statistics
        self.update_deallocation_stats(allocation.size, start_time.elapsed());

        Ok(())
    }

    /// Reallocate memory with potential optimization
    pub fn reallocate(
        &mut self,
        ptr: *mut c_void,
        new_size: usize,
    ) -> Result<*mut c_void, GpuMemorySystemError> {
        // Get current allocation info
        let allocation = self
            .memory_regions
            .get(&ptr)
            .ok_or_else(|| GpuMemorySystemError::InvalidPointer("Pointer not found".to_string()))?;

        let old_size = allocation.size;
        let allocator_type = allocation.allocator_type.clone();

        // Try in-place reallocation first
        if let Ok(new_ptr) = self
            .allocation_engine
            .reallocate(ptr, new_size, allocator_type)
        {
            if new_ptr == ptr {
                // In-place reallocation successful
                if let Some(allocation) = self.memory_regions.get_mut(&ptr) {
                    allocation.size = new_size;
                    allocation.last_accessed = Some(Instant::now());
                    allocation.access_count += 1;
                }
                return Ok(ptr);
            }
        }

        // Fallback to allocate + copy + free
        let new_ptr = self.allocate(new_size, None)?;

        // Copy data (simulate)
        unsafe {
            std::ptr::copy_nonoverlapping(
                ptr as *const u8,
                new_ptr as *mut u8,
                old_size.min(new_size),
            );
        }

        // Free old memory
        self.free(ptr)?;

        Ok(new_ptr)
    }

    /// Record memory access for optimization
    pub fn record_access(
        &mut self,
        ptr: *mut c_void,
        access_type: AccessType,
    ) -> Result<(), GpuMemorySystemError> {
        if let Some(allocation) = self.memory_regions.get_mut(&ptr) {
            allocation.last_accessed = Some(Instant::now());
            allocation.access_count += 1;

            // Update memory manager with access pattern
            self.memory_manager
                .update_access_pattern(ptr, allocation.size, access_type)?;
        }

        Ok(())
    }

    /// Get memory information
    pub fn get_memory_info(&self, ptr: *mut c_void) -> Option<&MemoryAllocation> {
        self.memory_regions.get(&ptr)
    }

    /// Get system statistics
    pub fn get_stats(&mut self) -> SystemStats {
        // Update vendor stats
        self.stats.vendor_stats = self.gpu_backend.get_memory_stats();

        // Update allocation stats
        let unified_stats = self.allocation_engine.get_stats();
        self.stats.allocation_stats = AllocationStats {
            total_allocations: unified_stats.total_allocations,
            total_deallocations: unified_stats.total_deallocations,
            cache_hits: unified_stats.routing_cache_hits,
            cache_misses: unified_stats.routing_decisions - unified_stats.routing_cache_hits,
            // FEATURE STATUS (v1.0.0): Fragmentation tracking integration pending
            //
            // The defragmentation engine tracks fragmentation internally, but exposing
            // this metric through SystemStats requires aggregation across multiple
            // allocation strategies, which is planned for v1.1.0.
            //
            // For v1.0.0, users can access fragmentation data directly through:
            // - memory_manager.get_stats().fragmentation_ratio
            // - Defragmentation engine metrics
            //
            // PLANNED (v1.1.0+): Aggregate fragmentation events from all allocators
            fragmentation_events: 0,
            total_allocated_bytes: unified_stats.bytes_allocated,
            peak_allocated_bytes: unified_stats.peak_memory_usage as u64,
            average_allocation_size: if unified_stats.total_allocations > 0 {
                (unified_stats.bytes_allocated as f64) / (unified_stats.total_allocations as f64)
            } else {
                0.0
            },
            allocation_latency_ms: unified_stats.average_allocation_time_ns / 1_000_000.0,
        };

        // Update management stats
        self.stats.management_stats = self.memory_manager.get_stats().clone();

        // Calculate derived metrics
        self.calculate_system_metrics();

        self.stats.clone()
    }

    /// Optimize memory system based on usage patterns
    pub fn optimize(&mut self) -> Result<(), GpuMemorySystemError> {
        if !self.config.system_config.enable_auto_optimization {
            return Ok(());
        }

        let now = Instant::now();
        if now.duration_since(self.last_optimization)
            < self.config.system_config.optimization_interval
        {
            return Ok(());
        }

        // Run garbage collection
        let memory_regions: HashMap<usize, management::MemoryRegion> = self
            .memory_regions
            .iter()
            .map(|(ptr, alloc)| {
                let mut objects = HashMap::new();
                objects.insert(
                    *ptr as usize,
                    CacheObject {
                        address: *ptr as usize,
                        size: alloc.size,
                        created_at: alloc.allocated_at,
                        last_access: alloc.last_accessed.unwrap_or(alloc.allocated_at),
                        access_count: alloc.access_count as u32,
                        access_frequency: alloc.access_count as f64,
                        priority: ObjectPriority::Normal,
                        kernel_context: None,
                        object_type: ObjectType::Data,
                        eviction_cost: 1.0,
                        replacement_cost: 1.0,
                    },
                );

                (
                    *ptr as usize,
                    management::MemoryRegion {
                        base_addr: *ptr as usize,
                        size: alloc.size,
                        objects,
                        region_type: RegionType::Buffer,
                        pressure: 0.0,
                        last_eviction: None,
                    },
                )
            })
            .collect();

        let _ = self
            .memory_manager
            .run_garbage_collection(&memory_regions)?;

        // Optimize allocation strategies
        self.allocation_engine.optimize_strategies()?;

        // Optimize memory management policies
        self.memory_manager.optimize_policies()?;

        // Run defragmentation if needed
        if self.stats.fragmentation_ratio > 0.3 {
            let _ = self.memory_manager.defragment(&memory_regions)?;
        }

        self.last_optimization = now;
        self.stats.optimization_cycles += 1;

        Ok(())
    }

    /// Check and handle memory pressure
    fn handle_memory_pressure(&mut self) -> Result<(), GpuMemorySystemError> {
        let memory_usage_ratio = self.calculate_memory_usage_ratio();

        if memory_usage_ratio > self.config.system_config.memory_budget {
            let memory_regions: HashMap<usize, management::MemoryRegion> = self
                .memory_regions
                .iter()
                .map(|(ptr, alloc)| {
                    let mut objects = HashMap::new();
                    objects.insert(
                        *ptr as usize,
                        CacheObject {
                            address: *ptr as usize,
                            size: alloc.size,
                            created_at: alloc.allocated_at,
                            last_access: alloc.last_accessed.unwrap_or(alloc.allocated_at),
                            access_count: alloc.access_count as u32,
                            access_frequency: alloc.access_count as f64,
                            priority: ObjectPriority::Normal,
                            kernel_context: None,
                            object_type: ObjectType::Data,
                            eviction_cost: 1.0,
                            replacement_cost: 1.0,
                        },
                    );

                    (
                        *ptr as usize,
                        management::MemoryRegion {
                            base_addr: *ptr as usize,
                            size: alloc.size,
                            objects,
                            region_type: RegionType::Buffer,
                            pressure: 0.0,
                            last_eviction: None,
                        },
                    )
                })
                .collect();

            self.memory_manager
                .handle_memory_pressure(memory_usage_ratio, &memory_regions)?;
        }

        Ok(())
    }

    /// Choose optimal allocator based on allocation size and patterns
    fn choose_allocator(&self, size: usize) -> AllocatorType {
        // Simple heuristics - could be enhanced with ML
        if size < 1024 {
            AllocatorType::Slab // Small allocations
        } else if size < 1024 * 1024 {
            AllocatorType::Buddy // Medium allocations
        } else {
            AllocatorType::Arena // Large allocations
        }
    }

    /// Get vendor-specific memory type string
    fn get_vendor_memory_type(&self) -> String {
        match self.gpu_backend.get_vendor() {
            GpuVendor::Nvidia => "Device".to_string(),
            GpuVendor::Amd => "Device".to_string(),
            GpuVendor::Intel => "Device".to_string(),
            GpuVendor::Apple => "Private".to_string(),
            GpuVendor::Unknown => "Unknown".to_string(),
        }
    }

    /// Update allocation statistics
    fn update_allocation_stats(&mut self, size: usize, _duration: Duration) {
        self.stats.total_allocations += 1;
        self.stats.bytes_allocated += size as u64;
        self.stats.active_allocations += 1;

        if self.stats.bytes_allocated > self.stats.peak_memory_usage as u64 {
            self.stats.peak_memory_usage = self.stats.bytes_allocated as usize;
        }
    }

    /// Update deallocation statistics
    fn update_deallocation_stats(&mut self, size: usize, _duration: Duration) {
        self.stats.total_deallocations += 1;
        self.stats.bytes_deallocated += size as u64;
        self.stats.active_allocations = self.stats.active_allocations.saturating_sub(1);
    }

    /// Calculate memory usage ratio
    fn calculate_memory_usage_ratio(&self) -> f64 {
        let total_memory = self.get_total_gpu_memory();
        let used_memory = self.stats.bytes_allocated - self.stats.bytes_deallocated;
        used_memory as f64 / total_memory as f64
    }

    /// Get total GPU memory (vendor-specific)
    fn get_total_gpu_memory(&self) -> usize {
        // This would be implemented based on vendor-specific device queries
        match self.gpu_backend.get_vendor() {
            GpuVendor::Nvidia => 8 * 1024 * 1024 * 1024, // 8GB typical
            GpuVendor::Amd => 16 * 1024 * 1024 * 1024,   // 16GB typical
            GpuVendor::Intel => 12 * 1024 * 1024 * 1024, // 12GB typical
            GpuVendor::Apple => 32 * 1024 * 1024 * 1024, // 32GB unified memory
            GpuVendor::Unknown => 4 * 1024 * 1024 * 1024, // 4GB fallback
        }
    }

    /// Calculate system-wide metrics
    fn calculate_system_metrics(&mut self) {
        // Calculate fragmentation ratio
        let total_allocated = self
            .memory_regions
            .values()
            .map(|alloc| alloc.size)
            .sum::<usize>();
        let total_managed = self.stats.vendor_stats.bytes_allocated;
        self.stats.fragmentation_ratio = if total_managed > 0 {
            1.0 - (total_allocated as f64 / total_managed as f64)
        } else {
            0.0
        };

        // Calculate allocation efficiency
        self.stats.allocation_efficiency = if self.stats.total_allocations > 0 {
            let successful_allocations = self.stats.total_allocations;
            successful_allocations as f64 / self.stats.total_allocations as f64
        } else {
            1.0
        };
    }
}

// Safety: GpuMemorySystem manages GPU memory through multiple components (backend, allocator, manager).
// While it contains raw pointers via memory_regions HashMap<*mut c_void, MemoryAllocation>,
// it's safe to share across threads when protected by Arc<Mutex<>> because:
// 1. All raw pointers point to GPU memory managed by the GPU driver through the backend
// 2. The Mutex provides exclusive access for all mutable operations
// 3. All contained components (UnifiedGpuBackend, UnifiedAllocator, IntegratedMemoryManager) are already Send+Sync
// 4. No thread-local state is maintained
unsafe impl Send for GpuMemorySystem {}
unsafe impl Sync for GpuMemorySystem {}

/// GPU memory system errors
#[derive(Debug)]
pub enum GpuMemorySystemError {
    BackendError(UnifiedGpuError),
    AllocationError(String),
    ManagementError(String),
    InvalidPointer(String),
    SystemNotStarted,
    ConfigurationError(String),
    OptimizationFailed(String),
    InternalError(String),
}

impl std::fmt::Display for GpuMemorySystemError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GpuMemorySystemError::BackendError(err) => write!(f, "Backend error: {}", err),
            GpuMemorySystemError::AllocationError(msg) => write!(f, "Allocation error: {}", msg),
            GpuMemorySystemError::ManagementError(msg) => write!(f, "Management error: {}", msg),
            GpuMemorySystemError::InvalidPointer(msg) => write!(f, "Invalid pointer: {}", msg),
            GpuMemorySystemError::SystemNotStarted => write!(f, "System not started"),
            GpuMemorySystemError::ConfigurationError(msg) => {
                write!(f, "Configuration error: {}", msg)
            }
            GpuMemorySystemError::OptimizationFailed(msg) => {
                write!(f, "Optimization failed: {}", msg)
            }
            GpuMemorySystemError::InternalError(msg) => write!(f, "Internal error: {}", msg),
        }
    }
}

impl std::error::Error for GpuMemorySystemError {}

impl From<UnifiedGpuError> for GpuMemorySystemError {
    fn from(err: UnifiedGpuError) -> Self {
        GpuMemorySystemError::BackendError(err)
    }
}

impl From<allocation::AllocationError> for GpuMemorySystemError {
    fn from(err: allocation::AllocationError) -> Self {
        GpuMemorySystemError::AllocationError(format!("{}", err))
    }
}

impl From<MemoryManagementError> for GpuMemorySystemError {
    fn from(err: MemoryManagementError) -> Self {
        GpuMemorySystemError::ManagementError(format!("{}", err))
    }
}

/// Thread-safe wrapper for GPU memory system
pub struct ThreadSafeGpuMemorySystem {
    system: Arc<Mutex<GpuMemorySystem>>,
}

impl ThreadSafeGpuMemorySystem {
    pub fn new(config: GpuMemorySystemConfig) -> Result<Self, GpuMemorySystemError> {
        let system = GpuMemorySystem::new(config)?;
        Ok(Self {
            system: Arc::new(Mutex::new(system)),
        })
    }

    pub fn allocate(
        &self,
        size: usize,
        alignment: Option<usize>,
    ) -> Result<*mut c_void, GpuMemorySystemError> {
        let mut system = self.system.lock().map_err(|_| {
            GpuMemorySystemError::InternalError("memory system lock poisoned".into())
        })?;
        system.allocate(size, alignment)
    }

    pub fn free(&self, ptr: *mut c_void) -> Result<(), GpuMemorySystemError> {
        let mut system = self.system.lock().map_err(|_| {
            GpuMemorySystemError::InternalError("memory system lock poisoned".into())
        })?;
        system.free(ptr)
    }

    pub fn get_stats(&self) -> SystemStats {
        // Infallible accessor: recover the guard even if a previous holder
        // panicked, rather than propagating the poison as a panic.
        let mut system = self.system.lock().unwrap_or_else(|e| e.into_inner());
        system.get_stats()
    }

    pub fn optimize(&self) -> Result<(), GpuMemorySystemError> {
        let mut system = self.system.lock().map_err(|_| {
            GpuMemorySystemError::InternalError("memory system lock poisoned".into())
        })?;
        system.optimize()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Helper function to create test configuration with small memory limits
    fn create_test_config() -> GpuMemorySystemConfig {
        let mut config = GpuMemorySystemConfig::default();
        // Use very small memory budget for testing
        config.system_config.memory_budget = 0.001; // 0.1% of total memory
                                                    // Disable memory pools to avoid large allocations
        if let VendorConfig::Cuda(ref mut cuda_config) = config.vendor_config {
            cuda_config.enable_memory_pools = false;
        }
        config
    }

    #[test]
    fn test_system_creation() {
        let config = create_test_config();
        let system = GpuMemorySystem::new(config);
        // Accept both Ok and Err as valid outcomes since GPU might not be available
        assert!(system.is_ok() || system.is_err());
    }

    #[test]
    fn test_auto_create() {
        // Auto-create may fail if no GPU is available, which is fine for testing
        let system = GpuMemorySystem::auto_create();
        assert!(system.is_ok() || system.is_err());
    }

    #[test]
    fn test_thread_safe_wrapper() {
        let config = create_test_config();
        let system = ThreadSafeGpuMemorySystem::new(config);
        // Accept both Ok and Err as valid outcomes since GPU might not be available
        assert!(system.is_ok() || system.is_err());
    }

    #[test]
    fn test_allocator_selection() {
        let config = create_test_config();
        // Only run this test if we can actually create a system
        if let Ok(system) = GpuMemorySystem::new(config) {
            assert_eq!(system.choose_allocator(512), AllocatorType::Slab);
            assert_eq!(system.choose_allocator(64 * 1024), AllocatorType::Buddy);
            assert_eq!(
                system.choose_allocator(2 * 1024 * 1024),
                AllocatorType::Arena
            );
        }
    }
}