forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallelIterations.xml
More file actions
175 lines (175 loc) · 25.4 KB
/
Copy pathParallelIterations.xml
File metadata and controls
175 lines (175 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<doxygen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="compound.xsd" version="1.12.0" xml:lang="en-US">
<compounddef id="ParallelIterations" kind="page">
<compoundname>ParallelIterations</compoundname>
<title>Parallel Iterations</title>
<tableofcontents>
<tocsect>
<name>Include the Header</name>
<reference>ParallelIterations_1ParallelIterationsIncludeTheHeader</reference>
</tocsect>
<tocsect>
<name>Create an Index-based Parallel-Iteration Task</name>
<reference>ParallelIterations_1A1IndexBasedParallelFor</reference>
</tocsect>
<tocsect>
<name>Capture Indices by Reference</name>
<reference>ParallelIterations_1ParallelForEachCaptureIndicesByReference</reference>
</tocsect>
<tocsect>
<name>Create an Iterator-based Parallel-Iteration Task</name>
<reference>ParallelIterations_1A1IteratorBasedParallelFor</reference>
</tocsect>
<tocsect>
<name>Capture Iterators by Reference</name>
<reference>ParallelIterations_1ParallelForEachCaptureIteratorsByReference</reference>
</tocsect>
<tocsect>
<name>Configure a Partitioner</name>
<reference>ParallelIterations_1ParallelIterationsConfigureAPartitioner</reference>
</tocsect>
</tableofcontents>
<briefdescription>
</briefdescription>
<detaileddescription>
<para>Taskflow provides template functions for constructing tasks to perform parallel iterations over ranges of items.</para>
<sect1 id="ParallelIterations_1ParallelIterationsIncludeTheHeader">
<title>Include the Header</title><para>You need to include the header file, <computeroutput>taskflow/algorithm/for_each.hpp</computeroutput>, for using parallel-iteration algorithms.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="preprocessor">#include<sp/><taskflow/algorithm/for_each.hpp></highlight></codeline>
</programlisting></para>
</sect1>
<sect1 id="ParallelIterations_1A1IndexBasedParallelFor">
<title>Create an Index-based Parallel-Iteration Task</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 <ref refid="classtf_1_1FlowBuilder_1a3b132bd902331a11b04b4ad66cf8bf77" kindref="member">tf::Taskflow::for_each_index(B first, E last, S step, C callable, P part)</ref> represents parallel execution of the following loop:</para>
<para><programlisting filename=".cpp"><codeline><highlight class="comment">//<sp/>positive<sp/>step</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordflow">for</highlight><highlight class="normal">(</highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>i=first;<sp/>i<last;<sp/>i+=step)<sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>callable(i);</highlight></codeline>
<codeline><highlight class="normal">}</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>negative<sp/>step</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordflow">for</highlight><highlight class="normal">(</highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>i=first;<sp/>i>last;<sp/>i+=step)<sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>callable(i);</highlight></codeline>
<codeline><highlight class="normal">}</highlight></codeline>
</programlisting></para>
<para>We support only integer-based range. The range can go positive or negative direction.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal">taskflow.for_each_index(0,<sp/>100,<sp/><sp/>2,<sp/>[](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i)<sp/>{<sp/>});<sp/><sp/></highlight><highlight class="comment">//<sp/>50<sp/>loops<sp/>with<sp/>a<sp/>+<sp/>step</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">taskflow.for_each_index(100,<sp/>0,<sp/>-2,<sp/>[](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i)<sp/>{<sp/>});<sp/><sp/></highlight><highlight class="comment">//<sp/>50<sp/>loops<sp/>with<sp/>a<sp/>-<sp/>step</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><dotfile name="parallel_for_1.dot"></dotfile>
</para>
<para>Instead of explicitly specifying the index range and the callable for each index invocation, the overload <ref refid="classtf_1_1FlowBuilder_1a2582a216d54dacca2b7022ea7e89452a" kindref="member">tf::Taskflow::for_each_by_index(R range, C callable, P part)</ref> provides you with a more flexible way to iterate over subranges of indices. This overload uses <ref refid="classtf_1_1IndexRange" kindref="compound">tf::IndexRange</ref> to partition the range into subranges, allowing finer control over how each subrange is processed. For instance, the code below does the same thing using two different approaches:</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>data1(100),<sp/>data2(100);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>Approach<sp/>1:<sp/>initialize<sp/>data1<sp/>using<sp/>explicit<sp/>index<sp/>range</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">taskflow.for_each_index(0,<sp/>100,<sp/>1,<sp/>[&](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i){<sp/>data1[i]<sp/>=<sp/>10;<sp/>});</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>Approach<sp/>2:<sp/>initialize<sp/>data2<sp/>using<sp/>tf::IndexRange</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1IndexRange" kindref="compound">tf::IndexRange<int></ref><sp/>range(0,<sp/>100,<sp/>1);</highlight></codeline>
<codeline><highlight class="normal">taskflow.for_each_by_index(range,<sp/>[&](<ref refid="classtf_1_1IndexRange" kindref="compound">tf::IndexRange<int></ref><sp/>subrange){</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">for</highlight><highlight class="normal">(</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i=subrange.<ref refid="classtf_1_1IndexRange_1a2b52381358ab392efa257e185a33d4af" kindref="member">begin</ref>();<sp/>i<subrange.<ref refid="classtf_1_1IndexRange_1a280096cb4056bc19b86da77d019434e4" kindref="member">end</ref>();<sp/>i+=subrange.<ref refid="classtf_1_1IndexRange_1aafd4f2d04614e550649cd9b7912e0bf1" kindref="member">step_size</ref>())<sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/><sp/><sp/>data2[i]<sp/>=<sp/>10;</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>}</highlight></codeline>
<codeline><highlight class="normal">});</highlight></codeline>
</programlisting></para>
<para>Both approaches produce the same result, but the second approach offers more flexibility in terms of how each partitioned subrange is iterated. This is particularly useful for applications that benefit from SIMD optimizations or other range-based processing strategies.</para>
</sect1>
<sect1 id="ParallelIterations_1ParallelForEachCaptureIndicesByReference">
<title>Capture Indices by Reference</title><para>You can pass indices by reference using <ulink url="https://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</ulink> to marshal parameter update between dependent tasks. This is especially useful when the range indices are unknown at the time of creating a for-each-index task, but is initialized from another task.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="keywordtype">int</highlight><highlight class="normal">*<sp/>vec;</highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>first,<sp/>last;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>init<sp/>=<sp/>taskflow.emplace([&](){</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>first<sp/>=<sp/>0;</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>last<sp/><sp/>=<sp/>1000;</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>vec<sp/>=<sp/></highlight><highlight class="keyword">new</highlight><highlight class="normal"><sp/></highlight><highlight class="keywordtype">int</highlight><highlight class="normal">[1000];<sp/><sp/></highlight></codeline>
<codeline><highlight class="normal">});</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>pf<sp/>=<sp/>taskflow.for_each_index(<ref refid="cpp/utility/functional/ref" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::ref</ref>(first),<sp/><ref refid="cpp/utility/functional/ref" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::ref</ref>(last),<sp/>1,<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[&]<sp/>(</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i)<sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"parallel<sp/>iteration<sp/>on<sp/>index<sp/>"</highlight><highlight class="normal"><sp/><<<sp/>vec[i]<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>}</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>wrong!<sp/>must<sp/>use<sp/>std::ref,<sp/>or<sp/>first<sp/>and<sp/>last<sp/>are<sp/>captured<sp/>by<sp/>copy</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>auto<sp/>pf<sp/>=<sp/>taskflow.for_each_index(first,<sp/>last,<sp/>1,<sp/>[&](int<sp/>i)<sp/>{</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/><sp/><sp/>std::cout<sp/><<<sp/>"parallel<sp/>iteration<sp/>on<sp/>index<sp/>"<sp/><<<sp/>vec[i]<sp/><<<sp/>'\n';</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>});</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">init.precede(pf);</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.</para>
</sect1>
<sect1 id="ParallelIterations_1A1IteratorBasedParallelFor">
<title>Create an Iterator-based Parallel-Iteration Task</title><para>Iterator-based parallel-for performs parallel iterations over a range specified by two <ulink url="https://en.cppreference.com/w/cpp/iterator/iterator">STL-styled iterators</ulink>, <computeroutput>first</computeroutput> and <computeroutput>last</computeroutput>. The task created by <ref refid="classtf_1_1FlowBuilder_1aae3edfa278baa75b08414e083c14c836" kindref="member">tf::Taskflow::for_each(B first, E last, C callable, P part)</ref> represents a parallel execution of the following loop:</para>
<para><programlisting filename=".cpp"><codeline><highlight class="keywordflow">for</highlight><highlight class="normal">(</highlight><highlight class="keyword">auto</highlight><highlight class="normal"><sp/>i=first;<sp/>i<last;<sp/>i++)<sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>callable(*i);</highlight></codeline>
<codeline><highlight class="normal">}</highlight></codeline>
</programlisting></para>
<para>tf::Taskflow::for_each(B first, E last, C callable, P&& part) simultaneously 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.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/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>
<codeline><highlight class="normal">taskflow.for_each(vec.begin(),<sp/>vec.end(),<sp/>[](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i){<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>std::cout<sp/><<<sp/></highlight><highlight class="stringliteral">"parallel<sp/>for<sp/>on<sp/>item<sp/>"</highlight><highlight class="normal"><sp/><<<sp/>i<sp/><<<sp/></highlight><highlight class="stringliteral">'\n'</highlight><highlight class="normal">;<sp/><sp/></highlight></codeline>
<codeline><highlight class="normal">});</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="cpp/container/list" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::list<std::string></ref><sp/>list<sp/>=<sp/>{</highlight><highlight class="stringliteral">"hi"</highlight><highlight class="normal">,<sp/></highlight><highlight class="stringliteral">"from"</highlight><highlight class="normal">,<sp/></highlight><highlight class="stringliteral">"t"</highlight><highlight class="normal">,<sp/></highlight><highlight class="stringliteral">"a"</highlight><highlight class="normal">,<sp/></highlight><highlight class="stringliteral">"s"</highlight><highlight class="normal">,<sp/></highlight><highlight class="stringliteral">"k"</highlight><highlight class="normal">,<sp/></highlight><highlight class="stringliteral">"f"</highlight><highlight class="normal">,<sp/></highlight><highlight class="stringliteral">"low"</highlight><highlight class="normal">};</highlight></codeline>
<codeline><highlight class="normal">taskflow.for_each(list.begin(),<sp/>list.end(),<sp/>[](</highlight><highlight class="keyword">const</highlight><highlight class="normal"><sp/><ref refid="cpp/string/basic_string" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::string</ref>&<sp/>str){<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>std::cout<sp/><<<sp/></highlight><highlight class="stringliteral">"parallel<sp/>for<sp/>on<sp/>item<sp/>"</highlight><highlight class="normal"><sp/><<<sp/>str<sp/><<<sp/></highlight><highlight class="stringliteral">'\n'</highlight><highlight class="normal">;<sp/><sp/></highlight></codeline>
<codeline><highlight class="normal">});</highlight></codeline>
</programlisting></para>
</sect1>
<sect1 id="ParallelIterations_1ParallelForEachCaptureIteratorsByReference">
<title>Capture Iterators by Reference</title><para>Similar to <ref refid="classtf_1_1FlowBuilder_1a3b132bd902331a11b04b4ad66cf8bf77" kindref="member">tf::Taskflow::for_each_index</ref>, iterators of <ref refid="classtf_1_1FlowBuilder_1aae3edfa278baa75b08414e083c14c836" kindref="member">tf::Taskflow::for_each</ref> are templated to allow capturing range parameters by reference, such that one task can set up the range before another task performs the parallel-for algorithm. For example:</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>vec;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int>::iterator</ref><sp/>first,<sp/>last;;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>init<sp/>=<sp/>taskflow.emplace([&](){</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>vec.resize(1000);</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>first<sp/>=<sp/>vec.begin();</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>last<sp/><sp/>=<sp/>vec.end();</highlight></codeline>
<codeline><highlight class="normal">});</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>pf<sp/>=<sp/>taskflow.for_each(<ref refid="cpp/utility/functional/ref" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::ref</ref>(first),<sp/><ref refid="cpp/utility/functional/ref" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::ref</ref>(last),<sp/>[&](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i)<sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/><ref refid="cpp/io/basic_ostream" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::cout</ref><sp/><<<sp/></highlight><highlight class="stringliteral">"parallel<sp/>iteration<sp/>on<sp/>item<sp/>"</highlight><highlight class="normal"><sp/><<<sp/>i<sp/><<<sp/></highlight><highlight class="charliteral">'\n'</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal">});</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>wrong!<sp/>must<sp/>use<sp/>std::ref,<sp/>or<sp/>first<sp/>and<sp/>last<sp/>are<sp/>captured<sp/>by<sp/>copy</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>tf::Task<sp/>pf<sp/>=<sp/>taskflow.for_each(first,<sp/>last,<sp/>[&](int<sp/>i)<sp/>{</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/><sp/><sp/>std::cout<sp/><<<sp/>"parallel<sp/>iteration<sp/>on<sp/>item<sp/>"<sp/><<<sp/>i<sp/><<<sp/>'\n';</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>});</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(pf);</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>
<sect1 id="ParallelIterations_1ParallelIterationsConfigureAPartitioner">
<title>Configure a Partitioner</title><para>You can configure a partitioner for parallel-iteration tasks to run with different scheduling methods, such as guided partitioning, dynamic partitioning, and static partitioning. The following example creates two parallel-iteration tasks using two different partitioners, one with the static partitioning algorithm and another one with the guided partitioning algorithm:</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>vec(1024,<sp/>0);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>create<sp/>two<sp/>partitioners<sp/>with<sp/>a<sp/>chunk<sp/>size<sp/>of<sp/>10</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1StaticPartitioner" kindref="compound">tf::StaticPartitioner</ref><sp/>static_partitioner(10);</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1GuidedPartitioner" kindref="compound">tf::GuidedPartitioner</ref><sp/>guided_partitioner(10);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>create<sp/>a<sp/>parallel-iteration<sp/>task<sp/>with<sp/>static<sp/>partitioner</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">taskflow.for_each(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>vec.begin(),<sp/>vec.end(),<sp/>[&](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i)<sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/><sp/><sp/>std::cout<sp/><<<sp/></highlight><highlight class="stringliteral">"parallel<sp/>iteration<sp/>on<sp/>item<sp/>"</highlight><highlight class="normal"><sp/><<<sp/>i<sp/><<<sp/></highlight><highlight class="stringliteral">'\n'</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>static_partitioner</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>create<sp/>a<sp/>parallel-iteration<sp/>task<sp/>with<sp/>guided<sp/>partitioner</highlight><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">taskflow.for_each(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>vec.begin(),<sp/>vec.end(),<sp/>[&](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i)<sp/>{</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/><sp/><sp/>std::cout<sp/><<<sp/></highlight><highlight class="stringliteral">"parallel<sp/>iteration<sp/>on<sp/>item<sp/>"</highlight><highlight class="normal"><sp/><<<sp/>i<sp/><<<sp/></highlight><highlight class="stringliteral">'\n'</highlight><highlight class="normal">;</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>},</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>guided_partitioner</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
</programlisting></para>
<para><simplesect kind="attention"><para>By default, parallel-iteration tasks use <ref refid="namespacetf_1ace2c5adcd5039483eebb6dbdbb6f33e3" kindref="member">tf::DefaultPartitioner</ref> if no partitioner is specified. </para>
</simplesect>
</para>
</sect1>
</detaileddescription>
<location file="doxygen/algorithms/for_each.dox"/>
</compounddef>
</doxygen>