title: parallel 'task's inside an already parallelized 'for' loop in OpenMP
body: <p><em>[Background: OpenMP v4+ on Intel's icc compiler]</em> </p>
  
  <p>I want to parallelize tasks inside a loop that is already parallelized.  I saw quite a few queries on subjects close to this one, e.g.:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/13810400/parallel-sections-in-openmp-using-a-loop">Parallel sections in OpenMP using a loop</a></li>
  <li><a href="https://stackoverflow.com/questions/23178183/doing-a-section-with-one-thread-and-a-for-loop-with-multiple-threads">Doing a section with one thread and a for-loop with multiple threads</a></li>
  <li>and <a href="https://stackoverflow.com/questions/13788638/difference-between-section-and-task-openmp">others</a> with more concentrated wisdom still.</li>
  </ul>
  
  <p>but I could not get a definite answer other than a compile time error message when trying it.</p>
  
  <p>Code:</p>
  
  <pre><code> #pragma omp parallel for private(a,bd) reduction(+:sum)
      for (int i=0; i&lt;128; i++) {
          a = i%2;
          for (int j=a; j&lt;128; j=j+2) {
               u_n = 0.25 * ( u[ i*128 + (j-3) ]+
                              u[ i*128 + (j+3) ]+
                              u[ (i-1)*128 + j ]+
                              u[ (i+1)*128 + j ]);
            // #pragma omp single nowait 
            // {
            // #pragma omp task shared(sum1) firstprivate(i,j)
            // sum1 = (u[i*128+(j-3)]+u[i*128+(j-2)] + u[i*128+(j-1)])/3;
            // #pragma omp task shared(sum2) firstprivate(i,j)
            // sum2 = (u[i*128+(j+3)]+u[i*128+(j+2)]+u[i*128+(j+1)])/3; 
            // #pragma omp task shared(sum3) firstprivate(i,j)
            // sum3 = (u[(i-1)*128+j]+u[(i-2)*128+j]+u[(i-3)*128+j])/3;
            // #pragma omp task shared(sum4) firstprivate(i,j)
            // sum4 = (u[(i+1)*128+j]+u[(i+2)*128+j]+u[(i+3)*128+j])/3;
            // }
            // #pragma omp taskwait 
            // {
            // u_n = 0.25*(sum1+sum2+sum3+sum4);
            // }
               bd = u_n - u[i*128+ j];
               sum += diff * diff;
               u[i*128+j]=u_n;
         }    
    }
  </code></pre>
  
  <p>In the above code, I tried replacing the <code>u_n = 0.25 *(...);</code> line with the 15 commented lines, to try not only to paralllelize the iterations  over the 2 <code>for</code> loops, but also to acheive a degree of parallelism on each of the 4 calculations  (<code>sum1</code> to <code>sum4</code>) involving array <code>u[]</code>. </p>
  
  <p>The compile error is fairly explicit:</p>
  
  <blockquote>
    <p>error: the OpenMP "single" pragma must not be enclosed by the
    "parallel for" pragma</p>
  </blockquote>
  
  <p>Is there a way around this so I can optimize that calculation further with OpenMP ?</p>