Skip to main content

with_batch_policy

Function with_batch_policy 

Source
pub fn with_batch_policy<R>(
    engine: &mut dyn Engine,
    policy: CpuBatchPolicy,
    f: impl FnOnce(&mut dyn Engine) -> R,
) -> Result<R>
Expand description

Run f on engine with policy as the effective CPU batch policy.

This is the scoped override of the precedence described in the module docs: wrapping a single call expresses a per-operation choice. The previous policy is restored when f returns, returns an error or unwinds, and scopes nest with the innermost winning. f runs on the caller’s own engine, so a forwarding engine keeps its own overrides inside the scope. Nothing here mutates process-global state.

§Examples

use tenferro_cpu::{with_batch_policy, with_cpu_engine, CpuBatchPolicy, CpuBatchStrategy, CpuEngine};

let mut engine = CpuEngine::serial();
let observed = with_batch_policy(
    &mut engine,
    CpuBatchPolicy::new(CpuBatchStrategy::Outer),
    |engine| {
        with_cpu_engine(engine, |cpu| {
            cpu.with_linalg_pool(|context, _| Ok(context.batch_policy().strategy))
        })
        .expect("a CPU engine")
    },
)??;
assert_eq!(observed, CpuBatchStrategy::Outer);
assert_eq!(engine.batch_policy().strategy, CpuBatchStrategy::Auto);

§Errors

Returns tenferro_tensor::Error::Unsupported without running f when engine is neither a crate::CpuEngine nor an engine that forwards one through tenferro_tensor::Engine::native_engine (a CUDA or other custom engine). f’s own result is returned unchanged inside Ok.

§Panics

A panic in f propagates after the previous policy is restored.