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.





About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 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.....????

WithDegreeOfParallelism(int)?