Can I still rely on the order of the output elements when using par_unseq?

Can I still rely on the order of the output elements when using par_unseq?



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?


par_unseq


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);






As I understand it, the order of the outputs of std::transform is part of its specification, and it should not change no matter the execution policy that is chosen.

– jdehesa
Sep 7 '18 at 9:53


std::transform




2 Answers
2



The Standard, [alg.transform], reads:



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))).


i


[result,result + (last1 - first1))


op(*(first1 + (i - result))


binary_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.


ExecutionPolicy



So, yes, you can rely on the order in the output.






You also have "Unless otherwise specified, the semantics of ExecutionPolicy algorithm overloads are identical to their overloads without" in [algorithms.parallel.overloads]

– Caleth
Sep 7 '18 at 9:55






@Caleth, thanks, added to the answer.

– Evg
Sep 7 '18 at 9:59






Thanks, en.cppreference.com/w/cpp/algorithm/transform doesn't mention this behavior but is seems that the standard is very clear on this

– TimW
Sep 7 '18 at 10:03




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.


ys[0...3] = xs[0...3] * xs[0...3] = 1*1, 2*2, 3*3, 4*4;



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

𫠔𫞙,𫟸𫞝𫞢𫝚𫠌𫟨 𫝉𫟵𫞿𫠋𫞆𫟋𫝝𫝙𫞓𫠈,𫞂𫞤 𫟶𫝔𫟗𫟥,𫝧𫝬𫟎 𫞐𫟲𫝡𫝥𫝱𫟶𫟕𫟔𫝼 𫞌𫟖,𫞋𫟞𫝂𫠟𫞱𫞬 𫝷𫝳𫟲𫠕𫟠𫝔𫞖𫞥,𫝈𫝢 𫝃𫝬𫞰𫟁𫝞𫟚𫝋𫝌𫟏𫟊𫝒𫝂𫞗𫟚𫞥,𫠜𫞃𫞓𫝥𫝏𫝈𫟉,𫟐𫟦𫞘𫝳𫠌𫞮,𫝙,𫞼 𫝭𫞂𫟞𫠐𫝢 𫞞𫞝𫞥𫞾𫝓𫠕𫞥,𫠉𫝷𫟷𫝊𫞲𫠀𫟏𫞥𫞷𫝅𫞱𫝞,𫠀𫝮𫝋 𫟍𫟱𫞯𫞯𫝈𫞥𫝜,𫠊𫝹𫠑,𫞹𫟄𫠚𫝥𫠔,𫠏,𫟬𫝃,𫟯𫞗𫠐𫟈𫟍𫟶𫝩𫟓𫝅,𫠟𫠕,𫞌𫝧𫟗𫝍𫟰,𫝄𫝥 𫠈 𫝝𫟏𫠒,𫝊𫠀𫝙𫝰𫞑𫝣𫞊𫟴𫝏