]>
Commit | Line | Data |
---|---|---|
94b46f34 XL |
1 | Note: This is an unstable fork made for use in rustc |
2 | ||
3 | # Rayon | |
4 | ||
5 | [![Rayon crate](https://img.shields.io/crates/v/rayon.svg)](https://crates.io/crates/rayon) | |
6 | [![Rayon documentation](https://docs.rs/rayon/badge.svg)](https://docs.rs/rayon) | |
7 | [![Travis Status](https://travis-ci.org/rayon-rs/rayon.svg?branch=master)](https://travis-ci.org/rayon-rs/rayon) | |
8 | [![Appveyor status](https://ci.appveyor.com/api/projects/status/wre5dkx08gayy8hc/branch/master?svg=true)](https://ci.appveyor.com/project/cuviper/rayon/branch/master) | |
9 | [![Join the chat at https://gitter.im/rayon-rs/Lobby](https://badges.gitter.im/rayon-rs/Lobby.svg)](https://gitter.im/rayon-rs/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | |
10 | ||
11 | Rayon is a data-parallelism library for Rust. It is extremely | |
12 | lightweight and makes it easy to convert a sequential computation into | |
13 | a parallel one. It also guarantees data-race freedom. (You may also | |
14 | enjoy [this blog post][blog] about Rayon, which gives more background | |
15 | and details about how it works, or [this video][video], from the Rust | |
16 | Belt Rust conference.) Rayon is | |
17 | [available on crates.io](https://crates.io/crates/rayon), and | |
18 | [API Documentation is available on docs.rs](https://docs.rs/rayon/). | |
19 | ||
20 | [blog]: http://smallcultfollowing.com/babysteps/blog/2015/12/18/rayon-data-parallelism-in-rust/ | |
21 | [video]: https://www.youtube.com/watch?v=gof_OEv71Aw | |
22 | ||
23 | ## Parallel iterators and more | |
24 | ||
25 | Rayon makes it drop-dead simple to convert sequential iterators into | |
26 | parallel ones: usually, you just change your `foo.iter()` call into | |
27 | `foo.par_iter()`, and Rayon does the rest: | |
28 | ||
29 | ```rust | |
30 | use rayon::prelude::*; | |
31 | fn sum_of_squares(input: &[i32]) -> i32 { | |
32 | input.par_iter() // <-- just change that! | |
33 | .map(|&i| i * i) | |
34 | .sum() | |
35 | } | |
36 | ``` | |
37 | ||
38 | [Parallel iterators] take care of deciding how to divide your data | |
39 | into tasks; it will dynamically adapt for maximum performance. If you | |
40 | need more flexibility than that, Rayon also offers the [join] and | |
41 | [scope] functions, which let you create parallel tasks on your own. | |
42 | For even more control, you can create [custom threadpools] rather than | |
43 | using Rayon's default, global threadpool. | |
44 | ||
45 | [Parallel iterators]: https://docs.rs/rayon/*/rayon/iter/index.html | |
46 | [join]: https://docs.rs/rayon/*/rayon/fn.join.html | |
47 | [scope]: https://docs.rs/rayon/*/rayon/fn.scope.html | |
48 | [custom threadpools]: https://docs.rs/rayon/*/rayon/struct.ThreadPool.html | |
49 | ||
50 | ## No data races | |
51 | ||
52 | You may have heard that parallel execution can produce all kinds of | |
53 | crazy bugs. Well, rest easy. Rayon's APIs all guarantee **data-race | |
54 | freedom**, which generally rules out most parallel bugs (though not | |
55 | all). In other words, **if your code compiles**, it typically does the | |
56 | same thing it did before. | |
57 | ||
58 | For the most, parallel iterators in particular are guaranteed to | |
59 | produce the same results as their sequential counterparts. One caevat: | |
60 | If your iterator has side effects (for example, sending methods to | |
61 | other threads through a [Rust channel] or writing to disk), those side | |
62 | effects may occur in a different order. Note also that, in some cases, | |
63 | parallel iterators offer alternative versions of the sequential | |
64 | iterator methods that can have higher performance. | |
65 | ||
66 | [Rust channel]: https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html | |
67 | ||
68 | ## Using Rayon | |
69 | ||
70 | [Rayon is available on crates.io](https://crates.io/crates/rayon). The | |
71 | recommended way to use it is to add a line into your Cargo.toml such | |
72 | as: | |
73 | ||
74 | ```toml | |
75 | [dependencies] | |
76 | rayon = "1.0" | |
77 | ``` | |
78 | ||
79 | and then add the following to to your `lib.rs`: | |
80 | ||
81 | ```rust | |
82 | extern crate rayon; | |
83 | ``` | |
84 | ||
85 | To use the Parallel Iterator APIs, a number of traits have to be in | |
86 | scope. The easiest way to bring those things into scope is to use the | |
87 | [Rayon prelude](https://docs.rs/rayon/*/rayon/prelude/index.html). In | |
88 | each module where you would like to use the parallel iterator APIs, | |
89 | just add: | |
90 | ||
91 | ```rust | |
92 | use rayon::prelude::*; | |
93 | ``` | |
94 | ||
95 | Rayon currently requires `rustc 1.13.0` or greater. | |
96 | ||
97 | ## Contribution | |
98 | ||
99 | Rayon is an open source project! If you'd like to contribute to Rayon, check out [the list of "help wanted" issues](https://github.com/rayon-rs/rayon/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22). These are all (or should be) issues that are suitable for getting started, and they generally include a detailed set of instructions for what to do. Please ask questions if anything is unclear! Also, check out the [Guide to Development](https://github.com/rayon-rs/rayon/wiki/Guide-to-Development) page on the wiki. Note that all code submitted in PRs to Rayon is assumed to [be licensed under Rayon's dual MIT/Apache2 licensing](https://github.com/rayon-rs/rayon/blob/master/README.md#license). | |
100 | ||
101 | ## Quick demo | |
102 | ||
103 | To see Rayon in action, check out the `rayon-demo` directory, which | |
104 | includes a number of demos of code using Rayon. For example, run this | |
105 | command to get a visualization of an nbody simulation. To see the | |
106 | effect of using Rayon, press `s` to run sequentially and `p` to run in | |
107 | parallel. | |
108 | ||
109 | ``` | |
110 | > cd rayon-demo | |
111 | > cargo +nightly run --release -- nbody visualize | |
112 | ``` | |
113 | ||
114 | For more information on demos, try: | |
115 | ||
116 | ``` | |
117 | > cd rayon-demo | |
118 | > cargo +nightly run --release -- --help | |
119 | ``` | |
120 | ||
121 | **Note:** While Rayon is usable as a library with the stable compiler, running demos or executing tests requires nightly Rust. | |
122 | ||
123 | ## Other questions? | |
124 | ||
125 | See [the Rayon FAQ][faq]. | |
126 | ||
127 | [faq]: https://github.com/rayon-rs/rayon/blob/master/FAQ.md | |
128 | ||
129 | ## License | |
130 | ||
131 | Rayon is distributed under the terms of both the MIT license and the | |
132 | Apache License (Version 2.0). See [LICENSE-APACHE](LICENSE-APACHE) and | |
133 | [LICENSE-MIT](LICENSE-MIT) for details. Opening a pull requests is | |
134 | assumed to signal agreement with these licensing terms. |