|
|
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Rayon is a data-parallelism library that makes it easy to convert sequential computations into parallel."><title>rayon - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-ac92e1bbe349e143.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="rayon" data-themes="" data-resource-suffix="" data-rustdoc-version="1.76.0 (07dca489a 2024-02-04)" data-channel="1.76.0" data-search-js="search-2b6ce74ff89ae146.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-f2adc0d6ca4d09fb.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-305769736d49e732.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-feafe1bb7466e4bd.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../rayon/index.html">rayon</a><span class="version">1.10.0</span></h2></div><div class="sidebar-elems"><ul class="block">
|
|
|
<li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#functions">Functions</a></li></ul></section></div></nav><div class="sidebar-resizer"></div>
|
|
|
<main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../rayon/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">rayon</a><button id="copy-path" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../src/rayon/lib.rs.html#1-160">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Rayon is a data-parallelism library that makes it easy to convert sequential
|
|
|
computations into parallel.</p>
|
|
|
<p>It is lightweight and convenient for introducing parallelism into existing
|
|
|
code. It guarantees data-race free executions and takes advantage of
|
|
|
parallelism when sensible, based on work-load at runtime.</p>
|
|
|
<h2 id="how-to-use-rayon"><a href="#how-to-use-rayon">How to use Rayon</a></h2>
|
|
|
<p>There are two ways to use Rayon:</p>
|
|
|
<ul>
|
|
|
<li><strong>High-level parallel constructs</strong> are the simplest way to use Rayon and also
|
|
|
typically the most efficient.
|
|
|
<ul>
|
|
|
<li><a href="iter/index.html">Parallel iterators</a> make it easy to convert a sequential iterator to
|
|
|
execute in parallel.
|
|
|
<ul>
|
|
|
<li>The <a href="iter/trait.ParallelIterator.html"><code>ParallelIterator</code></a> trait defines general methods for all parallel iterators.</li>
|
|
|
<li>The <a href="iter/trait.IndexedParallelIterator.html"><code>IndexedParallelIterator</code></a> trait adds methods for iterators that support random
|
|
|
access.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li>The <a href="slice/trait.ParallelSliceMut.html#method.par_sort"><code>par_sort</code></a> method sorts <code>&mut [T]</code> slices (or vectors) in parallel.</li>
|
|
|
<li><a href="iter/trait.ParallelExtend.html#tymethod.par_extend"><code>par_extend</code></a> can be used to efficiently grow collections with items produced
|
|
|
by a parallel iterator.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
<li><strong>Custom tasks</strong> let you divide your work into parallel tasks yourself.
|
|
|
<ul>
|
|
|
<li><a href="fn.join.html"><code>join</code></a> is used to subdivide a task into two pieces.</li>
|
|
|
<li><a href="fn.scope.html"><code>scope</code></a> creates a scope within which you can create any number of parallel tasks.</li>
|
|
|
<li><a href="struct.ThreadPoolBuilder.html"><code>ThreadPoolBuilder</code></a> can be used to create your own thread pools or customize
|
|
|
the global one.</li>
|
|
|
</ul>
|
|
|
</li>
|
|
|
</ul>
|
|
|
<h2 id="basic-usage-and-the-rayon-prelude"><a href="#basic-usage-and-the-rayon-prelude">Basic usage and the Rayon prelude</a></h2>
|
|
|
<p>First, you will need to add <code>rayon</code> to your <code>Cargo.toml</code>.</p>
|
|
|
<p>Next, to use parallel iterators or the other high-level methods,
|
|
|
you need to import several traits. Those traits are bundled into
|
|
|
the module <a href="prelude/index.html"><code>rayon::prelude</code></a>. It is recommended that you import
|
|
|
all of these traits at once by adding <code>use rayon::prelude::*</code> at
|
|
|
the top of each module that uses Rayon methods.</p>
|
|
|
<p>These traits give you access to the <code>par_iter</code> method which provides
|
|
|
parallel implementations of many iterative functions such as <a href="iter/trait.ParallelIterator.html#method.map"><code>map</code></a>,
|
|
|
<a href="iter/trait.ParallelIterator.html#method.for_each"><code>for_each</code></a>, <a href="iter/trait.ParallelIterator.html#method.filter"><code>filter</code></a>, <a href="iter/trait.ParallelIterator.html#method.fold"><code>fold</code></a>, and <a href="iter/trait.ParallelIterator.html#provided-methods">more</a>.</p>
|
|
|
<h2 id="crate-layout"><a href="#crate-layout">Crate Layout</a></h2>
|
|
|
<p>Rayon extends many of the types found in the standard library with
|
|
|
parallel iterator implementations. The modules in the <code>rayon</code>
|
|
|
crate mirror <a href="https://doc.rust-lang.org/std/"><code>std</code></a> itself: so, e.g., the <code>option</code> module in
|
|
|
Rayon contains parallel iterators for the <code>Option</code> type, which is
|
|
|
found in <a href="https://doc.rust-lang.org/std/option/index.html">the <code>option</code> module of <code>std</code></a>. Similarly, the
|
|
|
<code>collections</code> module in Rayon offers parallel iterator types for
|
|
|
<a href="https://doc.rust-lang.org/std/collections/index.html">the <code>collections</code> from <code>std</code></a>. You will rarely need to access
|
|
|
these submodules unless you need to name iterator types
|
|
|
explicitly.</p>
|
|
|
<h2 id="targets-without-threading"><a href="#targets-without-threading">Targets without threading</a></h2>
|
|
|
<p>Rayon has limited support for targets without <code>std</code> threading implementations.
|
|
|
See the <a href="../rayon_core/index.html" title="mod rayon_core"><code>rayon_core</code></a> documentation for more information about its global fallback.</p>
|
|
|
<h2 id="other-questions"><a href="#other-questions">Other questions?</a></h2>
|
|
|
<p>See <a href="https://github.com/rayon-rs/rayon/blob/main/FAQ.md">the Rayon FAQ</a>.</p>
|
|
|
</div></details><h2 id="modules" class="section-header"><a href="#modules">Modules</a></h2><ul class="item-table"><li><div class="item-name"><a class="mod" href="array/index.html" title="mod rayon::array">array</a></div><div class="desc docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/std/primitive.array.html">arrays</a> (<code>[T; N]</code>)</div></li><li><div class="item-name"><a class="mod" href="collections/index.html" title="mod rayon::collections">collections</a></div><div class="desc docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/collections/">standard collections</a></div></li><li><div class="item-name"><a class="mod" href="iter/index.html" title="mod rayon::iter">iter</a></div><div class="desc docblock-short">Traits for writing parallel programs using an iterator-style interface</div></li><li><div class="item-name"><a class="mod" href="option/index.html" title="mod rayon::option">option</a></div><div class="desc docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/option/">options</a></div></li><li><div class="item-name"><a class="mod" href="prelude/index.html" title="mod rayon::prelude">prelude</a></div><div class="desc docblock-short">The rayon prelude imports the various <code>ParallelIterator</code> traits.
|
|
|
The intention is that one can include <code>use rayon::prelude::*</code> and
|
|
|
have easy access to the various traits and methods you will need.</div></li><li><div class="item-name"><a class="mod" href="range/index.html" title="mod rayon::range">range</a></div><div class="desc docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/core/ops/struct.Range.html">ranges</a>,
|
|
|
the type for values created by <code>a..b</code> expressions</div></li><li><div class="item-name"><a class="mod" href="range_inclusive/index.html" title="mod rayon::range_inclusive">range_inclusive</a></div><div class="desc docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/core/ops/struct.RangeInclusive.html">inclusive ranges</a>,
|
|
|
the type for values created by <code>a..=b</code> expressions</div></li><li><div class="item-name"><a class="mod" href="result/index.html" title="mod rayon::result">result</a></div><div class="desc docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/result/">results</a></div></li><li><div class="item-name"><a class="mod" href="slice/index.html" title="mod rayon::slice">slice</a></div><div class="desc docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/slice/">slices</a></div></li><li><div class="item-name"><a class="mod" href="str/index.html" title="mod rayon::str">str</a></div><div class="desc docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/str/">strings</a></div></li><li><div class="item-name"><a class="mod" href="string/index.html" title="mod rayon::string">string</a></div><div class="desc docblock-short">This module contains the parallel iterator types for owned strings
|
|
|
(<code>String</code>). You will rarely need to interact with it directly
|
|
|
unless you have need to name one of the iterator types.</div></li><li><div class="item-name"><a class="mod" href="vec/index.html" title="mod rayon::vec">vec</a></div><div class="desc docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/vec/">vectors</a> (<code>Vec<T></code>)</div></li></ul><h2 id="structs" class="section-header"><a href="#structs">Structs</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.BroadcastContext.html" title="struct rayon::BroadcastContext">BroadcastContext</a></div><div class="desc docblock-short">Provides context to a closure called by <code>broadcast</code>.</div></li><li><div class="item-name"><a class="struct" href="struct.FnContext.html" title="struct rayon::FnContext">FnContext</a></div><div class="desc docblock-short">Provides the calling context to a closure called by <code>join_context</code>.</div></li><li><div class="item-name"><a class="struct" href="struct.Scope.html" title="struct rayon::Scope">Scope</a></div><div class="desc docblock-short">Represents a fork-join scope which can be used to spawn any number of tasks.
|
|
|
See <a href="fn.scope.html"><code>scope()</code></a> for more information.</div></li><li><div class="item-name"><a class="struct" href="struct.ScopeFifo.html" title="struct rayon::ScopeFifo">ScopeFifo</a></div><div class="desc docblock-short">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 <a href="fn.scope_fifo.html"><code>scope_fifo()</code></a> for more information.</div></li><li><div class="item-name"><a class="struct" href="struct.ThreadBuilder.html" title="struct rayon::ThreadBuilder">ThreadBuilder</a></div><div class="desc docblock-short">Thread builder used for customization via
|
|
|
<a href="struct.ThreadPoolBuilder.html#method.spawn_handler"><code>ThreadPoolBuilder::spawn_handler</code></a>.</div></li><li><div class="item-name"><a class="struct" href="struct.ThreadPool.html" title="struct rayon::ThreadPool">ThreadPool</a></div><div class="desc docblock-short">Represents a user created <a href="https://en.wikipedia.org/wiki/Thread_pool">thread-pool</a>.</div></li><li><div class="item-name"><a class="struct" href="struct.ThreadPoolBuildError.html" title="struct rayon::ThreadPoolBuildError">ThreadPoolBuildError</a></div><div class="desc docblock-short">Error when initializing a thread pool.</div></li><li><div class="item-name"><a class="struct" href="struct.ThreadPoolBuilder.html" title="struct rayon::ThreadPoolBuilder">ThreadPoolBuilder</a></div><div class="desc docblock-short">Used to create a new <a href="struct.ThreadPool.html"><code>ThreadPool</code></a> or to configure the global rayon thread pool.</div></li></ul><h2 id="enums" class="section-header"><a href="#enums">Enums</a></h2><ul class="item-table"><li><div class="item-name"><a class="enum" href="enum.Yield.html" title="enum rayon::Yield">Yield</a></div><div class="desc docblock-short">Result of <a href="fn.yield_now.html" title="fn rayon::yield_now"><code>yield_now()</code></a> or <a href="fn.yield_local.html" title="fn rayon::yield_local"><code>yield_local()</code></a>.</div></li></ul><h2 id="functions" class="section-header"><a href="#functions">Functions</a></h2><ul class="item-table"><li><div class="item-name"><a class="fn" href="fn.broadcast.html" title="fn rayon::broadcast">broadcast</a></div><div class="desc docblock-short">Executes <code>op</code> within every thread in the current threadpool. If this is
|
|
|
called from a non-Rayon thread, it will execute in the global threadpool.
|
|
|
Any attempts to use <code>join</code>, <code>scope</code>, or parallel iterators will then operate
|
|
|
within that threadpool. When the call has completed on each thread, returns
|
|
|
a vector containing all of their return values.</div></li><li><div class="item-name"><a class="fn" href="fn.current_num_threads.html" title="fn rayon::current_num_threads">current_num_threads</a></div><div class="desc docblock-short">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.</div></li><li><div class="item-name"><a class="fn" href="fn.current_thread_index.html" title="fn rayon::current_thread_index">current_thread_index</a></div><div class="desc docblock-short">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 <code>None</code>.</div></li><li><div class="item-name"><a class="fn" href="fn.in_place_scope.html" title="fn rayon::in_place_scope">in_place_scope</a></div><div class="desc docblock-short">Creates a “fork-join” scope <code>s</code> and invokes the closure with a
|
|
|
reference to <code>s</code>. This closure can then spawn asynchronous tasks
|
|
|
into <code>s</code>. Those tasks may run asynchronously with respect to the
|
|
|
closure; they may themselves spawn additional tasks into <code>s</code>. When
|
|
|
the closure returns, it will block until all tasks that have been
|
|
|
spawned into <code>s</code> complete.</div></li><li><div class="item-name"><a class="fn" href="fn.in_place_scope_fifo.html" title="fn rayon::in_place_scope_fifo">in_place_scope_fifo</a></div><div class="desc docblock-short">Creates a “fork-join” scope <code>s</code> with FIFO order, and invokes the
|
|
|
closure with a reference to <code>s</code>. This closure can then spawn
|
|
|
asynchronous tasks into <code>s</code>. Those tasks may run asynchronously with
|
|
|
respect to the closure; they may themselves spawn additional tasks
|
|
|
into <code>s</code>. When the closure returns, it will block until all tasks
|
|
|
that have been spawned into <code>s</code> complete.</div></li><li><div class="item-name"><a class="fn" href="fn.join.html" title="fn rayon::join">join</a></div><div class="desc docblock-short">Takes two closures and <em>potentially</em> runs them in parallel. It
|
|
|
returns a pair of the results from those closures.</div></li><li><div class="item-name"><a class="fn" href="fn.join_context.html" title="fn rayon::join_context">join_context</a></div><div class="desc docblock-short">Identical to <code>join</code>, 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 where <code>join_context</code> was called. This will occur if
|
|
|
the second job is stolen by a different thread, or if
|
|
|
<code>join_context</code> was called from outside the thread pool to begin
|
|
|
with.</div></li><li><div class="item-name"><a class="fn" href="fn.max_num_threads.html" title="fn rayon::max_num_threads">max_num_threads</a></div><div class="desc docblock-short">Returns the maximum number of threads that Rayon supports in a single thread-pool.</div></li><li><div class="item-name"><a class="fn" href="fn.scope.html" title="fn rayon::scope">scope</a></div><div class="desc docblock-short">Creates a “fork-join” scope <code>s</code> and invokes the closure with a
|
|
|
reference to <code>s</code>. This closure can then spawn asynchronous tasks
|
|
|
into <code>s</code>. Those tasks may run asynchronously with respect to the
|
|
|
closure; they may themselves spawn additional tasks into <code>s</code>. When
|
|
|
the closure returns, it will block until all tasks that have been
|
|
|
spawned into <code>s</code> complete.</div></li><li><div class="item-name"><a class="fn" href="fn.scope_fifo.html" title="fn rayon::scope_fifo">scope_fifo</a></div><div class="desc docblock-short">Creates a “fork-join” scope <code>s</code> with FIFO order, and invokes the
|
|
|
closure with a reference to <code>s</code>. This closure can then spawn
|
|
|
asynchronous tasks into <code>s</code>. Those tasks may run asynchronously with
|
|
|
respect to the closure; they may themselves spawn additional tasks
|
|
|
into <code>s</code>. When the closure returns, it will block until all tasks
|
|
|
that have been spawned into <code>s</code> complete.</div></li><li><div class="item-name"><a class="fn" href="fn.spawn.html" title="fn rayon::spawn">spawn</a></div><div class="desc docblock-short">Puts the task into the Rayon threadpool’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 <code>'static</code> lifetime. If you want
|
|
|
to spawn a task that references stack data, use <a href="fn.scope.html">the <code>scope()</code>
|
|
|
function</a> to create a scope.</div></li><li><div class="item-name"><a class="fn" href="fn.spawn_broadcast.html" title="fn rayon::spawn_broadcast">spawn_broadcast</a></div><div class="desc docblock-short">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 <code>move</code> closure).</div></li><li><div class="item-name"><a class="fn" href="fn.spawn_fifo.html" title="fn rayon::spawn_fifo">spawn_fifo</a></div><div class="desc docblock-short">Fires off a task into the Rayon threadpool 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 <code>'static</code> lifetime. If you want
|
|
|
to spawn a task that references stack data, use <a href="fn.scope_fifo.html">the <code>scope_fifo()</code>
|
|
|
function</a> to create a scope.</div></li><li><div class="item-name"><a class="fn" href="fn.yield_local.html" title="fn rayon::yield_local">yield_local</a></div><div class="desc docblock-short">Cooperatively yields execution to local Rayon work.</div></li><li><div class="item-name"><a class="fn" href="fn.yield_now.html" title="fn rayon::yield_now">yield_now</a></div><div class="desc docblock-short">Cooperatively yields execution to Rayon.</div></li></ul></section></div></main></body></html> |