Skip to main content

YscvPool

Struct YscvPool 

Source
pub struct YscvPool { /* private fields */ }
Expand description

Thread pool with a fixed number of worker threads. Uses per-worker Chase-Lev deques (via crossbeam-deque) + random-victim work-stealing. Use YscvPool::new to construct, YscvPool::install to run work.

Implementations§

Source§

impl YscvPool

Source

pub fn new(nthreads: usize) -> Result<Self, PoolError>

Builds a pool with nthreads worker threads. Returns Err on nthreads == 0 (rayon also rejects that).

Source

pub const fn num_threads(&self) -> usize

Number of worker threads in this pool.

Source

pub fn current_worker_index(&self) -> Option<usize>

The caller’s worker index within this pool (0..num_threads), or None if called from a thread that isn’t one of our workers (e.g. the main thread before install, a test thread, a rayon worker). Mirrors rayon::current_thread_index().

Note: TLS is process-global, so this returns Some(i) on a yscv-pool worker even when the function is invoked via a different YscvPool instance — acceptable because kernels use this purely to avoid re-entrant dispatch, and any yscv worker is by definition busy.

Source

pub fn install<F, R>(&self, f: F) -> R
where F: FnOnce() -> R + Send, R: Send,

Runs f on one of the pool’s worker threads and blocks the caller until it returns. Matches rayon::ThreadPool::install semantics — the closure may borrow from the caller’s stack.

§Safety

The call blocks until the task completes, so any references captured in f remain valid for the full task lifetime. To fit the closure in the shared Task queue (FnOnce + 'static), we widen its lifetime via transmute. Same pattern as rayon::ThreadPool::install.

Source

pub fn par_for_each_index<F>(&self, count: usize, f: F)
where F: Fn(usize) + Send + Sync,

Parallel for-each over 0..count. Runs f(index) on pool workers in parallel; blocks until all iterations complete. Equivalent to rayon’s (0..count).into_par_iter().for_each(f).

Dispatch strategy: zero Arc, no mutex, no condvar. All shared state (the next-index counter and the completion latch) lives on this function’s stack frame. Helpers receive raw pointers into that frame; the function blocks on the latch with a spin→yield loop until every helper has finished, guaranteeing the frame outlives all accesses. The closure is passed via a &dyn Fn trait-object pointer — no allocation, no Arc<ClosureHolder> layer.

This is the hot path of the pool on inference workloads (100-200 fine-grained parallel regions per inference). The previous design allocated 3 Arcs + 1 Arc<ClosureHolder> + 4 Arc::clones per helper + waited on a Condvar. Each of those disappears here; the only remaining heap alloc per call is helpers Boxes (required by the Injector<Box<dyn FnOnce>> task type — addressing that would need a custom lock-free queue).

§Safety

This function contains unsafe blocks — see the SAFETY: comments at each site for the invariants. In summary: the function blocks on latch until every helper decrements it, guaranteeing that next, latch, and the closure f all outlive any helper access through the raw pointers we hand them.

Source

pub fn join<A, B, RA, RB>(&self, a: A, b: B) -> (RA, RB)
where A: FnOnce() -> RA + Send, B: FnOnce() -> RB + Send, RA: Send, RB: Send,

Fork-join: run a and b in parallel, return both results. Equivalent to rayon::join(a, b). Implemented by submitting a to the pool and running b inline on the caller’s thread.

§Safety

Same lifetime-extension rule as install — we block on the result channel for a, and b runs synchronously, so both closures’ captures remain live.

Source

pub fn enter_section<R, F>(&self, f: F) -> R
where F: FnOnce(&PersistentSection) -> R,

enter a session-scoped PersistentSection. All pool workers are held in section_worker_loop for the duration of f; regular submit / install paths are effectively blocked during the section (all workers busy). Each PersistentSection::parallel_for call inside the closure dispatches work via a single atomic pointer store + chunk-counter CAS — no rayon scope creation, no Box alloc, no condvar/futex.

Target: the tracker’s ~1.6 ms MT-overhead budget. See reflective-fluttering-backus.md Step 3 for projection.

§Safety

Section and trampoline context are stack-allocated on this function’s frame. Main thread spins on workers_remaining before returning, guaranteeing every worker’s section_worker_loop has exited and released the pointer before the stack frame is freed.

Trait Implementations§

Source§

impl Debug for YscvPool

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for YscvPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl ParallelScope for YscvPool

Source§

fn install_session(&self, f: &mut dyn FnMut())

enter a session-scoped parallel region. On backends that support persistent sections (currently YscvPool), all pool workers enter a tight-poll loop, and parallel dispatch inside f routes through the section’s single-atomic-pointer mechanism instead of rayon’s fork-join. On rayon, this is a pass-through that just invokes f. Read more
Source§

fn par_for_each_index(&self, count: usize, f: &(dyn Fn(usize) + Send + Sync))

Runs f(i) for each i in 0..count, in parallel. Blocks until every iteration completes. Equivalent to rayon’s (0..count).into_par_iter().for_each(f).
Source§

fn par_chunks_mut_dyn( &self, data: &mut [f32], chunk_size: usize, f: &(dyn Fn(usize, &mut [f32]) + Send + Sync), )

Runs f(chunk_idx, chunk) for each chunk_size-wide slice of data, in parallel. The last chunk may be short when data.len() is not a multiple of chunk_size. Blocks until every chunk completes. Equivalent to data.par_chunks_mut(chunk_size).enumerate().for_each(…).
Source§

fn join_dyn(&self, a: &mut (dyn FnMut() + Send), b: &mut (dyn FnMut() + Send))

Runs a and b in parallel, returns both results. Equivalent to rayon::join(a, b).
Source§

fn current_worker_index(&self) -> Option<usize>

The caller’s worker index, 0..num_threads, if running on one of this pool’s workers. None when called from outside the pool (e.g. from the main thread before install). Used by matmul.rs at 538/684 to decide whether to further parallelise — we don’t want nested pool dispatch on an already-parallel worker.
Source§

fn num_threads(&self) -> usize

Number of worker threads.

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, 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.