title: Parallelizing a for loop in Visual Studio 2010 (OpenMP)
body: <p>I've recently reading up about OpenMP and was trying to parallelize some existing for loops in my program to get a speed-up. However, for some reason I seem to be getting garbage data written to the file. What I mean by that is I don't have Points 1,2,3,4 etc. written to my file, I have Points 1,4,7,8 etc. I suspect this is because I am not keeping track of the threads and it just leads to race conditions? </p>
  
  <p>I have been reading as much as I can find about OpenMP, since it seems like a great abstraction to do multi-threaded programing. I'd appreciate any pointers please to get to the bottom of what I might be doing incorrectly.</p>
  
  <p>Here is what I have been trying to do so far (only the relevant bit of code):</p>
  
  <pre><code>#include &lt;omp.h&gt;
  
  pixelIncrement = Image.rowinc/2;
  
  #pragma omp parallel for
  for (int i = 0; i &lt; Image.nrows; i++ )
     {
        int k =0;
        row     = Image.data + i * pixelIncrement;
  
        #pragma omp parallel for
        for (int j = 0; j &lt; Image.ncols; j++)
        {
            k++; 
            disparityThresholdValue = row[j];  
                   // Don't want to save certain points
               if ( disparityThresholdValue &lt; threshHold)
               {       
                  // Get the data points
                  x = (int)Image.x[k]; 
                  y = (int)Image.y[k]; 
                  z = (int)Image.z[k]; 
                  grayValue= (int)Image.gray[k];      
  
                  cloudObject-&gt;points[k].x = x;
                  cloudObject-&gt;points[k].y = y;
                  cloudObject-&gt;points[k].z = z;
                  cloudObject-&gt;points[k].grayValue = grayValue;
  
          fprintf( cloudPointsFile, "%f %f %f %d\n", x, y, z, grayValue);
               }
        }
     }
  
     fclose( pointFile );
  </code></pre>
  
  <p>I did enable OpenMP in my Compiler settings (C/C++ -> Language -> Open MP Support (/openmp).</p>
  
  <p>Any suggestions as to what might be the problem? I am using a Quadcore processor on Windows XP 32-bit.</p>