title: Why this OpenMP parallel for loop doesn't work properly?
body: <p>I would like to implement OpenMP to parallelize my code. I am starting from a very basic example to understand how it works, but I am missing something...</p>
  
  <p>So, my example looks like this, without parallelization:</p>
  
  <pre><code>int main() {
    ...
  
    for (i = 0; i &lt; n-1; i++) {
      u[i+1] = (1+h)*u[i]; // Euler
      v[i+1] = v[i]/(1-h); // implicit Euler
    }
  
    ...
  
    return 0;
  }
  </code></pre>
  
  <p>Where I omitted some parts in the "..." because are not relevant. It works, and if I print the <code>u[]</code> and <code>v[]</code> arrays on a file, I get the expected results.</p>
  
  <p>Now, if I try to parallelize it just by adding:</p>
  
  <pre><code>#include &lt;omp.h&gt;
  
  int main() {
    ...
  
    omp_set_num_threads(2);
  
    #pragma omp parallel for
    for (i = 0; i &lt; n-1; i++) {
      u[i+1] = (1+h)*u[i]; // Euler
      v[i+1] = v[i]/(1-h); // implicit Euler
    }
  
    ...
  
    return 0;
  }
  </code></pre>
  
  <p>The code compiles and the program runs, BUT the <strong><code>u[]</code></strong> and <strong><code>v[]</code></strong> arrays are half full of zeros.</p>
  
  <p>If I set <strong><code>omp_set_num_threads( 4 )</code></strong>, I get three quarters of zeros.<br>
  If I set <strong><code>omp_set_num_threads( 1 )</code></strong>, I get the expected result.</p>
  
  <p>So it looks like only the first thread is being executed, while not the other ones... </p>
  
  <p>What am I doing wrong?</p>