use std::{
mem,
sync::{Arc, Mutex},
};
use singe_core::report_cleanup_failure;
use crate::{
error::{Error, Result},
graph::{ExecutableGraph, Graph, GraphInstantiateFlags},
stream::{Stream, StreamCaptureMode},
submission::{DeviceOperation, Execution, Operation, Resources, Submission},
};
pub unsafe trait Recordable: Sized {
type Output: 'static;
fn record(self, recording: &mut Recording) -> Result<Self::Output>;
}
#[derive(Debug)]
pub struct Recording {
stream: Stream,
resources: Resources,
}
impl Recording {
#[must_use]
pub fn stream(&self) -> &Stream {
&self.stream
}
pub unsafe fn enqueue<T>(
&mut self,
resources: Resources,
enqueue: impl FnOnce(&Stream) -> Result<T>,
) -> Result<T> {
for access in &resources.accesses {
access.validate_context(self.stream.context())?;
}
self.resources.extend(resources);
enqueue(&self.stream)
}
}
impl Drop for Recording {
fn drop(&mut self) {
if let Err(error) = self.stream.ensure_not_capturing() {
mem::forget(mem::take(&mut self.resources));
report_cleanup_failure(
"release the recorded resources of",
"cuda stream capture",
&error,
);
}
}
}
#[derive(Debug)]
pub struct OwnedGraph<T> {
graph: Graph,
resources: Resources,
output: Option<T>,
}
impl<T> OwnedGraph<T> {
pub fn take_output(&mut self) -> Option<T> {
self.output.take()
}
pub fn graph(&self) -> &Graph {
&self.graph
}
pub fn instantiate(&self) -> Result<OwnedExecutable> {
Ok(OwnedExecutable {
executable: Arc::new(Mutex::new(
self.graph.instantiate(GraphInstantiateFlags::empty())?,
)),
resources: self.resources.clone(),
})
}
}
#[derive(Debug, Clone)]
pub struct OwnedExecutable {
executable: Arc<Mutex<ExecutableGraph>>,
resources: Resources,
}
impl OwnedExecutable {
#[must_use]
pub unsafe fn from_executable(executable: ExecutableGraph, resources: Resources) -> Self {
Self {
executable: Arc::new(Mutex::new(executable)),
resources,
}
}
pub fn launch(&self, stream: &Stream) -> Result<Submission<()>> {
self.clone().submit_on(stream)
}
pub unsafe fn with_raw<T>(
&self,
access: impl FnOnce(&mut ExecutableGraph) -> Result<T>,
) -> Result<T> {
let mut executable = self
.executable
.lock()
.map_err(|_| Error::lock_poisoned("executable graph"))?;
access(&mut executable)
}
}
unsafe impl Operation for OwnedExecutable {
type Pending = Self::Output;
type Output = ();
fn enqueue(self, execution: &mut Execution) -> Result<()> {
let mut resources = self.resources;
resources.retain(Arc::clone(&self.executable));
unsafe {
execution.enqueue(resources, |stream| {
self.executable
.lock()
.map_err(|_| Error::lock_poisoned("executable graph"))?
.launch_async(stream)
})
}
}
unsafe fn resolve(pending: Self::Pending) -> Result<Self::Output> {
Ok(pending)
}
}
unsafe impl DeviceOperation for OwnedExecutable {}
impl Stream {
pub fn capture_owned<O: Recordable>(&self, operation: O) -> Result<OwnedGraph<O::Output>> {
let mut recording = Recording {
stream: self.clone(),
resources: Resources::new(),
};
let mut output = None;
let graph = self.capture(StreamCaptureMode::Relaxed, |_| {
output = Some(operation.record(&mut recording)?);
Ok(())
})?;
Ok(OwnedGraph {
graph,
resources: mem::take(&mut recording.resources),
output,
})
}
}