diff --git a/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource.rs b/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource.rs index e692d235..2aeec91a 100644 --- a/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource.rs +++ b/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource.rs @@ -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; diff --git a/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource/build_phases.rs b/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource/build_phases.rs index d63369ec..67bcb843 100644 --- a/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource/build_phases.rs +++ b/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource/build_phases.rs @@ -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], next: &AtomicUsize, first: &Mutex>) { - 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], 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()?; diff --git a/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource/fill_workers.rs b/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource/fill_workers.rs new file mode 100644 index 00000000..bde62f0c --- /dev/null +++ b/crates/cuda-driver/src/original_file_expert_source/impl_originalfileexpertsource/fill_workers.rs @@ -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], next: &AtomicUsize, first: &Mutex>) { + 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], 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(()) +}