While using Parallel.ForEach
we have the option to define the Parallel options and set the Max Degree of Parallelism like :
Parallel.ForEach(values, new ParallelOptions {MaxDegreeOfParallelism = number}, value = > { // Do Work })
But while doing PLINQ like:
Tabel.AsEnumberable() .AsParallel() .Where(//Logic)
I was not able to find a way to set MaxDegreeOfParallelism
. I looked up on the net as well, but didn't find anything. As anyone found a way around this? Any help is appreciated.
Degree of parallelism is the maximum number of concurrently executing tasks that will be used to process the query. var result = Tabel.
AsParallel(IEnumerable) Enables parallelization of a query. AsParallel<TSource>(Partitioner<TSource>) Enables parallelization of a query, as sourced by a custom partitioner that is responsible for splitting the input sequence into partitions.
Note, in this situation Parallel. ForEach expects you to store your results in a thread-safe manner, while PLINQ handles those details for you.
It could start 1000 threads for some degenerate 'DoSomething'.
You can use ParallelEnumerable.WithDegreeOfParallelism
:
Sets the degree of parallelism to use in a query. Degree of parallelism is the maximum number of concurrently executing tasks that will be used to process the query.
var result = Tabel.AsEnumberable() .AsParallel() .WithDegreeOfParallelism(number) .Where(/* predicate */);
Edit:
@svick provided an excellent on ParallelOptions.MaxDegreeOfParallelism vs PLINQ’s WithDegreeOfParallelism which emphasizes the difference between the two:
Parallel works using an under-the-covers concept we refer to as replicating tasks. The concept is that a loop will start with one task for processing the loop, but if more threads become available to assist in the processing, additional tasks will be created to run on those threads. This enables minimization of resource consumption. Given this, it would be inaccurate to state that ParallelOptions enables the specification of a DegreeOfParallelism, because it’s really a maximum degree: the loop starts with a degree of 1, and may work its way up to any maximum that’s specified as resources become available.
PLINQ is different. Some important Standard Query Operators in PLINQ require communication between the threads involved in the processing of the query, including some that rely on a Barrier to enable threads to operate in lock-step. The PLINQ design requires that a specific number of threads be actively involved for the query to make any progress. Thus when you specify a DegreeOfParallelism for PLINQ, you’re specifying the actual number of threads that will be involved, rather than just a maximum.
Yes, you can certainly do that. You just use WithDegreeOfParallelism extension method
yourSequence.AsParallel() .WithDegreeOfParallelism(5)//Whatever number as you like .Where(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With