title: Parallelizing an inner loop with OpenMP
body: <p>Assume we have two nested loops. The inner loop should be parallelized, but the outer loop needs to be executed sequentially. Then the following code does what we want:</p>
  
  <pre><code>for (int i = 0; i &lt; N; ++i) {
    #pragma omp parallel for schedule(static)
    for (int j = first(i); j &lt; last(i); ++j) {
      // Do some work
    }
  }
  </code></pre>
  
  <p>Now assume that each thread has to obtain some thread-local object to carry out the work in the inner loop, and that getting these thread-local objects is costly. Therefore, we don't want to do the following:</p>
  
  <pre><code>for (int i = 0; i &lt; N; ++i) {
    #pragma omp parallel for schedule(static)
    for (int j = first(i); j &lt; last(i); ++j) {
      ThreadLocalObject &amp;obj = GetTLO(omp_get_thread_num()); // Costly!
      // Do some work with the help of obj
    }
  }
  </code></pre>
  
  <p>How can I solve this issue?</p>
  
  <ol>
  <li><p>Each thread should ask for its local object only once.</p></li>
  <li><p>The inner loop should be parallelized among all threads.</p></li>
  <li><p>The iterations of the outer loop should be executed one after the other.</p></li>
  </ol>
  
  <p>My idea is the following, but does it really want I want?</p>
  
  <pre><code>#pragma omp parallel
  {
    ThreadLocalObject &amp;obj = GetTLS(omp_get_thread_num());
    for (int i = 0; i &lt; N; ++i) {
      #pragma omp for schedule(static)
      for (int j = first(i); j &lt; last(i); ++j) {
        // Do some work with the help of obj
      }
    }
  }
  </code></pre>