Module rayon
Data parallelism library for Rust.
rayon is a data-parallelism library for Rust that makes it easy to convert
sequential computations into parallel ones with minimal changes to existing code.
The crate provides parallel versions of common iterator methods through the
ParallelIterator trait, allowing you to simply change .iter() to .par_iter()
to parallelize operations. Rayon uses work-stealing to efficiently distribute
computations across CPU cores.
Key features include parallel iteration with par_iter, parallel collection
operations like map, filter, and reduce, parallel sorting with
par_sort, and parallel searching. The library also provides join for
fork-join parallelism and scope for structured parallelism with lifetimes.
Rayon automatically manages thread pools and work distribution, making parallelism accessible without manual thread management. It's particularly effective for CPU-bound tasks that can be decomposed into independent units.
Rayon's global ThreadPool is the recommended threadpool implementation
in rustmax for general-purpose parallel computation. It provides excellent
work-stealing performance and integrates seamlessly with rayon's parallel
iterators.
Examples
Basic parallel iteration:
use rayon::prelude::*;
let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
let sum: i32 = data.par_iter().map(|&x| x * x).sum();
assert_eq!(sum, 204); // 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64
Parallel filtering and collection:
use rayon::prelude::*;
let numbers = (1..100).collect::<Vec<_>>();
let evens: Vec<_> = numbers
.par_iter()
.filter(|&&n| n % 2 == 0)
.map(|&n| n * 2)
.collect();
assert_eq!(evens[0], 4); // 2 * 2
assert_eq!(evens[1], 8); // 4 * 2
Parallel reduction with custom operation:
use rayon::prelude::*;
let data = vec![1, 5, 3, 9, 2, 8, 4];
let max = data.par_iter().cloned().reduce(|| 0, |a, b| a.max(b));
assert_eq!(max, 9);
Using rayon's threadpool for custom parallel work:
use rayon::ThreadPoolBuilder;
let pool = ThreadPoolBuilder::new().num_threads(4).build().unwrap();
let result = pool.install(|| {
// This closure runs on the custom threadpool
(0..1000).into_iter().map(|i| i * i).sum::<i32>()
});
assert_eq!(result, 332833500);
Modules
-
array
Parallel iterator types for [arrays] (
[T; N]) - collections Parallel iterator types for [standard collections]
- iter Traits for writing parallel programs using an iterator-style interface
- option Parallel iterator types for [options]
-
prelude
The rayon prelude imports the various
ParallelIteratortraits. The intention is that one can includeuse rayon::prelude::*and have easy access to the various traits and methods you will need. -
range
Parallel iterator types for [ranges],
the type for values created by
a..bexpressions -
range_inclusive
Parallel iterator types for [inclusive ranges],
the type for values created by
a..=bexpressions - result Parallel iterator types for [results]
- slice Parallel iterator types for [slices]
- str Parallel iterator types for [strings]
-
string
This module contains the parallel iterator types for owned strings
(
String). You will rarely need to interact with it directly unless you have need to name one of the iterator types. -
vec
Parallel iterator types for [vectors] (
Vec<T>)
Structs
-
BroadcastContext
Provides context to a closure called by
broadcast. -
FnContext
Provides the calling context to a closure called by
join_context. -
Scope
Represents a fork-join scope which can be used to spawn any number of tasks.
See [
scope()] for more information. -
ScopeFifo
Represents a fork-join scope which can be used to spawn any number of tasks.
Those spawned from the same thread are prioritized in relative FIFO order.
See [
scope_fifo()] for more information. -
ThreadBuilder
Thread builder used for customization via [
ThreadPoolBuilder::spawn_handler()]. - ThreadPool Represents a user-created [thread pool].
- ThreadPoolBuildError Error when initializing a thread pool.
-
ThreadPoolBuilder
Used to create a new
ThreadPoolor to configure the global rayon thread pool.Creating a ThreadPool
The following creates a thread pool with 22 threads.
Enums
-
Yield
Result of [
yield_now()] or [yield_local()].
Functions
-
broadcast
Executes
opwithin every thread in the current thread pool. If this is called from a non-Rayon thread, it will execute in the global thread pool. Any attempts to usejoin,scope, or parallel iterators will then operate within that thread pool. When the call has completed on each thread, returns a vector containing all of their return values. - current_num_threads Returns the number of threads in the current registry. If this code is executing within a Rayon thread pool, then this will be the number of threads for the thread pool of the current thread. Otherwise, it will be the number of threads for the global thread pool.
-
current_thread_index
If called from a Rayon worker thread, returns the index of that
thread within its current pool; if not called from a Rayon thread,
returns
None. -
in_place_scope
Creates a "fork-join" scope
sand invokes the closure with a reference tos. This closure can then spawn asynchronous tasks intos. Those tasks may run asynchronously with respect to the closure; they may themselves spawn additional tasks intos. When the closure returns, it will block until all tasks that have been spawned intoscomplete. -
in_place_scope_fifo
Creates a "fork-join" scope
swith FIFO order, and invokes the closure with a reference tos. This closure can then spawn asynchronous tasks intos. Those tasks may run asynchronously with respect to the closure; they may themselves spawn additional tasks intos. When the closure returns, it will block until all tasks that have been spawned intoscomplete. - join Takes two closures and potentially runs them in parallel. It returns a pair of the results from those closures.
-
join_context
Identical to
join, except that the closures have a parameter that provides context for the way the closure has been called, especially indicating whether they're executing on a different thread than wherejoin_contextwas called. This will occur if the second job is stolen by a different thread, or ifjoin_contextwas called from outside the thread pool to begin with. - max_num_threads Returns the maximum number of threads that Rayon supports in a single thread pool.
-
scope
Creates a "fork-join" scope
sand invokes the closure with a reference tos. This closure can then spawn asynchronous tasks intos. Those tasks may run asynchronously with respect to the closure; they may themselves spawn additional tasks intos. When the closure returns, it will block until all tasks that have been spawned intoscomplete. -
scope_fifo
Creates a "fork-join" scope
swith FIFO order, and invokes the closure with a reference tos. This closure can then spawn asynchronous tasks intos. Those tasks may run asynchronously with respect to the closure; they may themselves spawn additional tasks intos. When the closure returns, it will block until all tasks that have been spawned intoscomplete. -
spawn
Puts the task into the Rayon thread pool's job queue in the "static"
or "global" scope. Just like a standard thread, this task is not
tied to the current stack frame, and hence it cannot hold any
references other than those with
'staticlifetime. If you want to spawn a task that references stack data, use [thescope()function] to create a scope. -
spawn_broadcast
Spawns an asynchronous task on every thread in this thread pool. This task
will run in the implicit, global scope, which means that it may outlast the
current stack frame -- therefore, it cannot capture any references onto the
stack (you will likely need a
moveclosure). -
spawn_fifo
Fires off a task into the Rayon thread pool in the "static" or
"global" scope. Just like a standard thread, this task is not
tied to the current stack frame, and hence it cannot hold any
references other than those with
'staticlifetime. If you want to spawn a task that references stack data, use [thescope_fifo()function] to create a scope. - yield_local Cooperatively yields execution to local Rayon work.
- yield_now Cooperatively yields execution to Rayon.