title: private clause with loop iterators and omp simd
body: <p>When I try this</p>
  
  <pre><code>#pragma omp simd private(i)
  for(i=0; i&lt;n; i++)
  </code></pre>
  
  <p>with GCC 5.2.1 I get the error </p>
  
  <pre><code>error: iteration variable ‘i’ should not be private
  </code></pre>
  
  <p>I get the same error for <code>firstprivate</code> and <code>lastprivate</code> as well.  I discovered this from this code</p>
  
  <pre><code>int i;
  #pragma omp parallel for simd reduction(+:sum) lastprivate(i)
  for(i=0; i&lt;(n/8)*8; i++) sum += a[i];
  for(; i&lt;n; i++) sum += a[i];
  </code></pre>
  
  <p>However, this works fine</p>
  
  <pre><code>int i,j;
  #pragma omp parallel for simd reduction(+:sum) lastprivate(j)
  for(i=0; i&lt;(n/8)*8; i++) {sum += a[i]; j=i;}
  for(; j&lt;n; j++) sum += a[j];
  </code></pre>
  
  <p>but that seems like a silly hack. </p>
  
  <p>GCC also gives me the warning </p>
  
  <pre><code>warning: ‘i’ may be used uninitialized in this function
  </code></pre>
  
  <p>with <code>parallel for simd</code> but no warning when only using <code>parallel for</code>.</p>
  
  <p>Why can't I explicitly declare the parallel loop iterator as <code>private</code> with <code>omp simd</code> or <code>omp for simd</code>? Isn't it implicitly <code>private</code> anyway?</p>
  
  <hr>
  
  <p>From the OpenMP 4.0 specification section 14.1.2 recommended in Hristo Iliev's answer.</p>
  
  <blockquote>
    <p>The loop iteration variable in the associated for-loop of a simd construct with just one associated for-loop may be listed in a linear clause with a constant-linear-step
    that is the increment of the associated for-loop.</p>
    
    <p>The loop iteration variables in the associated for-loops of a simd construct with multiple associated for-loops may be listed in a lastprivate clause.</p>
  </blockquote>
  
  <p>This works</p>
  
  <pre><code>int i;
  #pragma omp parallel for simd reduction(+:sum) linear(i)
  for(i=0; i&lt;(n/8)*8; i++) sum += a[i];
  for(; i&lt;n; i++) sum += a[i];
  </code></pre>
  
  <p>but it appears <code>linear(i)</code> is not needed either so the iterator is by default <code>linear(i)</code>.</p>
  
  <p>So I could just have done</p>
  
  <pre><code> #pragma omp parallel for simd reduction(+:sum)
  </code></pre>
  
  <p>It appears the iterator is effectively <code>lastprivate</code> by default with <code>omp simd</code> and <code>omp for simd</code>.</p>
  
  <p>I still get a warning</p>
  
  <pre><code>‘i’ may be used uninitialized in this function
  </code></pre>
  
  <hr>
  
  <p>I should have picked section 14.1.1</p>
  
  <blockquote>
    <p>Certain variables and objects have predetermined data-sharing attributes as follows:</p>
    
    <p>The loop iteration variable in the associated for-loop of a <strong>simd</strong> construct with just one associated for-loop is linear with a constant-linear-step that is the increment of the associated for-loop.</p>
  </blockquote>
  
  <hr>
  
  <p>Here is a working example which gives the warning with <code>-fopenmp</code> but no warning without <code>-fopenmp</code></p>
  
  <pre><code>‘i’ may be used uninitialized in this function
  </code></pre>
  
  <p>The code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  //#define N 101                                                                                                       
  float foo(float *a, int n)
  {
      float sum = 0;
      int i;
      #pragma omp parallel for simd reduction(+:sum)
      for(i=0; i&lt;(n/8)*8; i++) sum += a[i];
      for(; i&lt;n; i++) sum += a[i];
      return sum;
  }
  
  int main(void) {
      const int N = 101;
      float a[N]; for(int i=0; i&lt;N; i++) a[i] = i;
      printf("%f\n", foo(a,N));
  }
  </code></pre>