OMP error

Could anyone help me to understand why the call of the intrinsic RANDOM_NUMBER within a PARALLEL DO directive in some cases it returns values larger than 1?

Here it follows the test code I made to show the problem.

      program test
      implicit none
      real*8   e_r
      integer*4 nps,i,j
      write(*,*) 'Number of tests' 
      read(*,*) nps
      i=0
c$OMP parallel do private(e_r) reduction(+:i)
      do j=1,nps
         call random_number(e_r)
         if(e_r.gt.1.0d0)then
            write(*,*) e_r
            i=i+1
         endif
      end do
c$OMP end parallel do
      write(*,*) i
      end

the above code was compiled as follows

pgfortran test.f -mp -o test

Here below you can find the output

valerio@giusti-145 ~/Documents $ ./test
 Number of tests
1000
    1.539883970671497     
    1.476670404429072     
    1.412861223654971     
            3

Thanks in advance for any suggestion (…and forgive me if I did something stupid :-) ).

The above Fortran code compiled with the Intel Fortran Compiler through the following command:

ifort test.f -openmp -o test

does not return any random number larger than 1.

valerio@giusti-145 ~/Documents $ ./test
 Number of tests
1000000
           0

Hi Valerio,

I don’t think our Randon_number’s default seed generator is thread safe and might be the root of the problem. I added a problem report (TPR#19454) to see what our engineers can do.

I was able to work around the problem by explicitly setting the seed.

% cat rand.F90
      program test
      use omp_lib
      implicit none
      real*8   e_r
      integer*4 nps,i,j, thid, seed(32)
      write(*,*) 'Number of tests'
      read(*,*) nps
      seed=123
      call random_seed(put=seed)
      i=0
c$OMP parallel do private(j,e_r) reduction(+:i)
      do j=1,nps
         e_r = 0.0d0
         call random_number(e_r)
         if(e_r.gt.1.0d0)then
            write(*,*) e_r
            i=i+1
         endif
      end do
c$OMP end parallel do
      write(*,*) i
      end
% pgf90 rand.F90 -Mfixed -mp; a.out
 Number of tests
10000
            0
  • Mat

Hi Mat,
thanks for your answer. I will use your trick to workaround the problem.

Valerio

Valerio,

TPR 19454 has been fixed as of the 13.9 release.

thanks,
dave