ghostflow_cuda/
lib.rs

1//! GhostFlow CUDA Backend - Real GPU Acceleration
2//!
3//! This module provides real CUDA GPU acceleration when compiled with the `cuda` feature.
4//! Without the feature, it provides CPU fallback implementations.
5
6pub mod ffi;
7pub mod device;
8pub mod memory;
9pub mod stream;
10pub mod tensor;
11pub mod ops;
12pub mod kernels;
13pub mod error;
14pub mod blas;
15
16// Re-export main types
17pub use device::{CudaDevice, DeviceGuard, get_all_devices, select_best_device};
18pub use memory::{GpuMemoryPool, GpuTensor, get_global_gpu_pool};
19pub use stream::{CudaStream, CudaEvent, CudaTimer};
20pub use tensor::CudaTensor;
21pub use error::{CudaError, CudaResult};
22pub use blas::CuBlas;
23
24/// Check if CUDA is available at runtime
25pub fn is_available() -> bool {
26    #[cfg(feature = "cuda")]
27    {
28        match CudaDevice::count() {
29            Ok(count) => count > 0,
30            Err(_) => false,
31        }
32    }
33    
34    #[cfg(not(feature = "cuda"))]
35    {
36        false
37    }
38}
39
40/// Get CUDA version
41pub fn cuda_version() -> Option<(i32, i32)> {
42    #[cfg(feature = "cuda")]
43    {
44        // Would call cudaRuntimeGetVersion
45        Some((12, 0)) // Placeholder
46    }
47    
48    #[cfg(not(feature = "cuda"))]
49    {
50        None
51    }
52}
53
54/// Initialize CUDA runtime
55pub fn init() -> CudaResult<()> {
56    CudaDevice::init()
57}
58
59/// Get number of available CUDA devices
60pub fn device_count() -> CudaResult<i32> {
61    CudaDevice::count()
62}
63
64/// Synchronize all CUDA operations on current device
65pub fn synchronize() -> CudaResult<()> {
66    CudaDevice::synchronize()
67}
68
69/// Set current CUDA device
70pub fn set_device(device_id: i32) -> CudaResult<()> {
71    let device = CudaDevice::new(device_id)?;
72    device.set_current()
73}
74
75/// Get current CUDA device ID
76pub fn current_device() -> CudaResult<i32> {
77    CudaDevice::current()
78}
79
80/// Memory info for current device
81pub fn memory_info() -> CudaResult<(usize, usize)> {
82    #[cfg(feature = "cuda")]
83    {
84        let mut free: usize = 0;
85        let mut total: usize = 0;
86        
87        unsafe {
88            let err = ffi::cudaMemGetInfo(&mut free, &mut total);
89            if err != 0 {
90                return Err(CudaError::DriverError(err));
91            }
92        }
93        
94        Ok((free, total))
95    }
96    
97    #[cfg(not(feature = "cuda"))]
98    {
99        Ok((0, 0))
100    }
101}
102
103/// Empty CUDA cache (free cached memory)
104pub fn empty_cache() {
105    // This would trigger garbage collection on all memory pools
106    // Implementation depends on global pool management
107}