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.