Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max Degree of Parallelism for AsParallel()

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.

like image 433
codingpirate Avatar asked Aug 13 '14 15:08

codingpirate


People also ask

What is Max degree of parallelism in C#?

Degree of parallelism is the maximum number of concurrently executing tasks that will be used to process the query. var result = Tabel.

What is AsParallel?

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.

When should I use parallel ForEach When should I use Plinq?

Note, in this situation Parallel. ForEach expects you to store your results in a thread-safe manner, while PLINQ handles those details for you.

How many threads will parallel ForEach use?

It could start 1000 threads for some degenerate 'DoSomething'.


2 Answers

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.

like image 125
Yuval Itzchakov Avatar answered Oct 01 '22 09:10

Yuval Itzchakov


Yes, you can certainly do that. You just use WithDegreeOfParallelism extension method

yourSequence.AsParallel()     .WithDegreeOfParallelism(5)//Whatever number as you like     .Where(...); 
like image 21
Sriram Sakthivel Avatar answered Oct 01 '22 09:10

Sriram Sakthivel