PLINQ: Set Degree of Parallelism using WithDegreeOfParallelism

Degree of parallelism is the maximum number of ‘concurrently’ executing tasks that will be used to process the query. A query could become a long running one if it is waiting for a resource to be released or hardware to respond. PLINQ can parallelize this query by calling WithDegreeOfParallelism (which sets the maximum processor cores) after AsParallel().

AsParallel() method enables parallelization of a query.

The degreeOfParallelism is less than 1 or greater than 63. The WithDegreeOfParallelism method sets the degree of parallelism to use in a query. Let us see how to set the maximum number of processor cores to 3 using this method.

PLINQ Parallelism

Depending on the number of cores you have, PLINQ may process that many chunks of data at once. So if you have a quadcore machine, it would be 4 threads. However remember that specifying WithDegreeOfParallelism as 8 on a quad core machine wouldn't really improve performance because only 4 threads could be active at the same time. Having said that, for File I/O ops, I usually specify a number greater than the number of cores.

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

Anonymous said...

Mine is intel i3 processor.Total number of processors will be 4. If I provide WithDegreeOfParallelism(4) , my program will utilize all 4 processors. But even if i go for higher values like WithDegreeOfParallelism(344),WithDegreeOfParallelism(500),WithDegreeOfParallelism(510)......
etc. Then also its working.if count exceeds > 512, im getting error.
Available threads is 1023. So , whats logic behind this?
for WithDegreeOfParallelism(4) we should pass pass processors count means why it is accepting values 344,500,510,etc.....????