119 lines
21 KiB
HTML
119 lines
21 KiB
HTML
<!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><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.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.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.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" title="show sidebar"></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 id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#how-to-use-rayon" title="How to use Rayon">How to use Rayon</a></li><li><a href="#basic-usage-and-the-rayon-prelude" title="Basic usage and the Rayon prelude">Basic usage and the Rayon prelude</a></li><li><a href="#crate-layout" title="Crate Layout">Crate Layout</a></li><li><a href="#targets-without-threading" title="Targets without threading">Targets without threading</a></li><li><a href="#other-questions" title="Other questions?">Other questions?</a></li></ul><h3><a href="#modules">Crate Items</a></h3><ul class="block"><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#enums" title="Enums">Enums</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>rayon</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/rayon/lib.rs.html#1-160">Source</a> </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 class="doc-anchor" href="#how-to-use-rayon">§</a>How to use Rayon</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 class="doc-anchor" href="#basic-usage-and-the-rayon-prelude">§</a>Basic usage and the Rayon prelude</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 class="doc-anchor" href="#crate-layout">§</a>Crate Layout</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 class="doc-anchor" href="#targets-without-threading">§</a>Targets without threading</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 class="doc-anchor" href="#other-questions">§</a>Other questions?</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">Modules<a href="#modules" class="anchor">§</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_<wbr>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">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.BroadcastContext.html" title="struct rayon::BroadcastContext">Broadcast<wbr>Context</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">Scope<wbr>Fifo</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">Thread<wbr>Builder</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">Thread<wbr>Pool</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">Thread<wbr>Pool<wbr>Build<wbr>Error</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">Thread<wbr>Pool<wbr>Builder</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">Enums<a href="#enums" class="anchor">§</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">Functions<a href="#functions" class="anchor">§</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_<wbr>num_<wbr>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_<wbr>thread_<wbr>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_<wbr>place_<wbr>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_<wbr>place_<wbr>scope_<wbr>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_<wbr>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_<wbr>num_<wbr>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_<wbr>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_<wbr>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_<wbr>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_<wbr>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_<wbr>now</a></div><div class="desc docblock-short">Cooperatively yields execution to Rayon.</div></li></ul></section></div></main></body></html> |