std::experimental::parallel:: reduce
|
定义于头文件
<experimental/numeric>
|
||
|
template
<
class
InputIt
>
typename
std::
iterator_traits
<
InputIt
>
::
value_type
reduce
(
|
(1) | (并行性 TS) |
|
template
<
class
ExecutionPolicy,
class
InputIterator
>
typename
std::
iterator_traits
<
InputIt
>
::
value_type
reduce
(
|
(2) | (并行性 TS) |
|
template
<
class
InputIt,
class
T
>
T reduce ( InputIt first, InputIt last, T init ) ; |
(3) | (并行性 TS) |
|
template
<
class
ExecutionPolicy,
class
InputIt,
class
T
>
T reduce ( ExecutionPolicy && policy, InputIt first, InputIt last, T init ) ; |
(4) | (并行性 TS) |
|
template
<
class
InputIt,
class
T,
class
BinaryOp
>
T reduce ( InputIt first, InputIt last, T init, BinaryOp binary_op ) ; |
(5) | (并行性 TS) |
|
template
<
class
ExecutionPolicy,
class
InputIt,
class
T,
class
BinaryOp
>
T reduce
(
ExecutionPolicy
&&
policy,
|
(6) | (并行性 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 另见
| |||||||||||||||||||||||||