Ordered PLINQ ForAll

后端 未结 6 1286
长情又很酷
长情又很酷 2021-01-04 08:09

The msdn documentation about order preservation in PLINQ states the following about ForAll().

  • Result when the source sequence is ordered
6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 08:22

    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(this ParallelQuery source, Action 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(this ParallelQuery source, Action 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.

提交回复
热议问题