blob: c5e55dc75ede9d1377f1bcbde49097de8816ff00 [file] [log] [blame]
<!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`."><meta name="keywords" content="rust, rustlang, rust-lang, split"><title>split in rayon::iter - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../../rayon/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../rayon/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2><a href="index.html">In rayon::iter</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Function <a href="../index.html">rayon</a>::<wbr><a href="index.html">iter</a>::<wbr><a class="fn" href="#">split</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/rayon/iter/splitter.rs.html#106-112">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust fn"><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;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;D: Send,<br>&nbsp;&nbsp;&nbsp;&nbsp;S: Fn(D) -&gt; (D, Option&lt;D&gt;) + Sync,</span></code></pre></div><details class="rustdoc-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 href="#examples">Examples</a></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><div id="rustdoc-vars" data-root-path="../../" data-current-crate="rayon" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>