use crate::backend::{
private, BackendError, DispatchConfig, OutputBuffers, Resource, TimedDispatchResult,
};
use smallvec::SmallVec;
pub trait CompiledPipeline: private::Sealed + Send + Sync {
fn id(&self) -> &str;
fn dispatch(
&self,
inputs: &[Vec<u8>],
config: &DispatchConfig,
) -> Result<Vec<Vec<u8>>, BackendError>;
fn dispatch_borrowed(
&self,
inputs: &[&[u8]],
config: &DispatchConfig,
) -> Result<Vec<Vec<u8>>, BackendError> {
let owned: SmallVec<[Vec<u8>; 8]> = inputs.iter().map(|input| (*input).to_vec()).collect();
self.dispatch(&owned, config)
}
fn dispatch_borrowed_timed(
&self,
inputs: &[&[u8]],
config: &DispatchConfig,
) -> Result<TimedDispatchResult, BackendError> {
let started = std::time::Instant::now();
let outputs = self.dispatch_borrowed(inputs, config)?;
Ok(TimedDispatchResult {
outputs,
wall_ns: started.elapsed().as_nanos() as u64,
device_ns: None,
enqueue_ns: None,
wait_ns: None,
})
}
fn dispatch_borrowed_into(
&self,
inputs: &[&[u8]],
config: &DispatchConfig,
outputs: &mut OutputBuffers,
) -> Result<(), BackendError> {
let result = self.dispatch_borrowed(inputs, config)?;
outputs.clear();
outputs.extend(result);
Ok(())
}
fn dispatch_borrowed_batched(
&self,
batches: &[&[&[u8]]],
config: &DispatchConfig,
) -> Result<Vec<OutputBuffers>, BackendError> {
let mut outputs = Vec::with_capacity(batches.len());
for batch in batches {
outputs.push(self.dispatch_borrowed(batch, config)?);
}
Ok(outputs)
}
fn dispatch_persistent_handles(
&self,
_inputs: &[Resource],
_config: &DispatchConfig,
) -> Result<OutputBuffers, BackendError> {
Err(BackendError::UnsupportedFeature {
name: "persistent handle dispatch".to_string(),
backend: "unspecified".to_string(),
})
}
fn dispatch_persistent_handles_batched(
&self,
batches: &[&[Resource]],
config: &DispatchConfig,
) -> Result<Vec<OutputBuffers>, BackendError> {
let mut outputs = Vec::with_capacity(batches.len());
for batch in batches {
outputs.push(self.dispatch_persistent_handles(batch, config)?);
}
Ok(outputs)
}
}