std::experimental::parallel:: reduce
|
Definiert im Header
<experimental/numeric>
|
||
|
template
<
class
InputIt
>
typename
std::
iterator_traits
<
InputIt
>
::
value_type
reduce
(
|
(1) | (Parallelism TS) |
|
template
<
class
ExecutionPolicy,
class
InputIterator
>
typename
std::
iterator_traits
<
InputIt
>
::
value_type
reduce
(
|
(2) | (Parallelism TS) |
|
template
<
class
InputIt,
class
T
>
T reduce ( InputIt first, InputIt last, T init ) ; |
(3) | (Parallelism TS) |
|
template
<
class
ExecutionPolicy,
class
InputIt,
class
T
>
T reduce ( ExecutionPolicy && policy, InputIt first, InputIt last, T init ) ; |
(4) | (Parallelism TS) |
|
template
<
class
InputIt,
class
T,
class
BinaryOp
>
T reduce ( InputIt first, InputIt last, T init, BinaryOp binary_op ) ; |
(5) | (Parallelism TS) |
|
template
<
class
ExecutionPolicy,
class
InputIt,
class
T,
class
BinaryOp
>
T reduce
(
ExecutionPolicy
&&
policy,
|
(6) | (Parallelism TS) |
[
first
,
last
)
, möglicherweise permutiert und auf nicht spezifizierte Weise aggregiert, zusammen mit dem Anfangswert
init
über
binary_op
.
Das Verhalten ist nicht deterministisch, wenn binary_op nicht assoziativ oder nicht kommutativ ist.
Das Verhalten ist undefiniert, falls
binary_op
irgendein Element modifiziert oder irgendeinen Iterator im Bereich
[
first
,
last
)
ungültig macht.
InhaltParameter
RückgabewertVerallgemeinerte Summe von init und * first , * ( first + 1 ) , ... * ( last - 1 ) über binary_op , wobei der generalisierte Summenoperator GSUM(op, a 1 , ..., a N ) wie folgt definiert ist:
mit anderen Worten, die Elemente des Bereichs können in beliebiger Reihenfolge gruppiert und neu angeordnet werden. KomplexitätO(last - first) Anwendungen von binary_op . Ausnahmen
HinweiseWenn der Bereich leer ist, init wird unverändert zurückgegeben.
Beispielreduce ist die nicht geordnete Version von std::accumulate :
Code ausführen
#include <chrono> #include <experimental/execution_policy> #include <experimental/numeric> #include <iostream> #include <numeric> #include <vector> int main() { std::vector<double> v(10'000'007, 0.5); { auto t1 = std::chrono::high_resolution_clock::now(); double result = std::accumulate(v.begin(), v.end(), 0.0); auto t2 = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> ms = t2 - t1; std::cout << std::fixed << "std::accumulate result " << result << " took " << ms.count() << " ms\n"; } { auto t1 = std::chrono::high_resolution_clock::now(); double result = std::experimental::parallel::reduce( std::experimental::parallel::par, v.begin(), v.end()); auto t2 = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> ms = t2 - t1; std::cout << "parallel::reduce result " << result << " took " << ms.count() << " ms\n"; } } Mögliche Ausgabe: std::accumulate result 5000003.50000 took 12.7365 ms parallel::reduce result 5000003.50000 took 5.06423 ms Siehe auch
| |||||||||||||||||||||||||