15 releases (6 breaking)
0.7.0 | May 18, 2024 |
---|---|
0.6.0 | May 18, 2024 |
0.5.0 | May 13, 2024 |
0.4.2 | May 12, 2024 |
0.1.3 | May 9, 2024 |
#551 in Concurrency
31 downloads per month
66KB
1.5K
SLoC
syncthreads
is a library providing synchronous and asynchronous barrier types.
These can be constructed using BarrierInit
and AsyncBarrierInit
.
Example
Sequential code:
let n = 1000;
let x = &mut *vec![1.0; n];
for i in 0..n / 2 {
let head = x[i];
for x in &mut x[i + 1..] {
*x += head;
}
}
for i in 0..n / 2 {
assert_eq!(x[i], 2.0f64.powi(i as _));
}
Multithreaded code using Barrier
.
use syncthreads::{iter, sync_blocking, BarrierInit};
let n = 1000;
let x = &mut *vec![1.0; n];
let nthreads = 3;
let init = BarrierInit::new(&mut *x, nthreads, Default::default(), Default::default());
std::thread::scope(|scope| {
for _ in 0..nthreads {
scope.spawn(|| {
let mut barrier = init.barrier_ref();
for i in 0..n / 2 {
let Ok((head, data)) = sync_blocking!(barrier, |x| {
let (head, x) = x[i..].split_at_mut(1);
(head[0], iter::split_mut(x, nthreads))
}) else {
break;
};
let head = *head;
let mine = &mut **data;
for x in mine.iter_mut() {
*x += head;
}
}
});
}
});
for i in 0..n / 2 {
assert_eq!(x[i], 2.0f64.powi(i as _));
}
Dependencies
~2.6–8.5MB
~75K SLoC