Ordered PLINQ ForAll
Order preservation is usually only applied to results - i.e. the input can be processed in any order, but is returned in the original order.
As ForAll
doesn't return anything, it doesn't really have any effect that I'm aware of.
The only way of making ordering apply to the processing would be to finish item 0 before processing item 1, before processing item 2 etc... at which point you've got no parallelism.
AsOrdered()
wouldn't change anything - if you want to enforce order on the result of a parallel query you can simply use foreach()
ForAll()
is there to take advantage of parallelism, that means executing the side effect on more than one item in the collection at a time. In fact ordering only applies to the results of a query (the order of items in the result collection), but this has nothing to do with ForAll()
, since ForAll()
does not affect the order at all.
In PLINQ, the goal is to maximize performance while maintaining correctness. A query should run as fast as possible but still produce the correct results. In some cases, correctness requires the order of the source sequence to be preserved
Note that ForAll()
is not transforming the collection (it's not i.e projecting to a new collection), it's purely for executing side effects on the results of a PLINQ query.
As others have rightly answered, the ForAll
method is never guaranteed to execute an action for enumerable elements in any particular order, and will ignore the AsOrdered()
method call silently.
For the benefit of readers having a valid reason to execute an action for enumerable elements in a way that stay's as close to the original order (as far as is reasonable in a parallel processing context) the extension methods below might help.
public static void ForAllInApproximateOrder<TSource>(this ParallelQuery<TSource> source, Action<TSource> action) {
Partitioner.Create( source )
.AsParallel()
.AsOrdered()
.ForAll( e => action( e ) );
}
This can then be used as follows:
orderedElements.AsParallel()
.ForAllInApproximateOrder( e => DoSomething( e ) );
It should be noted that the above extension method uses PLINQ ForAll
and not Parallel.ForEach
and so inherits the threading model used interally by PLINQ (which is different to that used by Parallel.ForEach
-- less aggressive by default in my experience). A similar extension method using Parallel.ForEach
is below.
public static void ForEachInApproximateOrder<TSource>(this ParallelQuery<TSource> source, Action<TSource> action) {
source = Partitioner.Create( source )
.AsParallel()
.AsOrdered();
Parallel.ForEach( source , e => action( e ) );
}
This can then be used as follows:
orderedElements.AsParallel()
.ForEachInApproximateOrder( e => DoSomething( e ) );
There is no need to chain AsOrdered()
to your query when using either of the above extension methods, it gets called internally anyway.
I have found these methods useful in processing elements that have coarse grained significance. It can be useful, for example, to process records starting at the oldest and working towards the newest. In many cases the exact order of records isn't required - so far as older records generally get processed before newer records. Similarly, records having low/med/high priority levels can be processed such that high priority records will be processed before lower priority records for the majority of cases, with the edge cases not far behind.