-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathParallelFind.html
More file actions
402 lines (400 loc) · 29.8 KB
/
Copy pathParallelFind.html
File metadata and controls
402 lines (400 loc) · 29.8 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<!-- HTML header for doxygen 1.13.1-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.13.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Taskflow: A General-purpose Task-parallel Programming System: Parallel Find</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="custom.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="taskflow_logo.png"/></td>
<td id="projectalign">
<div id="projectname"><a href="https://github.com/taskflow/taskflow" style="color:inherit; text-decoration:none;">Taskflow: A General-purpose Task-parallel Programming System</a>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.13.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(function() {
initMenu('',true,false,'search.php','Search',true);
$(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(function(){initNavTree('ParallelFind.html',''); initResizable(true); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div><div class="header">
<div class="headertitle"><div class="title">Parallel Find</div></div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul>
<li class="level1">
<a href="#ParallelFindIncludeTheHeader">Include the Header</a>
</li>
<li class="level1">
<a href="#ParallelFindWhatIsFind">What is a Find Algorithm?</a>
</li>
<li class="level1">
<a href="#ParallelFindIf">Create a Parallel Find-If Task</a>
<ul>
<li class="level2">
<a href="#ParallelFindIfCaptureByReference">Capture Iterators by Reference</a>
</li>
</ul>
</li>
<li class="level1">
<a href="#ParallelFindIfNot">Create a Parallel Find-If-Not Task</a>
<ul>
<li class="level2">
<a href="#ParallelFindIfNotCaptureByReference">Capture Iterators by Reference</a>
</li>
</ul>
</li>
<li class="level1">
<a href="#ParallelFindMinElement">Find the Minimum Element</a>
<ul>
<li class="level2">
<a href="#ParallelFindMinElementCaptureByReference">Capture Iterators by Reference</a>
</li>
</ul>
</li>
<li class="level1">
<a href="#ParallelFindMaxElement">Find the Maximum Element</a>
<ul>
<li class="level2">
<a href="#ParallelFindMaxElementCaptureByReference">Capture Iterators by Reference</a>
</li>
</ul>
</li>
<li class="level1">
<a href="#ParallelFindConfigureAPartitioner">Configure a Partitioner</a>
</li>
</ul>
</div>
<div class="textblock"><p>Taskflow provides template functions for constructing tasks to perform parallel find over a range of items.</p>
<h1><a class="anchor" id="ParallelFindIncludeTheHeader"></a>
Include the Header</h1>
<p>You need to include the header file, <code>taskflow/algorithm/find.hpp</code>, for using parallel-find algorithms.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include <taskflow/algorithm/find.hpp></span></div>
</div><!-- fragment --><h1><a class="anchor" id="ParallelFindWhatIsFind"></a>
What is a Find Algorithm?</h1>
<p>A find algorithm searches a range <code>[first, last)</code> for an element that satisfies a given criterion and returns an iterator to the <em>first</em> such element, or <code>last</code> if no such element exists.</p>
<p>What distinguishes a parallel find from a parallel for-each is its <em>short-circuit</em> semantics: as soon as any worker finds a qualifying element, the remaining workers abandon their portions of the range. This means the algorithm does not always process every element — it stops as early as possible once a result is found.</p>
<p>Taskflow provides the following parallel-find algorithms:</p>
<ul>
<li><a class="el" href="classtf_1_1FlowBuilder.html#a46a96f5889e6ac87b1ff8d6313b5f471" title="constructs a task to perform STL-styled find-if algorithm">tf::Taskflow::find_if</a> finds the first element for which a predicate returns <code>true</code> </li>
<li><a class="el" href="classtf_1_1FlowBuilder.html#a95fa2719fa7bbe7d171cf474ddb06726" title="constructs a task to perform STL-styled find-if-not algorithm">tf::Taskflow::find_if_not</a> finds the first element for which a predicate returns <code>false</code> </li>
<li><a class="el" href="classtf_1_1FlowBuilder.html#a6bf43eeaa81900084a472be1d36d46a6" title="constructs a task to perform STL-styled min-element algorithm">tf::Taskflow::min_element</a> finds the element with the smallest value under a comparator</li>
<li><a class="el" href="classtf_1_1FlowBuilder.html#a6be5d7f053a868647c3b9e0d9cdf6b68" title="constructs a task to perform STL-styled max-element algorithm">tf::Taskflow::max_element</a> finds the element with the largest value under a comparator</li>
</ul>
<p>All four algorithms store the result iterator in a variable passed by reference, so the result is available after the task completes.</p>
<h1><a class="anchor" id="ParallelFindIf"></a>
Create a Parallel Find-If Task</h1>
<p><a class="el" href="classtf_1_1FlowBuilder.html#a46a96f5889e6ac87b1ff8d6313b5f471" title="constructs a task to perform STL-styled find-if algorithm">tf::Taskflow::find_if(B first, E last, T& result, UOP predicate, P part)</a> performs a parallel search over the range <code>[first, last)</code> and stores in <code>result</code> an iterator to the first element for which <code>predicate</code> returns <code>true</code>, or <code>last</code> if no such element exists. It resembles a parallel implementation of the following loop:</p>
<div class="fragment"><div class="line"><span class="keyword">template</span> <<span class="keyword">typename</span> InputIt, <span class="keyword">typename</span> UnaryPredicate></div>
<div class="line">InputIt find_if(InputIt first, InputIt last, UnaryPredicate predicate) {</div>
<div class="line"> <span class="keywordflow">for</span> (; first != last; ++first) {</div>
<div class="line"> <span class="keywordflow">if</span> (predicate(*first)) {</div>
<div class="line"> <span class="keywordflow">return</span> first;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">return</span> last;</div>
<div class="line">}</div>
</div><!-- fragment --><p>The example below creates a task to find the element equal to <code>22</code> in a range of 10 integers:</p>
<div class="fragment"><div class="line">std::vector<int> input = {1, 9, 22, 3, -6, 13, 12, 0, 9, 11};</div>
<div class="line">std::vector<int>::iterator result;</div>
<div class="line"> </div>
<div class="line">taskflow.find_if(</div>
<div class="line"> input.begin(), input.end(), result, [](<span class="keywordtype">int</span> i) { return i == 22; }</div>
<div class="line">);</div>
<div class="line">executor.run(taskflow).wait();</div>
<div class="line"> </div>
<div class="line">assert(*result == 22);</div>
</div><!-- fragment --><h2><a class="anchor" id="ParallelFindIfCaptureByReference"></a>
Capture Iterators by Reference</h2>
<p>You can pass iterators by reference using <a href="https://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</a> to marshal parameter updates between dependent tasks. This is useful when the range is not known at task-graph construction time but is initialized by an upstream task.</p>
<div class="fragment"><div class="line">std::vector<int> input;</div>
<div class="line">std::vector<int>::iterator result, first, last;</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> init = taskflow.emplace([&]() {</div>
<div class="line"> input = {1, 9, 22, 3, -6, 13, 12, 0, 9, 11};</div>
<div class="line"> first = input.begin();</div>
<div class="line"> last = input.end();</div>
<div class="line">});</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> task = taskflow.find_if(</div>
<div class="line"> std::ref(first), std::ref(last), result, [](<span class="keywordtype">int</span> i) { <span class="keywordflow">return</span> i == 22; }</div>
<div class="line">);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// wrong! first and last are captured by copy at construction time</span></div>
<div class="line"><span class="comment">// tf::Task task = taskflow.find_if(first, last, result, [](int i){ return i == 22; });</span></div>
<div class="line"> </div>
<div class="line">init.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(task);</div>
<div class="line">executor.run(taskflow).wait();</div>
<div class="line"> </div>
<div class="line">assert(*result == 22);</div>
<div class="ttc" id="aclasstf_1_1Task_html"><div class="ttname"><a href="classtf_1_1Task.html">tf::Task</a></div><div class="ttdoc">class to create a task handle over a taskflow node</div><div class="ttdef"><b>Definition</b> task.hpp:569</div></div>
<div class="ttc" id="aclasstf_1_1Task_html_a8c78c453295a553c1c016e4062da8588"><div class="ttname"><a href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">tf::Task::precede</a></div><div class="ttdeci">Task & precede(Ts &&... tasks)</div><div class="ttdoc">adds precedence links from this to other tasks</div><div class="ttdef"><b>Definition</b> task.hpp:1258</div></div>
</div><!-- fragment --><p>When <code>init</code> finishes, <code>first</code> and <code>last</code> point to the initialized data range of <code>input</code>, and the find-if task searches over all 10 elements.</p>
<h1><a class="anchor" id="ParallelFindIfNot"></a>
Create a Parallel Find-If-Not Task</h1>
<p><a class="el" href="classtf_1_1FlowBuilder.html#a95fa2719fa7bbe7d171cf474ddb06726" title="constructs a task to perform STL-styled find-if-not algorithm">tf::Taskflow::find_if_not(B first, E last, T& result, UOP predicate, P part)</a> performs a parallel search over the range <code>[first, last)</code> and stores in <code>result</code> an iterator to the first element for which <code>predicate</code> returns <code>false</code>, or <code>last</code> if no such element exists. It resembles a parallel implementation of the following loop:</p>
<div class="fragment"><div class="line"><span class="keyword">template</span> <<span class="keyword">typename</span> InputIt, <span class="keyword">typename</span> UnaryPredicate></div>
<div class="line">InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate predicate) {</div>
<div class="line"> <span class="keywordflow">for</span> (; first != last; ++first) {</div>
<div class="line"> <span class="keywordflow">if</span> (!predicate(*first)) {</div>
<div class="line"> <span class="keywordflow">return</span> first;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">return</span> last;</div>
<div class="line">}</div>
</div><!-- fragment --><p>The example below creates a task to find the first element that is <em>not</em> equal to <code>1</code> in a range where only one element differs:</p>
<div class="fragment"><div class="line">std::vector<int> input = {1, 1, 22, 1, 1, 1, 1, 1, 1, 1};</div>
<div class="line">std::vector<int>::iterator result;</div>
<div class="line"> </div>
<div class="line">taskflow.find_if_not(</div>
<div class="line"> input.begin(), input.end(), result, [](<span class="keywordtype">int</span> i) { return i == 1; }</div>
<div class="line">);</div>
<div class="line">executor.run(taskflow).wait();</div>
<div class="line"> </div>
<div class="line">assert(*result == 22);</div>
</div><!-- fragment --><h2><a class="anchor" id="ParallelFindIfNotCaptureByReference"></a>
Capture Iterators by Reference</h2>
<p>As with <a class="el" href="classtf_1_1FlowBuilder.html#a46a96f5889e6ac87b1ff8d6313b5f471" title="constructs a task to perform STL-styled find-if algorithm">tf::Taskflow::find_if</a>, iterators can be passed by reference using <a href="https://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</a> so that an upstream task can set up the range before the parallel find-if-not runs.</p>
<div class="fragment"><div class="line">std::vector<int> input;</div>
<div class="line">std::vector<int>::iterator result, first, last;</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> init = taskflow.emplace([&]() {</div>
<div class="line"> input = {1, 1, 22, 1, 1, 1, 1, 1, 1, 1};</div>
<div class="line"> first = input.begin();</div>
<div class="line"> last = input.end();</div>
<div class="line">});</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> task = taskflow.find_if_not(</div>
<div class="line"> std::ref(first), std::ref(last), result, [](<span class="keywordtype">int</span> i) { <span class="keywordflow">return</span> i == 1; }</div>
<div class="line">);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// wrong! first and last are captured by copy at construction time</span></div>
<div class="line"><span class="comment">// tf::Task task = taskflow.find_if_not(first, last, result, [](int i){ return i == 1; });</span></div>
<div class="line"> </div>
<div class="line">init.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(task);</div>
<div class="line">executor.run(taskflow).wait();</div>
<div class="line"> </div>
<div class="line">assert(*result == 22);</div>
</div><!-- fragment --><h1><a class="anchor" id="ParallelFindMinElement"></a>
Find the Minimum Element</h1>
<p><a class="el" href="classtf_1_1FlowBuilder.html#a6bf43eeaa81900084a472be1d36d46a6" title="constructs a task to perform STL-styled min-element algorithm">tf::Taskflow::min_element(B first, E last, T& result, C comp, P part)</a> performs a parallel search over the range <code>[first, last)</code> and stores in <code>result</code> an iterator to the element with the smallest value under the comparator <code>comp</code>. It resembles a parallel implementation of the following loop:</p>
<div class="fragment"><div class="line"><span class="keyword">template</span> <<span class="keyword">typename</span> InputIt, <span class="keyword">typename</span> Compare></div>
<div class="line">InputIt min_element(InputIt first, InputIt last, Compare comp) {</div>
<div class="line"> <span class="keywordflow">if</span> (first == last) <span class="keywordflow">return</span> last;</div>
<div class="line"> InputIt smallest = first;</div>
<div class="line"> <span class="keywordflow">for</span> (++first; first != last; ++first) {</div>
<div class="line"> <span class="keywordflow">if</span> (comp(*first, *smallest)) {</div>
<div class="line"> smallest = first;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">return</span> smallest;</div>
<div class="line">}</div>
</div><!-- fragment --><p>The example below finds the smallest element in a range of 10 integers:</p>
<div class="fragment"><div class="line">std::vector<int> input = {1, 1, 1, 1, 1, -1, 1, 1, 1, 1};</div>
<div class="line">std::vector<int>::iterator result;</div>
<div class="line"> </div>
<div class="line">taskflow.min_element(</div>
<div class="line"> input.begin(), input.end(), std::less<int>(), result</div>
<div class="line">);</div>
<div class="line">executor.run(taskflow).wait();</div>
<div class="line"> </div>
<div class="line">assert(*result == -1);</div>
</div><!-- fragment --><h2><a class="anchor" id="ParallelFindMinElementCaptureByReference"></a>
Capture Iterators by Reference</h2>
<p>You can pass iterators by reference using <a href="https://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</a> so that an upstream task can set up the range before the parallel min-element search runs.</p>
<div class="fragment"><div class="line">std::vector<int> input;</div>
<div class="line">std::vector<int>::iterator result, first, last;</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> init = taskflow.emplace([&]() {</div>
<div class="line"> input = {1, 1, 1, 1, 1, -1, 1, 1, 1, 1};</div>
<div class="line"> first = input.begin();</div>
<div class="line"> last = input.end();</div>
<div class="line">});</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> task = taskflow.min_element(</div>
<div class="line"> std::ref(first), std::ref(last), std::less<int>(), result</div>
<div class="line">);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// wrong! first and last are captured by copy at construction time</span></div>
<div class="line"><span class="comment">// tf::Task task = taskflow.min_element(first, last, std::less<int>(), result);</span></div>
<div class="line"> </div>
<div class="line">init.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(task);</div>
<div class="line">executor.run(taskflow).wait();</div>
<div class="line"> </div>
<div class="line">assert(*result == -1);</div>
</div><!-- fragment --><h1><a class="anchor" id="ParallelFindMaxElement"></a>
Find the Maximum Element</h1>
<p><a class="el" href="classtf_1_1FlowBuilder.html#a6be5d7f053a868647c3b9e0d9cdf6b68" title="constructs a task to perform STL-styled max-element algorithm">tf::Taskflow::max_element(B first, E last, T& result, C comp, P part)</a> performs a parallel search over the range <code>[first, last)</code> and stores in <code>result</code> an iterator to the element with the largest value under the comparator <code>comp</code>. It resembles a parallel implementation of the following loop:</p>
<div class="fragment"><div class="line"><span class="keyword">template</span> <<span class="keyword">typename</span> InputIt, <span class="keyword">typename</span> Compare></div>
<div class="line">InputIt max_element(InputIt first, InputIt last, Compare comp) {</div>
<div class="line"> <span class="keywordflow">if</span> (first == last) <span class="keywordflow">return</span> last;</div>
<div class="line"> InputIt largest = first;</div>
<div class="line"> <span class="keywordflow">for</span> (++first; first != last; ++first) {</div>
<div class="line"> <span class="keywordflow">if</span> (comp(*largest, *first)) {</div>
<div class="line"> largest = first;</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">return</span> largest;</div>
<div class="line">}</div>
</div><!-- fragment --><p>The example below finds the largest element in a range of 10 integers:</p>
<div class="fragment"><div class="line">std::vector<int> input = {1, 1, 1, 1, 1, 2, 1, 1, 1, 1};</div>
<div class="line">std::vector<int>::iterator result;</div>
<div class="line"> </div>
<div class="line">taskflow.max_element(</div>
<div class="line"> input.begin(), input.end(), std::less<int>(), result</div>
<div class="line">);</div>
<div class="line">executor.run(taskflow).wait();</div>
<div class="line"> </div>
<div class="line">assert(*result == 2);</div>
</div><!-- fragment --><dl class="section note"><dt>Note</dt><dd><a class="el" href="classtf_1_1FlowBuilder.html#a6be5d7f053a868647c3b9e0d9cdf6b68" title="constructs a task to perform STL-styled max-element algorithm">tf::Taskflow::max_element</a> uses <code>std::less</code> as its comparator, which may seem counterintuitive. The comparator <code>comp</code> defines an ordering such that <code>comp(a, b)</code> returns <code>true</code> if <code>a</code> comes <em>before</em> <code>b</code>. For finding the maximum, we want the element that nothing else comes after, so <code>std::less</code> naturally puts the largest element last — and <code>max_element</code> returns it. Refer to <a href="https://en.cppreference.com/w/cpp/algorithm/max_element">std::max_element</a> for further details.</dd></dl>
<h2><a class="anchor" id="ParallelFindMaxElementCaptureByReference"></a>
Capture Iterators by Reference</h2>
<p>You can pass iterators by reference using <a href="https://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</a> so that an upstream task can set up the range before the parallel max-element search runs.</p>
<div class="fragment"><div class="line">std::vector<int> input;</div>
<div class="line">std::vector<int>::iterator result, first, last;</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> init = taskflow.emplace([&]() {</div>
<div class="line"> input = {1, 1, 1, 1, 1, 2, 1, 1, 1, 1};</div>
<div class="line"> first = input.begin();</div>
<div class="line"> last = input.end();</div>
<div class="line">});</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="classtf_1_1Task.html">tf::Task</a> task = taskflow.max_element(</div>
<div class="line"> std::ref(first), std::ref(last), std::less<int>(), result</div>
<div class="line">);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// wrong! first and last are captured by copy at construction time</span></div>
<div class="line"><span class="comment">// tf::Task task = taskflow.max_element(first, last, std::less<int>(), result);</span></div>
<div class="line"> </div>
<div class="line">init.<a class="code hl_function" href="classtf_1_1Task.html#a8c78c453295a553c1c016e4062da8588">precede</a>(task);</div>
<div class="line">executor.run(taskflow).wait();</div>
<div class="line"> </div>
<div class="line">assert(*result == 2);</div>
</div><!-- fragment --><h1><a class="anchor" id="ParallelFindConfigureAPartitioner"></a>
Configure a Partitioner</h1>
<p>A partitioner controls how the iteration space is divided among workers. Taskflow provides four partitioners, each suited to different workload characteristics:</p>
<ul>
<li><a class="el" href="classtf_1_1StaticPartitioner.html" title="class to construct a static partitioner for scheduling parallel algorithms">tf::StaticPartitioner</a> divides the range into equal-sized chunks ahead of execution and assigns them to workers in order. It has the lowest scheduling overhead and works best when the predicate or comparator costs the same for every element.</li>
<li><a class="el" href="classtf_1_1DynamicPartitioner.html" title="class to create a dynamic partitioner for scheduling parallel algorithms">tf::DynamicPartitioner</a> distributes fixed-sized chunks to workers on demand as they become available. It adapts well to workloads where element evaluation cost varies, at the expense of slightly higher coordination overhead.</li>
<li><a class="el" href="classtf_1_1GuidedPartitioner.html" title="class to create a guided partitioner for scheduling parallel algorithms">tf::GuidedPartitioner</a> distributes chunks whose size decreases adaptively as work is consumed — large chunks early to reduce overhead, smaller chunks late to balance the tail. This is the default partitioner and delivers stable, near-optimal performance across a wide range of workloads.</li>
<li><a class="el" href="classtf_1_1RandomPartitioner.html" title="class to construct a random partitioner for scheduling parallel algorithms">tf::RandomPartitioner</a> distributes chunks of randomly sampled sizes, which can help avoid systematic load imbalances caused by data-dependent cost patterns.</li>
</ul>
<p>The following example creates two parallel find-if tasks using different partitioners:</p>
<div class="fragment"><div class="line">std::vector<int> input(1024, -1);</div>
<div class="line">std::vector<int>::iterator result;</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="classtf_1_1StaticPartitioner.html">tf::StaticPartitioner</a> static_partitioner(0); <span class="comment">// chunk size auto-determined</span></div>
<div class="line"><a class="code hl_class" href="classtf_1_1GuidedPartitioner.html">tf::GuidedPartitioner</a> guided_partitioner(0); <span class="comment">// minimum chunk size auto-determined</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// parallel find-if with static partitioner</span></div>
<div class="line">taskflow.find_if(</div>
<div class="line"> input.begin(), input.end(), result,</div>
<div class="line"> [](<span class="keywordtype">int</span> i) { return i == -1; },</div>
<div class="line"> static_partitioner</div>
<div class="line">);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// parallel find-if with guided partitioner</span></div>
<div class="line">taskflow.find_if(</div>
<div class="line"> input.begin(), input.end(), result,</div>
<div class="line"> [](<span class="keywordtype">int</span> i) { return i == -1; },</div>
<div class="line"> guided_partitioner</div>
<div class="line">);</div>
<div class="ttc" id="aclasstf_1_1GuidedPartitioner_html"><div class="ttname"><a href="classtf_1_1GuidedPartitioner.html">tf::GuidedPartitioner</a></div><div class="ttdoc">class to create a guided partitioner for scheduling parallel algorithms</div><div class="ttdef"><b>Definition</b> partitioner.hpp:417</div></div>
<div class="ttc" id="aclasstf_1_1StaticPartitioner_html"><div class="ttname"><a href="classtf_1_1StaticPartitioner.html">tf::StaticPartitioner</a></div><div class="ttdoc">class to construct a static partitioner for scheduling parallel algorithms</div><div class="ttdef"><b>Definition</b> partitioner.hpp:262</div></div>
</div><!-- fragment --><p>As a rule of thumb, prefer <a class="el" href="classtf_1_1StaticPartitioner.html" title="class to construct a static partitioner for scheduling parallel algorithms">tf::StaticPartitioner</a> when the predicate or comparator costs the same for every element and <a class="el" href="classtf_1_1GuidedPartitioner.html" title="class to create a guided partitioner for scheduling parallel algorithms">tf::GuidedPartitioner</a> for irregular workloads where evaluation cost varies across elements. <a class="el" href="classtf_1_1DynamicPartitioner.html" title="class to create a dynamic partitioner for scheduling parallel algorithms">tf::DynamicPartitioner</a> is a good choice when chunks must be kept small and strictly equal in size.</p>
<dl class="section note"><dt>Note</dt><dd>By default, parallel-find tasks use <a class="el" href="namespacetf.html#ace2c5adcd5039483eebb6dbdbb6f33e3" title="default partitioner set to tf::GuidedPartitioner">tf::DefaultPartitioner</a> (currently <a class="el" href="classtf_1_1GuidedPartitioner.html" title="class to create a guided partitioner for scheduling parallel algorithms">tf::GuidedPartitioner</a>) if no partitioner is specified. </dd></dl>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.13.1-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="Algorithms.html">Taskflow Algorithms</a></li>
<li class="footer">
Maintained by <a href="https://tsung-wei-huang.github.io/">Dr. Tsung-Wei Huang</a>
—
Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.1
</li>
</ul>
</div>