1use std::time::Duration;
4
5#[derive(Debug)]
7pub struct CudaStream {
8 pub handle: *mut std::ffi::c_void,
9 pub device_id: i32,
10}
11
12unsafe impl Send for CudaStream {}
13unsafe impl Sync for CudaStream {}
14
15impl CudaStream {
16 pub fn new(device_id: i32) -> anyhow::Result<Self> {
17 #[cfg(feature = "cuda")]
18 {
19 Err(anyhow::anyhow!("CUDA support needs proper implementation"))
22 }
23
24 #[cfg(not(feature = "cuda"))]
25 {
26 Ok(Self {
27 handle: std::ptr::null_mut(),
28 device_id,
29 })
30 }
31 }
32
33 pub fn synchronize(&self) -> anyhow::Result<()> {
34 #[cfg(feature = "cuda")]
35 {
36 Err(anyhow::anyhow!("CUDA support needs proper implementation"))
38 }
39
40 #[cfg(not(feature = "cuda"))]
41 {
42 Ok(())
43 }
44 }
45}
46
47impl Drop for CudaStream {
48 fn drop(&mut self) {
49 #[cfg(feature = "cuda")]
50 {
51 }
53 }
54}
55
56#[derive(Debug)]
58pub struct CudaKernel {
59 pub function: *mut std::ffi::c_void,
60 pub module: *mut std::ffi::c_void,
61 pub name: String,
62}
63
64unsafe impl Send for CudaKernel {}
65unsafe impl Sync for CudaKernel {}
66
67impl CudaKernel {
68 #[allow(unused_variables)]
69 pub fn load(ptx_code: &str, function_name: &str) -> anyhow::Result<Self> {
70 #[cfg(feature = "cuda")]
71 {
72 Err(anyhow::anyhow!("CUDA support needs proper implementation"))
74 }
75
76 #[cfg(not(feature = "cuda"))]
77 {
78 Ok(Self {
79 function: std::ptr::null_mut(),
80 module: std::ptr::null_mut(),
81 name: function_name.to_string(),
82 })
83 }
84 }
85}
86
87impl Drop for CudaKernel {
88 fn drop(&mut self) {
89 #[cfg(feature = "cuda")]
90 {
91 }
93 }
94}
95
96#[derive(Debug, Default, Clone)]
98pub struct GpuPerformanceStats {
99 pub total_operations: u64,
100 pub total_compute_time: Duration,
101 pub total_memory_transfers: u64,
102 pub total_transfer_time: Duration,
103 pub peak_memory_usage: usize,
104 pub current_memory_usage: usize,
105}
106
107impl GpuPerformanceStats {
108 pub fn new() -> Self {
109 Self::default()
110 }
111
112 pub fn record_operation(&mut self, compute_time: Duration) {
113 self.total_operations += 1;
114 self.total_compute_time += compute_time;
115 }
116
117 pub fn record_transfer(&mut self, transfer_time: Duration) {
118 self.total_memory_transfers += 1;
119 self.total_transfer_time += transfer_time;
120 }
121
122 pub fn update_memory_usage(&mut self, current: usize) {
123 self.current_memory_usage = current;
124 if current > self.peak_memory_usage {
125 self.peak_memory_usage = current;
126 }
127 }
128
129 pub fn average_compute_time(&self) -> Duration {
130 if self.total_operations > 0 {
131 self.total_compute_time / self.total_operations as u32
132 } else {
133 Duration::ZERO
134 }
135 }
136
137 pub fn average_transfer_time(&self) -> Duration {
138 if self.total_memory_transfers > 0 {
139 self.total_transfer_time / self.total_memory_transfers as u32
140 } else {
141 Duration::ZERO
142 }
143 }
144
145 pub fn throughput_ops_per_sec(&self) -> f64 {
146 if !self.total_compute_time.is_zero() {
147 self.total_operations as f64 / self.total_compute_time.as_secs_f64()
148 } else {
149 0.0
150 }
151 }
152}