title: Initialize variable for omp reduction
body: <p>The OpenMP standard specifies an initial value for a reduction variable. So do I have to initialize the variable and how would I do that in the following case:</p>
  
  <pre><code>int sum;
  //...
  for(int it=0;i&lt;maxIt;i++){
  #pragma omp parallel
  {
    #pragma omp for nowait
    for(int i=0;i&lt;ct;i++)
      arrayX[i]=arrayY[i];
  
    sum = 0;
    #pragma omp for reduction(+:sum)
    for(int i=0;i&lt;ct;i++)
      sum+=arrayZ[i];
  }
  //Use sum
  }
  </code></pre>
  
  <p>Note that I use only 1 parallel region to minimize overhead and to allow the nowait in the first loop. Using this as-is would lead to a data race (IMO) because the threads coming from the first loop after other threads started the 2nd loop will reset sum.<br>
  Of course I can do this at the top of the outer loop but in a general case and for large code bases you may forget that you need or had set it there which produces unexpected results.<br>
  Does "omp single" help here? I suspect that while thread A executes the single, another thread may already enter the reduction loop.
  "omp barrier" is possible but I want to avoid that as it defeats the "nowait".</p>
  
  <p>And last another example:</p>
  
  <pre><code>#pragma omp parallel
  {
    sum = 0;
    #pragma omp for reduction(+:sum)
    for(int i=0;i&lt;ct;i++)
      sum+=arrayZ[i];
    //Use sum
    sum = 0;
    #pragma omp for reduction(+:sum)
    for(int i=0;i&lt;ct;i++)
      sum+=arrayZ[i];
    //Use sum
  }
  </code></pre>
  
  <p>How would I (re)initialize here?</p>