考虑:
std::vector<double> u, v;
#pragma omp parallel for
for (std::size_t i = 0u; i < u.size(); ++i)
u[i] += v[i];
要使用C ++ 17并行算法表达相似的代码,到目前为止,我发现的解决方案是使用std::transform
的两个输入范围版本:
std::transform(std::execution::par_unseq,
std::begin(u), std::end(u), std::begin(v), std::begin(u),
std::plus())
我根本不喜欢它,因为它绕过了我的类型的+=
运算符,并且在我的实际用例中,导致的代码比原始的OpenMP代码冗长得多(长4倍)(我不能使用std::plus
,因为我必须首先对RHS范围元素进行操作。
还有我要监督的另一种算法吗?
还请注意,如果我使用ranges::zip
,则该代码将不会在GCC 9中并行运行,因为如果iterator_category
至少为forward_iterator
,则PSTL后端会退回到顺序算法:https://godbolt.org/z/XGtPwc。
答案 0 :(得分:0)
您是否尝试过tbb :: zip_iterator(https://www.threadingbuildingblocks.org/docs/help/reference/iterators/zip_iterator.html)?
其iterator_category
是random_access_iterator
。
所以代码看起来像
auto zip_begin = tbb::make_zip_iterator(std::begin(u), std::begin(v));
std::for_each(par_unseq, zip_begin, zip_begin + u.size(),
[](auto &&x) { std::get<0u>(x) += std::get<1u>(x); });