Skip to main content

linkcell/
place.rs

1//! CPUs for Rayon workers.
2//!
3//! The caller records the cpus this process may use with `lc_set_places`
4//! before it pins itself. Each worker then runs on any of those cpus
5//! instead of inheriting the caller's single cpu. A later call replaces
6//! the list; a pool already built keeps the cpus it was started with.
7//!
8//! A worker is not tied to one cpu of the list. Work stealing evens out
9//! the load only while the scheduler can move a worker off a cpu another
10//! process is using: tied one to a cpu each beside an 8-thread spinner,
11//! the 110592-molecule k-nearest search took 78 ms instead of 36. On an
12//! idle machine the two placements time the same.
13
14use std::ffi::c_int;
15use std::sync::Mutex;
16
17static PLACES: Mutex<Vec<i32>> = Mutex::new(Vec::new());
18
19/// CPUs Rayon workers may use. Every worker may run on any of them.
20/// Replaces any list recorded earlier. A pool that has already started
21/// keeps the cpus it was built with. `n <= 0` is ignored.
22#[no_mangle]
23pub extern "C" fn lc_set_places(cpus: *const c_int, n: c_int) {
24    if cpus.is_null() || n <= 0 {
25        return;
26    }
27    let incoming = unsafe { std::slice::from_raw_parts(cpus, n as usize) };
28    let mut guard = PLACES.lock().unwrap_or_else(|err| err.into_inner());
29    guard.clear();
30    guard.extend_from_slice(incoming);
31}
32
33#[cfg(feature = "parallel")]
34fn places_snapshot() -> Vec<i32> {
35    PLACES.lock().unwrap_or_else(|err| err.into_inner()).clone()
36}
37
38#[cfg(feature = "parallel")]
39fn bind_cpus(cpus: &[i32]) {
40    #[cfg(target_os = "linux")]
41    {
42        extern "C" {
43            fn sched_setaffinity(pid: i32, cpusetsize: usize, mask: *const u8) -> i32;
44        }
45        let mut mask = [0u8; 128];
46        let mut any = false;
47        for &cpu in cpus {
48            let Ok(cpu) = usize::try_from(cpu) else {
49                continue;
50            };
51            if let Some(byte) = mask.get_mut(cpu / 8) {
52                *byte |= 1u8 << (cpu % 8);
53                any = true;
54            }
55        }
56        if any {
57            unsafe {
58                sched_setaffinity(0, mask.len(), mask.as_ptr());
59            }
60        }
61    }
62    #[cfg(not(target_os = "linux"))]
63    {
64        let _ = cpus;
65    }
66}
67
68#[cfg(feature = "parallel")]
69pub(crate) fn build_pool(n: usize) -> rayon::ThreadPool {
70    let places = places_snapshot();
71    let fallback = || {
72        rayon::ThreadPoolBuilder::new()
73            .num_threads(1)
74            .build()
75            .expect("rayon")
76    };
77    if places.is_empty() {
78        return rayon::ThreadPoolBuilder::new()
79            .num_threads(n)
80            .build()
81            .unwrap_or_else(|_| fallback());
82    }
83    let places = std::sync::Arc::new(places);
84    rayon::ThreadPoolBuilder::new()
85        .num_threads(n)
86        .spawn_handler(move |thread| {
87            let cpus = std::sync::Arc::clone(&places);
88            let mut builder = std::thread::Builder::new();
89            if let Some(name) = thread.name() {
90                builder = builder.name(name.to_string());
91            }
92            builder.spawn(move || {
93                bind_cpus(&cpus);
94                thread.run();
95            })?;
96            Ok(())
97        })
98        .build()
99        .unwrap_or_else(|_| fallback())
100}
101
102#[cfg(all(test, feature = "parallel", target_os = "linux"))]
103mod tests {
104    use super::*;
105
106    extern "C" {
107        fn sched_getaffinity(pid: i32, cpusetsize: usize, mask: *mut u8) -> i32;
108    }
109
110    fn allowed() -> Vec<i32> {
111        let mut mask = [0u8; 128];
112        if unsafe { sched_getaffinity(0, mask.len(), mask.as_mut_ptr()) } != 0 {
113            return Vec::new();
114        }
115        (0..mask.len() * 8)
116            .filter(|cpu| mask[cpu / 8] & (1u8 << (cpu % 8)) != 0)
117            .map(|cpu| cpu as i32)
118            .collect()
119    }
120
121    #[test]
122    fn every_worker_may_use_every_listed_cpu() {
123        let cpus = allowed();
124        if cpus.len() < 2 {
125            return;
126        }
127        let list: Vec<c_int> = cpus[..2].iter().map(|&cpu| cpu as c_int).collect();
128        lc_set_places(list.as_ptr(), 2);
129        let seen = build_pool(3).broadcast(|_| allowed());
130        assert!(seen.iter().all(|mask| mask == &cpus[..2]));
131    }
132}