title: How can I achieve maximum parallelism and utilize maximum CPU with Parallel.ForEach?
body: <p>There is a C# function <code>A(arg1, arg2)</code> which needs to be called lots of times. To do this fastest, I am using parallel programming.</p>
  
  <p>Take the example of the following code:</p>
  
  <pre><code>long totalCalls = 2000000;
  int threads = Environment.ProcessorCount;
  
  ParallelOptions options = new ParallelOptions(); 
  options.MaxDegreeOfParallelism = threads;
  
  Parallel.ForEach(Enumerable.Range(1, threads), options, range =&gt;
  {
      for (int i = 0; i &lt; total / threads; i++)
      {
          // init arg1 and arg2
          var value = A(arg1, agr2);
          // do something with value
      }
  });
  </code></pre>
  
  <p>Now the issue is that this is not scaling up with an increase in number of cores; e.g. on 8 cores it is using 80% of CPU and on 16 cores it is using 40-50% of CPU. I want to use the CPU to maximum extent.</p>
  
  <p>You may assume <code>A(arg1, arg2)</code> internally contains a complex calculation, but it doesn't have any IO or network-bound operations, and also there is no thread locking. What are other possibilities to find out which part of the code is making it not perform in a 100% parallel manner?</p>
  
  <p>I also tried increasing the degree of parallelism, e.g.</p>
  
  <pre><code>int threads = Environment.ProcessorCount * 2;
  // AND
  int threads = Environment.ProcessorCount * 4;
  // etc.
  </code></pre>
  
  <p>But it was of no help.</p>
  
  <p><strong>Update 1</strong> - if I run the same code by replacing <code>A()</code> with a simple function which is calculating prime number then it is utilizing 100 CPU and scaling up well. So this proves that other piece of code is correct. Now issue could be within the original function <code>A()</code>. I need a way to detect that issue which is causing some sort of sequencing.</p>