Keeping data-parallelism simple with Rayon
In Chapter 3, Understanding Concurrency we processed our Fibonacci numbers in parallel. While it was interesting to look into concurrency, when we are building our own applications, we should lean on other crates to reduce the complexity of our application. This is where the rayon
crate comes in. This will enable us to loop through numbers to be calculated and process them in parallel. In order to do this, we initially have to define the crate in the Cargo.toml
file as seen here:
[dependencies]
rayon = "1.5.1"
With this, we import this crate in our main.rs
file with the
following code:
extern crate rayon;
use rayon::prelude::*;
Then, if we do not import the macros with use rayon::prelude::*;
our compiler will refuse to compile when we try and turn a standard vector into a parallel iterator. With these macros, we can execute parallel Fibonacci calculations with the following code:
pub fn fibonacci_reccursive(n: i32) ->...