title: OpenMP reduction, variable not private?
body: <p>I have an array like this (0,0 is bottom left):</p>
  
  <pre><code>0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0
  0 0 1 0 0 0 0 0 0
  0 0 1 0 1 0 1 0 0
  0 0 1 1 1 1 1 1 1
  1 0 1 1 1 1 1 1 1
  1 1 1 1 1 1 1 1 1
  </code></pre>
  
  <p>My goal is to get the index of the higher line who is not completely set to <code>0</code>. For this I made the code below (which works fine):</p>
  
  <pre><code>max=0;
  for (i=0 ; i&lt;width ; ++i) {
    for (j=max ; j&lt;height ; ++j) {
      if (array[i*height+j]!=0) {
        max=j;
      }
    }
  }
  </code></pre>
  
  <p>For the second loop I initialize <code>j</code> to max, because the global maximum cannot be less than a local maximum. And this way I can reduce the number of tests.</p>
  
  <p>The I tried to parallelize it with <code>OpenMp</code>. My code is now:</p>
  
  <pre><code>max=0;
  #pragma omp parallel for  default(none)                 \
                            shared(spec, width, height)   \
                            collapse(2)                   \
                            reduction(max:max)
  for (i=0 ; i&lt;width ; ++i) {
    for (j=max ; j&lt;height ; ++j) {
      if (array[i*height+j]!=0) {
        max=j;
      }
    }
  }
  </code></pre>
  
  <p>Which leads to a segmentation fault. In order to make it works, I changed <code>j=max</code> to <code>j=0</code>. So the problem seems to come from the <code>max</code> variable.</p>
  
  <p>I don't understand why, because with the reduction this variable should be private (or lastprivate) between each threads. So why does it make it crash ? And how can I use my "optimization" with OpenMP ?</p>