<li><ahref="#ParallelReductionInclude">Include the Header</a></li>
<li><ahref="#A2ParallelReduction">Create a Parallel-Reduction Task</a></li>
<li><ahref="#A2ParallelTransformationReduction">Create a Parallel-Transform-Reduction Task</a></li>
</ul>
</nav>
<p>Taskflow provides template function that constructs a task to perform parallel reduction over a range of items.</p><sectionid="ParallelReductionInclude"><h2><ahref="#ParallelReductionInclude">Include the Header</a></h2><p>You need to include the header file, <code>taskflow/algorithm/reduce.hpp</code>, for creating a parallel-reduction task.</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="w"></span><spanclass="cpf"><taskflow/algorithm/reduce.hpp></span><spanclass="cp"></span></pre></section><sectionid="A2ParallelReduction"><h2><ahref="#A2ParallelReduction">Create a Parallel-Reduction Task</a></h2><p>The reduction task created by <ahref="classtf_1_1FlowBuilder.html#aa9494fe7b862fc832884ce318e8a37f5" class="m-doc">tf::<wbr/>Taskflow::<wbr/>reduce(B first, E last, T& result, O bop)</a> performs parallel reduction over a range of elements specified by <code>[first, last)</code> using the binary operator <code>bop</code> and stores the reduced result in <code>result</code>. It represents the parallel execution of the following reduction loop:</p><preclass="m-code"><spanclass="k">for</span><spanclass="p">(</span><spanclass="k">auto</span><spanclass="w"></span><spanclass="n">itr</span><spanclass="o">=</span><spanclass="n">first</span><spanclass="p">;</span><spanclass="w"></span><spanclass="n">itr</span><spanclass="o"><</span><spanclass="n">last</span><spanclass="p">;</span><spanclass="w"></span><spanclass="n">itr</span><spanclass="o">++</span><spanclass="p">)</span><spanclass="w"></span><spanclass="p">{</span><spanclass="w"></span>
<spanclass="p">}</span><spanclass="w"></span></pre><p>At runtime, the reduction task spawns a subflow to perform parallel reduction. The reduced result is stored in <code>result</code> that will be captured by reference in the reduction task. It is your responsibility to ensure <code>result</code> remains alive during the parallel execution.</p><preclass="m-code"><spanclass="kt">int</span><spanclass="w"></span><spanclass="n">sum</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="mi">100</span><spanclass="p">;</span><spanclass="w"></span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">sum</span><spanclass="w"></span><spanclass="o">==</span><spanclass="w"></span><spanclass="mi">100</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">55</span><spanclass="p">);</span><spanclass="w"></span></pre><p>The order in which the binary operator is applied to pairs of elements is <em>unspecified</em>. In other words, the elements of the range may be grouped and rearranged in arbitrary order. The result and the argument types of the binary operator must be consistent with the input data type .</p><p>Similar to <ahref="ParallelIterations.html" class="m-doc">Parallel Iterations</a>, you can use <ahref="http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper.html" class="m-doc-external">std::<wbr/>reference_wrapper</a> to enable stateful parameter passing between the reduction task and others.</p><preclass="m-code"><spanclass="kt">int</span><spanclass="w"></span><spanclass="n">sum</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="mi">100</span><spanclass="p">;</span><spanclass="w"></span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">sum</span><spanclass="w"></span><spanclass="o">==</span><spanclass="w"></span><spanclass="mi">100</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">55</span><spanclass="p">);</span><spanclass="w"></span></pre><p>In the above example, when <code>init</code> finishes, <code>vec</code> has been initialized to 10 elements with <code>first</code> pointing to the first element and <code>last</code> pointing to the next of the last element (i.e., end of the range). These changes are visible to the execution context of the reduction task.</p></section><sectionid="A2ParallelTransformationReduction"><h2><ahref="#A2ParallelTransformationReduction">Create a Parallel-Transform-Reduction Task</a></h2><p>It is common to transform each element into a new data type and then perform reduction on the transformed elements. Taskflow provides a method, <ahref="classtf_1_1FlowBuilder.html#a024a1e5b4f138b6caebb427411fb0a2d" class="m-doc">tf::<wbr/>Taskflow::<wbr/>transform_reduce(B first, E last, T& result, BOP bop, UOP uop)</a>, that applies <code>uop</code> to transform each element in the specified range and then perform parallel reduction over <code>result</code> and transformed elements. It represents the parallel execution of the following reduction loop:</p><preclass="m-code"><spanclass="k">for</span><spanclass="p">(</span><spanclass="k">auto</span><spanclass="w"></span><spanclass="n">itr</span><spanclass="o">=</span><spanclass="n">first</span><spanclass="p">;</span><spanclass="w"></span><spanclass="n">itr</span><spanclass="o"><</span><spanclass="n">last</span><spanclass="p">;</span><spanclass="w"></span><spanclass="n">itr</span><spanclass="o">++</span><spanclass="p">)</span><spanclass="w"></span><spanclass="p">{</span><spanclass="w"></span>
<spanclass="p">}</span><spanclass="w"></span></pre><p>The example below transforms each digit in a string to an integer number and then sums up all integers in parallel.</p><preclass="m-code"><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">string</span><spanclass="w"></span><spanclass="n">str</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="s">"12345678"</span><spanclass="p">;</span><spanclass="w"></span>
<spanclass="n">assert</span><spanclass="p">(</span><spanclass="n">sum</span><spanclass="w"></span><spanclass="o">==</span><spanclass="w"></span><spanclass="mi">1</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">2</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">3</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">4</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">5</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">6</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">7</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">8</span><spanclass="p">);</span><spanclass="w"></span><spanclass="c1">// sum will be 36 </span></pre><p>The order in which we apply the binary operator on the transformed elements is <em>unspecified</em>. It is possible that the binary operator will take <em>r-value</em> in both arguments, for example, <code>bop(uop(*itr1), uop(*itr2))</code>, due to the transformed temporaries. When data passing is expensive, you may define the result type <code>T</code> to be move-constructible.</p></section>
</div>
</div>
</div>
</article></main>
<divclass="m-doc-search" id="search">
<ahref="#!" onclick="return hideSearch()"></a>
<divclass="m-container">
<divclass="m-row">
<divclass="m-col-m-8 m-push-m-2">
<divclass="m-doc-search-header m-text m-small">
<div><spanclass="m-label m-default">Tab</span> / <spanclass="m-label m-default">T</span> to search, <spanclass="m-label m-default">Esc</span> to close</div>