title: DO WHILE loop with OpenMP in Fortran
body: <p>I am creating a random distribution of points in Fortran, and this is being done by a do while loop. I want to speed up this process via OpenMP, but I read that you can't simply use !$OMP PARALLEL DO for do while loops. I tried converting my original do while into a do loop nested in the do while. However, I can't see any speedups in the code,by this I mean it takes the same time as the serial version. I can't seem to figure out what the issue is and I've been stuck, would appreciate any advice. I've shown the code below. </p>
  
  <p>The original loop:</p>
  
  <pre><code>!OMP PARALLEL DO
  do while (count &lt; size(zeta_list,2))
      call random_number(x)
      call random_number(y)
      x1 = a + FLOOR((b+1-a)*x)
      y1 = a + FLOOR((b+1-a)*y)
      if (abs(y1) &lt;= abs(1/x1)) then
          count = count + 1
          call random_number(theta)
          zeta_list(1,count) = x1*sin(2*pi_16*theta)
          zeta_list(2,count) = x1*cos(2*pi_16*theta)
      end if 
  end do  
  !OMP END PARALLEL DO
  </code></pre>
  
  <p>and after I tried to convert it, </p>
  
  <pre><code>!$OMP PARALLEL 
  do while (count &lt; size(zeta_list,2))
      !$OMP DO
      do i=1,size(zeta_list,2),1
          call random_number(x)
          call random_number(y)
          x1 = a + FLOOR((b+1-a)*x)
          y1 = a + FLOOR((b+1-a)*y)
          if (abs(y1) &lt;= abs(1/x1)) then
              call random_number(theta)
              count = count + 1
              zeta_list(1,i) = x1*sin(2*pi_16*theta)
              zeta_list(2,i) = x1*cos(2*pi_16*theta)
          end if
      end do
      !$OMP END DO 
  end do  
  !$OMP END PARALLEL
  </code></pre>
  
  <p>The entire code is </p>
  
  <pre><code>PROGRAM RANDOM_DISTRIBUTION
  
  IMPLICIT NONE 
  
  DOUBLE PRECISION, DIMENSION(2,1000000)::zeta_list
  DOUBLE PRECISION::x,y,x1,y1,theta
  REAL::a,b,n
  INTEGER::count,t1,t2,clock_rate,clock_max,i
  DOUBLE PRECISION,PARAMETER::pi_16=4*atan(1.0_16)
  
  call system_clock ( t1, clock_rate, clock_max )
  
  n = 1000
  b = n/2
  a = -n/2
  count = 0
  zeta_list = 0
  x = 0
  y = 0
  x1 = 0 
  y1 = 0 
  theta = 0
  
  call random_seed()
  
  
  
  !$OMP PARALLEL 
  do while (count &lt; size(zeta_list,2))
      !$OMP DO
      do i=1,size(zeta_list,2),1
          call random_number(x)
          call random_number(y)
          x1 = a + FLOOR((b+1-a)*x)
          y1 = a + FLOOR((b+1-a)*y)
          if (abs(y1) &lt;= abs(1/x1)) then
              call random_number(theta)
              count = count + 1
              zeta_list(1,i) = x1*sin(2*pi_16*theta)
              zeta_list(2,i) = x1*cos(2*pi_16*theta)
          end if
      end do
      !$OMP END DO 
  end do  
  !$OMP END PARALLEL
  
  
  call system_clock ( t2, clock_rate, clock_max )
  write ( *, * ) 'Elapsed real time = ', real ( t2 - t1 ) / real ( clock_rate) ,'seconds' 
  
  
  stop
  END PROGRAM RANDOM_DISTRIBUTION
  </code></pre>
  
  <p>compiled with gfortran test.f90 -fopenmp </p>