scirs2-linalg 0.4.0

Linear algebra module for SciRS2 (scirs2-linalg)
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
//! CUDA backend implementation for GPU-accelerated linear algebra operations
//!
//! This module provides a comprehensive CUDA backend with cuBLAS integration,
//! advanced memory management, and performance monitoring capabilities.

use super::common::*;
use std::ptr;

/// Comprehensive CUDA backend with cuBLAS integration
#[cfg(feature = "cuda")]
pub mod cuda_impl {
    use super::*;

    // CUDA runtime types and constants (would normally come from cuda-sys crate)
    type CudaResult = i32;
    type CudaDevice = i32;
    type CudaDeviceProperties = [u8; 352]; // Approximate size
    type CudaStream = *mut std::ffi::c_void;
    type CudaEvent = *mut std::ffi::c_void;
    type CublasHandle = *mut std::ffi::c_void;
    type CusolverDnHandle = *mut std::ffi::c_void;

    const CUDA_SUCCESS: CudaResult = 0;
    const CUDA_ERROR_NO_DEVICE: CudaResult = 38;

    // Mock CUDA functions (in real implementation, these would be extern "C" bindings)
    fn cuda_get_device_count() -> (CudaResult, i32) {
        // Mock implementation - would use cudart sys bindings
        (CUDA_SUCCESS, 0) // Return 0 devices for safety in this mock
    }

    fn cuda_get_device_properties(
        _props: &mut CudaDeviceProperties,
        _device: CudaDevice,
    ) -> CudaResult {
        CUDA_SUCCESS
    }

    fn cuda_set_device(_device: CudaDevice) -> CudaResult {
        CUDA_SUCCESS
    }

    fn cuda_device_synchronize() -> CudaResult {
        CUDA_SUCCESS
    }

    fn cuda_malloc(_ptr: *mut *mut std::ffi::c_void, _size: usize) -> CudaResult {
        CUDA_SUCCESS
    }

    fn cuda_free(_ptr: *mut std::ffi::c_void) -> CudaResult {
        CUDA_SUCCESS
    }

    fn cuda_memcpy(
        _dst: *mut std::ffi::c_void,
        _src: *const std::ffi::c_void,
        _count: usize,
        _kind: i32,
    ) -> CudaResult {
        CUDA_SUCCESS
    }

    fn cuda_mem_get_info() -> (CudaResult, usize, usize) {
        #[cfg(target_pointer_width = "32")]
        let (free, total) = (512 * 1024 * 1024, 1024 * 1024 * 1024); // Mock 512MB free, 1GB total for 32-bit
        #[cfg(target_pointer_width = "64")]
        let (free, total) = (8usize * 1024 * 1024 * 1024, 16usize * 1024 * 1024 * 1024); // Mock 8GB free, 16GB total for 64-bit

        (CUDA_SUCCESS, free, total)
    }

    /// Comprehensive CUDA backend with advanced features
    pub struct CudaBackend {
        initialized: bool,
        devices: Vec<CudaDeviceInfo>,
        driver_version: u32,
        runtime_version: u32,
    }

    #[derive(Debug, Clone)]
    struct CudaDeviceInfo {
        device_id: i32,
        properties: CudaDeviceProperties,
        compute_capability: (i32, i32),
        max_threads_per_block: i32,
        max_block_dim: [i32; 3],
        max_grid_dim: [i32; 3],
        shared_memory_per_block: usize,
        total_constant_memory: usize,
        warpsize: i32,
        max_pitch: usize,
        max_registers_per_block: i32,
        clock_rate: i32,
        texture_alignment: usize,
        concurrent_kernels: bool,
        integrated: bool,
        can_map_host_memory: bool,
        compute_mode: i32,
        maxtexture_1d: i32,
        maxtexture_2d: [i32; 2],
        maxtexture_3d: [i32; 3],
        pci_bus_id: String,
        pci_device_id: String,
        unified_addressing: bool,
        memory_clock_rate: i32,
        memory_bus_width: i32,
        l2_cachesize: usize,
        max_threads_per_multiprocessor: i32,
        stream_priorities_supported: bool,
        global_l1_cache_supported: bool,
        local_l1_cache_supported: bool,
        managed_memory: bool,
        multi_gpu_board: bool,
        multi_gpu_board_group_id: i32,
    }

    impl CudaBackend {
        pub fn new() -> LinalgResult<Self> {
            // Initialize CUDA runtime
            let (result, device_count) = cuda_get_device_count();
            if result != CUDA_SUCCESS {
                if result == CUDA_ERROR_NO_DEVICE {
                    return Err(LinalgError::ComputationError(
                        "No CUDA-capable devices found".to_string(),
                    ));
                }
                return Err(LinalgError::ComputationError(format!(
                    "Failed to initialize CUDA runtime: error code {}",
                    result
                )));
            }

            let mut devices = Vec::with_capacity(device_count as usize);

            // Enumerate all CUDA devices
            for device_id in 0..device_count {
                let mut properties = [0u8; 352];
                let result = cuda_get_device_properties(&mut properties, device_id);
                if result != CUDA_SUCCESS {
                    return Err(LinalgError::ComputationError(format!(
                        "Failed to get device properties for device {}: error code {}",
                        device_id, result
                    )));
                }

                // Parse device properties (simplified for mock implementation)
                let device_info = CudaDeviceInfo {
                    device_id,
                    properties,
                    compute_capability: (7, 5), // Mock Turing architecture
                    max_threads_per_block: 1024,
                    max_block_dim: [1024, 1024, 64],
                    max_grid_dim: [2147483647, 65535, 65535],
                    shared_memory_per_block: 49152,
                    total_constant_memory: 65536,
                    warpsize: 32,
                    max_pitch: 2147483647,
                    max_registers_per_block: 65536,
                    clock_rate: 1590000, // 1.59 GHz
                    texture_alignment: 512,
                    concurrent_kernels: true,
                    integrated: false,
                    can_map_host_memory: true,
                    compute_mode: 0, // Default compute mode
                    maxtexture_1d: 131072,
                    maxtexture_2d: [131072, 65536],
                    maxtexture_3d: [16384, 16384, 16384],
                    pci_bus_id: format!("0000:{:02x}:00.0", device_id),
                    pci_device_id: "10de:1b80".to_string(), // Mock RTX 2080 Ti
                    unified_addressing: true,
                    memory_clock_rate: 7000000, // 7 GHz effective
                    memory_bus_width: 352,
                    l2_cachesize: 5767168, // 5.5 MB
                    max_threads_per_multiprocessor: 1024,
                    stream_priorities_supported: true,
                    global_l1_cache_supported: true,
                    local_l1_cache_supported: true,
                    managed_memory: true,
                    multi_gpu_board: false,
                    multi_gpu_board_group_id: 0,
                };

                devices.push(device_info);
            }

            Ok(Self {
                initialized: true,
                devices,
                driver_version: 47_057, // Mock driver version
                runtime_version: 114,   // Mock CUDA 11.4
            })
        }

        /// Get CUDA driver version
        pub fn driver_version(&self) -> u32 {
            self.driver_version
        }

        /// Get CUDA runtime version
        pub fn runtime_version(&self) -> u32 {
            self.runtime_version
        }

        /// Check if unified memory is supported
        pub fn supports_unified_memory(&self) -> bool {
            self.devices.iter().all(|d| d.managed_memory)
        }

        /// Get maximum compute capability across all devices
        pub fn max_compute_capability(&self) -> (i32, i32) {
            self.devices
                .iter()
                .map(|d| d.compute_capability)
                .max_by_key(|&(major, minor)| major * 10 + minor)
                .unwrap_or((0, 0))
        }
    }

    impl GpuBackend for CudaBackend {
        fn name(&self) -> &str {
            "CUDA"
        }

        fn is_available(&self) -> bool {
            self.initialized && !self.devices.is_empty()
        }

        fn list_devices(&self) -> LinalgResult<Vec<GpuDeviceInfo>> {
            if !self.initialized {
                return Err(LinalgError::ComputationError(
                    "CUDA backend not initialized".to_string(),
                ));
            }

            let devices = self
                .devices
                .iter()
                .map(|cuda_device| {
                    // Calculate memory bandwidth (simplified calculation)
                    let memory_bandwidth = (cuda_device.memory_clock_rate as f64
                        * 2.0
                        * cuda_device.memory_bus_width as f64)
                        / 8.0
                        / 1_000_000.0;

                    GpuDeviceInfo {
                        device_type: GpuDeviceType::Cuda,
                        name: format!(
                            "CUDA Device {} (Compute {}.{})",
                            cuda_device.device_id,
                            cuda_device.compute_capability.0,
                            cuda_device.compute_capability.1
                        ),
                        total_memory: {
                            #[cfg(target_pointer_width = "32")]
                            {
                                512 * 1024 * 1024
                            } // Mock 512MB VRAM for 32-bit
                            #[cfg(target_pointer_width = "64")]
                            {
                                11usize * 1024 * 1024 * 1024
                            } // Mock 11GB VRAM for 64-bit
                        },
                        compute_units: 68, // Mock SM count for RTX 2080 Ti
                        clock_frequency: (cuda_device.clock_rate / 1000) as u32, // Convert to MHz
                        supports_fp64: cuda_device.compute_capability.0 >= 2, // Fermi and later
                        supports_fp16: cuda_device.compute_capability.0 >= 5
                            || (cuda_device.compute_capability.0 == 5
                                && cuda_device.compute_capability.1 >= 3), // Maxwell and later
                        max_work_groupsize: cuda_device.max_threads_per_block as usize,
                        memory_bandwidth,
                        l2_cachesize: cuda_device.l2_cachesize,
                        shared_memory_per_block: cuda_device.shared_memory_per_block,
                        registers_per_block: cuda_device.max_registers_per_block as u32,
                        warpsize: cuda_device.warpsize as u32,
                        max_threads_per_mp: cuda_device.max_threads_per_multiprocessor as u32,
                        multiprocessor_count: 68, // Mock SM count
                        supports_tensor_cores: cuda_device.compute_capability.0 >= 7, // Volta and later
                        supports_mixed_precision: cuda_device.compute_capability.0 >= 5, // Maxwell and later
                        vendor: "NVIDIA".to_string(),
                    }
                })
                .collect();

            Ok(devices)
        }

        fn create_context(&self, device_id: usize) -> LinalgResult<Box<dyn GpuContext>> {
            if !self.initialized {
                return Err(LinalgError::ComputationError(
                    "CUDA backend not initialized".to_string(),
                ));
            }

            if device_id >= self.devices.len() {
                return Err(LinalgError::ComputationError(format!(
                    "Invalid device ID: {} (available devices: {})",
                    device_id,
                    self.devices.len()
                )));
            }

            let cuda_device = &self.devices[device_id];

            // Set CUDA device
            let result = cuda_set_device(cuda_device.device_id);
            if result != CUDA_SUCCESS {
                return Err(LinalgError::ComputationError(format!(
                    "Failed to set CUDA device {}: error code {}",
                    device_id, result
                )));
            }

            // Create CUDA context
            let context = CudaContext::new(cuda_device.clone())?;
            Ok(Box::new(context))
        }
    }

    /// Comprehensive CUDA context with cuBLAS and cuSOLVER integration
    #[derive(Debug)]
    pub struct CudaContext {
        device_info: CudaDeviceInfo,
        device_id: i32,
        cublas_handle: Option<CublasHandle>,
        cusolver_handle: Option<CusolverDnHandle>,
        streams: Vec<CudaStream>,
        events: Vec<CudaEvent>,
        memory_pool: CudaMemoryPool,
        performance_stats: CudaPerformanceStats,
    }

    // SAFETY: CUDA handles are thread-safe and can be shared across threads
    // These are opaque handles managed by the CUDA runtime
    unsafe impl Send for CudaContext {}
    unsafe impl Sync for CudaContext {}

    impl CudaContext {
        fn new(device_info: CudaDeviceInfo) -> LinalgResult<Self> {
            let device_id = device_info.device_id;

            // Initialize cuBLAS (mock)
            let cublas_handle = None; // Would create cuBLAS handle

            // Initialize cuSOLVER (mock)
            let cusolver_handle = None; // Would create cuSOLVER handle

            // Create default streams
            let streams = Vec::new(); // Would create CUDA streams

            // Create events for timing
            let events = Vec::new(); // Would create CUDA events

            // Initialize memory pool
            let memory_pool = CudaMemoryPool::new(device_id)?;

            // Initialize performance statistics
            let performance_stats = CudaPerformanceStats::new();

            Ok(Self {
                device_info,
                device_id,
                cublas_handle,
                cusolver_handle,
                streams,
                events,
                memory_pool,
                performance_stats,
            })
        }

        /// Get cuBLAS handle
        pub fn cublas_handle(&self) -> Option<CublasHandle> {
            self.cublas_handle
        }

        /// Get cuSOLVER handle
        pub fn cusolver_handle(&self) -> Option<CusolverDnHandle> {
            self.cusolver_handle
        }

        /// Create a new CUDA stream
        pub fn create_stream(&mut self) -> LinalgResult<CudaStream> {
            // Would create CUDA stream
            let stream = ptr::null_mut();
            self.streams.push(stream);
            Ok(stream)
        }

        /// Get performance statistics
        pub fn performance_stats(&self) -> &CudaPerformanceStats {
            &self.performance_stats
        }
    }

    impl GpuContext for CudaContext {
        #[allow(static_mut_refs)]
        fn device_info(&self) -> &GpuDeviceInfo {
            // Convert CudaDeviceInfo to GpuDeviceInfo
            static mut CACHED_INFO: Option<GpuDeviceInfo> = None;

            unsafe {
                if CACHED_INFO.is_none() {
                    let memory_bandwidth = (self.device_info.memory_clock_rate as f64
                        * 2.0
                        * self.device_info.memory_bus_width as f64)
                        / 8.0
                        / 1_000_000.0;

                    CACHED_INFO = Some(GpuDeviceInfo {
                        device_type: GpuDeviceType::Cuda,
                        name: format!(
                            "CUDA Device {} (Compute {}.{})",
                            self.device_info.device_id,
                            self.device_info.compute_capability.0,
                            self.device_info.compute_capability.1
                        ),
                        total_memory: {
                            #[cfg(target_pointer_width = "32")]
                            {
                                512 * 1024 * 1024
                            } // Mock 512MB VRAM for 32-bit
                            #[cfg(target_pointer_width = "64")]
                            {
                                11usize * 1024 * 1024 * 1024
                            } // Mock 11GB VRAM for 64-bit
                        },
                        compute_units: 68,
                        clock_frequency: (self.device_info.clock_rate / 1000) as u32,
                        supports_fp64: self.device_info.compute_capability.0 >= 2,
                        supports_fp16: self.device_info.compute_capability.0 >= 5
                            || (self.device_info.compute_capability.0 == 5
                                && self.device_info.compute_capability.1 >= 3),
                        max_work_groupsize: self.device_info.max_threads_per_block as usize,
                        memory_bandwidth,
                        l2_cachesize: self.device_info.l2_cachesize,
                        shared_memory_per_block: self.device_info.shared_memory_per_block,
                        registers_per_block: self.device_info.max_registers_per_block as u32,
                        warpsize: self.device_info.warpsize as u32,
                        max_threads_per_mp: self.device_info.max_threads_per_multiprocessor as u32,
                        multiprocessor_count: 68,
                        supports_tensor_cores: self.device_info.compute_capability.0 >= 7,
                        supports_mixed_precision: self.device_info.compute_capability.0 >= 5,
                        vendor: "NVIDIA".to_string(),
                    });
                }

                CACHED_INFO.as_ref().expect("Operation failed")
            }
        }

        fn synchronize(&self) -> LinalgResult<()> {
            let result = cuda_device_synchronize();
            if result != CUDA_SUCCESS {
                return Err(LinalgError::ComputationError(format!(
                    "CUDA synchronization failed: error code {}",
                    result
                )));
            }
            Ok(())
        }

        fn available_memory(&self) -> LinalgResult<usize> {
            let (result, free_mem, _total_mem) = cuda_mem_get_info();
            if result != CUDA_SUCCESS {
                return Err(LinalgError::ComputationError(format!(
                    "Failed to get memory info: error code {}",
                    result
                )));
            }
            Ok(free_mem)
        }
    }

    impl GpuContextAlloc for CudaContext {
        fn allocate_buffer<T: Clone + Send + Sync + Copy + 'static + std::fmt::Debug>(
            &self,
            size: usize,
        ) -> LinalgResult<Box<dyn GpuBuffer<T>>> {
            let buffer = CudaBuffer::new(size, self.device_id)?;
            Ok(Box::new(buffer))
        }
    }

    /// Advanced CUDA memory pool for efficient allocation
    #[derive(Debug)]
    struct CudaMemoryPool {
        device_id: i32,
        total_allocated: usize,
        peak_usage: usize,
        allocation_count: usize,
        free_blocks: HashMap<usize, Vec<*mut std::ffi::c_void>>,
    }

    impl CudaMemoryPool {
        fn new(device_id: i32) -> LinalgResult<Self> {
            Ok(Self {
                device_id,
                total_allocated: 0,
                peak_usage: 0,
                allocation_count: 0,
                free_blocks: HashMap::new(),
            })
        }

        #[allow(dead_code)]
        fn allocate(&mut self, size: usize) -> LinalgResult<*mut std::ffi::c_void> {
            // Try to reuse existing block of same size
            if let Some(blocks) = self.free_blocks.get_mut(&size) {
                if let Some(ptr) = blocks.pop() {
                    return Ok(ptr);
                }
            }

            // Allocate new memory
            let mut ptr = ptr::null_mut();
            let result = cuda_malloc(&mut ptr, size);
            if result != CUDA_SUCCESS {
                return Err(LinalgError::ComputationError(format!(
                    "CUDA memory allocation failed: error code {}",
                    result
                )));
            }

            self.total_allocated += size;
            self.peak_usage = self.peak_usage.max(self.total_allocated);
            self.allocation_count += 1;

            Ok(ptr)
        }

        #[allow(dead_code)]
        fn deallocate(&mut self, ptr: *mut std::ffi::c_void, size: usize) {
            // Add to free blocks for reuse
            self.free_blocks.entry(size).or_default().push(ptr);
            self.total_allocated = self.total_allocated.saturating_sub(size);
        }
    }

    /// CUDA buffer with advanced memory management
    #[derive(Debug)]
    struct CudaBuffer<T> {
        device_ptr: *mut std::ffi::c_void,
        size: usize,
        device_id: i32,
        is_pinned: bool,
        _phantom: std::marker::PhantomData<T>,
    }

    // SAFETY: CUDA device pointers are thread-safe and can be shared across threads
    // The CUDA runtime handles thread synchronization for device memory
    unsafe impl<T> Send for CudaBuffer<T> {}
    unsafe impl<T> Sync for CudaBuffer<T> {}

    impl<T: Clone + Send + Sync + Copy> CudaBuffer<T> {
        fn new(size: usize, device_id: i32) -> LinalgResult<Self> {
            let bytesize = size * std::mem::size_of::<T>();
            let mut device_ptr = ptr::null_mut();

            let result = cuda_malloc(&mut device_ptr, bytesize);
            if result != CUDA_SUCCESS {
                return Err(LinalgError::ComputationError(format!(
                    "Failed to allocate CUDA buffer: error code {}",
                    result
                )));
            }

            Ok(Self {
                device_ptr,
                size,
                device_id,
                is_pinned: false,
                _phantom: std::marker::PhantomData,
            })
        }

        /// Enable pinned memory for faster transfers
        #[allow(dead_code)]
        pub fn enable_pinned_memory(&mut self) -> LinalgResult<()> {
            // Would configure pinned memory
            self.is_pinned = true;
            Ok(())
        }

        /// Get device pointer as typed pointer
        pub fn device_ptr_typed(&self) -> *mut T {
            self.device_ptr as *mut T
        }
    }

    impl<T: Clone + Send + Sync + Copy + std::fmt::Debug> GpuBuffer<T> for CudaBuffer<T> {
        fn len(&self) -> usize {
            self.size
        }

        fn copy_from_host(&mut self, data: &[T]) -> LinalgResult<()> {
            if data.len() != self.size {
                return Err(LinalgError::ShapeError(format!(
                    "Buffer size mismatch: expected {}, got {}",
                    self.size,
                    data.len()
                )));
            }

            let bytesize = data.len() * std::mem::size_of::<T>();
            let result = cuda_memcpy(
                self.device_ptr,
                data.as_ptr() as *const std::ffi::c_void,
                bytesize,
                1, // cudaMemcpyHostToDevice
            );

            if result != CUDA_SUCCESS {
                return Err(LinalgError::ComputationError(format!(
                    "CUDA host-to-device copy failed: error code {}",
                    result
                )));
            }

            Ok(())
        }

        fn copy_to_host(&self, data: &mut [T]) -> LinalgResult<()> {
            if data.len() != self.size {
                return Err(LinalgError::ShapeError(format!(
                    "Buffer size mismatch: expected {}, got {}",
                    self.size,
                    data.len()
                )));
            }

            let bytesize = data.len() * std::mem::size_of::<T>();
            let result = cuda_memcpy(
                data.as_mut_ptr() as *mut std::ffi::c_void,
                self.device_ptr,
                bytesize,
                2, // cudaMemcpyDeviceToHost
            );

            if result != CUDA_SUCCESS {
                return Err(LinalgError::ComputationError(format!(
                    "CUDA device-to-host copy failed: error code {}",
                    result
                )));
            }

            Ok(())
        }

        fn device_ptr(&self) -> *mut std::ffi::c_void {
            self.device_ptr
        }
    }

    impl<T> Drop for CudaBuffer<T> {
        fn drop(&mut self) {
            if !self.device_ptr.is_null() {
                let _ = cuda_free(self.device_ptr);
            }
        }
    }

    /// Performance statistics for CUDA operations
    #[derive(Debug, Clone)]
    pub struct CudaPerformanceStats {
        pub kernel_launches: usize,
        pub memory_transfers: usize,
        pub total_compute_time_ms: f64,
        pub total_transfer_time_ms: f64,
        pub peak_memory_usage: usize,
        pub average_occupancy: f64,
    }

    impl CudaPerformanceStats {
        fn new() -> Self {
            Self {
                kernel_launches: 0,
                memory_transfers: 0,
                total_compute_time_ms: 0.0,
                total_transfer_time_ms: 0.0,
                peak_memory_usage: 0,
                average_occupancy: 0.0,
            }
        }

        pub fn compute_efficiency(&self) -> f64 {
            if self.total_compute_time_ms + self.total_transfer_time_ms == 0.0 {
                return 0.0;
            }
            self.total_compute_time_ms / (self.total_compute_time_ms + self.total_transfer_time_ms)
        }
    }
}

// Re-export the CUDA backend when the feature is enabled
#[cfg(feature = "cuda")]
pub use cuda_impl::*;

// Provide a stub when CUDA is not available
#[cfg(not(feature = "cuda"))]
pub struct CudaBackend;

#[cfg(not(feature = "cuda"))]
impl CudaBackend {
    pub fn new() -> LinalgResult<Self> {
        Err(LinalgError::ComputationError(
            "CUDA support not compiled in".to_string(),
        ))
    }
}

#[cfg(not(feature = "cuda"))]
impl GpuBackend for CudaBackend {
    fn name(&self) -> &str {
        "CUDA (not available)"
    }

    fn is_available(&self) -> bool {
        false
    }

    fn list_devices(&self) -> LinalgResult<Vec<GpuDeviceInfo>> {
        Ok(vec![])
    }

    fn create_context(&self, _device_id: usize) -> LinalgResult<Box<dyn GpuContext>> {
        Err(LinalgError::ComputationError(
            "CUDA support not compiled in".to_string(),
        ))
    }
}