<p>The reduction task created by <aclass="el" href="classtf_1_1FlowBuilder.html#ad999cd75045db225a42d5881d6db1223" title="constructs a STL-styled parallel-reduce task ">tf::Taskflow::reduce(B&& first, E&& last, T& result, O&& bop)</a> performs parallel reduction over a range of elements specified by <code>[beg, end)</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>
<divclass="fragment"><divclass="line"><spanclass="keywordflow">for</span>(<spanclass="keyword">auto</span> itr=first; itr<last; itr++) {</div><divclass="line"> result = bop(result, *itr);</div><divclass="line">}</div></div><!-- fragment --><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>
<divclass="fragment"><divclass="line"><spanclass="keywordtype">int</span> sum = 100;</div><divclass="line"><aclass="codeRef" doxygen="/home/tsung-wei/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<int></a> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};</div><divclass="line"></div><divclass="line"><aclass="code" href="classtf_1_1Task.html">tf::Task</a> task = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#ad999cd75045db225a42d5881d6db1223">reduce</a>(vec.begin(), vec.end(), sum, [] (<spanclass="keywordtype">int</span> l, <spanclass="keywordtype">int</span> r) { </div><divclass="line"><spanclass="keywordflow">return</span> l + r; <spanclass="comment">// binary reducer operator</span></div><divclass="line">});</div><divclass="line">executor.<aclass="code" href="classtf_1_1Executor.html#a81f35d5b0a20ac0646447eb80d97c0aa">run</a>(taskflow).wait();</div><divclass="line"></div><divclass="line">assert(sum == 100 + 55);</div></div><!-- fragment --><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 <aclass="el" href="A0ForEach.html">A0: Parallel Iterations</a>, you may leverage <aclass="elRef" doxygen="/home/tsung-wei/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper.html">std::reference_wrapper</a> to enable stateful parameter passing between tasks for expressing end-to-end parallelism.</p>
<divclass="fragment"><divclass="line"><spanclass="keywordtype">int</span> sum = 100;</div><divclass="line"><aclass="codeRef" doxygen="/home/tsung-wei/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<int></a> vec;</div><divclass="line"><aclass="codeRef" doxygen="/home/tsung-wei/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<int>::iterator</a> first, last;</div><divclass="line"></div><divclass="line"><aclass="code" href="classtf_1_1Task.html">tf::Task</a> init = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([&](){</div><divclass="line"> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};</div><divclass="line"> first = vec.begin();</div><divclass="line"> last = vec.end();</div><divclass="line">});</div><divclass="line"></div><divclass="line"><aclass="code" href="classtf_1_1Task.html">tf::Task</a> task = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#ad999cd75045db225a42d5881d6db1223">reduce</a>(std::ref(first), std::ref(last), sum, [] (<spanclass="keywordtype">int</span> l, <spanclass="keywordtype">int</span> r) { </div><divclass="line"><spanclass="keywordflow">return</span> l + r; <spanclass="comment">// binary reducer operator</span></div><divclass="line">});</div><divclass="line"></div><divclass="line"><spanclass="comment">// wrong! must use std::ref, or first and last are captured by copy</span></div><divclass="line"><spanclass="comment">// tf::Task task = taskflow.reduce(first, last, sum, [] (int l, int r) { </span></div><divclass="line"><spanclass="comment">// return l + r; // binary reducer operator</span></div><divclass="line"><spanclass="comment">// });</span></div><divclass="line"></div><divclass="line">init.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(task);</div><divclass="line"></div><divclass="line">executor.<aclass="code" href="classtf_1_1Executor.html#a81f35d5b0a20ac0646447eb80d97c0aa">run</a>(taskflow).wait();</div><divclass="line"></div><divclass="line">assert(sum == 100 + 55);</div></div><!-- fragment --><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>
<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, <aclass="el" href="classtf_1_1FlowBuilder.html#ad8d03524f15292610ebee63d53b89579" title="constructs a STL-styled parallel transform-reduce task ">tf::Taskflow::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>
<divclass="fragment"><divclass="line"><spanclass="keywordflow">for</span>(<spanclass="keyword">auto</span> itr=first; itr<last; itr++) {</div><divclass="line"> result = bop(result, uop(*itr));</div><divclass="line">}</div></div><!-- fragment --><p>The example below transforms each digit in a string to an integer number and then sums up all integers in parallel.</p>
<divclass="fragment"><divclass="line"><aclass="codeRef" doxygen="/home/tsung-wei/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml:http://en.cppreference.com/w/" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> str = <spanclass="stringliteral">"12345678"</span>;</div><divclass="line"><spanclass="keywordtype">int</span> sum {0};</div><divclass="line"></div><divclass="line"><aclass="code" href="classtf_1_1Task.html">tf::Task</a> task = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#ad8d03524f15292610ebee63d53b89579">transform_reduce</a>(str.begin(), str.end(), sum,</div><divclass="line"> [] (<spanclass="keywordtype">int</span> a, <spanclass="keywordtype">int</span> b) { <spanclass="comment">// binary reduction operator</span></div><divclass="line"><spanclass="keywordflow">return</span> a + b;</div><divclass="line"> }, </div><divclass="line"> [] (<spanclass="keywordtype">char</span> c) -> <spanclass="keywordtype">int</span> { <spanclass="comment">// unary transformation operator</span></div><divclass="line"><spanclass="keywordflow">return</span> c - <spanclass="charliteral">'0'</span>;</div><divclass="line"> } </div><divclass="line">); </div><divclass="line"></div><divclass="line">executor.<aclass="code" href="classtf_1_1Executor.html#a81f35d5b0a20ac0646447eb80d97c0aa">run</a>(taskflow).wait(); </div><divclass="line"></div><divclass="line">assert(sum == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8); <spanclass="comment">// sum will be 36 </span></div></div><!-- fragment --><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>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<divid="nav-path" class="navpath"><!-- id is needed for treeview function! -->