With parallel collection, does aggregate respect order?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 16:44:15

问题


in scala, i have a parallel Iterable of items and i want to iterate over them and aggregate the results in some way, but in order. i'll simplify my use case and say that we start with an Iterable of integers and want to concatenate the string representation of them in paralle, with the result in order.

is this possible with either fold or aggregate? it's unclear from the documentation which methods work parallelized but maintain order.


回答1:


Yes, order is gauranteed to be preserved for fold/aggregate/reduce operations on parallel collections. This is not very well documented. The trick is that the operation you which to fold over must be associative (and thus capable of being arbitrarily split up and recombined), but need not be commutative (and so not capable of being safely reordered). String concatenation is a perfect example of an associative, non-commutative operation, so the fold can be done in parallel.

val concat = myParallelList.map(_.toString).reduce(_+_)



回答2:


For folds: foldRight and foldLeft cannot be processed in parallel, you'll need to use the new fold method (more info there).

Like fold, aggregate can do its work in parallel: it “traverses the elements in different partitions sequentially” (Scaladoc), though it looks like you have no direct influence on how the partitions are chosen.




回答3:


I THINK the preservation of 'order' in the sense of the comment to Jean-Philippe Pellets answer is guaranteed due to the way parallel collections are implemented according to a publication of Odersky (http://infoscience.epfl.ch/record/150220/files/pc.pdf) IFF the part that splits your collection is behaving well with respect to order.

i.e. if you have elements a < b < c and a and c end up in one partition it follows that b is in the same partition as well.

I don't remember what exactly was the part responsible for the splitting, but if you find it, you might sufficient information in its documentation or source code in order to answer your question.



来源:https://stackoverflow.com/questions/6303878/with-parallel-collection-does-aggregate-respect-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!