gpt4 book ai didi

c++ - 使用 par_unseq 时,我仍然可以依赖输出元素的顺序吗?

转载 作者:IT老高 更新时间:2023-10-28 22:38:48 26 4
gpt4 key购买 nike

阅读文档后,我仍然对 par_unseq 的用法感到困惑。我知道由于线程和矢量化,我无法说出执行顺序,但我仍然可以依赖输出的顺序吗?

transform([x0, x1, x2], f) == [f(x0), f(x1), f(x2)]]

换句话说,这个测试会失败吗?

std::vector<int> xs = {1, 2, 3, 4};
std::vector<int> ys(xs.size());

std::transform(
std::execution::par_unseq,
cbegin(xs), cend(xs),
begin(ys),
[](int x) { return x*x; });

std::vector<int> expected = {1, 4, 9, 16};
ASSERT_EQ(expected , ys);

最佳答案

标准,[alg.transform] ,读作:

Effects: Assigns through every iterator i in the range [result,result + (last1 - first1)) a new corresponding value equal to op(*(first1 + (i - result)) or binary_op(*(first1 + (i - result)), *(first2 + (i - result))).

和(感谢@Caleth),[algorithms.parallel.overloads] :

Unless otherwise specified, the semantics of ExecutionPolicy algorithm overloads are identical to their overloads without.

所以,是的,您可以依赖输出中的顺序。

关于c++ - 使用 par_unseq 时,我仍然可以依赖输出元素的顺序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52219796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com