STRA-338 size: cuda-driver original_file_expert_source: fill workers move to their own file

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 16:21:45 +01:00
parent 3122762d35
commit 08a615c7eb
3 changed files with 66 additions and 60 deletions
@@ -4,6 +4,7 @@
use super::*;
mod build_phases;
mod fill_workers;
mod impl_originalfileexpertsource_2;
mod impl_originalfileexpertsource_2_2;
mod impl_originalfileexpertsource_3;
@@ -1,6 +1,7 @@
//! STRA-338 size: the phases of `OriginalFileExpertSource::build_cache_complement`, extracted in call order.
#[allow(unused_imports)]
use super::fill_workers::{run_fill_workers, straddling_cut};
use super::*;
use crate::CurrentGuard;
@@ -73,66 +74,6 @@ pub(super) fn discard_arena(
combine(Err(primary), cleanup)
}
/// The first offset at or below `want` whose expert straddles `want`, else `want`.
fn straddling_cut(layer_bytes: &[usize; LAYERS], offsets: &[usize], want: usize) -> usize {
for (i, &off) in offsets.iter().enumerate() {
if off != NONE && off < want && off.checked_add(layer_bytes[i / EXPERTS]).is_some_and(|end| end > want) {
return off;
}
}
want
}
/// One worker: claims layers until none are left or an earlier error was recorded.
fn fill_worker(per_layer: &[Vec<abi::Copy>], next: &AtomicUsize, first: &Mutex<Option<Error>>) {
loop {
let l = next.fetch_add(1, Ordering::Relaxed);
if l >= LAYERS {
break;
}
if first.lock().is_ok_and(|e| e.is_some()) {
break;
}
for j in &per_layer[l] {
if let Err(e) = j.run() {
if let Ok(mut f) = first.lock() {
if f.is_none() {
*f = Some(e);
}
}
break;
}
}
}
}
/// Six Original layer workers; one actor participates. No Context/Rc is sent.
fn run_fill_workers(per_layer: &[Vec<abi::Copy>], stack: usize) -> Result<()> {
let next = AtomicUsize::new(0);
let first = Mutex::new(None);
let work = || fill_worker(per_layer, &next, &first);
thread::scope(|s| {
let mut workers = Vec::new();
workers.try_reserve_exact(5).map_err(|_| Error::HostAllocation)?;
for _ in 0..5 {
workers.push(
thread::Builder::new().stack_size(stack).spawn_scoped(s, work).map_err(|_| Error::HostAllocation)?,
);
}
work();
for w in workers {
if w.join().is_err() {
return Err(Error::HostAllocation);
}
}
Ok(())
})?;
if let Some(e) = first.into_inner().map_err(|_| Error::HostAllocation)? {
return Err(e);
}
Ok(())
}
impl<'ctx> OriginalFileExpertSource<'ctx> {
pub(super) fn build_preflight(&mut self, cache: &OriginalExpertCache<'ctx>) -> Result<()> {
self.live()?;
@@ -0,0 +1,64 @@
//! STRA-338 size: the fill workers of `OriginalFileExpertSource::build_cache_complement`.
#[allow(unused_imports)]
use super::*;
/// The first offset at or below `want` whose expert straddles `want`, else `want`.
pub(super) fn straddling_cut(layer_bytes: &[usize; LAYERS], offsets: &[usize], want: usize) -> usize {
for (i, &off) in offsets.iter().enumerate() {
if off != NONE && off < want && off.checked_add(layer_bytes[i / EXPERTS]).is_some_and(|end| end > want) {
return off;
}
}
want
}
/// One worker: claims layers until none are left or an earlier error was recorded.
fn fill_worker(per_layer: &[Vec<abi::Copy>], next: &AtomicUsize, first: &Mutex<Option<Error>>) {
loop {
let l = next.fetch_add(1, Ordering::Relaxed);
if l >= LAYERS {
break;
}
if first.lock().is_ok_and(|e| e.is_some()) {
break;
}
for j in &per_layer[l] {
if let Err(e) = j.run() {
if let Ok(mut f) = first.lock() {
if f.is_none() {
*f = Some(e);
}
}
break;
}
}
}
}
/// Six Original layer workers; one actor participates. No Context/Rc is sent.
pub(super) fn run_fill_workers(per_layer: &[Vec<abi::Copy>], stack: usize) -> Result<()> {
let next = AtomicUsize::new(0);
let first = Mutex::new(None);
let work = || fill_worker(per_layer, &next, &first);
thread::scope(|s| {
let mut workers = Vec::new();
workers.try_reserve_exact(5).map_err(|_| Error::HostAllocation)?;
for _ in 0..5 {
workers.push(
thread::Builder::new().stack_size(stack).spawn_scoped(s, work).map_err(|_| Error::HostAllocation)?,
);
}
work();
for w in workers {
if w.join().is_err() {
return Err(Error::HostAllocation);
}
}
Ok(())
})?;
if let Some(e) = first.into_inner().map_err(|_| Error::HostAllocation)? {
return Err(e);
}
Ok(())
}