forked from cplusplus/parallelism-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.html
925 lines (801 loc) · 37.4 KB
/
algorithms.html
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
<cxx-clause id="parallel.alg">
<h1>Parallel algorithms</h1>
<cxx-section id="parallel.alg.general">
<h1>In general</h1>
This clause describes components that C++ programs may use to perform operations on containers
and other sequences in parallel.
<cxx-section id="parallel.alg.general.user">
<h1>Requirements on user-provided function objects</h1>
<p>
Function objects passed into parallel algorithms as objects of type <code>BinaryPredicate</code>,
<code>Compare</code>, and <code>BinaryOperation</code> shall not directly or indirectly modify
objects via their arguments.
</p>
</cxx-section>
<cxx-section id="parallel.alg.general.exec">
<h1>Effect of execution policies on algorithm execution</h1>
<p>
Parallel algorithms have template parameters named <code>ExecutionPolicy</code> which describe
the manner in which the execution of these algorithms may be parallelized and the manner in
which they apply the element access functions.
</p>
<p>
The invocations of element access functions in parallel algorithms invoked with an execution
policy object of type <code>sequential_execution_policy</code> execute in sequential order in
the calling thread.
</p>
<p>
The invocations of element access functions in parallel algorithms invoked with an execution
policy object of type <code>parallel_execution_policy</code> are permitted to execute in an
unordered fashion in either the invoking thread or in a thread implicitly created by the library
to support parallel algorithm execution. Any such invocations executing in the same thread are
indeterminately sequenced with respect to each other.
<cxx-note>
It is the caller's responsibility to ensure correctness, for example that the invocation does
not introduce data races or deadlocks.
</cxx-note>
</p>
<cxx-example><pre>using namespace std::experimental::parallel;
int a[] = {0,1};
std::vector<int> v;
for_each(par, std::begin(a), std::end(a), [&](int i) {
v.push_back(i*2+1);
});
</pre>
The program above has a data race because of the unsynchronized access to the container
<code>v</code>.
</cxx-example><pre>
</pre>
<cxx-example><pre>
using namespace std::experimental::parallel;
std::atomic<int> x = 0;
int a[] = {1,2};
for_each(par, std::begin(a), std::end(a), [&](int n) {
x.fetch_add(1, std::memory_order_relaxed);
// spin wait for another iteration to change the value of x
while (x.load(std::memory_order_relaxed) == 1) { }
});</pre>
The above example depends on the order of execution of the iterations, and is therefore
undefined (may deadlock).
</cxx-example><pre>
</pre>
<cxx-example><pre>
using namespace std::experimental::parallel;
int x=0;
std::mutex m;
int a[] = {1,2};
for_each(par, std::begin(a), std::end(a), [&](int) {
m.lock();
++x;
m.unlock();
});</pre>
The above example synchronizes access to object <code>x</code> ensuring that it is
incremented correctly.
</cxx-example>
<p>
The invocations of element access functions in parallel algorithms invoked with an execution
policy of type <code>parallel_vector_execution_policy</code>
are permitted to execute in an unordered fashion in unspecified threads, and unsequenced
with respect to one another within each thread.
<cxx-note>
This means that multiple function object invocations may be interleaved on a single thread.
</cxx-note>
<pre>
</pre>
<cxx-note>
This overrides the usual guarantee from the C++ standard, Section 1.9 [intro.execution] that
function executions do not interleave with one another.
</cxx-note>
<pre>
</pre>
Since <code>parallel_vector_execution_policy</code> allows the execution of element access functions to be
interleaved on a single thread, synchronization, including the use of mutexes, risks deadlock. Thus the
synchronization with <code>parallel_vector_execution_policy</code> is restricted as follows:<pre>
</pre>
A standard library function is <em>vectorization-unsafe</em> if it is specified to synchronize with
another function invocation, or another function invocation is specified to synchronize with it, and if
it is not a memory allocation or deallocation function. Vectorization-unsafe standard library functions
may not be invoked by user code called from <code>parallel_vector_execution_policy</code> algorithms.<pre>
</pre>
<cxx-note>
Implementations must ensure that internal synchronization inside standard library routines does not
induce deadlock.
</cxx-note>
</p>
<cxx-example><pre>
using namespace std::experimental::parallel;
int x=0;
std::mutex m;
int a[] = {1,2};
for_each(par_vec, std::begin(a), std::end(a), [&](int) {
m.lock();
++x;
m.unlock();
});</pre>
The above program is invalid because the applications of the function object are not
guaranteed to run on different threads.
</cxx-example><pre>
</pre>
<cxx-note>
The application of the function object may result in two consecutive calls to
<code>m.lock</code> on the same thread, which may deadlock.
</cxx-note><pre>
</pre>
<cxx-note>
The semantics of the <code>parallel_execution_policy</code> or the
<code>parallel_vector_execution_policy</code> invocation allow the implementation to fall back to
sequential execution if the system cannot parallelize an algorithm invocation due to lack of
resources.
</cxx-note>
<p>
Algorithms invoked with an execution policy object of type <code>execution_policy</code>
execute internally as if invoked with the contained execution policy object.
</p>
<p>
The semantics of parallel algorithms invoked with an execution policy object of
implementation-defined type are implementation-defined.
</p>
</cxx-section>
<cxx-section id="parallel.alg.overloads">
<h1><code>ExecutionPolicy</code> algorithm overloads</h1>
<p>
The Parallel Algorithms Library provides overloads for each of the algorithms named in
Table 1, corresponding to the algorithms with the same name in the C++ Standard Algorithms Library.
For each algorithm in <cxx-ref to="tab.parallel.algorithms"></cxx-ref>, if there are overloads for
corresponding algorithms with the same name
in the C++ Standard Algorithms Library,
the overloads shall have an additional template type parameter named
<code>ExecutionPolicy</code>, which shall be the first template parameter.
In addition, each such overload shall have the new function parameter as the
first function parameter of type <code>ExecutionPolicy&&</code>.
</p>
<p>
Unless otherwise specified, the semantics of <code>ExecutionPolicy</code> algorithm overloads
are identical to their overloads without.
</p>
<p>
Parallel algorithms shall not participate in overload resolution unless
<code>is_execution_policy<decay_t<ExecutionPolicy>>::value</code> is <code>true</code>.
</p>
<table is="cxx-table" id="tab.parallel.algorithms" class="list">
<caption>Table of parallel algorithms</caption>
<tr>
<td><code>adjacent_difference</code></td>
<td><code>adjacent_find</code></td>
<td><code>all_of</code></td>
<td><code>any_of</code></td>
</tr>
<tr>
<td><code>copy</code></td>
<td><code>copy_if</code></td>
<td><code>copy_n</code></td>
<td><code>count</code></td>
</tr>
<tr>
<td><code>count_if</code></td>
<td><code>equal</code></td>
<td><code>exclusive_scan</code></td>
<td><code>fill</code></td>
</tr>
<tr>
<td><code>fill_n</code></td>
<td><code>find</code></td>
<td><code>find_end</code></td>
<td><code>find_first_of</code></td>
</tr>
<tr>
<td><code>find_if</code></td>
<td><code>find_if_not</code></td>
<td><code>for_each</code></td>
<td><code>for_each_n</code></td>
</tr>
<tr>
<td><code>generate</code></td>
<td><code>generate_n</code></td>
<td><code>includes</code></td>
<td><code>inclusive_scan</code></td>
</tr>
<tr>
<td><code>inner_product</code></td>
<td><code>inplace_merge</code></td>
<td><code>is_heap</code></td>
<td><code>is_heap_until</code></td>
</tr>
<tr>
<td><code>is_partitioned</code></td>
<td><code>is_sorted</code></td>
<td><code>is_sorted_until</code></td>
<td><code>lexicographical_compare</code></td>
</tr>
<tr>
<td><code>max_element</code></td>
<td><code>merge</code></td>
<td><code>min_element</code></td>
<td><code>minmax_element</code></td>
</tr>
<tr>
<td><code>mismatch</code></td>
<td><code>move</code></td>
<td><code>none_of</code></td>
<td><code>nth_element</code></td>
</tr>
<tr>
<td><code>partial_sort</code></td>
<td><code>partial_sort_copy</code></td>
<td><code>partition</code></td>
<td><code>partition_copy</code></td>
</tr>
<tr>
<td><code>reduce</code></td>
<td><code>remove</code></td>
<td><code>remove_copy</code></td>
<td><code>remove_copy_if</code></td>
</tr>
<tr>
<td><code>remove_if</code></td>
<td><code>replace</code></td>
<td><code>replace_copy</code></td>
<td><code>replace_copy_if</code></td>
</tr>
<tr>
<td><code>replace_if</code></td>
<td><code>reverse</code></td>
<td><code>reverse_copy</code></td>
<td><code>rotate</code></td>
</tr>
<tr>
<td><code>rotate_copy</code></td>
<td><code>search</code></td>
<td><code>search_n</code></td>
<td><code>set_difference</code></td>
</tr>
<tr>
<td><code>set_intersection</code></td>
<td><code>set_symmetric_difference</code></td>
<td><code>set_union</code></td>
<td><code>sort</code></td>
</tr>
<tr>
<td><code>stable_partition</code></td>
<td><code>stable_sort</code></td>
<td><code>swap_ranges</code></td>
<td><code>transform</code></td>
</tr>
<tr>
<td><code>transform_exclusive_scan</code></td>
<td><code>transform_inclusive_scan</code></td>
<td><code>transform_reduce</code></td>
<td><code>uninitialized_copy</code></td>
</tr>
<tr>
<td><code>uninitialized_copy_n</code></td>
<td><code>uninitialized_fill</code></td>
<td><code>uninitialized_fill_n</code></td>
<td><code>unique</code></td>
</tr>
<tr>
<td><code>unique_copy</code></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<cxx-note>
Not all algorithms in the Standard Library have counterparts in <cxx-ref to="tab.parallel.algorithms"></cxx-ref>.
</cxx-note>
</cxx-section>
</cxx-section>
<cxx-section id="parallel.alg.defns">
<h1>Definitions</h1>
<p>
Define <code><em>GENERALIZED_SUM</em>(op, a1, ..., aN)</code> as follows:
<ul>
<li><code>a1</code> when <code>N</code> is <code>1</code></li>
<li>
<code>op(<em>GENERALIZED_SUM</em>(op, b1, ..., bK)</code>, <code><em>GENERALIZED_SUM</em>(op, bM, ..., bN))</code> where
<ul>
<li><code>b1, ..., bN</code> may be any permutation of <code>a1, ..., aN</code> and</li>
<li><code>1 < K+1 = M ≤ N</code>.</li>
</ul>
</li>
</ul>
</p>
<p>
Define <code><em>GENERALIZED_NONCOMMUTATIVE_SUM</em>(op, a1, ..., aN)</code> as follows:
<ul>
<li><code>a1</code> when <code>N</code> is <code>1</code></li>
<li>
<code>op(<em>GENERALIZED_NONCOMMUTATIVE_SUM</em>(op, a1, ..., aK), <em>GENERALIZED_NONCOMMUTATIVE_SUM</em>(op, aM, </code><br>
<code>..., aN)</code> where <code>1 < K+1 = M ≤ N</code>.
</li>
</ul>
</p>
</cxx-section>
<cxx-section id="parallel.alg.ops">
<h1>Non-Numeric Parallel Algorithms</h1>
<cxx-section id="parallel.alg.ops.synopsis">
<h1>Header <code><experimental/algorithm></code> synopsis</h1>
<pre>
namespace std {
namespace experimental {
namespace parallel {
inline namespace v1 {
template<class ExecutionPolicy,
class InputIterator, class Function>
void for_each(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
Function f);
template<class InputIterator, class Size, class Function>
InputIterator for_each_n(InputIterator first, Size n,
Function f);
template<class ExecutionPolicy,
class InputIterator, class Size, class Function>
InputIterator for_each_n(ExecutionPolicy&& exec,
InputIterator first, Size n,
Function f);
}
}
}
}
</pre>
</cxx-section>
<cxx-section id="parallel.alg.foreach">
<h1>For each</h1>
<cxx-function>
<cxx-signature>template<class ExecutionPolicy,
class InputIterator, class Function>
void for_each(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
Function f);</cxx-signature>
<cxx-effects>
Applies <code>f</code> to the result of dereferencing every iterator in the range <code>[first,last)</code>.
<cxx-note>
If the type of <code>first</code> satisfies the requirements of a mutable iterator, <code>f</code> may
apply nonconstant functions through the dereferenced iterator.
</cxx-note>
</cxx-effects>
<cxx-complexity>
Applies <code>f</code> exactly <code>last - first</code> times.
</cxx-complexity>
<cxx-remarks>
If <code>f</code> returns a result, the result is ignored.
</cxx-remarks>
<cxx-notes>
Unlike its sequential form, the parallel overload of <code>for_each</code> does not return a copy of
its <code>Function</code> parameter, since parallelization may not permit efficient state
accumulation.
</cxx-notes>
<cxx-requires>
Unlike its sequential form, the parallel overload of <code>for_each</code> requires
<code>Function</code> to meet the requirements of <code>CopyConstructible</code>.
</cxx-requires>
</cxx-function>
<cxx-function>
<cxx-signature>template<class InputIterator, class Size, class Function>
InputIterator for_each_n(InputIterator first, Size n,
Function f);</cxx-signature>
<cxx-requires>
<code>Function</code> shall meet the requirements of <code>MoveConstructible</code>
<cxx-note>
<code>Function</code> need not meet the requirements of <code>CopyConstructible</code>.
</cxx-note>
</cxx-requires>
<cxx-effects>
Applies <code>f</code> to the result of dereferencing every iterator in the range
<code>[first,first + n)</code>, starting from <code>first</code> and proceeding to <code>first + n - 1</code>.
<cxx-note>
If the type of <code>first</code> satisfies the requirements of a mutable iterator,
<code>f</code> may apply nonconstant functions through the dereferenced iterator.
</cxx-note>
</cxx-effects>
<cxx-returns>
<code>first + n</code> for non-negative values of <code>n</code> and <code>first</code> for negative values.
</cxx-returns>
<cxx-remarks>
If <code>f</code> returns a result, the result is ignored.
</cxx-remarks>
</cxx-function>
<cxx-function>
<cxx-signature>template<class ExecutionPolicy,
class InputIterator, class Size, class Function>
InputIterator for_each_n(ExecutionPolicy && exec,
InputIterator first, Size n,
Function f);</cxx-signature>
<cxx-effects>
Applies <code>f</code> to the result of dereferencing every iterator in the range
<code>[first,first + n)</code>, starting from <code>first</code> and proceeding to <code>first + n - 1</code>.
<cxx-note>
If the type of <code>first</code> satisfies the requirements of a mutable iterator,
<code>f</code> may apply nonconstant functions through the dereferenced iterator.
</cxx-note>
</cxx-effects>
<cxx-returns>
<code>first + n</code> for non-negative values of <code>n</code> and <code>first</code> for negative values.
</cxx-returns>
<cxx-remarks>
If <code>f</code> returns a result, the result is ignored.
</cxx-remarks>
<cxx-notes>
Unlike its sequential form, the parallel overload of <code>for_each_n</code> requires
<code>Function</code> to meet the requirements of <code>CopyConstructible</code>.
</cxx-notes>
</cxx-function>
</cxx-section>
</cxx-section>
<cxx-section id="parallel.alg.numeric">
<h1>Numeric Parallel Algorithms</h1>
<cxx-section id="parallel.alg.numeric.synopsis">
<h1>Header <code><experimental/numeric></code> synopsis</h1>
<pre>
namespace std {
namespace experimental {
namespace parallel {
inline namespace v1 {
template<class InputIterator>
typename iterator_traits<InputIterator>::value_type
reduce(InputIterator first, InputIterator last);
template<class ExecutionPolicy,
class InputIterator>
typename iterator_traits<InputIterator>::value_type
reduce(ExecutionPolicy&& exec,
InputIterator first, InputIterator last);
template<class InputIterator, class T>
T reduce(InputIterator first, InputIterator last, T init);
template<class ExecutionPolicy,
class InputIterator, class T>
T reduce(ExecutionPolicy&& exec,
InputIterator first, InputIterator last, T init);
template<class InputIterator, class T, class BinaryOperation>
T reduce(InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);
template<class ExecutionPolicy, class InputIterator, class T, class BinaryOperation>
T reduce(ExecutionPolicy&& exec,
InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);
template<class InputIterator, class OutputIterator,
class T>
OutputIterator
exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
T init);
template<class ExecutionPolicy,
class InputIterator, class OutputIterator,
class T>
OutputIterator
exclusive_scan(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
OutputIterator result,
T init);
template<class InputIterator, class OutputIterator,
class T, class BinaryOperation>
OutputIterator
exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
T init, BinaryOperation binary_op);
template<class ExecutionPolicy,
class InputIterator, class OutputIterator,
class T, class BinaryOperation>
OutputIterator
exclusive_scan(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
OutputIterator result,
T init, BinaryOperation binary_op);
template<class InputIterator, class OutputIterator>
OutputIterator
inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result);
template<class ExecutionPolicy,
class InputIterator, class OutputIterator>
OutputIterator
inclusive_scan(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
OutputIterator result);
template<class InputIterator, class OutputIterator,
class BinaryOperation>
OutputIterator
inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op);
template<class ExecutionPolicy,
class InputIterator, class OutputIterator,
class BinaryOperation>
OutputIterator
inclusive_scan(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op);
template<class InputIterator, class OutputIterator,
class BinaryOperation, class T>
OutputIterator
inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op, T init);
template<class ExecutionPolicy,
class InputIterator, class OutputIterator,
class BinaryOperation, class T>
OutputIterator
inclusive_scan(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op, T init);
template<class InputIterator, class UnaryOperation,
class T, class BinaryOperation>
T transform_reduce(InputIterator first, InputIterator last,
UnaryOperation unary_op,
T init, BinaryOperation binary_op);
template<class ExecutionPolicy,
class InputIterator, class UnaryOperation,
class T, class BinaryOperation>
T transform_reduce(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
UnaryOperation unary_op,
T init, BinaryOperation binary_op);
template<class InputIterator, class OutputIterator,
class UnaryOperation, class T, class BinaryOperation>
OutputIterator
transform_exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
UnaryOperation unary_op,
T init, BinaryOperation binary_op);
template<class ExecutionPolicy,
class InputIterator, class OutputIterator,
class UnaryOperation, class T, class BinaryOperation>
OutputIterator
transform_exclusive_scan(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
OutputIterator result,
UnaryOperation unary_op,
T init, BinaryOperation binary_op);
template<class InputIterator, class OutputIterator,
class UnaryOperation, class BinaryOperation>
OutputIterator
transform_inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
UnaryOperation unary_op,
BinaryOperation binary_op);
template<class ExecutionPolicy,
class InputIterator, class OutputIterator,
class UnaryOperation, class BinaryOperation>
OutputIterator
transform_inclusive_scan(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
OutputIterator result,
UnaryOperation unary_op,
BinaryOperation binary_op);
template<class InputIterator, class OutputIterator,
class UnaryOperation, class BinaryOperation, class T>
OutputIterator
transform_inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
UnaryOperation unary_op,
BinaryOperation binary_op, T init);
template<class ExecutionPolicy,
class InputIterator, class OutputIterator,
class UnaryOperation, class BinaryOperation, class T>
OutputIterator
transform_inclusive_scan(ExecutionPolicy&& exec,
InputIterator first, InputIterator last,
OutputIterator result,
UnaryOperation unary_op,
BinaryOperation binary_op, T init);
}
}
}
}
</pre>
</cxx-section>
<cxx-section id="parallel.alg.reduce">
<h1>Reduce</h1>
<cxx-function>
<cxx-signature>template<class InputIterator>
typename iterator_traits<InputIterator>::value_type
reduce(InputIterator first, InputIterator last);</cxx-signature>
<cxx-effects>
Same as <code>reduce(first, last, typename iterator_traits<InputIterator>::value_type{})</code>.
</cxx-effects>
</cxx-function>
<cxx-function>
<cxx-signature>template<class InputIterator, class T>
T reduce(InputIterator first, InputIterator last, T init);</cxx-signature>
<cxx-effects>
Same as <code>reduce(first, last, init, plus<>())</code>.
</cxx-effects>
</cxx-function>
<cxx-function>
<cxx-signature>template<class InputIterator, class T, class BinaryOperation>
T reduce(InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);</cxx-signature>
<cxx-returns>
<code><em>GENERALIZED_SUM</em>(binary_op, init, *first, ..., *(first + (last - first) - 1))</code>.
</cxx-returns>
<cxx-requires>
<code>binary_op</code> shall not invalidate iterators or subranges, nor modify elements in the
range <code>[first,last)</code>.
</cxx-requires>
<cxx-complexity>
O(<code>last - first</code>) applications of <code>binary_op</code>.
</cxx-complexity>
<cxx-notes>
The primary difference between <code>reduce</code> and <code>accumulate</code> is that the behavior
of <code>reduce</code> may be non-deterministic for non-associative or non-commutative <code>binary_op</code>.
</cxx-notes>
</cxx-function>
</cxx-section>
<cxx-section id="parallel.alg.exclusive.scan">
<h1>Exclusive scan</h1>
<cxx-function>
<cxx-signature>template<class InputIterator, class OutputIterator, class T>
OutputIterator exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
T init);</cxx-signature>
<cxx-effects>
Same as <code>exclusive_scan(first, last, result, init, plus<>())</code>.
</cxx-effects>
</cxx-function>
<cxx-function>
<cxx-signature>template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
OutputIterator exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
T init, BinaryOperation binary_op);</cxx-signature>
<cxx-effects>
Assigns through each iterator <code>i</code> in <code>[result,result + (last - first))</code> the
value of <code><em>GENERALIZED_NONCOMMUTATIVE_SUM</em>(binary_op, init, *first, ..., *(first + (i - result) - 1))</code>.
</cxx-effects>
<cxx-returns>
The end of the resulting range beginning at <code>result</code>.
</cxx-returns>
<cxx-requires>
<code>binary_op</code> shall not invalidate iterators or subranges, nor modify elements in the
ranges <code>[first,last)</code> or <code>[result,result + (last - first))</code>.
</cxx-requires>
<cxx-complexity>
O(<code>last - first</code>) applications of <code>binary_op</code>.
</cxx-complexity>
<cxx-notes>
The difference between <code>exclusive_scan</code> and <code>inclusive_scan</code> is that
<code>exclusive_scan</code> excludes the <code>i</code>th input element from the <code>i</code>th
sum. If <code>binary_op</code> is not mathematically associative, the behavior of
<code>exclusive_scan</code> may be non-deterministic.
</cxx-notes>
</cxx-function>
</cxx-section>
<cxx-section id="parallel.alg.inclusive.scan">
<h1>Inclusive scan</h1>
<cxx-function>
<cxx-signature>template<class InputIterator, class OutputIterator>
OutputIterator inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result);</cxx-signature>
<cxx-effects>
Same as <code>inclusive_scan(first, last, result, plus<>())</code>.
</cxx-effects>
</cxx-function>
<cxx-function>
<cxx-signature>template<class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op);</cxx-signature>
<cxx-signature>template<class InputIterator, class OutputIterator, class BinaryOperation, class T>
OutputIterator inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op, T init);</cxx-signature>
<cxx-effects>
Assigns through each iterator <code>i</code> in <code>[result,result + (last - first))</code> the value of
<code><em>GENERALIZED_NONCOMMUTATIVE_SUM</em>(binary_op, *first, ..., *(first + (i - result)))</code> or
<code><em>GENERALIZED_NONCOMMUTATIVE_SUM</em>(binary_op, init, *first, ..., *(first + (i - result)))</code>
if <code>init</code> is provided.
</cxx-effects>
<cxx-returns>
The end of the resulting range beginning at <code>result</code>.
</cxx-returns>
<cxx-requires>
<code>binary_op</code> shall not invalidate iterators or subranges, nor modify elements in the
ranges <code>[first,last)</code> or <code>[result,result + (last - first))</code>.
</cxx-requires>
<cxx-complexity>
O(<code>last - first</code>) applications of <code>binary_op</code>.
</cxx-complexity>
<cxx-notes>
The difference between <code>exclusive_scan</code> and <code>inclusive_scan</code> is that
<code>inclusive_scan</code> includes the <code>i</code>th input element in the <code>i</code>th sum.
If <code>binary_op</code> is not mathematically associative, the behavior of
<code>inclusive_scan</code> may be non-deterministic.
</cxx-notes>
</cxx-function>
</cxx-section>
<cxx-section id="parallel.alg.transform.reduce">
<h1>Transform reduce</h1>
<cxx-function>
<cxx-signature>template<class InputIterator, class UnaryFunction, class T, class BinaryOperation>
T transform_reduce(InputIterator first, InputIterator last,
UnaryOperation unary_op, T init, BinaryOperation binary_op);</cxx-signature>
<cxx-returns>
<code><em>GENERALIZED_SUM</em>(binary_op, init, unary_op(*first), ..., unary_op(*(first + (last - first) -</code><br>
<code>1)))</code>.
</cxx-returns>
<cxx-requires>
Neither <code>unary_op</code> nor <code>binary_op</code> shall invalidate subranges, or modify elements in the range <code>[first,last)</code>
</cxx-requires>
<cxx-complexity>
O(<code>last - first</code>) applications each of <code>unary_op</code> and <code>binary_op</code>.
</cxx-complexity>
<cxx-notes>
<code>transform_reduce</code> does not apply <code>unary_op</code> to <code>init</code>.
</cxx-notes>
</cxx-function>
</cxx-section>
<cxx-section id="parallel.alg.transform.exclusive.scan">
<h1>Transform exclusive scan</h1>
<cxx-function>
<cxx-signature>template<class InputIterator, class OutputIterator,
class UnaryOperation,
class T, class BinaryOperation>
OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
UnaryOperation unary_op,
T init, BinaryOperation binary_op);</cxx-signature>
<cxx-effects>
Assigns through each iterator <code>i</code> in <code>[result,result + (last - first))</code> the value of
<code><em>GENERALIZED_NONCOMMUTATIVE_SUM</em>(binary_op, init, unary_op(*first), ..., unary_op(*(first + (i</code><br>
<code>- result) - 1)))</code>.
</cxx-effects>
<cxx-returns>
The end of the resulting range beginning at <code>result</code>.
</cxx-returns>
<cxx-requires>
Neither <code>unary_op</code> nor <code>binary_op</code> shall invalidate iterators or subranges, or modify elements in the
ranges <code>[first,last)</code> or <code>[result,result + (last - first))</code>.
</cxx-requires>
<cxx-complexity>
O(<code>last - first</code>) applications each of <code>unary_op</code> and <code>binary_op</code>.
</cxx-complexity>
<cxx-notes>
The difference between <code>transform_exclusive_scan</code> and <code>transform_inclusive_scan</code> is that <code>transform_exclusive_scan</code>
excludes the ith input element from the ith sum. If <code>binary_op</code> is not mathematically associative, the behavior of
<code>transform_exclusive_scan</code> may be non-deterministic. <code>transform_exclusive_scan</code> does not apply <code>unary_op</code> to <code>init</code>.
</cxx-notes>
</cxx-function>
</cxx-section>
<cxx-section id="parallel.alg.transform.inclusive.scan">
<h1>Transform inclusive scan</h1>
<cxx-function>
<cxx-signature>template<class InputIterator, class OutputIterator,
class UnaryOperation,
class BinaryOperation>
OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
UnaryOperation unary_op,
BinaryOperation binary_op);</cxx-signature>
<cxx-signature>template<class InputIterator, class OutputIterator,
class UnaryOperation,
class BinaryOperation, class T>
OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
OutputIterator result,
UnaryOperation unary_op,
BinaryOperation binary_op, T init);</cxx-signature>
<cxx-effects>
Assigns through each iterator <code>i</code> in <code>[result,result + (last - first))</code> the value of
<code><em>GENERALIZED_NONCOMMUTATIVE_SUM</em>(binary_op, unary_op(*first), ..., unary_op(*(first + (i -</code><br>
<code>result))))</code> or
<code><em>GENERALIZED_NONCOMMUTATIVE_SUM</em>(binary_op, init, unary_op(*first), ..., unary_op(*(first + (i</code><br>
<code>- result))))</code>
if <code>init</code> is provided.
</cxx-effects>
<cxx-returns>
The end of the resulting range beginning at <code>result</code>.
</cxx-returns>
<cxx-requires>
Neither <code>unary_op</code> nor <code>binary_op</code> shall invalidate iterators or subranges, or modify elements in the ranges <code>[first,last)</code>
or <code>[result,result + (last - first))</code>.
</cxx-requires>
<cxx-complexity>
O(<code>last - first</code>) applications each of <code>unary_op</code> and <code>binary_op</code>.
</cxx-complexity>
<cxx-notes>
The difference between <code>transform_exclusive_scan</code> and <code>transform_inclusive_scan</code> is that <code>transform_inclusive_scan</code>
includes the ith input element from the ith sum. If <code>binary_op</code> is not mathematically associative, the behavior of
<code>transform_inclusive_scan</code> may be non-deterministic. <code>transform_inclusive_scan</code> does not apply <code>unary_op</code> to <code>init</code>.
</cxx-notes>
</cxx-function>
</cxx-section>
</cxx-section>
</cxx-clause>