Parallel iterator with iproduct use

I am accessing pixels from a buffer using the itertools iproduct macro and I was wondering as I am accessing pixels independantly if there was a way to parallelize the iterator produced by the macro using Rayon?

Yes, as long as the problem can be split into smaller pieces of work that don't overlap.

As far as I can tell your algorithm writes pixels at consecutive locations, so you could divide the work by line, e.g.:

buffer.chunks_mut(width).par_iter().for_each(|line| {
    // it's usually enough to parallelize lines,
    // which avoids overhead of per-pixel parallelization.
    for pixel in line { 
       *pixel = // compute value

Complex problems can also be parallelized, with some clever wrapper types:

1 Like