Control.NumberOfParallelWorkerThreads is not system wide

Feb 24, 2014 at 12:58 PM
It seems that NumberOfParallelWorkerThreads is per call and not global.

If i set NumberOfParallelWorkerThreads=4 and start my algorithm on two threads MathNet.Numerics is using 8 worker threads instead of 4.
So more worker threads are created than available CPU cores.

The question is what is the better approach:
  • limit the number of parallel algorithms (user code)
  • use process wide NumberOfParallelWorkerThreads independent of how many algorithms are running in parallel (MathNet code)
Feb 24, 2014 at 1:23 PM
LimitedConcurrencyLevelTaskScheduler from Samples for Parallel Programming with the .NET Framework might be the solution here.
Also this should work for Portable Class Library so the code in CommonParallel would be simpler.
Feb 24, 2014 at 1:35 PM
Edited Feb 24, 2014 at 1:35 PM
The naming is very misleading, Max- or even DesiredDegreeOfParallelism would describe much more closely what it is about. We do not do any active thread management at all and therefore cannot limit the number of threads created and used, neither per call nor globally. Instead, as you noted, it does only control the desired or maximum number of parallel execution, per call.

What we could do is expose a TPL TaskScheduler in Control (.Net 4 builds and higher) which would default to ThreadPoolTaskScheduler (status quo) but could be replaced with your own if needed. What do you think?


Edit: ah, you came to a similar conclusion regarding TaskScheduler :)
Feb 24, 2014 at 1:43 PM
I will do some experiments and send a pull when having a working version ;-)
Feb 25, 2014 at 3:52 PM
Created pull
Only one question is open: Where to put the TaskScheduler property?
Marked as answer by cdrnet on 3/10/2014 at 12:07 AM