title: Private 'for' loop for every thread in OpenMP
body: <p><strong>See edit below for my preliminary solution</strong> </p>
  
  <p>Consider the following code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;omp.h&gt;
  
  int main(void) {
  
  int counter = 0;
  int i;
  
  omp_set_num_threads(8); 
  
  #pragma omp parallel
          { 
              int id = omp_get_thread_num();
              #pragma omp for private(i)
              for (i = 0; i&lt;10; i++) {
                  printf("id: %d thread: %d\n", i, id);
                  #pragma omp critical // or atomic
                  counter++;
              }
          }
  
  printf("counter %d\n", counter);
  
  return 0;
  }
  </code></pre>
  
  <p>I define the number of threads to be 8. For each of the 8 threads I would like to have a <code>for</code> loop for every individual threads that increments the variable <code>counter</code>. However, it seems that OpenMP parallelize the <code>for</code> loop:</p>
  
  <pre><code>i: 0 thread: 0
  i: 1 thread: 0
  i: 4 thread: 2
  i: 6 thread: 4
  i: 2 thread: 1
  i: 3 thread: 1
  i: 7 thread: 5
  i: 8 thread: 6
  i: 5 thread: 3
  i: 9 thread: 7
  counter 10
  </code></pre>
  
  <p>Consequently, <code>counter=10</code>, but I want <code>counter=80</code>. What can I do so that every threads performs its own <code>for</code> loop while all threads increment <code>counter</code>?</p>
  
  <p><strong>The following code gives the desired result:</strong>
  I added another outer <code>for</code> loop that loops from 0 to the maximal number of threads. Inside this loop I can then declare my <code>for</code> loop private for each thread. Indeed, <code>counter=80</code> in this case. <strong>Is this the optimal solution for this problem or is there a better one?</strong></p>
  
  <pre><code>int main(void) {
  
  
  omp_set_num_threads(8); 
  
  int mthreads = omp_get_max_threads();
  
  #pragma omp parallel for private(i)
      for (n=0; n&lt;mthreads; n++) {
              int id = omp_get_thread_num();
          for (i = 0; i&lt;10; i++) {
              printf("i: %d thread: %d\n", i, id);
              #pragma omp critical
              counter++;
          }
      }
  
  }
  printf("counter %d\n", counter);
  
  return 0;
  }
  </code></pre>