Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The default options for <code>Parallel.ForEach</code> <strong>only work well when the task is CPU-bound and scales linearly</strong>. When the task is CPU-bound, everything works perfectly. If you have a quad-core and no other processes running, then <code>Parallel.ForEach</code> uses all four processors. If you have a quad-core and some other process on your computer is using one full CPU, then <code>Parallel.ForEach</code> uses roughly three processors.</p> <p>But if the task is not CPU-bound, then <code>Parallel.ForEach</code> keeps starting tasks, trying hard to keep all CPUs busy. Yet no matter how many tasks are running in parallel, there is always more unused CPU horsepower and so it keeps creating tasks.</p> <p>How can you tell if your task is CPU-bound? Hopefully just by inspecting it. If you are factoring prime numbers, it is obvious. But other cases are not so obvious. The empirical way to tell if your task is CPU-bound is to limit the maximum degree of parallelism with <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism.aspx"><code>ParallelOptions.MaximumDegreeOfParallelism</code></a> and observe how your program behaves. If your task is CPU-bound then you should see a pattern like this on a quad-core system:</p> <ul> <li><code>ParallelOptions.MaximumDegreeOfParallelism = 1</code>: use one full CPU or 25% CPU utilization</li> <li><code>ParallelOptions.MaximumDegreeOfParallelism = 2</code>: use two CPUs or 50% CPU utilization</li> <li><code>ParallelOptions.MaximumDegreeOfParallelism = 4</code>: use all CPUs or 100% CPU utilization</li> </ul> <p>If it behaves like this then you can use the default <code>Parallel.ForEach</code> options and get good results. Linear CPU utilization means good task scheduling.</p> <p>But if I run your sample application on my Intel i7, I get about 20% CPU utilization no matter what maximum degree of parallelism I set. Why is this? So much memory is being allocated that the garbage collector is blocking threads. The application is resource-bound and the resource is memory.</p> <p>Likewise an I/O-bound task that performs long running queries against a database server will also never be able to effectively utilize all the CPU resources available on the local computer. And in cases like that the task scheduler is unable to "know when to stop" starting new tasks.</p> <p>If your task is not CPU-bound or the CPU utilization doesn't scale linearly with the maximum degree of parallelism, then you should advise <code>Parallel.ForEach</code> not to start too many tasks at once. The simplest way is to specify a number that permits some parallelism for overlapping I/O-bound tasks, but not so much that you overwhelm the local computer's demand for resources or overtax any remote servers. Trial and error is involved to get the best results:</p> <pre><code>static void Main(string[] args) { Parallel.ForEach(CreateData(), new ParallelOptions { MaxDegreeOfParallelism = 4 }, (data) =&gt; { data[0] = 1; }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload