diff --git a/d3d12/crates/leonida-gpu/src/land.rs b/d3d12/crates/leonida-gpu/src/land.rs index 2eaad29437..d0c8ef1170 100644 --- a/d3d12/crates/leonida-gpu/src/land.rs +++ b/d3d12/crates/leonida-gpu/src/land.rs @@ -242,7 +242,7 @@ struct Pool { turn: Mutex<()>, threads: usize, /// How many of `threads` (this one counted) take part now: all of them unless a switch - /// changes it while the title runs ([`each_view`]). + /// changes it while the title runs ([`each_view`]). Asked once a batch ([`State::limit`]). limit: fn() -> usize, } @@ -259,6 +259,10 @@ struct State { /// Helpers inside a call. running: usize, panicked: bool, + /// [`Pool::limit`] for the batch under way, asked once when it began: helpers + /// compare with this under the lock instead of asking for every number + /// (the `_WIDE` switches read the environment). + limit: usize, } #[derive(Clone, Copy)] @@ -279,7 +283,7 @@ impl Pool { fn with_limit(name: &'static str, threads: usize, limit: fn() -> usize) -> &'static Pool { let pool: &'static Pool = Box::leak(Box::new(Pool { - state: Mutex::new(State { work: None, next: 0, count: 0, running: 0, panicked: false }), + state: Mutex::new(State { work: None, next: 0, count: 0, running: 0, panicked: false, limit: 0 }), more: Condvar::new(), idle: Condvar::new(), turn: Mutex::new(()), @@ -308,7 +312,7 @@ impl Pool { loop { // (A helper past the pool's limit now leaves the work to the others; the thread that // called `each` always works, so the batch ends.) - let taken = if helper < (self.limit)() { take(&mut state) } else { None }; + let taken = if helper < state.limit { take(&mut state) } else { None }; let Some((work, i)) = taken else { state = self.more.wait(state).unwrap_or_else(|e| e.into_inner()); continue; @@ -341,10 +345,11 @@ impl Pool { // SAFETY: the lifetime is left out only until this call returns, and // it does not before no helper is inside `f` and none can enter. let work = Work(unsafe { std::mem::transmute::<*const (dyn Fn(usize) + Sync + '_), *const (dyn Fn(usize) + Sync + 'static)>(f) }); + let limit = (self.limit)(); let mut state = self.state(); - (state.work, state.next, state.count, state.panicked) = (Some(work), 0, count, false); + (state.work, state.next, state.count, state.panicked, state.limit) = (Some(work), 0, count, false, limit); drop(state); - if count > self.threads || (self.limit)() < self.threads { + if count > self.threads || limit < self.threads { self.more.notify_all(); } else { (1..count).for_each(|_| self.more.notify_one()); @@ -408,11 +413,24 @@ pub(crate) fn each_view(records: bool, count: usize, f: &(dyn Fn(usize) + Sync)) .get_or_init(|| { let asked: Option = std::env::var("LEONIDA_VIEW_THREADS").ok().and_then(|v| v.trim().parse().ok()); let name = if records { "leonida-records" } else { "leonida-views" }; + let (counts, switch): ([usize; 2], &'static str) = if records { + (RECORD_THREADS, "LEONIDA_RECORD_THREADS_WIDE") + } else { + (VIEW_THREADS, "LEONIDA_VIEW_THREADS_WIDE") + }; match asked { Some(threads) => Pool::new(name, threads.clamp(1, 16)), - // Leonida: the wide count is made, and how many take part follows the switch. - None if records => Pool::with_limit(name, RECORD_THREADS[1], || RECORD_THREADS[usize::from(wide("LEONIDA_RECORD_THREADS_WIDE"))]), - None => Pool::with_limit(name, VIEW_THREADS[1], || VIEW_THREADS[usize::from(wide("LEONIDA_VIEW_THREADS_WIDE"))]), + // Leonida: by turns (`LEONIDA_AB`), the wide count is made, and how many take + // part follows the switch. + None if by_turns(switch) && records => Pool::with_limit(name, counts[1], || { + RECORD_THREADS[usize::from(wide("LEONIDA_RECORD_THREADS_WIDE"))] + }), + None if by_turns(switch) => Pool::with_limit(name, counts[1], || { + VIEW_THREADS[usize::from(wide("LEONIDA_VIEW_THREADS_WIDE"))] + }), + // Otherwise the switch holds for the run: as many threads as take part, none + // parked to be woken by every batch. + None => Pool::new(name, counts[usize::from(wide(switch))]), } }) .each(count, f); @@ -427,6 +445,17 @@ const VIEW_THREADS: [usize; 2] = [4, 8]; /// the end of a submission (same runs). const RECORD_THREADS: [usize; 2] = [6, 10]; +/// Whether `switch` changes by turns while the title runs (`LEONIDA_AB` names it). +fn by_turns(switch: &str) -> bool { + #[cfg(feature = "diagnostics")] + return crate::ab::bit(switch).is_some(); + #[cfg(not(feature = "diagnostics"))] + { + let _ = switch; + false + } +} + /// Whether a `..._WIDE` switch is on: by turns with `LEONIDA_AB`, else `1` in its variable. fn wide(switch: &'static str) -> bool { #[cfg(feature = "diagnostics")] diff --git a/d3d12/crates/leonida-gpu/src/land/tests.rs b/d3d12/crates/leonida-gpu/src/land/tests.rs index 8851ea5d0a..0dedbade69 100644 --- a/d3d12/crates/leonida-gpu/src/land/tests.rs +++ b/d3d12/crates/leonida-gpu/src/land/tests.rs @@ -66,6 +66,26 @@ fn the_pool_calls_every_number_once() { } } +/// A pool whose share of threads can change asks for it once a batch, not for +/// every number under its lock (the `_WIDE` switches read the environment). +#[test] +fn a_pools_limit_is_asked_once_a_batch() { + static ASKED: AtomicUsize = AtomicUsize::new(0); + fn two() -> usize { + ASKED.fetch_add(1, Ordering::Relaxed); + 2 + } + let pool = Pool::with_limit("leonida-limit-test", 4, two); + for round in 1..=3 { + let calls: Vec = (0..256).map(|_| AtomicUsize::new(0)).collect(); + pool.each(calls.len(), &|i| { + calls[i].fetch_add(1, Ordering::Relaxed); + }); + assert!(calls.iter().all(|c| c.load(Ordering::Relaxed) == 1)); + assert_eq!(ASKED.load(Ordering::Relaxed), round, "asked once a batch"); + } +} + /// A job that panics is told to the caller, and the pool goes on. #[test] fn a_panicking_job_reaches_the_caller() {