5

I'm trying to understand how the MaxDegreeOfParallelism actually affects the parallelism when calling Parallel.For. Here's the code I'm experimenting with:

  static void Main(string[] args)
  {
     var parallelOptions = new ParallelOptions()
        {
           MaxDegreeOfParallelism = 1000,
        };

     Parallel.For(1, 1000, parallelOptions, i =>
        {
           Console.WriteLine(i);
           Thread.Sleep(TimeSpan.FromHours(1));
        });
  }

When I run this code, I see the console output 1 to 9 instantly (within ~0.1 second). Then once every second a new number will be added - 10, 11, 12 and so on. Meanwhile, in the Windows Task Manager I see that the number of executing threads in the process increases with one new thread per second.

With this code, why don't I see the output of the values 1 to 1000 instantly?

(I realize that this code might make zero sense and that it's probably a bad idea to spin up 1000 threads on my laptop, but I want to understand what's going on here)

EDIT: This question was - in my opinion - incorrectly marked as a duplicate. I understand that MaxDegreeOfParallelism is the max degree of parallelism. Of course there would be a large amount of context-switching if I have 1000 threads running at the same time, but the linked question does not explain how it actually works. What if I just want to run a more reasonable number of threads, say 32? My computer is well capable of handling that, but with the behavior of Parallel.For described above it takes ~20 seconds to spin up that number of threads.

Nitramk
  • 1,542
  • 6
  • 25
  • 42
  • 1
    Because it is a *Max* DOP, not an exact value of how many things are done in parallel. Since you computer is incapable of executing 1000 tasks simultaneously (probably), it doesn't try. MaxDOP is meant to be a limiter, like limiting MaxDOP to 2 instead of how many cores are on your box. – vcsjones Mar 17 '15 at 21:26
  • Possible duplicate of: http://stackoverflow.com/questions/9538452/what-does-maxdegreeofparallelism-do – Greg Mar 17 '15 at 21:28
  • In short, if you don't utilize the processor. It will hinder all task running in parallel for the maximum that you specify. This is quite common for SQL in particular. – Greg Mar 17 '15 at 21:29
  • 1
    My computer is capable of running 1000 threads at the same time. If I tell Parallel.For to run say 32 threads, why does it wait 1 second between starting each new thread? – Nitramk Mar 17 '15 at 21:48

1 Answers1

3

MaxDegreeOfParallelism refers to the maximum number of worker tasks that will be scheduled at any one time by a parallel loop.

The degree of parallelism is automatically managed by the implementation of the Parallel class, the default task scheduler, and the .NET thread pool. The throughput is optimized under a wide range of conditions.

For very large degree of parallelism, you may also want to use the ThreadPool class’s SetMinThreads method so that these threads are created without delay. If you don't do this then the thread pool’s thread injection algorithm may limit how quickly threads can be added to the pool of worker threads that is used by the parallel loop. It may take more time than you want to create the required number of threads.

Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73