kn_cuda_sys/wrapper/
event.rs

1use std::ptr::null_mut;
2
3use crate::bindings::{cudaEventCreate, cudaEventDestroy, cudaEventElapsedTime, cudaEventSynchronize, cudaEvent_t};
4use crate::wrapper::status::Status;
5
6#[derive(Debug)]
7pub struct CudaEvent(cudaEvent_t);
8
9impl CudaEvent {
10    pub fn new() -> Self {
11        unsafe {
12            let mut inner = null_mut();
13            cudaEventCreate(&mut inner as *mut _).unwrap();
14            CudaEvent(inner)
15        }
16    }
17
18    /// Return the elapsed time since `start` _in seconds_.
19    pub fn time_elapsed_since(&self, start: &CudaEvent) -> f32 {
20        unsafe {
21            let mut result: f32 = 0.0;
22            cudaEventElapsedTime(&mut result as *mut _, start.inner(), self.inner()).unwrap();
23            result / 1000.0
24        }
25    }
26
27    pub fn synchronize(&self) {
28        unsafe { cudaEventSynchronize(self.inner()).unwrap() }
29    }
30
31    pub unsafe fn inner(&self) -> cudaEvent_t {
32        self.0
33    }
34}
35
36impl Drop for CudaEvent {
37    fn drop(&mut self) {
38        unsafe { cudaEventDestroy(self.0).unwrap_in_drop() }
39    }
40}