title: List ranking implementation in C with OpenMP -- Output error
body: <p>The following code shows my implementation of list ranking algorithm with OpenMP. When I execute this code without the pragmas I get the correct results, but when I include the pragmas, I get errors (occasionally) in the output. The outputs are shown at the bottom. You can see that the second time the output was wrong. This occurs randomly. <strong>When I remove the pragmas my output is always correct.</strong> Is there an error in the way I used the pragmas or is there a dependency I am missing. 
  (The sequential output is the expected output. When the parallel output matches sequential output the program prints DATA OK)</p>
  
  <p>length and number of threads are 16.</p>
  
  <pre><code>#define NSIZE 1
  #define NMAX 16
  int Ns[NSIZE] = {16};
  int A[NMAX] = {14,13,5,16,11,10,9,12,0,8,7,15,4,3,2,1}; 
  int B[NMAX + 1] = {0};
  
  int S[NMAX + 1] = {0};
  
  int Rp[NMAX + 1] = {0};
  int next[NMAX+1] = {0};
  
  for(int i = 1, j=0; i &lt;= n; i++, j++)
  {
      B[i] = A[j];
  
  }
  
  int chunk = ceil(length/nthreads);
  int i, j;
  int tid;
  //#pragma omp parallel num_threads(nthreads)
  //{
  //#pragma omp for schedule(dynamic, chunk) private(i)
  for(i = 1; i &lt;= length; i++)
  {
  
      Rp[i] = 1;
      next[i] = S[i];
  }
  
  
  for(i = 1; i&lt;=log2(length); i++)
  {
  #pragma omp parallel num_threads(nthreads) shared(Rp,next,chunk) private(j)
  {
      #pragma omp for schedule(dynamic,chunk)
      for(j = 1; j &lt;= length; j++)
      {
          if(next[j]!=0)
          {
              Rp[j] = Rp[j] + Rp[next[j]];
  
              next[j] = next[next[j]];
          }
      }
  
  }
  }
  </code></pre>
  
  <p>OUTPUT:</p>
  
  <p>./a.out -- (This was the output when I ran the program the first time)</p>
  
  <p>from parallel<br>
  data OK<br>
  Input:   14  13   5  16  11  10   9  12   0   8   7  15   4   3   2   1<br>
  Sequential:    6  10   4   8   3  15   1  13   0  14   2  12   9   5  11   7<br>
  Parallel :   6 10  4  8  3 15  1 13  0 14  2 12  9  5 11  7 </p>
  
  <p>./a.out -- (output when I ran the program the second time)
  from parallel<br>
  data MISMATCH!!!<br>
  Input:   14  13   5  16  11  10   9  12   0   8   7  15   4   3   2   1<br>
  Sequential:    6  10   4   8   3  15   1  13   0  14   2  12   9   5  11   7<br>
  Parallel :   6 10  4  8  3 15  1 13  0 10  2 12  9  5 11  7</p>
  
  <p>./a.out -- (output when I ran the program the third time)</p>
  
  <p>from parallel<br>
  data OK<br>
  Input:   14  13   5  16  11  10   9  12   0   8   7  15   4   3   2   1<br>
  Sequential:    6  10   4   8   3  15   1  13   0  14   2  12   9   5  11   7<br>
  Parallel :   6 10  4  8  3 15  1 13  0 14  2 12  9  5 11  7 </p>