Exercise: Parallel GC content with rayon

Goal

Take a sequential function that computes per-sequence GC content over many sequences:

fn per_seq_gc_sequential(sequences: &[Vec<u8>]) -> Vec<f64>

…and write the rayon-parallel version:

fn per_seq_gc_parallel(sequences: &[Vec<u8>]) -> Vec<f64>

Both must return the same result. The tests assert this; what changes is wall-clock time.

Background

rayon is the standard data-parallelism crate for Rust. The big idea: if your computation is expressible as .iter()...collect(), you can usually make it parallel by changing .iter() to .par_iter(), with zero other changes. The pieces are independent (one per sequence in our case), so rayon schedules them across CPU threads using a work-stealing thread pool.

You do not have to think about threads, mutexes, or locks. The compiler does.

Why this is safe

rayon requires that the closure run on each element is Send (movable to another thread) and that any captured references are Sync (safe to share between threads). For our exercise — every element is computed independently from an immutable input slice — the compiler accepts this without a fuss. If you tried to share a mutable counter across the threads, the compiler would refuse to compile (this is the famous “fearless concurrency” pitch).

Bar chart of speedup factor versus number of rayon worker threads: 1.0x at 1 core, 1.9x at 2 cores, 3.7x at 4 cores, 7.2x at 8 cores, with dashed grey markers at 1, 2, 4, and 8 showing perfect linear scaling above each bar.
Figure 1: Illustrative speedup for per-sequence GC content over 100 000 sequences as the rayon thread pool grows. Observed scaling is sub-linear — the dashed markers show ideal Nx speedup, and real bars fall short because of thread-pool overhead, memory bandwidth, and Amdahl’s law. The only user-code change is swapping .iter() for .par_iter(); rayon supplies the work-stealing thread pool, and no concurrency primitives appear in your function.

The crate

[dependencies]
rayon = "1"

Import the parallel iterator traits:

use rayon::prelude::*;

The exercise

cd day5/ex-parallel-gc

Implement per_seq_gc_parallel. Make all three tests pass.

How to run

cargo test
cargo run --release -- 10000   # 10000 sequences, both versions, with timings

Hints

Wherever the sequential version writes .iter(), the parallel version writes .par_iter():

sequences.par_iter().map(|seq| gc_content(seq)).collect()

Everything else — .map, .collect, the inner closure — stays identical. The result is a Vec<f64> in the same order as the input.

For very small inputs the parallel version may be slower than the sequential one because of the thread-pool overhead. As a rough rule, parallelism is worth it once the total work is at least a few microseconds per item and there are at least thousands of items. Real bioinformatics workloads (millions of reads) blow past these thresholds.

You will see the speedup most clearly with --release, large N, and a machine with several CPU cores. Compare cargo run --release -- 100000 between the sequential and parallel paths in the same run.

Solution

See day5/ex-parallel-gc-solution/src/main.rs for the reference. The parallel function body is one line.

Where this leads

  • par_iter_mut for mutable parallelism over a slice.
  • rayon::join to run two arbitrary closures in parallel — handy for divide-and-conquer (e.g. parallel quicksort, parallel tree traversal of the phylogenetic tree from day 3 exercise 5).
  • The same .par_iter() trick applied to a HashMap parallelises k-mer counting across reads, with each thread producing a partial HashMap and rayon merging them.
  • A note on numerical reproducibility: for sums over floats, parallel reductions may produce slightly different last-bit results than sequential ones, because floating-point addition is not associative. Our per-sequence-then-collect pattern is deterministic; a parallel .sum() over many floats would not be.