rayon README

The project's README from github.com/rayon-rs/rayon. As one markdown file: README.md.

Rayon

Rayon crate Rayon documentation minimum rustc 1.85 build status

Rayon is a data-parallelism library for Rust. It is extremely lightweight and makes it easy to convert a sequential computation into a parallel one. It also guarantees data-race freedom. (You may also enjoy this blog post about Rayon, which gives more background and details about how it works, or this video, from the Rust Belt Rust conference.) Rayon is available on crates.io, and API documentation is available on docs.rs.

Parallel iterators and more

Rayon makes it drop-dead simple to convert sequential iterators into parallel ones: usually, you just change your foo.iter() call into foo.par_iter(), and Rayon does the rest:

use rayon::prelude::*;
fn sum_of_squares(input: &[i32]) -> i32 {
    input.par_iter() // <-- just change that!
         .map(|&i| i * i)
         .sum()
}

Parallel iterators take care of deciding how to divide your data into tasks; it will dynamically adapt for maximum performance. If you need more flexibility than that, Rayon also offers the join and scope functions, which let you create parallel tasks on your own. For even more control, you can create custom thread pools rather than using Rayon's default, global thread pool.

No data races

You may have heard that parallel execution can produce all kinds of crazy bugs. Well, rest easy. Rayon's APIs all guarantee data-race freedom, which generally rules out most parallel bugs (though not all). In other words, if your code compiles, it typically does the same thing it did before.

For the most part, parallel iterators in particular are guaranteed to produce the same results as their sequential counterparts. One caveat: If your iterator has side effects (for example, sending methods to other threads through a Rust channel or writing to disk), those side effects may occur in a different order. Note also that, in some cases, parallel iterators offer alternative versions of the sequential iterator methods that can have higher performance.

Using Rayon

Rayon is available on crates.io. The recommended way to use it is to add a line into your Cargo.toml such as:

[dependencies]
rayon = "1.12"

To use the parallel iterator APIs, a number of traits have to be in scope. The easiest way to bring those things into scope is to use the Rayon prelude. In each module where you would like to use the parallel iterator APIs, just add:

use rayon::prelude::*;

Rayon currently requires rustc 1.85.0 or greater.

Usage with WebAssembly

By default, when building to WebAssembly, Rayon will treat it as any other platform without multithreading support and will fall back to sequential iteration. This allows existing code to compile and run successfully with no changes necessary, but it will run slower as it will only use a single CPU core.

You can build Rayon-based projects with proper multithreading support for the Web, but you'll need an adapter and some project configuration to account for differences between WebAssembly threads and threads on the other platforms.

Check out the wasm-bindgen-rayon docs for more details.

Contribution

Rayon is an open source project! If you'd like to contribute to Rayon, check out the list of "help wanted" issues. 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 page on the wiki. Note that all code submitted in PRs to Rayon is assumed to be licensed under Rayon's dual MIT/Apache 2.0 licensing.

Quick demo

To see Rayon in action, check out the rayon-demo directory, which includes a number of demos of code using Rayon. For example, run this command to get a visualization of an N-body simulation. To see the effect of using Rayon, press s to run sequentially and p to run in parallel.

> cd rayon-demo
> cargo run --release -- nbody visualize

For more information on demos, try:

> cd rayon-demo
> cargo run --release -- --help

Other questions?

See the Rayon FAQ.

License

Rayon is distributed under the terms of both the MIT license and the Apache License (Version 2.0). See LICENSE-APACHE and LICENSE-MIT for details. Opening a pull request is assumed to signal agreement with these licensing terms.

rayon (rayon-rs/rayon)

Searching Database…

13.4K ·GitHub
这是什么?Rayon 是一个 Rust 的数据并行库。它非常轻量,能让你轻松把串行计算改造成并行计算,并且保证不会出现数据竞争。
它是一个我直接运行的应用,还是我在代码里调用的库?是一个库:你把它作为依赖添加到你的 Rust 项目中,然后在代码里调用它的 API。没有什么需要单独安装或运行的东西——这个 crate 托管在 crates.io 上,API 文档在 docs.rs 上。
在项目里怎么开始使用它?在 Cargo.toml 里添加一行 rayon 依赖,然后在你想要使用并行迭代器 API 的每个模块中引入 Rayon prelude,这样所需的 trait 就都在作用域内了。
使用它需要额外安装什么吗?只需要一个较新的 Rust 工具链:Rayon 目前要求 rustc 1.85.0 或更高版本,任何保持更新的稳定版安装都能满足。
并行版本的行为会和我原来的串行代码完全一样吗?大部分情况下一样:数据竞争自由是有保证的,并行迭代器产生的结果也和串行版本相同。唯一需要注意的是:像发送通道消息或写磁盘这类副作用,发生的顺序可能不一样。
我可以免费在自己的项目里使用它吗?可以——Rayon 同时以 MIT 许可证和 Apache 许可证(2.0 版)的条款发布。
有没有快捷的方式看看 Rayon 的实际效果?有——rayon-demo 目录里包含几个演示程序,比如一个 N 体模拟的可视化。按 s 串行运行,按 p 并行运行,就能对比两者的差异。
比起自己手写线程,为什么选 Rayon?这正是它的核心卖点:把串行迭代器并行化被形容为简单至极,而且它的 API 保证没有数据竞争,所以能通过编译的代码通常行为和原来一样——你完全不用自己去拆分任务。
这个项目成熟度如何?Rayon 从 2014 年 10 月起就在 GitHub 上了,大约有 13,400 颗星。README 里还链接了一篇背景博客文章、一次会议演讲,以及一份解答其他问题的 FAQ。
What are similar projects?conc, taskflow, tokio, perry, bend, compio, muduo, concurrentqueue
Creator
rayon-rs
Size
3.6 MB
Stars
13.4K
Updated
CommitAug 28
Age
12y
Commits · 90 Days