-->

Plinq, Cores and WithDegreeOfParallelism?

2019-02-16 23:42发布

问题:

AS far as I understood , Plinq decides how many thread to open ( each on a thread on different core) by cores count.

__________

  Core 1
  Core 2
  Core 3
  Core 4
___________

So If I Have a Plinq task which finds all the first 1000 prime numbers , Plink will open a new Thread on each Core in order to maximize the efficiency.

So here , each core will be running on 1000/4 numbers , the logic of finding the prime numbers.

However I've read that a blocking operations like IO should be used with WithDegreeOfParallelism so that the cpu won't think that this is an intensive cpu operation , and it allowed to use more threads than cores.

Question :

1) Is it accurate ? Did I understood it correctly ?

2)If I set WithDegreeOfParallelism (7) so it will definitely use all the 4 cores , but what about the other 3 ? ( 7-4) where will they be running ? on which core/s ?

回答1:

First, .Net doesn't choose which core executes which thread, the OS does. If there is no other CPU-intensive application running on the system, you can expect that each thread will execute on a separate core. But if there is some other application, the OS might for example decide to run all of your threads on a single core, switching between them.

And it's even more complicated than that. A thread usually doesn't run on a single core, the OS switches it from core to core all the time. For example, have a look at the following screenshot from Task Manager showing the execution of a single-threaded CPU-intensive application.

You'll notice that the single thread executed on all of my 4 cores, and utilized approximately 25 % of each core over the few seconds it ran.

.Net has no knowledge of the CPU usage of your computer, so it assumes that the optimal number of threads doing CPU-intensive work is the same as the number of cores.

I don't know how exactly does PLINQ work, but I wouldn't expect each core to produce exactly 1000/4 prime numbers in your example. If one thread already produced its share of prime numbers and another one isn't done yet, it wouldn't be efficient to let the first thread stay idle.

And yes, with IO operations, the optimal number of threads doesn't depend on the number of cores, so you should set the degree of parallelism manually. (Don't forget that the optimal number of threads may be 1; harddisks are fastest with sequential reads, not seeking back and forth between many files.)

If you set WithDegreeOfParallelism(7) it will definitely use 7 threads (again, no guarantee on the number of cores). The OS will decide how to run those 7 threads on your 4 cores. If all of those threads are CPU-intensive, it will most likely give each thread something like 4/7 ≈ 57 % of a core. If they are IO-bound, it will execute the code for a thread that just woke up (unblocked) on any core that is just available.

And WithDegreeOfParallelism() really does set exact number of threads, not their maximum number, see Stephen Toub's ParallelOptions.MaxDegreeOfParallelism vs PLINQ’s WithDegreeOfParallelism.