pub trait ComputeOp {
type Input<'a>;
type Output;
// Required methods
fn compute_gpu(
&self,
ctx: &GpuContext,
input: &Self::Input<'_>,
) -> Result<Self::Output, GpuOpError>;
fn compute_cpu(&self, input: &Self::Input<'_>) -> Self::Output;
}Expand description
An operation implemented on BOTH the GPU and the CPU.
Implementors provide the two paths; the Executor chooses between them.
Keep Input/Output on plain Rust types (&[f32], Vec<f32>, …) so an
op is usable without pulling in wgpu types at the call site.
Contract the two paths MUST honour: for the same input, compute_gpu (when
it succeeds) and compute_cpu must produce the same result — same length
and, for floats, bit-for-bit equal for exactly-representable arithmetic like
elementwise add. The test suite asserts exactly this whenever a GPU is
present. If a kernel is only approximately equal to its CPU twin (e.g. a
fast-math reduction), say so in that op’s docs and loosen its test to a
tolerance — do not weaken this trait’s default expectation silently.
Required Associated Types§
Sourcetype Input<'a>
type Input<'a>
The operation’s input, borrowed. A GAT (Input<'a>) so the input can
hold borrows (&[f32]) without forcing them to be 'static — the whole
reason a plain associated type wouldn’t do here. Both paths take it by
shared reference, so the cascade can hand the SAME input to the GPU path
and then, on failure, to the CPU path.
Required Methods§
Sourcefn compute_gpu(
&self,
ctx: &GpuContext,
input: &Self::Input<'_>,
) -> Result<Self::Output, GpuOpError>
fn compute_gpu( &self, ctx: &GpuContext, input: &Self::Input<'_>, ) -> Result<Self::Output, GpuOpError>
Run on the GPU. Fallible: returns Err if the GPU path could not
complete for ANY reason (shader/pipeline error, buffer map failure,
device lost). A returned Err is the cascade’s signal to fall back to
CPU — it must never be a panic.
Sourcefn compute_cpu(&self, input: &Self::Input<'_>) -> Self::Output
fn compute_cpu(&self, input: &Self::Input<'_>) -> Self::Output
Run on the CPU in pure Rust. Infallible and always correct — this is the floor the cascade lands on, so it must be a real implementation.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".