Files
GopherGate/target/doc/rayon/iter/fn.split.html
2026-02-26 12:00:21 -05:00

84 lines
8.9 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="The `split` function takes arbitrary data and a closure that knows how to split it, and turns this into a `ParallelIterator`."><title>split in rayon::iter - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.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"href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-ca0dd0c4.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.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">split</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../rayon/index.html">rayon</a><span class="version">1.11.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">split</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li></ul></section><div id="rustdoc-modnav"><h2><a href="index.html">In rayon::<wbr>iter</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">rayon</a>::<wbr><a href="index.html">iter</a></div><h1>Function <span class="fn">split</span>&nbsp;<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/iter/splitter.rs.html#106-112">Source</a> </span></div><pre class="rust item-decl"><code>pub fn split&lt;D, S&gt;(data: D, splitter: S) -&gt; <a class="struct" href="struct.Split.html" title="struct rayon::iter::Split">Split</a>&lt;D, S&gt;<div class="where">where
D: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,
S: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(D) -&gt; (D, <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;D&gt;) + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a>,</div></code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>The <code>split</code> function takes arbitrary data and a closure that knows how to
split it, and turns this into a <code>ParallelIterator</code>.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<p>As a simple example, Rayon can recursively split ranges of indices</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rayon::iter;
<span class="kw">use </span>rayon::prelude::<span class="kw-2">*</span>;
<span class="kw">use </span>std::ops::Range;
<span class="comment">// We define a range of indices as follows
</span><span class="kw">type </span>Range1D = Range&lt;usize&gt;;
<span class="comment">// Splitting it in two can be done like this
</span><span class="kw">fn </span>split_range1(r: Range1D) -&gt; (Range1D, <span class="prelude-ty">Option</span>&lt;Range1D&gt;) {
<span class="comment">// We are mathematically unable to split the range if there is only
// one point inside of it, but we could stop splitting before that.
</span><span class="kw">if </span>r.end - r.start &lt;= <span class="number">1 </span>{ <span class="kw">return </span>(r, <span class="prelude-val">None</span>); }
<span class="comment">// Here, our range is considered large enough to be splittable
</span><span class="kw">let </span>midpoint = r.start + (r.end - r.start) / <span class="number">2</span>;
(r.start..midpoint, <span class="prelude-val">Some</span>(midpoint..r.end))
}
<span class="comment">// By using iter::split, Rayon will split the range until it has enough work
// to feed the CPU cores, then give us the resulting sub-ranges
</span>iter::split(<span class="number">0</span>..<span class="number">4096</span>, split_range1).for_each(|sub_range| {
<span class="comment">// As our initial range had a power-of-two size, the final sub-ranges
// should have power-of-two sizes too
</span><span class="macro">assert!</span>((sub_range.end - sub_range.start).is_power_of_two());
});</code></pre></div>
<p>This recursive splitting can be extended to two or three dimensions,
to reproduce a classic “block-wise” parallelization scheme of graphics and
numerical simulations:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// A two-dimensional range of indices can be built out of two 1D ones
</span><span class="kw">struct </span>Range2D {
<span class="comment">// Range of horizontal indices
</span><span class="kw">pub </span>rx: Range1D,
<span class="comment">// Range of vertical indices
</span><span class="kw">pub </span>ry: Range1D,
}
<span class="comment">// We want to recursively split them by the largest dimension until we have
// enough sub-ranges to feed our mighty multi-core CPU. This function
// carries out one such split.
</span><span class="kw">fn </span>split_range2(r2: Range2D) -&gt; (Range2D, <span class="prelude-ty">Option</span>&lt;Range2D&gt;) {
<span class="comment">// Decide on which axis (horizontal/vertical) the range should be split
</span><span class="kw">let </span>width = r2.rx.end - r2.rx.start;
<span class="kw">let </span>height = r2.ry.end - r2.ry.start;
<span class="kw">if </span>width &gt;= height {
<span class="comment">// This is a wide range, split it on the horizontal axis
</span><span class="kw">let </span>(split_rx, ry) = (split_range1(r2.rx), r2.ry);
<span class="kw">let </span>out1 = Range2D {
rx: split_rx.<span class="number">0</span>,
ry: ry.clone(),
};
<span class="kw">let </span>out2 = split_rx.<span class="number">1</span>.map(|rx| Range2D { rx, ry });
(out1, out2)
} <span class="kw">else </span>{
<span class="comment">// This is a tall range, split it on the vertical axis
</span><span class="kw">let </span>(rx, split_ry) = (r2.rx, split_range1(r2.ry));
<span class="kw">let </span>out1 = Range2D {
rx: rx.clone(),
ry: split_ry.<span class="number">0</span>,
};
<span class="kw">let </span>out2 = split_ry.<span class="number">1</span>.map(|ry| Range2D { rx, ry, });
(out1, out2)
}
}
<span class="comment">// Again, rayon can handle the recursive splitting for us
</span><span class="kw">let </span>range = Range2D { rx: <span class="number">0</span>..<span class="number">800</span>, ry: <span class="number">0</span>..<span class="number">600 </span>};
iter::split(range, split_range2).for_each(|sub_range| {
<span class="comment">// If the sub-ranges were indeed split by the largest dimension, then
// if no dimension was twice larger than the other initially, this
// property will remain true in the final sub-ranges.
</span><span class="kw">let </span>width = sub_range.rx.end - sub_range.rx.start;
<span class="kw">let </span>height = sub_range.ry.end - sub_range.ry.start;
<span class="macro">assert!</span>((width / <span class="number">2 </span>&lt;= height) &amp;&amp; (height / <span class="number">2 </span>&lt;= width));
});</code></pre></div></div></details></section></div></main></body></html>