Data parallelism with rayon
9 steps in 4 sets of Rust.
Rayon turns a sequential iterator chain into a parallel one by changing iter to par_iter, and the type system checks that this is actually safe. That is close to the whole idea.
Parallel iterators, reductions, and then splitting work with join and scope for the cases that are not just a map over a collection. Nine steps, and it is the most immediate payoff of Rust's ownership rules.
Parallel iterators
- Parallel iteratorspar_iter is iter with a work-stealing thread pool behind it.
- Filtering in parallelfilter, map and collect all have parallel versions that preserve order.
- Parallel sortingpar_sort splits, sorts and merges — worth it once the slice is large.
Reductions
- Reduce and foldParallel reduction needs an identity value and an associative operation.
- Counting in parallelMerging per-thread maps beats fighting over one shared lock.
Splitting work
- rayon::joinjoin runs two closures that may go in parallel — the base of divide and conquer.
- Parallel chunksWhen work is cheap per item, batch it: one task per chunk beats one per element.
- Bridging a serial iteratorpar_bridge parallelises any iterator, even one that cannot be split.
Encore
- par_wordcount.rsWord frequencies over a corpus, serial and parallel, with the timings compared.