Parallel chunks in Rust
When work is cheap per item, batch it: one task per chunk beats one per element.
use rayon::prelude::*;
fn main() {
let values: Vec<f64> = (1..=10_000).map(|n| n as f64).collect();
let sums: Vec<f64> = values
.par_chunks(1_000)
.map(|chunk| chunk.iter().sum())
.collect();
println!("{} chunk sums, first {:.0}", sums.len(), sums[0]);
let mut scaled = values.clone();
scaled.par_chunks_mut(500).for_each(|chunk| {
for v in chunk {
*v *= 2.0;
}
});
println!("{:.0} {:.0}", scaled[0], scaled[9_999]);
}
How it works
par_chunkshands each thread a slice, not an item.- The chunk size controls the granularity of the work.
par_chunks_mutwrites into the slice in place.
Keywords and builtins used here
Vecasf64fnforinletmainmutuse
The run, in numbers
- Lines
- 19
- Characters to type
- 421
- Tokens
- 150
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 82 seconds.
Step 2 of 3 in Splitting work, step 7 of 9 in Data parallelism with rayon.