pub struct Executor { /* private fields */ }Expand description
Runs ComputeOps through the GPU->CPU cascade.
Build ONE and reuse it: it probes the GPU exactly once at construction and
caches the handle. A program that makes an Executor per call pays the
adapter-enumeration cost every time and defeats the caching.
Implementations§
Source§impl Executor
impl Executor
Sourcepub fn new() -> Self
pub fn new() -> Self
Build an executor, probing for a GPU once.
Never fails: a missing GPU is not an error here, it just means every op
will take the CPU path. Use Executor::has_gpu if you need to know
which way the cascade will go.
Sourcepub fn cpu_only() -> Self
pub fn cpu_only() -> Self
Build an executor that is FORCED onto the CPU path, ignoring any GPU.
This is what makes the CPU fallback testable on a machine that does
have a GPU: with no adapter to borrow, run can only cascade to CPU.
Sourcepub fn has_gpu(&self) -> bool
pub fn has_gpu(&self) -> bool
Does this executor have a GPU to use? false means every run lands on
CPU. Handy for tests (skip the GPU==CPU assertion when there is no GPU)
and for logging which path a workload will take.
Sourcepub fn gpu_context(&self) -> Option<&GpuContext>
pub fn gpu_context(&self) -> Option<&GpuContext>
The cached GPU context, if any. Lets a caller run the GPU path directly (e.g. a test asserting GPU==CPU) without going through the cascade.
Sourcepub fn run<O>(&self, op: &O, input: &O::Input<'_>) -> O::Outputwhere
O: ComputeOp,
pub fn run<O>(&self, op: &O, input: &O::Input<'_>) -> O::Outputwhere
O: ComputeOp,
Run an op through the cascade: GPU first, CPU on any failure.
This is the one method callers normally use. It always returns a result:
- GPU present and its path succeeds -> the GPU result.
- GPU present but its path returns
Err-> the CPU result (logged as a fall-back atwarn… once tracing is wired; today theErris simply swallowed into the CPU path). - no GPU at all -> straight to the CPU result.
The cascade itself is the small ?/unwrap_or_else dance below: build a
Result for the GPU attempt (short-circuiting to Err if there is no
context), then unwrap_or_else onto the infallible CPU path.