pub fn with_runtime_engine<R>(
runtime: &EagerRuntime,
f: impl FnOnce(&mut dyn ExecutionEngine) -> R,
) -> Result<R, ExecutionContextEntryError>Expand description
Run f on the engine of the context that owns runtime’s eager runtime.
This is the transitional implicit route: an implicit (context-free) eager operation has no context argument, so it resolves the engine from the runtime of the tensor it acts on instead of always taking the process-global default context’s engine. That keeps a caller-owned context’s thread policy, device domain, and engine serialization intact for context-free readouts and intermediate operations, including on a context that is not the global one.
The closure receives the owning engine as [ExecutionEngine]: the owner may
be a CPU context ([CpuEngine]) or, with the tenferro-cuda feature, a CUDA
context (tenferro_gpu::cuda::CudaEngine). The closure runs through the
owning context’s normal session entry, so a nested entry on the same thread
is reported as ExecutionContextEntryError’s typed CPU or CUDA re-entry
variant instead of waiting for the engine. An entry from a Rayon worker runs
inline on a serial CPU engine that shares the context’s prepared-plan cache;
a CUDA context keeps its single engine and never opens a second one.
When no live context owns runtime (for example after the context was
dropped), the entry falls back to the process-global CPU context; the global
context’s own runtime resolves to itself.
§Errors
Returns ExecutionContextEntryError when the owning context’s session
entry is rejected.
§Examples
use tensor4all_tensorbackend::{with_runtime_engine, CpuBackend, CpuExecutionContext};
use tenferro_cpu::CpuEngine;
let context = CpuExecutionContext::from_backend(CpuBackend::with_threads(1)?);
let runtime = context.eager_runtime()?;
let threads = with_runtime_engine(&runtime, |engine| {
engine
.as_any_mut()
.downcast_mut::<CpuEngine>()
.map(|engine| engine.num_threads())
})?;
assert_eq!(threads, Some(1));