<p>Index-based parallel-for performs parallel iterations over a range <code>[beg, end)</code> with the given <code>step</code> size. The task created by <aclass="el" href="classtf_1_1FlowBuilder.html#ab8417b211b18bb1e0f45a049331f084d" title="constructs an index-based parallel-for task ">tf::Taskflow::for_each_index(B&& first, E&& last, S&& step, C&& callable)</a> represents parallel execution of the following loop:</p>
<divclass="fragment"><divclass="line"><spanclass="comment">// positive step</span></div><divclass="line"><spanclass="keywordflow">for</span>(<spanclass="keyword">auto</span> i=first; i<last; i+=step) {</div><divclass="line"> callable(i);</div><divclass="line">}</div><divclass="line"></div><divclass="line"><spanclass="comment">// negative step</span></div><divclass="line"><spanclass="keywordflow">for</span>(<spanclass="keyword">auto</span> i=first; i>last; i+=step) {</div><divclass="line"> callable(i);</div><divclass="line">}</div></div><!-- fragment --><p>We support only integer-based range. The range can go positive or negative direction.</p>
<divclass="fragment"><divclass="line">taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#ab8417b211b18bb1e0f45a049331f084d">for_each_index</a>(0, 100, 2, [](<spanclass="keywordtype">int</span> i) { }); <spanclass="comment">// loop 50 items with a positive step</span></div><divclass="line">taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#ab8417b211b18bb1e0f45a049331f084d">for_each_index</a>(100, 0, -2, [](<spanclass="keywordtype">int</span> i) { }); <spanclass="comment">// loop 50 items with a negative step</span></div></div><!-- fragment --><p>Notice that either positive or negative direction is defined in terms of the range, <code>[first, last)</code>, where <code>end</code> is excluded. In the positive case, the 50 items are 0, 2, 4, 6, 8, ..., 96, 98. In the negative case, the 50 items are 100, 98, 96, 04, ... 4, 2. An example of the Taskflow graph for the positive case under 12 workers is depicted below:</p>
<p>The index types, <code>B</code>, <code>E</code>, and <code>S</code>, are templates to preserve the variable types and their underlying types must be of the same <em>integral</em> type (e.g., <code>int</code>, <code>size_t</code>, <code>unsigned</code>). By default, <aclass="el" href="classtf_1_1FlowBuilder.html#ab8417b211b18bb1e0f45a049331f084d" title="constructs an index-based parallel-for task ">tf::Taskflow::for_each_index</a> creates a task that spawns a subflow (see <aclass="el" href="chapter3.html">C3: Dynamic Tasking</a>) to run iterations in parallel. The subflow closure captures all input arguments through perfect forwarding to form a stateful closure such that any changes on the arguments will be visible to the execution context of the subflow. For example:</p>
<divclass="fragment"><divclass="line"><spanclass="keywordtype">int</span>* vec;</div><divclass="line"><spanclass="keywordtype">int</span> first, last;</div><divclass="line"></div><divclass="line"><spanclass="keyword">auto</span> init = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a796e29175380f70246cf2a5639adc437">emplace</a>([&](){</div><divclass="line"> first = 0;</div><divclass="line"> last = 1000;</div><divclass="line"> vec = <spanclass="keyword">new</span><spanclass="keywordtype">int</span>[1000]; </div><divclass="line">});</div><divclass="line"></div><divclass="line"><spanclass="keyword">auto</span> pf = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#ab8417b211b18bb1e0f45a049331f084d">for_each_index</a>(std::ref(first), std::ref(last), 1, [&](<spanclass="keywordtype">int</span> i) {</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/io/basic_ostream.html">std::cout</a> << <spanclass="stringliteral">"parallel iteration on index "</span> << vec[i] << <spanclass="charliteral">'\n'</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">// auto pf = taskflow.for_each_index(first, last, 1, [&](int i) {</span></div><divclass="line"><spanclass="comment">// std::cout << "parallel iteration on index " << vec[i] << '\n';</span></div><divclass="line"><spanclass="comment">// });</span></div><divclass="line"></div><divclass="line">init.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(pf);</div></div><!-- fragment --><p>When <code>init</code> finishes, the parallel-for task <code>pf</code> will see <code>first</code> as 0 and <code>last</code> as 1000 and performs parallel iterations over the 1000 items. This property is especially important for task graph parallelism, because users can define end-to-end parallelism through stateful closures that marshal parameter exchange between dependent tasks.</p>
<p>Iterator-based parallel-for performs parallel iterations over a range specified by two <ahref="https://en.cppreference.com/w/cpp/iterator/iterator">STL-styled iterators</a>, <code>first</code> and <code>last</code>. The task created by <aclass="el" href="classtf_1_1FlowBuilder.html#a564252001be679600b20ca9ed9920f6a" title="constructs a STL-styled parallel-for task ">tf::Taskflow::for_each(B&& first, E&& last, C&& callable)</a> represents a parallel execution of the following loop:</p>
<divclass="fragment"><divclass="line"><spanclass="keywordflow">for</span>(<spanclass="keyword">auto</span> i=first; i<last; i++) {</div><divclass="line"> callable(*i);</div><divclass="line">}</div></div><!-- fragment --><p>By default, <aclass="el" href="classtf_1_1FlowBuilder.html#a564252001be679600b20ca9ed9920f6a" title="constructs a STL-styled parallel-for task ">tf::Taskflow::for_each(B&& first, E&& last, C&& callable)</a> creates a task that spawns a subflow (see <aclass="el" href="chapter3.html">C3: Dynamic Tasking</a>) that applies the callable to the object obtained by dereferencing every iterator in the range <code>[first, last)</code>. It is user's responsibility for ensuring the range is valid within the execution of the parallel-for task. Iterators must have the post-increment operator ++ defined. This version of parallel-for applies to all iterable STL containers.</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/container/vector.html">std::vector<int></a> vec = {1, 2, 3, 4, 5};</div><divclass="line">taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a564252001be679600b20ca9ed9920f6a">for_each</a>(vec.begin(), vec.end(), [](<spanclass="keywordtype">int</span> i){ </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/io/basic_ostream.html">std::cout</a> << <spanclass="stringliteral">"parallel for on item "</span> << i << <spanclass="charliteral">'\n'</span>; </div><divclass="line">});</div><divclass="line"></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/list.html">std::list<std::string></a> list = {<spanclass="stringliteral">"hi"</span>, <spanclass="stringliteral">"from"</span>, <spanclass="stringliteral">"t"</span>, <spanclass="stringliteral">"a"</span>, <spanclass="stringliteral">"s"</span>, <spanclass="stringliteral">"k"</span>, <spanclass="stringliteral">"f"</span>, <spanclass="stringliteral">"low"</span>};</div><divclass="line">taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a564252001be679600b20ca9ed9920f6a">for_each</a>(list.begin(), list.end(), [](<spanclass="keyword">const</span><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){ </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/io/basic_ostream.html">std::cout</a> << <spanclass="stringliteral">"parallel for on item "</span> << str << <spanclass="charliteral">'\n'</span>; </div><divclass="line">});</div></div><!-- fragment --><p>Similar to index-based parallel-for, the iterator types are templates to enable users to leverage the property of stateful closure. For example:</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/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.resize(1000);</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> pf = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a564252001be679600b20ca9ed9920f6a">for_each</a>(std::ref(first), std::ref(last), [&](<spanclass="keywordtype">int</span> i) {</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/io/basic_ostream.html">std::cout</a> << <spanclass="stringliteral">"parallel iteration on item "</span> << i << <spanclass="charliteral">'\n'</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 pf = taskflow.for_each(first, last, [&](int i) {</span></div><divclass="line"><spanclass="comment">// std::cout << "parallel iteration on item " << i << '\n';</span></div><divclass="line"><spanclass="comment">// });</span></div><divclass="line"></div><divclass="line">init.<aclass="code" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(pf);</div></div><!-- fragment --><p>When <code>init</code> finishes, the parallel-for task <code>pf</code> will see <code>first</code> pointing to the beginning of <code>vec</code> and <code>last</code> pointing to the end of <code>vec</code> and performs parallel iterations over the 1000 items. The two tasks form an end-to-end task graph where the parameters of parallel-for are computed on the fly.</p>
<p>At runtime, the parallel-for task automatically partitions the range into <em>chunks</em> and assign each chunk a task in the spawned subflow. Inspired by the <ahref="http://jakascorner.com/blog/2016/06/omp-for-scheduling.html">scheduling algorithms of OpenMP</a>, we support three partition algorithms, <em>static</em> partition, <em>dynamic</em> partition, and <em>guided</em> partition.</p>
<divclass="fragment"><divclass="line"><spanclass="keywordtype">size_t</span> first = 0, last = 1000, step = 1, chunk_size = 100;</div><divclass="line"></div><divclass="line"><spanclass="keyword">auto</span> task1 = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a71f204c670ca5857e5527d6000ba73c4">for_each_index_static</a> (first, last, step, [](<spanclass="keyword">auto</span>){ }, chunk_size)</div><divclass="line"><spanclass="keyword">auto</span> task1 = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a930d0343226874d1d40b9e230cccdd52">for_each_index_dynamic</a>(first, last, step, [](<spanclass="keyword">auto</span>){ }, chunk_size)</div><divclass="line"><spanclass="keyword">auto</span> task1 = taskflow.<aclass="code" href="classtf_1_1FlowBuilder.html#a942449afd25e656b9e14cb526cbd8ad2">for_each_index_guided</a> (first, last, step, [](<spanclass="keyword">auto</span>){ }, chunk_size)</div><divclass="line"></div><divclass="line"><spanclass="comment">// same syntax for the iterator-based parallel-for</span></div><divclass="line"><spanclass="comment">// ...</span></div></div><!-- fragment --><p>Each of these methods takes an additional unsigned argument of the chunk size.</p>
<ul>
<li>The static partition algorithm divides the workload into <em>equal-size</em> chunks. If the given chunk size is zero, it distributes the workload evenly across workers. Static partition has the lowest scheduling overhead but the least optimal workload distribution (i.e., load balancing). </li>
<li>The dynamic partition algorithm dynamically assigns chunks to threads using a data dispatching loop. Dynamic partition has the highest scheduling overhead but the optimal workload distribution in particular when the chunk size equals one. </li>
<li>The guided partition algorithm (1) starts with big chunks proportional to the number of unassigned iterations divided by the number of workers and (2) then makes them progressively smaller until the chunk size reaches at the given size. Guided partition attempts to seek a balance between scheduling overhead and workload distribution.</li>
</ul>
<p>The picture below illustrates the three partition algorithms.</p>
<p>By default, <aclass="el" href="classtf_1_1FlowBuilder.html#ab8417b211b18bb1e0f45a049331f084d" title="constructs an index-based parallel-for task ">tf::Taskflow::for_each_index</a> and <aclass="el" href="classtf_1_1FlowBuilder.html#a564252001be679600b20ca9ed9920f6a" title="constructs a STL-styled parallel-for task ">tf::Taskflow::for_each</a> adopt the <em>guided</em> partition algorithm with chunk size equal to one. In practice, guided partition produces decent performance in many applications and is the default of Taskflow's parallel-for algorithm. However, depending on the workload requirement, users may explicitly call for static, dynamic, or guided partition algorithms with a specified chunk size. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<divid="nav-path" class="navpath"><!-- id is needed for treeview function! -->