Skip to main content

Control

Struct Control 

Source
pub struct Control {
    pub cancelled: Arc<AtomicBool>,
    /* private fields */
}
Expand description

Cooperative execution control of one solve: a wall-time allowance, a cancellation flag, an optional external poll and optional phase profiling.

The solver calls check before every step and every eight iterations of the pressure and implicit inner solves, so cancellation is prompt but not instantaneous. The clock starts when the control is created. A stop between steps ends the run as cancelled or wall_time_limit with the last accepted state.

use cfd_craft::solver::Control;
use std::sync::atomic::Ordering;

let control = Control::new(60.0);
assert!(control.check().is_ok());
control.cancelled.store(true, Ordering::Relaxed);
assert_eq!(control.check().unwrap_err(), "cancelled");

// An external poll: any error stops the run as `cancelled`.
let polled = Control::with_poll(60.0, || Err("job deadline".to_string()));
assert_eq!(polled.check().unwrap_err(), "cancelled");

Fields§

§cancelled: Arc<AtomicBool>

Set to true, from any thread, to request cancellation. Clone the Arc before starting the solve; the CLI sets it from its SIGINT and SIGTERM handlers.

Implementations§

Source§

impl Control

Source

pub fn new(seconds: f64) -> Self

A control with a wall-time allowance of seconds, counted from now, and no poll or profiling. Pass the case’s run.wall_time_limit_s to reproduce the CLI’s limit.

§Panics

If seconds is negative, not finite or too large for a Duration; validate user input first (Case::check requires a finite positive run.wall_time_limit_s of at most seven days).

Source

pub fn with_poll( seconds: f64, poll: impl Fn() -> Result<()> + Send + Sync + 'static, ) -> Self

A wall-time allowance plus an external cancellation or deadline poll.

A poll error stops the run as cancelled, or as wall_time_limit when the poll returns exactly that text. Its own message is not kept, so a run is never reported as diverged because an external caller stopped it.

§Panics

As new, for an invalid seconds.

Source

pub fn check(&self) -> Result<()>

Ok while the solve may continue. Otherwise the error is exactly "cancelled" (a poll error other than "wall_time_limit", or the cancelled flag) or "wall_time_limit" (the poll returned that text, or the allowance is used up). The poll runs first.

Source

pub fn elapsed(&self) -> f64

Wall seconds since the control was created; reported as the summary’s elapsed_s.

Source

pub fn with_profiling(self) -> Self

Enable phase measurements without changing case or result schemas. Read them with profile after the solve; see the profiling module.

Source

pub fn span(&self, phase: Phase) -> Span

Start timing phase until the returned Span is dropped. Spans must finish in nested order on the thread that started them. Without with_profiling the span is disabled and costs neither a clock read nor a lock.

Source

pub fn profile(&self) -> Result<Option<Profile>>

Snapshot after every span has finished; work includes failed attempts.

Ok(None) without with_profiling. An active or out-of-order span is an error, CFD-B003 profile.active or CFD-B003 profile.nesting.

use cfd_craft::{profiling::Phase, solver::Control};

assert!(Control::new(10.0).profile().unwrap().is_none());
let control = Control::new(10.0).with_profiling();
drop(control.span(Phase::Monitor));
let profile = control.profile().unwrap().expect("profiling enabled");
assert_eq!(profile.schema_version, "cfd-craft.phase-profile/0.1");
assert_eq!(profile.phases["monitor"].calls, 1);

Auto Trait Implementations§

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> Same for T

Source§

type Output = T

Should always be Self
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.