| 1 | # Rayon |
| 2 | |
| 3 | [](https://crates.io/crates/rayon) |
| 4 | [](https://docs.rs/rayon) |
| 5 |  |
| 6 | [](https://github.com/rayon-rs/rayon/actions) |
| 7 | |
| 8 | Rayon is a data-parallelism library for Rust. It is extremely |
| 9 | lightweight and makes it easy to convert a sequential computation into |
| 10 | a parallel one. It also guarantees data-race freedom. (You may also |
| 11 | enjoy [this blog post][blog] about Rayon, which gives more background |
| 12 | and details about how it works, or [this video][video], from the Rust |
| 13 | Belt Rust conference.) Rayon is |
| 14 | [available on crates.io](https://crates.io/crates/rayon), and |
| 15 | [API documentation is available on docs.rs](https://docs.rs/rayon). |
| 16 | |
| 17 | [blog]: https://smallcultfollowing.com/babysteps/blog/2015/12/18/rayon-data-parallelism-in-rust/ |
| 18 | [video]: https://www.youtube.com/watch?v=gof_OEv71Aw |
| 19 | |
| 20 | ## Parallel iterators and more |
| 21 | |
| 22 | Rayon makes it drop-dead simple to convert sequential iterators into |
| 23 | parallel ones: usually, you just change your `foo.iter()` call into |
| 24 | `foo.par_iter()`, and Rayon does the rest: |
| 25 | |
| 26 | ```rust |
| 27 | use rayon::prelude::*; |
| 28 | fn sum_of_squares(input: &[i32]) -> i32 { |
| 29 | input.par_iter() // <-- just change that! |
| 30 | .map(|&i| i * i) |
| 31 | .sum() |
| 32 | } |
| 33 | ``` |
| 34 | |
| 35 | [Parallel iterators] take care of deciding how to divide your data |
| 36 | into tasks; it will dynamically adapt for maximum performance. If you |
| 37 | need more flexibility than that, Rayon also offers the [join] and |
| 38 | [scope] functions, which let you create parallel tasks on your own. |
| 39 | For even more control, you can create [custom thread pools] rather than |
| 40 | using Rayon's default, global thread pool. |
| 41 | |
| 42 | [Parallel iterators]: https://docs.rs/rayon/*/rayon/iter/index.html |
| 43 | [join]: https://docs.rs/rayon/*/rayon/fn.join.html |
| 44 | [scope]: https://docs.rs/rayon/*/rayon/fn.scope.html |
| 45 | [custom thread pools]: https://docs.rs/rayon/*/rayon/struct.ThreadPool.html |
| 46 | |
| 47 | ## No data races |
| 48 | |
| 49 | You may have heard that parallel execution can produce all kinds of |
| 50 | crazy bugs. Well, rest easy. Rayon's APIs all guarantee **data-race |
| 51 | freedom**, which generally rules out most parallel bugs (though not |
| 52 | all). In other words, **if your code compiles**, it typically does the |
| 53 | same thing it did before. |
| 54 | |
| 55 | For the most, parallel iterators in particular are guaranteed to |
| 56 | produce the same results as their sequential counterparts. One caveat: |
| 57 | If your iterator has side effects (for example, sending methods to |
| 58 | other threads through a [Rust channel] or writing to disk), those side |
| 59 | effects may occur in a different order. Note also that, in some cases, |
| 60 | parallel iterators offer alternative versions of the sequential |
| 61 | iterator methods that can have higher performance. |
| 62 | |
| 63 | [Rust channel]: https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html |
| 64 | |
| 65 | ## Using Rayon |
| 66 | |
| 67 | [Rayon is available on crates.io](https://crates.io/crates/rayon). The |
| 68 | recommended way to use it is to add a line into your Cargo.toml such |
| 69 | as: |
| 70 | |
| 71 | ```toml |
| 72 | [dependencies] |
| 73 | rayon = "1.11" |
| 74 | ``` |
| 75 | |
| 76 | To use the parallel iterator APIs, a number of traits have to be in |
| 77 | scope. The easiest way to bring those things into scope is to use the |
| 78 | [Rayon prelude](https://docs.rs/rayon/*/rayon/prelude/index.html). In |
| 79 | each module where you would like to use the parallel iterator APIs, |
| 80 | just add: |
| 81 | |
| 82 | ```rust |
| 83 | use rayon::prelude::*; |
| 84 | ``` |
| 85 | |
| 86 | Rayon currently requires `rustc 1.80.0` or greater. |
| 87 | |
| 88 | ### Usage with WebAssembly |
| 89 | |
| 90 | By default, when building to WebAssembly, Rayon will treat it as any |
| 91 | other platform without multithreading support and will fall back to |
| 92 | sequential iteration. This allows existing code to compile and run |
| 93 | successfully with no changes necessary, but it will run slower as it |
| 94 | will only use a single CPU core. |
| 95 | |
| 96 | You can build Rayon-based projects with proper multithreading support |
| 97 | for the Web, but you'll need an adapter and some project configuration |
| 98 | to account for differences between WebAssembly threads and threads on |
| 99 | the other platforms. |
| 100 | |
| 101 | Check out the |
| 102 | [wasm-bindgen-rayon](https://github.com/RReverser/wasm-bindgen-rayon) |
| 103 | docs for more details. |
| 104 | |
| 105 | ## Contribution |
| 106 | |
| 107 | Rayon is an open source project! If you'd like to contribute to Rayon, |
| 108 | check out |
| 109 | [the list of "help wanted" issues](https://github.com/rayon-rs/rayon/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22). |
| 110 | These are all (or should be) issues that are suitable for getting |
| 111 | started, and they generally include a detailed set of instructions for |
| 112 | what to do. Please ask questions if anything is unclear! Also, check |
| 113 | out the |
| 114 | [Guide to Development](https://github.com/rayon-rs/rayon/wiki/Guide-to-Development) |
| 115 | page on the wiki. Note that all code submitted in PRs to Rayon is |
| 116 | assumed to |
| 117 | [be licensed under Rayon's dual MIT/Apache 2.0 licensing](https://github.com/rayon-rs/rayon/blob/main/README.md#license). |
| 118 | |
| 119 | ## Quick demo |
| 120 | |
| 121 | To see Rayon in action, check out the `rayon-demo` directory, which |
| 122 | includes a number of demos of code using Rayon. For example, run this |
| 123 | command to get a visualization of an N-body simulation. To see the |
| 124 | effect of using Rayon, press `s` to run sequentially and `p` to run in |
| 125 | parallel. |
| 126 | |
| 127 | ```text |
| 128 | > cd rayon-demo |
| 129 | > cargo run --release -- nbody visualize |
| 130 | ``` |
| 131 | |
| 132 | For more information on demos, try: |
| 133 | |
| 134 | ```text |
| 135 | > cd rayon-demo |
| 136 | > cargo run --release -- --help |
| 137 | ``` |
| 138 | |
| 139 | ## Other questions? |
| 140 | |
| 141 | See [the Rayon FAQ][faq]. |
| 142 | |
| 143 | [faq]: https://github.com/rayon-rs/rayon/blob/main/FAQ.md |
| 144 | |
| 145 | ## License |
| 146 | |
| 147 | Rayon is distributed under the terms of both the MIT license and the |
| 148 | Apache License (Version 2.0). See [LICENSE-APACHE](LICENSE-APACHE) and |
| 149 | [LICENSE-MIT](LICENSE-MIT) for details. Opening a pull request is |
| 150 | assumed to signal agreement with these licensing terms. |