0

I need to restore vm groups in parallel, for which I'm using a parallel foreach loop. I also want to use MaxDegreeOfParallelism to limit the number of threads. Since mine is a CPU bound operation, I believe the maximum number of threads should equal the number of cores.

Does the number of threads depend on the actual number of cores or also on the number of logical processors? Are there any other factors that this number could depend on? Is there a way other than trial-and-error to test this?

Also, is this the relation between logical processors and actual cores:

(# of logical processors) = (# of cores) * (# of threads per core)

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
pesky_programmer
  • 131
  • 3
  • 12
  • _"Does the number of threads depend on the actual number of cores or also on the number of logical processors?"_ -- the number of threads depends on how many threads _your code_ decides should run. So, neither of those. Certain CPU designs can theoretically run more than one thread concurrently. See duplicate for that explanation. – Peter Duniho Jul 21 '21 at 02:17
  • See also https://stackoverflow.com/questions/27965962/c-sharp-environment-processorcount-does-not-always-return-the-full-number-of-log for a potential pitfall in determining your desired concurrency degree. – Peter Duniho Jul 21 '21 at 02:19
  • Have you tried not setting it and letting the scheduler decide? – mjwills Jul 21 '21 at 02:50
  • You are probably overthinking it. Just configure your parallel loop with `MaxDegreeOfParallelism = Environment.ProcessorCount` and you'll be OK, provided that the workload [is CPU-bound and not I/O-bound](https://stackoverflow.com/questions/868568/what-do-the-terms-cpu-bound-and-i-o-bound-mean). You should definitely configure the `Parallel.ForEach` loop, and [not rely on the default behavior](https://stackoverflow.com/questions/66261010/multiple-parallel-foreach-loops-in-net/66263583#66263583), which is to use the `ThreadPool` saturation (starvation) as the throttling mechanism. – Theodor Zoulias Jul 21 '21 at 05:06

0 Answers0