title: Parallelizing pi number calculation
body: <p>The program below calculates pi=3.1415 originally wrote it in C++ using OpenMP #pragma omp for and Reduction(+:sum) 
  I am trying to do the same calculation in C# (newbie) using Parallel.For 
  for some reason I can't make it work, how do I make sum private to each thread in C#? </p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.Threading.Tasks;
  
  
  namespace pi
  {
      class Program
      {
          static void Main(string[] args)
          {
              long num_steps = 100000000;
              double step;
              double x, pi, sum = 0.0;
              step = 1.0 / num_steps;
              Stopwatch timer = Stopwatch.StartNew();
              Parallel.For(1, num_steps + 1, new ParallelOptions { MaxDegreeOfParallelism = 4 }, i =&gt;
                  {
                      x = (i - 0.5) * step;                    
                      sum = sum + 4.0 / (1.0 + x * x);
                  });            
              pi = step * sum;
              timer.Stop();
              Console.WriteLine("\n pi with {0} steps is {1} in {2} miliseconds ", num_steps, pi, (timer.ElapsedMilliseconds));
              Console.ReadKey();
          }
      }
  }
  </code></pre>