Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get maximum degree of parallelism for task parallel library usage?

I want to use Parallel.invoke. If i assign 20 parallel task, only 8 of then are run concurrently. My CPU is http://ark.intel.com/products/47925 and the reported number of threads is 8. I assume number of task can be run in parallel is related to the cpu number of threads. I dont want to create more task than the number of threads. How do i know the number of threads in c#? I tried query ParallelOptions.MaxDegreeOfParallelism and all i get is -1.

like image 252
Syaiful Nizam Yahya Avatar asked Feb 03 '14 05:02

Syaiful Nizam Yahya


People also ask

How do you find the maximum degree of parallelism in C#?

So, in order to use Maximum Degree of Parallelism in C#, we need to create an instance of ParallelOptions class and we need to set the MaxDegreeOfParallelism properties to an integer number indicating the number of threads to execute the code.

Does task WhenAll run in parallel?

WhenAll() method in . NET Core. This will upload the first file, then the next file. There is no parallelism here, as the “async Task” does not automatically make something run in in parallel.

What are the 2 types of parallelism in C#?

C# supports two main models of parallelism: Data parallelism: where an operation is applied to each element in a collection. Task parallelism: where independent computations are executed in parallel.

Is a program run in parallel always faster?

Practically, when a program is executed in parallel, the hypothesis that the parallel program will run faster is not always satisfied. If the main goal of parallelizing a serial program is to obtain a faster run then the main criterion to be considered is the speedup gained from parallelization.


1 Answers

Parallel tasks are basically threads that can be shared. Because the number of active threads is limited by the number of logical processor cores that are available, a good guess would be to just take the number of logical cores available to the program.

Environment.ProcessorCount
like image 167
hsun324 Avatar answered Oct 19 '22 13:10

hsun324