pub struct CpuExecutionContext<'a> { /* private fields */ }Expand description
Borrowed execution policy for an already-entered CPU operation.
The context exposes immutable domain facts while keeping the resource lease, executor object, and the checked executor-entry boundary private. Providers cannot install or submit work through this value.
§Examples
Providers inspect this value inside a trait method:
use tenferro_cpu::provider::CpuExecutionContext;
assert!(context.thread_budget().get() >= 1);Implementations§
Source§impl<'a> CpuExecutionContext<'a>
impl<'a> CpuExecutionContext<'a>
Sourcepub fn batch_policy(&self) -> CpuBatchPolicy
pub fn batch_policy(&self) -> CpuBatchPolicy
The effective batch policy for batched work in this context.
It is the backend default unless a session scope overrides it; see
crate::with_batch_policy.
§Examples
use tenferro_cpu::{with_cpu_exec_session, CpuBackend, CpuBatchStrategy};
use tenferro_tensor::BackendSessionHost;
let mut backend = CpuBackend::with_threads(1)?;
let strategy = backend.with_backend_session(|session| {
with_cpu_exec_session(session, |cpu| {
cpu.with_linalg_pool(|context, _| Ok(context.batch_policy().strategy()))
})
.expect("a CPU backend session")
})??;
assert_eq!(strategy, CpuBatchStrategy::Auto);Sourcepub fn can_fan_out_lanes(&self) -> bool
pub fn can_fan_out_lanes(&self) -> bool
Whether Self::with_outer_lanes can fan out from this context.
§Examples
use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
use tenferro_tensor::BackendSessionHost;
let mut backend = CpuBackend::with_threads(1)?;
let fans_out = backend.with_backend_session(|session| {
with_cpu_exec_session(session, |cpu| {
cpu.with_linalg_pool(|context, _| Ok(context.can_fan_out_lanes()))
})
.expect("a CPU backend session")
})??;
assert!(!fans_out);Sourcepub fn is_outer_fan_out_lane(&self) -> bool
pub fn is_outer_fan_out_lane(&self) -> bool
Whether this context is one lane of tenferro’s own outer fan-out.
Such a lane runs concurrently with its siblings. Its mode is
ParallelMode::Sequential, so it may not start inner parallel work, and
a provider whose declared parallelism is an independent runtime (the
default for external BLAS/LAPACK) is rejected before dispatch. Running on
a Rayon worker is not by itself outer fan-out.
§Examples
use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
use tenferro_tensor::BackendSessionHost;
let mut backend = CpuBackend::with_threads(1)?;
let lane = backend.with_backend_session(|session| {
with_cpu_exec_session(session, |cpu| {
cpu.with_linalg_pool(|context, _| Ok(context.is_outer_fan_out_lane()))
})
.expect("a CPU backend session")
})??;
assert!(!lane);Sourcepub fn with_outer_lanes<I>(
&self,
jobs: I,
job: impl Fn(I::Item, &CpuExecutionContext<'a>) + Sync,
)
pub fn with_outer_lanes<I>( &self, jobs: I, job: impl Fn(I::Item, &CpuExecutionContext<'a>) + Sync, )
Run independent lane jobs as tenferro-owned outer fan-out inside this already-entered context’s own Rayon region.
Each item of jobs (typically one disjoint chunk of a batch) runs once
and receives a lane context: Self::is_outer_fan_out_lane is true and
the mode is ParallelMode::Sequential, so the lane neither starts
inner parallel work nor reaches an independent-runtime provider. Fan-out
needs an ParallelMode::Inner context of a Rayon executor with more
than one thread; otherwise the jobs run in order on the calling thread,
still with a lane context. No second pool is created: jobs run on the
Rayon pool this context was entered in.
§Examples
use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
use tenferro_tensor::BackendSessionHost;
let mut backend = CpuBackend::with_threads(2)?;
let mut data = vec![1.0_f64; 8];
backend.with_backend_session(|session| {
with_cpu_exec_session(session, |cpu| {
cpu.with_linalg_pool(|context, _| {
context.with_outer_lanes(data.chunks_mut(3), |chunk, lane| {
assert!(lane.is_outer_fan_out_lane());
chunk.iter_mut().for_each(|value| *value *= 2.0);
});
Ok(())
})
})
.expect("a CPU backend session")
})??;
assert_eq!(data, [2.0; 8]);Sourcepub fn domain_id(&self) -> CpuDomainId
pub fn domain_id(&self) -> CpuDomainId
Return the stable identity of the selected CPU resource domain.
§Examples
use tenferro_cpu::CpuExecutionContext;
let _domain_id = context.domain_id();Sourcepub fn cpus(&self) -> Option<&CpuSet>
pub fn cpus(&self) -> Option<&CpuSet>
Return the selected domain’s declared logical CPU set, when present.
§Examples
use tenferro_cpu::CpuExecutionContext;
if let Some(cpus) = context.cpus() {
assert!(!cpus.is_empty());
}Sourcepub fn admission_mode(&self) -> CpuAdmissionMode
pub fn admission_mode(&self) -> CpuAdmissionMode
Return the selected domain’s admission contract.
§Examples
use tenferro_cpu::{CpuAdmissionMode, CpuExecutionContext};
let _mode: CpuAdmissionMode = context.admission_mode();Sourcepub fn thread_budget(&self) -> NonZeroUsize
pub fn thread_budget(&self) -> NonZeroUsize
Return the non-zero maximum participating-thread budget.
§Examples
use tenferro_cpu::CpuExecutionContext;
assert!(context.thread_budget().get() >= 1);Sourcepub fn parallel_mode(&self) -> ParallelMode
pub fn parallel_mode(&self) -> ParallelMode
Return the engine-selected scheduling mode for this entered provider call.
Provider calls observe Sequential or Inner. Outer scheduling creates a separate Sequential context inside every submitted child.
§Examples
use tenferro_cpu::{CpuExecutionContext, ParallelMode};
assert!(matches!(
context.parallel_mode(),
ParallelMode::Sequential | ParallelMode::Inner
));Sourcepub fn rayon_pool(&self) -> Option<&'a ThreadPool>
pub fn rayon_pool(&self) -> Option<&'a ThreadPool>
The Rayon pool this context’s inner parallel region runs on.
Some exactly when the context owns an inner region: parallel mode
ParallelMode::Inner, a Rayon-backed executor, and a thread budget
above one (the same gate as the faer policy). The caller is already on
a worker of this pool, so work installed or scoped on it runs in
place. A provider running its own kernels on the pool must use at most
CpuExecutionContext::thread_budget threads, which can be smaller
than the pool, and declares
crate::CpuThreadCountControl::PerCallUpperBound with
crate::CpuPlacementControl::EngineWorkers.
§Examples
use tenferro_cpu::{with_cpu_exec_session, CpuBackend};
use tenferro_tensor::BackendSessionHost;
let mut backend = CpuBackend::with_threads(2)?;
let workers = backend.with_backend_session(|session| {
with_cpu_exec_session(session, |cpu| {
cpu.with_linalg_pool(|context, _| {
Ok(context.rayon_pool().map(|pool| pool.current_num_threads()))
})
})
.expect("a CPU backend session")
})??;
assert_eq!(workers, Some(2));Trait Implementations§
Source§impl<'a> Clone for CpuExecutionContext<'a>
impl<'a> Clone for CpuExecutionContext<'a>
Source§fn clone(&self) -> CpuExecutionContext<'a>
fn clone(&self) -> CpuExecutionContext<'a>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<'a> Copy for CpuExecutionContext<'a>
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for CpuExecutionContext<'a>
impl<'a> !UnwindSafe for CpuExecutionContext<'a>
impl<'a> Freeze for CpuExecutionContext<'a>
impl<'a> Send for CpuExecutionContext<'a>
impl<'a> Sync for CpuExecutionContext<'a>
impl<'a> Unpin for CpuExecutionContext<'a>
impl<'a> UnsafeUnpin for CpuExecutionContext<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more