</div><!-- fragment --><h1><aclass="anchor" id="ParallelScanWhatIsAScan"></a>
What is a Scan Operation?</h1>
<p>A <em>scan</em> (also known as <em>prefix</em><em>sum</em>) computes, for each position <code>k</code> in the output range, the cumulative result of applying a binary operator to all relevant elements in the input range. There are two variants:</p>
<ul>
<li>An <em>inclusive</em> scan includes the element at position <code>k</code> itself. The N-th output element is the combination of the first N input elements. It is equivalent to the following sequential loop:</li>
</ul>
<divclass="fragment"><divclass="line"><spanclass="keywordflow">for</span>(<spanclass="keywordtype">size_t</span> k = 0; k < n; k++) {</div>
<li>An <em>exclusive</em> scan excludes the element at position <code>k</code>. The N-th output element is the combination of <code>init</code> with the first N-1 input elements, so the N-th input element is <em>excluded</em>. It is equivalent to the following sequential loop:</li>
</ul>
<divclass="fragment"><divclass="line"><spanclass="keywordflow">for</span>(<spanclass="keywordtype">size_t</span> k = 0; k < n; k++) {</div>
</div><!-- fragment --><p>The following diagram illustrates an inclusive scan on the input <code></code>[1, 2, 3, 4, -2, 1, 8] using addition:</p>
<divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_parallel_scan_inclusive.svg" width="422" height="142"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>Each output cell accumulates all input elements up to and including its own position (solid green arrow).</p>
<p>The following diagram illustrates an exclusive scan on the same input with <code>init=0</code>:</p>
<divclass="dotgraph">
<iframescrolling="no" frameborder="0" src="dot_parallel_scan_exclusive.svg" width="498" height="142"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
<p>Each output cell accumulates all input elements <em>before</em> its position (dashed blue arrow), so the first output cell receives <code>init</code> directly and the result is shifted one position to the right compared to inclusive scan.</p>
<p><aclass="el" href="classtf_1_1FlowBuilder.html#a1c2ace9290d83c2a006614a4d66ad588" title="creates an STL-styled parallel inclusive-scan task">tf::Taskflow::inclusive_scan(B first, E last, D d_first, BOP bop)</a> performs an inclusive scan over the range <code>[first, last)</code> using the binary operator <code>bop</code> and writes results to the range beginning at <code>d_first</code>.</p>
<divclass="line"><spanclass="comment">// input is now {1, 3, 6, 10, 15}</span></div>
</div><!-- fragment --><p>The overload <aclass="el" href="classtf_1_1FlowBuilder.html#a0b589a5bbf9b18e6484fa9e554d39a39" title="creates an STL-styled parallel inclusive-scan task with an initial value">tf::Taskflow::inclusive_scan(B first, E last, D d_first, BOP bop, T init)</a> performs an inclusive scan with an additional initial value <code>init</code> that is combined with the first element before any other accumulation:</p>
<divclass="line"><spanclass="comment">// output is {0, 2, 5, 9, 14}</span></div>
</div><!-- fragment --><h2><aclass="anchor" id="ParallelScanInclusiveCaptureByReference"></a>
Capture Iterators by Reference</h2>
<p>You can pass iterators by reference using <ahref="https://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</a> to marshal parameter updates between dependent tasks. This is useful when the range is not known at task-graph construction time but is initialized by an upstream task.</p>
<divclass="line"><spanclass="comment">// output is {1, 3, 6, 10, 15}</span></div>
<divclass="ttc" id="aclasstf_1_1Task_html"><divclass="ttname"><ahref="classtf_1_1Task.html">tf::Task</a></div><divclass="ttdoc">class to create a task handle over a taskflow node</div><divclass="ttdef"><b>Definition</b> task.hpp:263</div></div>
<divclass="ttc" id="aclasstf_1_1Task_html_a8c78c453295a553c1c016e4062da8588"><divclass="ttname"><ahref="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">tf::Task::precede</a></div><divclass="ttdeci">Task & precede(Ts &&... tasks)</div><divclass="ttdoc">adds precedence links from this to other tasks</div><divclass="ttdef"><b>Definition</b> task.hpp:952</div></div>
</div><!-- fragment --><h1><aclass="anchor" id="ParallelScanTransformInclusive"></a>
Create a Parallel Transform-Inclusive Scan Task</h1>
<p><aclass="el" href="classtf_1_1FlowBuilder.html#a82f3c3f49a2d52cd52f6eac07a659e9c" title="creates an STL-styled parallel transform-inclusive scan task">tf::Taskflow::transform_inclusive_scan(B first, E last, D d_first, BOP bop, UOP uop)</a> applies the unary operator <code>uop</code> to transform each input element before performing the inclusive scan with <code>bop</code>. It represents parallel execution of the following loop:</p>
<divclass="fragment"><divclass="line"><spanclass="keywordflow">for</span>(<spanclass="keywordtype">size_t</span> k = 0; k < n; k++) {</div>
<divclass="line"><spanclass="comment">// output is {-1, -3, -6, -10, -15}</span></div>
</div><!-- fragment --><p>The overload <aclass="el" href="classtf_1_1FlowBuilder.html#a49f7e17d02c708035b9134d8c6c89f90" title="creates an STL-styled parallel transform-inclusive scan task">tf::Taskflow::transform_inclusive_scan(B first, E last, D d_first, BOP bop, UOP uop, T init)</a> adds an initial value <code>init</code>. Note that <code>init</code> does <em>not</em> participate in the unary transformation <code>uop</code> — only input elements are transformed:</p>
<divclass="line"><spanclass="comment">// output is {-2, -4, -7, -11, -16}</span></div>
</div><!-- fragment --><h1><aclass="anchor" id="ParallelScanExclusive"></a>
Create a Parallel Exclusive Scan Task</h1>
<p><aclass="el" href="classtf_1_1FlowBuilder.html#a4e0d618d8eb0b3b2e5e00443a10bf512" title="creates an STL-styled parallel exclusive-scan task">tf::Taskflow::exclusive_scan(B first, E last, D d_first, T init, BOP bop)</a> performs an exclusive scan over the range <code>[first, last)</code> with the given initial value <code>init</code> using the binary operator <code>bop</code>.</p>
<divclass="line"><spanclass="comment">// input is now {-1, 0, 2, 5, 9}</span></div>
</div><!-- fragment --><h2><aclass="anchor" id="ParallelScanExclusiveCaptureByReference"></a>
Capture Iterators by Reference</h2>
<p>As with inclusive scan, all iterators can be passed by reference using <ahref="https://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</a> so that an upstream task can set up the ranges before the parallel exclusive scan runs.</p>
<divclass="line"><spanclass="comment">// output is {-1, 0, 2, 5, 9}</span></div>
</div><!-- fragment --><h1><aclass="anchor" id="ParallelScanTransformExclusive"></a>
Create a Parallel Transform-Exclusive Scan Task</h1>
<p><aclass="el" href="classtf_1_1FlowBuilder.html#a8549478ef819699b30f8daf88f04d577" title="creates an STL-styled parallel transform-exclusive scan task">tf::Taskflow::transform_exclusive_scan(B first, E last, D d_first, T init, BOP bop, UOP uop)</a> applies the unary operator <code>uop</code> to transform each input element before performing the exclusive scan with <code>bop</code> and the initial value <code>init</code>. It represents parallel execution of the following loop:</p>
<divclass="fragment"><divclass="line"><spanclass="keywordflow">for</span>(<spanclass="keywordtype">size_t</span> k = 0; k < n; k++) {</div>
</div><!-- fragment --><p>Note that <code>init</code> does <em>not</em> participate in the unary transformation <code>uop</code> — only input elements are transformed:</p>