Plinq, Cores and WithDegreeOfParallelism?

前端 未结 1 1106
旧巷少年郎
旧巷少年郎 2021-01-05 03:11

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
          


        
相关标签:
1条回答
  • 2021-01-05 03:38

    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.

    Task Manager CPU usage screenshot

    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.

    0 讨论(0)
提交回复
热议问题