Skip to main content

Exec

Struct Exec 

Source
pub struct Exec<'a> { /* private fields */ }
Expand description

The resources one operation runs with: a placement token, optional reusable scratch, and the batch lane policy.

The strided_exec::Exec token decides where the work runs and how many tasks may run at once. This type adds what is specific to contraction: the workspace the call may reuse and the BatchPolicy. It owns no placement code. To start parallel work take the token with Exec::token (a reborrow, so one Exec serves a sequence of calls) or Exec::into_token, and call its fork, for_ranges, broadcast or external; build a child bundle for each forked job with Exec::from_token.

Like the token it wraps, an Exec is neither Clone nor Copy, so two operations cannot both claim the full budget; operations take &mut Exec.

§Examples

use tcontract_exec::Exec;
let mut exec = Exec::serial();
assert_eq!(exec.budget(), 1);
assert!(exec.token().is_serial());

Implementations§

Source§

impl<'a> Exec<'a>

Source

pub const fn serial() -> Exec<'static>

The serial context: the calling thread only, call-local scratch.

Source

pub fn serial_with_workspace(workspace: &'a dyn WorkspaceProvider) -> Self

Serial execution with caller-owned reusable storage.

Source

pub fn new(pool: &'a Pool<'_>, budget: usize) -> Result<Self, ExecError>

budget slots of pool, with the pool’s own workspace and the default batch policy. This is the only place a budget is clamped: to the pool width.

§Errors

ExecError::Placement when budget == 0.

§Examples
use tcontract_exec::{Exec, Pool};
let sp = strided_exec::Pool::builder().threads(2).build().unwrap();
let pool = Pool::borrow(&sp);
assert_eq!(Exec::new(&pool, 64).unwrap().budget(), pool.size());
assert!(Exec::new(&pool, 0).is_err());
Source

pub fn from_token( token: Exec<'a>, workspace: Option<&'a dyn WorkspaceProvider>, policy: BatchPolicy, ) -> Self

Bundle an existing token with a workspace and a policy: the way to give a forked job its own bundle, since fork hands out bare child tokens.

§Examples
use tcontract_exec::{BatchPolicy, Exec};
let exec = Exec::from_token(strided_exec::Exec::serial(), None, BatchPolicy::default());
assert_eq!(exec.budget(), 1);
Source

pub fn token(&mut self) -> Exec<'_>

The placement token, lent for one call. The bundle keeps its budget, so several calls can follow one another.

Source

pub fn into_token(self) -> Exec<'a>

The placement token, consuming the bundle (for fork, broadcast, or any entry that takes the token by value and outlives this borrow).

Source

pub fn reborrow(&mut self) -> Exec<'_>

The same resources, lent for as long as self is exclusively borrowed.

Source

pub fn limit(self, max: NonZeroUsize) -> Self

Narrow the budget to min(budget, max); the dropped slots stay unused.

§Examples
use tcontract_exec::Exec;
assert_eq!(Exec::serial().limit(4.try_into().unwrap()).budget(), 1);
Source

pub fn batch_policy(&self) -> &BatchPolicy

The batch lane policy this context applies.

A serial context has one thread but still carries a policy, so a forced strategy is resolved (and can be refused) at one thread as well.

§Examples
use tcontract_exec::{BatchPolicy, BatchStrategy, Exec};
assert_eq!(Exec::serial().batch_policy().strategy, BatchStrategy::Auto);
let exec = Exec::serial().with_batch_policy(BatchPolicy::new(BatchStrategy::Outer));
assert_eq!(exec.batch_policy().strategy, BatchStrategy::Outer);
Source

pub fn with_batch_policy(self, policy: BatchPolicy) -> Self

Return this context with a different batch lane policy.

A serial context has one thread, so the policy cannot change its lane count, but it is preserved: a forced strategy is still honoured (or refused with a typed error) at one thread instead of silently running Auto.

Source

pub fn batch_decision(&self, query: &BatchQuery) -> BatchDecision

Decide the batch route for query on this context’s budget and policy.

This is the one decision function every operation that owns batch lanes calls; see crate::batch. Whether a route can honour the returned decision is the route’s own preflight (see crate::batch::BatchDecision’s route contract); the decision itself is arithmetic and never fails.

§Examples
use tcontract_exec::{BatchQuery, Exec};
let d = Exec::serial().batch_decision(&BatchQuery::new(4, 1000.0));
assert_eq!((d.lanes, d.item_threads), (1, 1));
Source

pub fn budget(&self) -> usize

Most threads an operation may occupy (1 for a serial context).

Source

pub fn workspace(&self) -> Option<&'a dyn WorkspaceProvider>

Storage the operations on this context may share, or none.

A pool lends its own arena to every operation that runs on it; a serial context owns nothing, so a caller that wants reuse keeps a provider of its own and lends it through Exec::serial_with_workspace.

Source

pub fn width_for(&self, serial_ns: f64, policy: &WidthPolicy) -> usize

Partition width for work estimated at serial_ns on one thread, minimizing the WidthPolicy cost model within the budget.

§Examples
use tcontract_exec::{Exec, WidthPolicy};
assert_eq!(Exec::serial().width_for(1e9, &WidthPolicy::default()), 1);

Trait Implementations§

Source§

impl Debug for Exec<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Exec<'a>

§

impl<'a> !UnwindSafe for Exec<'a>

§

impl<'a> Freeze for Exec<'a>

§

impl<'a> Send for Exec<'a>

§

impl<'a> Sync for Exec<'a>

§

impl<'a> Unpin for Exec<'a>

§

impl<'a> UnsafeUnpin for Exec<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.