Crates.io | syncthreads |
lib.rs | syncthreads |
version | |
source | src |
created_at | 2024-05-08 22:43:14.463393 |
updated_at | 2024-11-17 02:55:47.578272 |
description | Safe thread synchronization |
homepage | |
repository | https://github.com/sarah-ek/syncthreads/ |
max_upload_size | |
id | 1234682 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
syncthreads
is a library providing synchronous and asynchronous barrier types.
These can be constructed using BarrierInit
and AsyncBarrierInit
.
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 _));
}