title: OpenMP parallel for slow down my code (C language)
body: <p>I'm trying to use openMP to speed up a the parallel version of list ranking. My implementation is as follows:</p>
  
  <pre><code>int ListRankingParallel(int *R1,int *S, int N)
  {
  int i;
  int *Q = (int*)malloc(N * sizeof(int));
  
  #pragma omp parallel for private(i)
  for (i=0; i&lt;N; i++){
  
      if( S[i] != -1)R1[i] = 1;
      else R1[i] = 0;
      Q[i] = S[i];
  
  }
  
  #pragma omp parallel for private(i)
  for(i=0; i&lt;N; i++)
      while (Q[i] != -1 &amp; Q[Q[i]] != -1) {
          R1[i] = R1[i] + R1[Q[i]];
          Q[i] = Q[Q[i]];
      }
  
  free(Q);
  
  return *R1;
  }
  </code></pre>
  
  <p>The serial version of my list ranking is</p>
  
  <pre><code>int ListRankingSerial(int *R2,int *S, int N)
  {
  int temp;  
  int j,i;
  for( i=0; i&lt;N; i++){
      j = 0;
      temp = S[i];
      while(S[i]!=-1)
      {
          j++;
          S[i] = S[S[i]];
      }
      R2[i] = j;
      S[i] = temp;
  }
  
  return *R2;
  }
  </code></pre>
  
  <p>When I run them repectively, using </p>
  
  <pre><code>get_walltime(&amp;S1);
  ListRankingParallel(R1,S,N);
  get_walltime(&amp;E1);
  
  get_walltime(&amp;S3);
  ListRankingSerial(R3,S,N);
  get_walltime(&amp;E3);
  </code></pre>
  
  <p>If I run my code on my Mac, the parallel version runs significantly faster than the serial version. However, if I run it on another linux cluster, the  parallel version is twice slower than the serial version.</p>
  
  <p>On my mac, I compile my code using</p>
  
  <pre><code>gcc-7 -fopenmp &lt;file name&gt;.c 
  </code></pre>
  
  <p>On the cluster, using</p>
  
  <pre><code>gcc -fopenmp &lt;file name&gt;.c 
  </code></pre>
  
  <p>If you want to test my code, please use:</p>
  
  <pre><code>int main(){
  
  int N = 1e+5;
  int *S = (int*)malloc(N * sizeof(int));
  int *R1 = (int*)malloc(N * sizeof(int));
  int *R3 = (int*)malloc(N * sizeof(int));
  double S1,S2,S3,E1,E2,E3;
  int i;
  
  for( i = 0; i &lt; N; i++)
      S[i] = i+1;
  
  S[N-1] = -1;
  
  get_walltime(&amp;S1);
  ListRankingParallel(R1,S,N);
  get_walltime(&amp;E1);
  printf("%f\n",E1-S1);
  
  get_walltime(&amp;S3);
  ListRankingSerial(R3,S,N);
  get_walltime(&amp;E3);
  printf("%f\n",E3-S3);
  
  }
  </code></pre>
  
  <p>Can anyone please give me some advice? Thank you!</p>