Say there are two arrays a and b having the same length.
How do you collect the sum of all a[i] * b[i] using
- normal iterators?
- parallel iterators?
Edit - multiple solutions below.
Say there are two arrays a and b having the same length.
How do you collect the sum of all a[i] * b[i] using
Edit - multiple solutions below.
Here's a code example, if you have any questions on how it works, ask away:
// Normal iterators
let sum: type = a.iter()
.zip(b.iter())
.map(|(x, y)| x * y)
.sum();
Playground
Note that when using that expression, you usually end up needing to specify the type, because the generic constraints on which iterators you can call .sum may confuse rust if you omit the type.
// Parallel iterators
let sum: type = a.par_iter()
.zip(b.par_iter())
.map(|(x, y)| x * y)
.sum();
All that needs to be changed is to make sure you have rayon as a dependency, rayon's traits in scope (usually through a use rayon::prelude). After that, usually just replacing .iter with .par_iter will work.
I really love how you used parallel iterators here.
a and b are ndarrays (as opposed to regular arrays)?O(n)/linear time or O(logn)/logarithmic time (where n is the size of the arrays)?Hi, I think I can answer those even though Iโve never used rayon or ndarray myself personally.
ndarrayโs Array type implements the IntoParallelIterator trait from rayon, so the drop-in-replacement approach of replacing the .iter() with .par_iter() ought to just work for those, too, as long as you activate the rayon feature for your ndarray dependency (in your Cargo.toml).for ndarray: