title: Using shared variable in Parallel For
body: <p>I am struggling in using Parallel.For in the below code instead of for loop.
  Since the size of the CoefficientVector vector array is rather big, it makes sense to me only to reset the array elements value instead of creating it new for each iteration. </p>
  
  <p>I try to replace the outer loop with Parallel For; and assuming each partition of the parallel for, ran by a separate thread, will have it's own copy of CoefficientVector class it therefore makes sense(?) to me to have one instance of the CoefficientVector object for each thread and reset the vector elements rather than recreating the array. I though find it hard to do this optimisation(?) on Parallel For. Could anyone help please.</p>
  
  <pre><code> static void Main(string[] args)
      {
          System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
          timer.Start(); 
  
          int numIterations = 20000;
          int numCalpoints = 5000;
          int vecSize = 10000;
  
          CalcPoint[] calcpoints = new CalcPoint[numCalpoints];
  
          CoefficientVector coeff = new CoefficientVector();
          coeff.vectors = new Vector[vecSize];
  
          //not sure how to correctly use Parallel.For here
          //Parallel.For(0, numCalpoints, =&gt;){
          for (int i = 0; i &lt; numCalpoints;i++)
          {
              CalcPoint cp = calcpoints[i];
  
              //coeff.vectors = new Vector[vecSize];
              coeff.ResetVectors();
  
              //doing some operation on the matrix n times
              for (int n = 0; n &lt; numIterations; n++)
              {
                  coeff.vectors[n].x += n;
                  coeff.vectors[n].y += n;
                  coeff.vectors[n].z += n;
              }
  
              cp.result = coeff.GetResults();
          }
  
          Console.Write(timer.Elapsed);
          Console.Read();
      }
  }
  
  class CoefficientVector
  {
      public Vector[] vectors;
  
      public void ResetVectors()
      {
          for (int i = 0; i &lt; vectors.Length; i++)
          {
              vectors[i].x = vectors[i].y = vectors[i].z = 0;
          }
      }
  
      public double GetResults()
      {
          double result = 0;
          for (int i = 0; i &lt; vectors.Length; i++)
          {
              result += vectors[i].x * vectors[i].y * vectors[i].z;
          }
          return result;
      }
  }
  
  struct Vector
  {
      public double x;
      public double y;
      public double z;
  }
  
  struct CalcPoint
  {
      public double result;
  }
  </code></pre>