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
impl YscvPool
Sourcepub fn new(nthreads: usize) -> Result<Self, PoolError>
pub fn new(nthreads: usize) -> Result<Self, PoolError>
Builds a pool with nthreads worker threads. Returns Err on
nthreads == 0 (rayon also rejects that).
Sourcepub fn num_threads(&self) -> usize
pub fn num_threads(&self) -> usize
Number of worker threads in this pool.
Sourcepub fn current_worker_index(&self) -> Option<usize>
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.
Sourcepub fn install<F, R>(&self, f: F) -> R
pub fn install<F, R>(&self, f: F) -> R
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.
Sourcepub fn par_for_each_index<F>(&self, count: usize, f: F)
pub fn par_for_each_index<F>(&self, count: usize, f: F)
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.
Sourcepub fn join<A, B, RA, RB>(&self, a: A, b: B) -> (RA, RB)
pub fn join<A, B, RA, RB>(&self, a: A, b: B) -> (RA, RB)
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.
Sourcepub fn enter_section<R, F>(&self, f: F) -> Rwhere
F: FnOnce(&PersistentSection) -> R,
pub fn enter_section<R, F>(&self, f: F) -> Rwhere
F: FnOnce(&PersistentSection) -> R,
Step 3: 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 ParallelScope for YscvPool
impl ParallelScope for YscvPool
Source§fn install_session(&self, f: &mut dyn FnMut())
fn install_session(&self, f: &mut dyn FnMut())
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 moreSource§fn par_for_each_index(&self, count: usize, f: &(dyn Fn(usize) + Send + Sync))
fn par_for_each_index(&self, count: usize, f: &(dyn Fn(usize) + Send + Sync))
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),
)
fn par_chunks_mut_dyn( &self, data: &mut [f32], chunk_size: usize, f: &(dyn Fn(usize, &mut [f32]) + Send + Sync), )
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))
fn join_dyn(&self, a: &mut (dyn FnMut() + Send), b: &mut (dyn FnMut() + Send))
a and b in parallel, returns both results. Equivalent
to rayon::join(a, b).Source§fn current_worker_index(&self) -> Option<usize>
fn current_worker_index(&self) -> Option<usize>
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
fn num_threads(&self) -> usize
Auto Trait Implementations§
impl Freeze for YscvPool
impl !RefUnwindSafe for YscvPool
impl Send for YscvPool
impl Sync for YscvPool
impl Unpin for YscvPool
impl UnsafeUnpin for YscvPool
impl !UnwindSafe for YscvPool
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> 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