std::experimental::parallel:: transform_reduce
|
定义于头文件
<experimental/numeric>
|
||
|
template
<
class
InputIt,
class
UnaryOp,
class
T,
class
BinaryOp
>
T transform_reduce
(
InputIt first, InputIt last,
|
(1) | (并行性TS) |
|
template
<
class
ExecutionPolicy,
class
InputIt,
class
UnaryOp,
class
T,
class
BinaryOp
>
|
(2) | (并行性TS) |
对范围
[
first
,
last
)
中的每个元素应用
unary_op
,并将结果(可能以未指定的方式重排和聚合)与初始值
init
通过
binary_op
进行归约。
如果 binary_op 不满足结合律或交换律,则该行为是不确定的。
如果
unary_op
或
binary_op
修改了
[
first
,
last
)
范围内的任何元素或使任何迭代器失效,则行为未定义。
目录参数
返回值init 与 unary_op ( * first ) 、 unary_op ( * ( first + 1 ) ) …… unary_op ( * ( last - 1 ) ) 在 binary_op 上的广义和, 其中广义和 GSUM(op, a 1 , ..., a N ) 定义如下:
换句话说, unary_op 的结果可以按任意顺序进行分组和排列。 复杂度O(last - first) 次对 unary_op 和 binary_op 的各自调用。 异常
注释unary_op 不应用于 init 。 如果范围为空,将返回未经修改的 init 。
示例transform_reduce 可用于并行化 std::inner_product :
运行此代码
#include <boost/iterator/zip_iterator.hpp> #include <boost/tuple.hpp> #include <experimental/execution_policy> #include <experimental/numeric> #include <functional> #include <iostream> #include <iterator> #include <vector> int main() { std::vector<double> xvalues(10007, 1.0), yvalues(10007, 1.0); double result = std::experimental::parallel::transform_reduce( std::experimental::parallel::par, boost::iterators::make_zip_iterator( boost::make_tuple(std::begin(xvalues), std::begin(yvalues))), boost::iterators::make_zip_iterator( boost::make_tuple(std::end(xvalues), std::end(yvalues))), [](auto r) { return boost::get<0>(r) * boost::get<1>(r); } 0.0, std::plus<>() ); std::cout << result << '\n'; } 输出: 10007 另请参阅
| ||||||||||||||||||||||||||||