std::experimental::parallel:: reduce
|
ヘッダーで定義
<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
)
を、指定されない方法で並べ替えおよび集約された可能性のある状態で、初期値
init
と共に
binary_op
に対して縮約します。
binary_op が結合的または可換的でない場合、動作は非決定的となります。
binary_op
が任意の要素を変更するか、
[
first
,
last
)
内の任意のイテレータを無効化する場合、動作は未定義です。
目次パラメータ
戻り値init と * first 、 * ( first + 1 ) 、... * ( last - 1 ) の一般化された総和を binary_op で計算します。 ここで一般化された合計 GSUM(op, a 1 , ..., a N ) は以下のように定義されます:
言い換えれば、範囲の要素は任意の順序でグループ化および再配置される可能性があります。 計算量O(last - first) 回の binary_op の適用。 例外
注記範囲が空の場合、 init が変更されずに返されます。
例reduceは std::accumulate の非順序実行バージョンです:
このコードを実行
#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"; } } 出力例: std::accumulate result 5000003.50000 took 12.7365 ms parallel::reduce result 5000003.50000 took 5.06423 ms 関連項目
| |||||||||||||||||||||||||