title: OpenMP while loop
body: <p>I have a code that runs many iterations and only if a condition is met, the result of the iteration is saved. This is naturally expressed as a while loop. I am attempting to make the code run in parallel, since each realisation is independent. So I have this:</p>
  
  <pre><code>while(nit&lt;avit){
      #pragma omp parallel shared(nit,avit)
      {
          //do some stuff
          if(condition){
              #pragma omp critical
              {
                  nit++;
                  \\save results
              }
          }
      }//implicit barrier here
  }
  </code></pre>
  
  <p>and this works fine... but there is a barrier after each realization, which means that if the stuff I am doing inside the parallel block takes longer in one iteration than the others, all my threads are waiting for it to finish, instead of continuing with the next iteration.</p>
  
  <p>Is there a way to avoid this barrier so that the threads keep working? I am averaging thousands of iterations, so a few more don't hurt (in case the <code>nit</code> variable has not been incremented in already running threads)...</p>
  
  <p>I have tried to turn this into a parallel for, but the automatic increment in the for loop makes the <code>nit</code> variable go wild. This is my attempt:</p>
  
  <pre><code>#pragma omp parallel shared(nit,avit)
  {
      #pragma omp for
      for(nit=0;nit&lt;avit;nit++){
          //do some stuff
          if(condition){
              \\save results
          } else {
              #pragma omp critical
              {
                  nit--;
              }
          }
      }
  }
  </code></pre>
  
  <p>and it keeps working and going around the for loop, as expected, but my <code>nit</code> variable takes unpredictable values... as one could expect from the increase and decrease of it by different threads at different times.</p>
  
  <p>I have also tried leaving the increment in the for loop blank, but it doesn't compile, or trying to trick my code to have no increment in the for loop, like</p>
  
  <pre><code>...
  incr=0;
  for(nit=0;nit&lt;avit;nit+=incr)
  ...
  </code></pre>
  
  <p>but then my code crashes...</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks</p>
  
  <p>Edit: Here's a working minimal example of the code on a while loop:</p>
  
  <pre><code>#include &lt;random&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;time.h&gt;
  #include &lt;omp.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  using namespace std;
  
  int main(){
  
      int nit,dit,avit=100,t,j,tmax=100,jmax=10;
      vector&lt;double&gt; Res(10),avRes(10);
  
      nit=0; dit=0;
      while(nit&lt;avit){
          #pragma omp parallel shared(tmax,nit,jmax,avRes,avit,dit) private(t,j) firstprivate(Res)
          {
              srand(int(time(NULL)) ^ omp_get_thread_num());
              t=0; j=0;
              while(t&lt;tmax&amp;&amp;j&lt;jmax){
                  Res[j]=rand() % 10;
                  t+=Res[j];
                  if(omp_get_thread_num()==5){
                      usleep(100000);
                  }
                  j++;
              }
              if(t&lt;tmax){
                  #pragma omp critical
                  {
                      nit++;
                      for(j=0;j&lt;jmax;j++){
                          avRes[j]+=Res[j];
                      }
                      for(j=0;j&lt;jmax;j++){
                          cout&lt;&lt;avRes[j]/nit&lt;&lt;"\t";
                      }
                      cout&lt;&lt;" \t nit="&lt;&lt;nit&lt;&lt;"\t thread: "&lt;&lt;omp_get_thread_num();
                      cout&lt;&lt;endl;
                  }
              } else{
                  #pragma omp critical
                  {
                      dit++;
                      cout&lt;&lt;"Discarded: "&lt;&lt;dit&lt;&lt;"\r"&lt;&lt;flush;
                  }
              }
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>I added the <code>usleep</code> part to simulate one thread taking longer than the others. If you run the program, all threads have to wait for thread 5 to finish, and then they start the next run. what I am trying to do is precisely to avoid such wait, i.e. I'd like the other threads to pick the next iteration without waiting for 5 to finish.</p>