<para>Taskflow provides template function that constructs a task to perform parallel iterations over a range of items.</para><sect1id="ParallelIterations_1A1IndexBasedParallelFor">
<title>Index-based Parallel Iterations</title>
<para>Index-based parallel-for performs parallel iterations over a range <computeroutput>[first, last)</computeroutput> with the given <computeroutput>step</computeroutput> size. The task created by <refrefid="classtf_1_1FlowBuilder_1ab8417b211b18bb1e0f45a049331f084d"kindref="member">tf::Taskflow::for_each_index(B&& first, E&& last, S&& step, C&& callable)</ref> represents parallel execution of the following loop:</para><para><programlistingfilename=".cpp"><codeline><highlightclass="comment">//<sp/>positive<sp/>step</highlight><highlightclass="normal"></highlight></codeline>
</programlisting></para><para>We support only integer-based range. The range can go positive or negative direction.</para><para><programlistingfilename=".cpp"><codeline><highlightclass="normal">taskflow.<refrefid="classtf_1_1FlowBuilder_1ab8417b211b18bb1e0f45a049331f084d"kindref="member">for_each_index</ref>(0,<sp/>100,<sp/><sp/>2,<sp/>[](</highlight><highlightclass="keywordtype">int</highlight><highlightclass="normal"><sp/>i)<sp/>{<sp/>});<sp/><sp/></highlight><highlightclass="comment">//<sp/>50<sp/>loops<sp/>with<sp/>a<sp/>+<sp/>step</highlight><highlightclass="normal"></highlight></codeline>
</programlisting></para><para>Notice that either positive or negative direction is defined in terms of the range, <computeroutput>[first, last)</computeroutput>, where <computeroutput>end</computeroutput> 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:</para><para><dotfilename="/home/twhuang/Code/taskflow/doxygen/images/parallel_for_1.dot"></dotfile>
</para><para>The index types, <computeroutput>B</computeroutput>, <computeroutput>E</computeroutput>, and <computeroutput>S</computeroutput>, are templates to preserve the variable types and their underlying types must be of the same <emphasis>integral</emphasis> type (e.g., <computeroutput>int</computeroutput>, <computeroutput>size_t</computeroutput>, <computeroutput>unsigned</computeroutput>). By default, <ref refid="classtf_1_1FlowBuilder_1ab8417b211b18bb1e0f45a049331f084d" kindref="member">tf::Taskflow::for_each_index</ref> creates a task that spawns a subflow (see <ref refid="DynamicTasking" kindref="compound">Dynamic Tasking</ref>) 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:</para><para><programlisting filename=".cpp"><codeline><highlight class="keywordtype">int</highlight><highlight class="normal">*<sp/>vec;</highlight></codeline>
</programlisting></para><para>When <computeroutput>init</computeroutput> finishes, the parallel-for task <computeroutput>pf</computeroutput> will see <computeroutput>first</computeroutput> as 0 and <computeroutput>last</computeroutput> 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.</para></sect1>
<para>Iterator-based parallel-for performs parallel iterations over a range specified by two <ulinkurl="https://en.cppreference.com/w/cpp/iterator/iterator">STL-styled iterators</ulink>, <computeroutput>first</computeroutput> and <computeroutput>last</computeroutput>. The task created by <refrefid="classtf_1_1FlowBuilder_1a564252001be679600b20ca9ed9920f6a"kindref="member">tf::Taskflow::for_each(B&& first, E&& last, C&& callable)</ref> represents a parallel execution of the following loop:</para><para><programlistingfilename=".cpp"><codeline><highlightclass="keywordflow">for</highlight><highlightclass="normal">(</highlight><highlightclass="keyword">auto</highlight><highlightclass="normal"><sp/>i=first;<sp/>i<last;<sp/>i++)<sp/>{</highlight></codeline>
</programlisting></para><para>By default, <ref refid="classtf_1_1FlowBuilder_1a564252001be679600b20ca9ed9920f6a" kindref="member">tf::Taskflow::for_each(B&& first, E&& last, C&& callable)</ref> creates a task that spawns a subflow (see <ref refid="DynamicTasking" kindref="compound">Dynamic Tasking</ref>) that applies the callable to the object obtained by dereferencing every iterator in the range <computeroutput>[first, last)</computeroutput>. 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.</para><para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/home/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>vec<sp/>=<sp/>{1,<sp/>2,<sp/>3,<sp/>4,<sp/>5};</highlight></codeline>
</programlisting></para><para>Similar to index-based parallel-for, the iterator types are templates to enable users to leverage the property of stateful closure. For example:</para><para><programlistingfilename=".cpp"><codeline><highlightclass="normal"><refrefid="cpp/container/vector"kindref="compound"external="/home/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>vec;</highlight></codeline>
</programlisting></para><para>When <computeroutput>init</computeroutput> finishes, the parallel-for task <computeroutput>pf</computeroutput> will see <computeroutput>first</computeroutput> pointing to the beginning of <computeroutput>vec</computeroutput> and <computeroutput>last</computeroutput> pointing to the end of <computeroutput>vec</computeroutput> 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. </para></sect1>