use crate::hip::Stream;
use crate::hip::error::{Error, Result};
use crate::hip::ffi;
use std::ptr;
pub struct Event {
event: ffi::hipEvent_t,
}
impl Event {
pub fn new() -> Result<Self> {
let mut event = ptr::null_mut();
let error = unsafe { ffi::hipEventCreate(&mut event) };
if error != ffi::hipError_t_hipSuccess {
return Err(Error::new(error));
}
Ok(Self { event })
}
pub fn with_flags(flags: u32) -> Result<Self> {
let mut event = ptr::null_mut();
let error = unsafe { ffi::hipEventCreateWithFlags(&mut event, flags) };
if error != ffi::hipError_t_hipSuccess {
return Err(Error::new(error));
}
Ok(Self { event })
}
pub fn record(&self, stream: &Stream) -> Result<()> {
let error = unsafe { ffi::hipEventRecord(self.event, stream.as_raw()) };
if error != ffi::hipError_t_hipSuccess {
return Err(Error::new(error));
}
Ok(())
}
pub fn synchronize(&self) -> Result<()> {
let error = unsafe { ffi::hipEventSynchronize(self.event) };
if error != ffi::hipError_t_hipSuccess {
return Err(Error::new(error));
}
Ok(())
}
pub fn query(&self) -> Result<()> {
let error = unsafe { ffi::hipEventQuery(self.event) };
if error == ffi::hipError_t_hipSuccess {
Ok(())
} else if error == ffi::hipError_t_hipErrorNotReady {
Err(Error::new(error))
} else {
Err(Error::new(error))
}
}
pub fn elapsed_time(&self, end: &Event) -> Result<f32> {
let mut time = 0.0;
let error = unsafe { ffi::hipEventElapsedTime(&mut time, self.event, end.event) };
if error != ffi::hipError_t_hipSuccess {
return Err(Error::new(error));
}
Ok(time)
}
pub fn as_raw(&self) -> ffi::hipEvent_t {
self.event
}
}
impl Drop for Event {
fn drop(&mut self) {
if !self.event.is_null() {
unsafe {
let _ = ffi::hipEventDestroy(self.event);
};
self.event = ptr::null_mut();
}
}
}
pub mod event_flags {
pub const DEFAULT: u32 = 0;
pub const BLOCKING_SYNC: u32 = 1;
pub const DISABLE_TIMING: u32 = 2;
pub const INTERPROCESS: u32 = 4;
}
pub struct Timer {
start: Event,
stop: Event,
}
impl Timer {
pub fn new() -> Result<Self> {
let start = Event::new()?;
let stop = Event::new()?;
Ok(Self { start, stop })
}
pub fn start(&self, stream: &Stream) -> Result<()> {
self.start.record(stream)
}
pub fn stop(&self, stream: &Stream) -> Result<()> {
self.stop.record(stream)
}
pub fn elapsed_time(&self) -> Result<f32> {
self.stop.synchronize()?;
self.start.elapsed_time(&self.stop)
}
}