Suraj
Suraj

Reputation: 36597

foreach %dopar% - guarantee on order of results?

By default (no .combine), foreach/%dopar% returns results in a list. Is the order of results in the list guaranteed to match the order of the loop/iteration? In other words, will the order be the same as when iterating sequentially? OR does the list get populated as a parallel task completes? Browsing the documentation, I see that there's a .inorder parameter, but it seems to only apply when using a .combine function.

Upvotes: 23

Views: 3048

Answers (1)

Nick Sabbe
Nick Sabbe

Reputation: 11946

When the call ends, the result of foreach will be in the same order as for a 'normal' loop. However, there is no guarantee for the order in which they 'get there': in theory (and when parallelizing, also in practice), the first item might be filled out later than the second one.

So you have no guarantee on the order of execution (e.g. progress bars or logging may be cobbled), but you can rest assured that the results will be in the order you expect them.

Upvotes: 35

Related Questions