title: Why can't I use reduction with default(shared)?
body: <p>Here's the outline of my code:</p>
  
  <pre><code>#pragma omp parallel default(shared)
  {
  for(i; i&lt;lim; i++)
      do_work();
  }
  </code></pre>
  
  <p>And a while later:</p>
  
  <pre><code>do_work(){
      foo();
      bar();
  }
  
  foo(){
      #pragma omp for //etc
      for(i;i&lt;l;i++) //your typical loop
  }
  
  bar(){ //here's the interesting part
      int i;
      int result;
  
      #pragma omp for reduction(+:result) private(i)
      for(i=0; i&lt;lim; i++)
          result++;
  }
  </code></pre>
  
  <p>When compiled I get the following error:</p>
  
  <blockquote>
    <p>reduction variable ‘result’ is private in outer context</p>
  </blockquote>
  
  <p>which shouldn't be happening because according to the <a href="http://publib.boulder.ibm.com/infocenter/cellcomp/v101v121/index.jsp?topic=/com.ibm.xlcpp101.cell.doc/compiler_ref/prag_omp_for.html" rel="nofollow">IBM compiler documentation</a> the <code>reduction</code> clause</p>
  
  <blockquote>
    <p>Performs a reduction on all scalar variables in list using the specified operator. Reduction variables in list are separated by commas.</p>
    
    <p>A private copy of each variable in list is created for each thread. At the end of the statement block, the final values of all private copies of the reduction variable are combined in a manner appropriate to the operator, and the result is placed back into the original value of the shared reduction variable.</p>
    
    <blockquote>
      <p>Variables specified in the reduction clause:</p>
    </blockquote>
    
    <ul>
    <li>must be of a type appropriate to the operator.</li>
    <li><strong>must be shared in the enclosing context.</strong></li>
    <li>must not be const-qualified.</li>
    <li>must not have pointer type.</li>
    </ul>
  </blockquote>
  
  <p>Emphasis added. Since the outer parallel region is supposed to handle all variables as <code>shared</code>, which means that <code>result</code> should get converted to <code>private</code> once a reduction on it is found. At the very least though it shouldn't be considered <code>private</code> in the outer scope, because the outer scope has explicitly told every variable to be <code>shared</code>. That's the source of my confusion.</p>
  
  <p>Granted I'm using GCC instead of the IBM compiler but is there a difference in this case?</p>
  
  <p>So I guess my question is: Why does OpenMP treat a reduction var. as a private variable when it's previously declared as shared in the outer context?</p>
  
  <p>I only have this problem with reductions, everything else works as it's supposed to (especially when there's an <a href="https://computing.llnl.gov/tutorials/openMP/#REDUCTION" rel="nofollow">example which does the exact same thing</a>).</p>