typestar

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.

Start this tour

Parallel iterators

Reductions

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.

The other Rust tours