use aocl_utils_sys as sys;
pub type ThreadHandle = sys::pthread_t;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PinStrategy {
Core,
Logical,
Spread,
}
pub unsafe fn pin_threads(strategy: PinStrategy, threads: &mut [ThreadHandle]) {
let n = threads.len();
let p = threads.as_mut_ptr();
match strategy {
PinStrategy::Core => unsafe { sys::au_pin_threads_core(p, n) },
PinStrategy::Logical => unsafe { sys::au_pin_threads_logical(p, n) },
PinStrategy::Spread => unsafe { sys::au_pin_threads_spread(p, n) },
}
}
pub unsafe fn pin_threads_custom(threads: &mut [ThreadHandle], affinity_vector: &[i32]) {
let n = threads.len();
if n == 0 || affinity_vector.is_empty() {
return;
}
unsafe {
sys::au_pin_threads_custom(
threads.as_mut_ptr(),
n,
affinity_vector.as_ptr() as *mut std::os::raw::c_int,
affinity_vector.len(),
);
}
}
#[cfg(all(test, windows))]
mod tests {
use super::*;
use std::os::windows::io::AsRawHandle;
use std::sync::mpsc;
#[repr(C)]
#[derive(Default)]
struct GroupAffinity {
mask: usize,
group: u16,
reserved: [u16; 3],
}
extern "system" {
fn GetThreadGroupAffinity(
thread: *mut core::ffi::c_void,
affinity: *mut GroupAffinity,
) -> i32;
}
fn pinned_mask(f: impl FnOnce(&mut [ThreadHandle])) -> usize {
let (tx, rx) = mpsc::channel::<()>();
let th = std::thread::spawn(move || {
let _ = rx.recv();
});
let mut handles = [th.as_raw_handle() as ThreadHandle];
f(&mut handles);
let mut ga = GroupAffinity::default();
let ok = unsafe { GetThreadGroupAffinity(handles[0] as *mut _, &mut ga) };
assert_ne!(ok, 0, "GetThreadGroupAffinity failed");
tx.send(()).unwrap();
th.join().unwrap();
ga.mask
}
#[test]
fn pin_threads_custom_binds_the_thread_to_the_requested_logical_processor() {
let unpinned = pinned_mask(|_| {});
let pinned = pinned_mask(|h| unsafe { pin_threads_custom(h, &[0]) });
assert_eq!(pinned, 1, "affinity mask after pin_threads_custom(&[0])");
if std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
> 1
{
assert!(
unpinned.count_ones() > 1,
"control: unpinned mask {unpinned:#x}"
);
}
}
#[test]
fn pin_threads_core_and_logical_and_spread_leave_a_single_processor_mask() {
let unpinned = pinned_mask(|_| {});
for strategy in [PinStrategy::Core, PinStrategy::Logical, PinStrategy::Spread] {
let m = pinned_mask(|h| unsafe { pin_threads(strategy, h) });
assert_ne!(m, 0, "{strategy:?}");
assert!(
m.count_ones() >= 1 && m.count_ones() <= unpinned.count_ones(),
"{strategy:?}: {m:#x} vs {unpinned:#x}"
);
if std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
> 1
{
assert_ne!(m, unpinned, "{strategy:?} left the thread unpinned");
}
}
}
}