use std::sync::Arc;
use crate::hip::{result, sys};
use super::{HipError, HipStream};
pub struct HipGraph {
hip_graph: sys::hipGraph_t,
hip_graph_exec: sys::hipGraphExec_t,
stream: Arc<HipStream>,
}
impl Drop for HipGraph {
fn drop(&mut self) {
let ctx = self.stream.context().clone();
let exec = std::mem::replace(&mut self.hip_graph_exec, std::ptr::null_mut());
if !exec.is_null() {
ctx.record_err(unsafe { result::graph::exec_destroy(exec) });
}
let graph = std::mem::replace(&mut self.hip_graph, std::ptr::null_mut());
if !graph.is_null() {
ctx.record_err(unsafe { result::graph::destroy(graph) });
}
}
}
impl HipStream {
pub fn begin_capture(&self, mode: sys::hipStreamCaptureMode) -> Result<(), HipError> {
self.context().bind_to_thread()?;
unsafe { result::stream::begin_capture(self.hip_stream(), mode) }
}
pub fn end_capture(
self: &Arc<Self>,
flags: sys::hipGraphInstantiateFlags,
) -> Result<Option<HipGraph>, HipError> {
self.context().bind_to_thread()?;
let graph = unsafe { result::stream::end_capture(self.hip_stream()) }?;
if graph.is_null() {
return Ok(None);
}
let exec = unsafe { result::graph::instantiate(graph, flags) }?;
Ok(Some(HipGraph {
hip_graph: graph,
hip_graph_exec: exec,
stream: self.clone(),
}))
}
pub fn capture_status(&self) -> Result<sys::hipStreamCaptureStatus, HipError> {
self.context().bind_to_thread()?;
unsafe { result::stream::is_capturing(self.hip_stream()) }
}
}
impl HipGraph {
pub fn launch(&self) -> Result<(), HipError> {
self.stream.context().bind_to_thread()?;
unsafe { result::graph::launch(self.hip_graph_exec, self.stream.hip_stream()) }
}
pub fn upload(&self) -> Result<(), HipError> {
self.stream.context().bind_to_thread()?;
unsafe { result::graph::upload(self.hip_graph_exec, self.stream.hip_stream()) }
}
pub fn hip_graph(&self) -> sys::hipGraph_t {
self.hip_graph
}
pub fn hip_graph_exec(&self) -> sys::hipGraphExec_t {
self.hip_graph_exec
}
}