<li><ahref="#ParallelTransformsInclude">Include the Header</a></li>
<li><ahref="#ParallelTransformsOverARange">Create a Unary Parallel-Transform Task</a></li>
<li><ahref="#ParallelTransformsCaptureIteratorsByReference">Capture Iterators by Reference</a></li>
<li><ahref="#ParallelBinaryTransformsOverARange">Create a Binary Parallel-Transform Task</a></li>
<li><ahref="#ParallelTransformsCfigureAPartitioner">Configure a Partitioner</a></li>
</ul>
</nav>
<p>Taskflow provides template functions for constructing tasks to perform parallel transforms over ranges of items.</p><sectionid="ParallelTransformsInclude"><h2><ahref="#ParallelTransformsInclude">Include the Header</a></h2><p>You need to include the header file, <code>taskflow/algorithm/transform.hpp</code>, for creating a parallel-transform task.</p><preclass="m-code"><spanclass="cp">#include</span><spanclass="w"></span><spanclass="cpf"><taskflow/algorithm/transform.hpp></span></pre></section><sectionid="ParallelTransformsOverARange"><h2><ahref="#ParallelTransformsOverARange">Create a Unary Parallel-Transform Task</a></h2><p>Parallel-transform transforms a range of items, possibly with a different type for the transformed data, and stores the result in another range. The task created by <ahref="classtf_1_1FlowBuilder.html#a97be7ceef6fa4276e3b074c10c13b826" class="m-doc">tf::<wbr/>Taskflow::<wbr/>transform(B first1, E last1, O d_first, C c, P part)</a> is equivalent to a parallel execution of the following loop:</p><preclass="m-code"><spanclass="k">while</span><spanclass="w"></span><spanclass="p">(</span><spanclass="n">first1</span><spanclass="w"></span><spanclass="o">!=</span><spanclass="w"></span><spanclass="n">last1</span><spanclass="p">)</span><spanclass="w"></span><spanclass="p">{</span>
<spanclass="p">}</span></pre><p><ahref="classtf_1_1FlowBuilder.html#a97be7ceef6fa4276e3b074c10c13b826" class="m-doc">tf::<wbr/>Taskflow::<wbr/>transform</a> simultaneously applies the callable <code>c</code> to the object obtained by dereferencing every iterator in the range <code>[first1, last1)</code> and stores the result in another range beginning at <code>d_first</code>. It is user's responsibility for ensuring the range is valid within the execution of the parallel-transform task.</p><preclass="m-code"><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">vector</span><spanclass="o"><</span><spanclass="kt">int</span><spanclass="o">></span><spanclass="w"></span><spanclass="n">src</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="p">{</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mi">2</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mi">3</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mi">4</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mi">5</span><spanclass="p">};</span>
<spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"transforming item "</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="n">i</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">" to "</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="n">i</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">1</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="sc">'\n'</span><spanclass="p">;</span>
<spanclass="p">});</span></pre></section><sectionid="ParallelTransformsCaptureIteratorsByReference"><h2><ahref="#ParallelTransformsCaptureIteratorsByReference">Capture Iterators by Reference</a></h2><p>You can pass iterators by reference using <ahref="https://en.cppreference.com/w/cpp/utility/functional/ref">std::<wbr/>ref</a> to marshal parameter update between dependent tasks. This is especially useful when the range is unknown at the time of creating a parallel-transform task, but needs initialization from another task.</p><preclass="m-code"><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">vector</span><spanclass="o"><</span><spanclass="kt">int</span><spanclass="o">></span><spanclass="w"></span><spanclass="n">src</span><spanclass="p">,</span><spanclass="w"></span><spanclass="n">tgt</span><spanclass="p">;</span>
<spanclass="w"></span><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">cout</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">"transforming item "</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="n">i</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="s">" to "</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="n">i</span><spanclass="w"></span><spanclass="o">+</span><spanclass="w"></span><spanclass="mi">1</span><spanclass="w"></span><spanclass="o"><<</span><spanclass="w"></span><spanclass="sc">'\n'</span><spanclass="p">;</span>
<spanclass="n">init</span><spanclass="p">.</span><spanclass="n">precede</span><spanclass="p">(</span><spanclass="n">transform</span><spanclass="p">);</span></pre><p>When <code>init</code> finishes, the parallel-transform task <code>transform</code> will see <code>first</code> pointing to the beginning of <code>src</code> and <code>last</code> pointing to the end of <code>src</code>. Then, it simultaneously transforms these 1000 items by adding one to each element and stores the result in another range starting at <code>d_first</code>.</p></section><sectionid="ParallelBinaryTransformsOverARange"><h2><ahref="#ParallelBinaryTransformsOverARange">Create a Binary Parallel-Transform Task</a></h2><p>You can use the overload, <ahref="classtf_1_1FlowBuilder.html#a7ea96d3fa0aa9e3ff337a9f1e37682b0" class="m-doc">tf::<wbr/>Taskflow::<wbr/>transform(B1 first1, E1 last1, B2 first2, O d_first, C c, P part)</a>, to perform parallel transforms on two source ranges pointed by <code>first1</code> and <code>first2</code> using the binary operator <code>c</code> and store the result in another range pointed by <code>d_first</code>. This method is equivalent to the parallel execution of the following loop:</p><preclass="m-code"><spanclass="k">while</span><spanclass="w"></span><spanclass="p">(</span><spanclass="n">first1</span><spanclass="w"></span><spanclass="o">!=</span><spanclass="w"></span><spanclass="n">last1</span><spanclass="p">)</span><spanclass="w"></span><spanclass="p">{</span>
<spanclass="p">}</span></pre><p>The following example creates a parallel-transform task that adds two ranges of elements one by one and stores the result in a target range:</p><preclass="m-code"><spanclass="n">std</span><spanclass="o">::</span><spanclass="n">vector</span><spanclass="o"><</span><spanclass="kt">int</span><spanclass="o">></span><spanclass="w"></span><spanclass="n">src1</span><spanclass="w"></span><spanclass="o">=</span><spanclass="w"></span><spanclass="p">{</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mi">2</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mi">3</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mi">4</span><spanclass="p">,</span><spanclass="w"></span><spanclass="mi">5</span><spanclass="p">};</span>
<spanclass="p">);</span></pre></section><sectionid="ParallelTransformsCfigureAPartitioner"><h2><ahref="#ParallelTransformsCfigureAPartitioner">Configure a Partitioner</a></h2><p>You can configure a partitioner for parallel-transform tasks to run with different scheduling methods, such as guided partitioning, dynamic partitioning, and static partitioning. The following example creates two parallel-transform tasks using two different partitioners, one with the static partitioning algorithm and another one with the guided partitioning algorithm:</p><preclass="m-code"><spanclass="n">tf</span><spanclass="o">::</span><spanclass="n">StaticPartitioner</span><spanclass="w"></span><spanclass="n">static_partitioner</span><spanclass="p">;</span>
<spanclass="p">);</span></pre><asideclass="m-note m-warning"><h4>Attention</h4><p>By default, parallel-transform tasks use <ahref="namespacetf.html#a66b72776c788898aee9e132b0ea9b405" class="m-doc">tf::<wbr/>DefaultPartitioner</a> if no partitioner is specified.</p></aside></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>