After reading the documentation I'm still confused about the usage of par_unseq
. I know I can't tell anything about the order of execution because of threading and vectorization but can I still rely on the order of the outputs?
transform([x0, x1, x2], f) == [f(x0), f(x1), f(x2)]]
In order words, is this test ever going to fail?
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);
The Standard, [alg.transform], reads:
Effects: Assigns through every iterator
i
in the range[result,result + (last1 - first1))
a new corresponding value equal toop(*(first1 + (i - result))
orbinary_op(*(first1 + (i - result)), *(first2 + (i - result)))
.
and (thanks, @Caleth), [algorithms.parallel.overloads]:
Unless otherwise specified, the semantics of
ExecutionPolicy
algorithm overloads are identical to their overloads without.
So, yes, you can rely on the order in the output.
No, your test is never going to fail because even if order of execution changes,
ys[0...3] = xs[0...3] * xs[0...3] = {1*1, 2*2, 3*3, 4*4};
won't change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With