title: Parallel.For causing huge context switching overhead for this case... Why?
body: <p>I am experimenting with the new parallelism tools in .NET 4, by calculating Pi using Monte Carlo methods.</p>
  
  <p>(The actual algorithm is not so important, but for clarity's sake, here it is:</p>
  
  <ol>
  <li>Pick <code>numIterations</code> random points inside a unit square.</li>
  <li>Count the number of these points that lie within a circle that is bounded by that square (i.e. the points whose distance from the centre of the square is less than 0.5)</li>
  <li>Then, for very large <code>numIterations</code>, <code>PI=4 * iterationsInsideCircle / numIterations</code>.)</li>
  </ol>
  
  <p>I have a method <code>int ThrowDarts(int numDarts)</code> which picks <code>numDarts</code> random points inside the unit square (described above) and returns the number of points which lie within the unit circle:</p>
  
  <pre><code>    protected static int ThrowDarts(int iterations)
      {
          int dartsInsideCircle = 0;
          Random random = new Random();
          for (int iteration = 0; iteration &lt; iterations; iteration++)
          {
              double pointX = random.NextDouble() - 0.5;
              double pointY = random.NextDouble() - 0.5;
  
              double distanceFromOrigin = Math.Sqrt(pointX*pointX + pointY*pointY);
              bool pointInsideCircle = distanceFromOrigin &lt;= 0.5;
  
              if (pointInsideCircle)
              {
                  dartsInsideCircle++;
              }
          }
          return dartsInsideCircle;
      }
  </code></pre>
  
  <p>Essentially, in each of my different implementations (which each use different parallel mechanisms), I am writing different ways of throwing and counting the darts inside the circle.</p>
  
  <p>For example, my single threaded implementation is simply:</p>
  
  <pre><code>    protected override int CountInterationsInsideCircle()
      {
          return ThrowDarts(_numInterations);
      }
  </code></pre>
  
  <p>I also have this method for one of my parallel algorithms:</p>
  
  <pre><code>    protected override int CountInterationsInsideCircle()
      {
          Task&lt;int&gt;[] tasks = new Task&lt;int&gt;[_numThreads];
  
          for (int i = 0; i &lt; _numThreads; i++)
          {
              tasks[i] = Task.Factory.StartNew(() =&gt; ThrowDarts(_numInterations/_numThreads));
          }
  
          int iterationsInsideCircle = 0;
          for (int i = 0; i &lt; _numThreads; i++)
          {
              iterationsInsideCircle += tasks[i].Result;
          }
  
          return iterationsInsideCircle;
      }
  </code></pre>
  
  <p>Hopefully you get the picture.</p>
  
  <p>Here, I get to my conundrum. The <code>Parallel.For</code> version I am writing causes massive amounts of context switching. The code is below:</p>
  
  <pre><code>    protected override int CountInterationsInsideCircle()
      {
          ConcurrentBag&lt;int&gt; results = new ConcurrentBag&lt;int&gt;();
          int result = 0;
  
          Parallel.For(0, _numInterations,
                       // initialise each thread by setting it's hit count to 0
                       () =&gt; 0,
                       //in the body, we throw one dart and see whether it hit or not
                       (iteration, state, localState) =&gt; localState + ThrowDarts(1),
                       // finally, we sum (in a thread-safe way) all the hit counts of each thread together
                       results.Add);
  
          foreach(var threadresult in results)
          {
              result+=threadresult;
          }
  
          return result;
      }
  </code></pre>
  
  <p>The version using <code>Parallel.For</code> does work, but very, very slowly, because of the aforementioned context switching (which does not occur in the previous two methods).</p>
  
  <p>Is anyone able to enlighten me as to why this may be happening?</p>