#![forbid(unsafe_code)]
#![warn(missing_docs)]
use rayon::prelude::*;
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, OnceLock, Weak};
static POOLS: OnceLock<Mutex<BTreeMap<usize, Weak<rayon::ThreadPool>>>> = OnceLock::new();
pub fn validate_threads(threads: usize) -> Result<(), String> {
if !(1..=8).contains(&threads) {
Err(format!("parallel threads must lie in 1..=8, got {threads}"))
} else {
Ok(())
}
}
fn pool(threads: usize) -> Result<rayon::ThreadPool, String> {
rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.thread_name(|i| format!("lpip-worker-{i}"))
.build()
.map_err(|e| format!("native worker creation: {e}"))
}
#[derive(Clone, Debug, Default)]
pub struct Execution {
pool: Option<Arc<rayon::ThreadPool>>,
}
impl Execution {
pub fn new(threads: usize) -> Result<Self, String> {
validate_threads(threads)?;
if threads == 1 {
return Ok(Self::default());
}
let mut pools = POOLS
.get_or_init(Default::default)
.lock()
.map_err(|_| "worker registry lock poisoned".to_string())?;
if let Some(pool) = pools.get(&threads).and_then(Weak::upgrade) {
return Ok(Self { pool: Some(pool) });
}
let pool = Arc::new(pool(threads)?);
pools.insert(threads, Arc::downgrade(&pool));
Ok(Self { pool: Some(pool) })
}
pub fn exclusive(threads: usize) -> Result<Self, String> {
validate_threads(threads)?;
if threads == 1 {
return Ok(Self::default());
}
Ok(Self {
pool: Some(Arc::new(pool(threads)?)),
})
}
pub fn threads(&self) -> usize {
self.pool.as_ref().map_or(1, |p| p.current_num_threads())
}
pub fn chunks_mut<T, F>(&self, data: &mut [T], chunk: usize, work: usize, f: F)
where
T: Send,
F: Fn(usize, &mut [T]) + Send + Sync,
{
self.chunks_mut_min(data, chunk, work, 262_144, f);
}
pub(crate) fn chunks_mut_min<T, F>(
&self,
data: &mut [T],
chunk: usize,
work: usize,
minimum: usize,
f: F,
) where
T: Send,
F: Fn(usize, &mut [T]) + Send + Sync,
{
assert!(chunk > 0);
if let Some(pool) = &self.pool {
if work >= minimum {
pool.install(|| {
data.par_chunks_mut(chunk)
.enumerate()
.for_each(|(i, part)| f(i * chunk, part))
});
return;
}
}
for (i, part) in data.chunks_mut(chunk).enumerate() {
f(i * chunk, part);
}
}
pub(crate) fn in_context<T: Send, F: FnOnce() -> T + Send>(&self, f: F) -> T {
match &self.pool {
Some(pool) => pool.install(f),
None => f(),
}
}
pub(crate) fn zip2_mut<T, F>(&self, a: &mut [T], b: &mut [T], chunk: usize, work: usize, f: F)
where
T: Send,
F: Fn(usize, &mut [T], &mut [T]) + Send + Sync,
{
assert_eq!(a.len(), b.len());
assert!(chunk > 0);
if let Some(pool) = &self.pool {
if work >= 32_768 {
pool.install(|| {
a.par_chunks_mut(chunk)
.zip(b.par_chunks_mut(chunk))
.enumerate()
.for_each(|(i, (a, b))| f(i * chunk, a, b))
});
return;
}
}
for (i, (a, b)) in a.chunks_mut(chunk).zip(b.chunks_mut(chunk)).enumerate() {
f(i * chunk, a, b);
}
}
pub(crate) fn zip3_mut<T, F>(
&self,
a: &mut [T],
b: &mut [T],
c: &mut [T],
chunk: usize,
work: usize,
f: F,
) where
T: Send,
F: Fn(usize, &mut [T], &mut [T], &mut [T]) + Send + Sync,
{
assert_eq!(a.len(), b.len());
assert_eq!(a.len(), c.len());
assert!(chunk > 0);
if let Some(pool) = &self.pool {
if work >= 32_768 {
pool.install(|| {
a.par_chunks_mut(chunk)
.zip(b.par_chunks_mut(chunk))
.zip(c.par_chunks_mut(chunk))
.enumerate()
.for_each(|(i, ((a, b), c))| f(i * chunk, a, b, c))
});
return;
}
}
for (i, ((a, b), c)) in a
.chunks_mut(chunk)
.zip(b.chunks_mut(chunk))
.zip(c.chunks_mut(chunk))
.enumerate()
{
f(i * chunk, a, b, c);
}
}
pub fn map_with_init<S, T, I, F>(&self, count: usize, init: I, f: F) -> Vec<T>
where
T: Send,
I: Fn(usize) -> S + Send + Sync,
F: Fn(&mut S, usize) -> T + Send + Sync,
{
use std::sync::atomic::{AtomicUsize, Ordering};
if count == 0 {
return Vec::new();
}
let next = AtomicUsize::new(0);
let workers = self.threads().min(count);
let claim = (count / (workers * 8)).clamp(1, 4);
let parts = self.map(workers, |worker| {
let mut state = init(worker);
let mut local = Vec::with_capacity(count / workers);
loop {
let first = next.fetch_add(claim, Ordering::Relaxed);
if first >= count {
break;
}
for i in first..count.min(first + claim) {
local.push((i, f(&mut state, i)));
}
}
local
});
let mut slots: Vec<_> = (0..count).map(|_| None).collect();
for (index, value) in parts.into_iter().flatten() {
assert!(slots[index].is_none());
slots[index] = Some(value);
}
slots
.into_iter()
.map(|value| value.expect("one result per task"))
.collect()
}
pub fn map<T, F>(&self, count: usize, f: F) -> Vec<T>
where
T: Send,
F: Fn(usize) -> T + Send + Sync,
{
match &self.pool {
Some(pool) => pool.install(|| (0..count).into_par_iter().map(f).collect()),
None => (0..count).map(f).collect(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
#[test]
fn width_order_disjoint_outputs_and_budget() {
for width in [1, 2, 4, 8] {
let exec = Execution::new(width).unwrap();
assert_eq!(exec.threads(), width);
assert_eq!(
exec.map(97, |i| i * i),
(0..97).map(|i| i * i).collect::<Vec<_>>()
);
let mut out = vec![0; 103];
exec.chunks_mut(&mut out, 7, 100_000, |start, part| {
for (i, v) in part.iter_mut().enumerate() {
*v = start + i;
}
});
assert_eq!(out, (0..103).collect::<Vec<_>>());
let active = AtomicUsize::new(0);
let peak = AtomicUsize::new(0);
exec.map(64, |_| {
let n = active.fetch_add(1, Ordering::SeqCst) + 1;
peak.fetch_max(n, Ordering::SeqCst);
std::thread::yield_now();
active.fetch_sub(1, Ordering::SeqCst);
});
assert!(peak.load(Ordering::SeqCst) <= width);
}
assert!(Execution::new(0).is_err());
assert!(Execution::new(9).is_err());
}
#[test]
fn panic_returns_to_caller_without_poisoning_pool() {
let exec = Execution::new(8).unwrap();
assert!(
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| exec.map(16, |i| {
assert_ne!(i, 7);
i
})))
.is_err()
);
assert_eq!(exec.map(4, |i| i), vec![0, 1, 2, 3]);
}
}
#[cfg(test)]
mod configuration_tests {
#[test]
fn explicit_widths_are_bounded() {
for width in 1..=8 {
assert_eq!(super::Execution::new(width).unwrap().threads(), width);
}
}
}
#[cfg(test)]
mod sharing_tests {
use super::*;
#[test]
fn compatible_owners_share_without_permanent_retention() {
let a = Execution::new(8).unwrap();
let b = Execution::new(8).unwrap();
assert!(Arc::ptr_eq(
a.pool.as_ref().unwrap(),
b.pool.as_ref().unwrap()
));
let weak = Arc::downgrade(a.pool.as_ref().unwrap());
drop(a);
assert!(weak.upgrade().is_some());
drop(b);
let c = Execution::new(4).unwrap();
assert_eq!(c.threads(), 4);
let d = Execution::new(8).unwrap();
assert!(!Arc::ptr_eq(
c.pool.as_ref().unwrap(),
d.pool.as_ref().unwrap()
));
let e = Execution::exclusive(8).unwrap();
assert_eq!(e.threads(), 8);
assert!(!Arc::ptr_eq(
d.pool.as_ref().unwrap(),
e.pool.as_ref().unwrap()
));
assert!(Execution::exclusive(1).unwrap().pool.is_none());
assert!(Execution::exclusive(9).is_err());
}
#[test]
fn a_long_exclusive_task_does_not_hold_up_another_owner() {
use std::time::{Duration, Instant};
let a = Execution::new(2).unwrap();
let b = Execution::exclusive(2).unwrap();
let worst = (0..3)
.map(|_| {
std::thread::scope(|s| {
let short = s.spawn(|| {
let t = Instant::now();
a.map(2, |i| {
std::thread::sleep(Duration::from_millis(40 + 60 * i as u64))
});
t.elapsed()
});
s.spawn(|| {
std::thread::sleep(Duration::from_millis(60));
b.map(1, |_| std::thread::sleep(Duration::from_secs(1)));
});
short.join().unwrap()
})
})
.max()
.unwrap();
assert!(worst < Duration::from_millis(600), "{worst:?}");
}
}
#[cfg(test)]
mod state_tests {
use super::*;
use std::{cell::Cell, rc::Rc, thread};
#[test]
fn state_is_local_and_results_are_ordered() {
let execution = Execution::new(8).unwrap();
let results = execution.map_with_init(
1000,
|_| (thread::current().id(), Rc::new(Cell::new(0))),
|state, i| {
assert_eq!(state.0, thread::current().id());
state.1.set(state.1.get() + 1);
i * i
},
);
assert_eq!(results, (0..1000).map(|i| i * i).collect::<Vec<_>>());
assert!(
execution
.map_with_init::<(), usize, _, _>(0, |_| panic!("empty init"), |_, _| 0)
.is_empty()
);
}
}