Skip to main content

ParallelScope

Trait ParallelScope 

Source
pub trait ParallelScope: Send + Sync {
    // Required methods
    fn install_session(&self, f: &mut dyn FnMut());
    fn par_for_each_index(
        &self,
        count: usize,
        f: &(dyn Fn(usize) + Send + Sync),
    );
    fn par_chunks_mut_dyn(
        &self,
        data: &mut [f32],
        chunk_size: usize,
        f: &(dyn Fn(usize, &mut [f32]) + Send + Sync),
    );
    fn join_dyn(
        &self,
        a: &mut (dyn FnMut() + Send),
        b: &mut (dyn FnMut() + Send),
    );
    fn current_worker_index(&self) -> Option<usize>;
    fn num_threads(&self) -> usize;
}
Expand description

Trait abstracting over thread-pool backends for the inference hot path.

Required Methods§

Source

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

Step 3: 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.

The downstream dispatch hook (yscv-kernels’s par_chunks_mut_dispatch) reads the thread-local section pointer via crate::current_section and picks the session path when present.

Uses a &mut dyn FnMut() rather than generic FnOnce so the trait stays dyn-compatible (the runner holds a Arc<dyn ParallelScope>).

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.

Implementations on Foreign Types§

Source§

impl ParallelScope for ThreadPool

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

fn num_threads(&self) -> usize

Implementors§