Skip to content

Commit a9441a1

Browse files
committed
feat: add parellel util
1 parent 1fd68f5 commit a9441a1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

starky/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod parallells;

starky/src/utils/parallells.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use rayon::{current_num_threads, scope};
2+
3+
/// This simple utility function will parallelize an operation that is to be
4+
/// performed over a mutable slice.
5+
pub fn parallelize<T: Send, F: Fn(&mut [T], usize) + Send + Sync + Clone>(v: &mut [T], f: F) {
6+
let n = v.len();
7+
let num_threads = current_num_threads();
8+
let mut chunk = n / num_threads;
9+
if chunk < num_threads {
10+
chunk = 1;
11+
}
12+
13+
scope(|scope| {
14+
for (chunk_num, v) in v.chunks_mut(chunk).enumerate() {
15+
let f = f.clone();
16+
scope.spawn(move |_| {
17+
let start = chunk_num * chunk;
18+
f(v, start);
19+
});
20+
}
21+
});
22+
}

0 commit comments

Comments
 (0)