1

I have given OpenAcc access to the subroutine called "lang_force", and within that subroutine I call the Fortran90 intrinsic RANDOM_NUMBER().

subroutine lang_force(damp, temp, noise)
  !$acc routine(lang_force)
  implicit none
  double precision, intent(in) :: damp, temp
  double precision, dimension(1:3), intent(out) :: noise
  double precision :: kb, a1, theta, phi, mag1, pi,r,s
  integer :: i,j,k
  double precision :: x,y,z

  kb = 1.3806E-23
  !kb = 1.0
  pi=4.D0*DATAN(1.D0)

  call random_number(a1)
  call random_number(r)
  call random_number(s)
  mag1 = sqrt(-2.0*log(a1))


  theta = r*pi
  phi = 2.0*s*pi
  x = mag1*cos(phi)*sin(theta)
  y = mag1*sin(theta)*sin(phi)
  z = mag1*cos(theta)

  noise(1) = sqrt(2.0*kb*temp*damp)*x
  noise(2) = sqrt(2.0*kb*temp*damp)*y
  noise(3) = sqrt(2.0*kb*temp*damp)*z
end subroutine lang_force

When compiled with the latest version of pdf90, it tells me that it needs access to RANDOM_NUMBER(). How do I declare a routine directive to such a fortran90 intrinsic subroutine?

Ian Bush
  • 6,996
  • 1
  • 21
  • 27
shan1224
  • 11
  • 1
  • Welcome, please take the [tour] and read [ask]. What *exactly* does it tell? Please copy and paste the error message exactly as it is printed. Is the intrinsic even supposed to be compatible with OpenACC? I somewhat doubt so. – Vladimir F Героям слава Sep 23 '20 at 16:25
  • The code seems fine, so the problem might be with compiler options. – JAlex Sep 23 '20 at 18:15

1 Answers1

2

Not all Fortran intrinsics are supported within device code including RANDOM_NUMBER.

RANDOM_NUMBER in particular is not thread safe since all threads would share the same state. Instead, you need to use cuRand for which we ship examples with the compilers under the "2020/examples/CUDA-Libraries/cuRAND/test_rand_oacc_ftn" directory.

While this is in C, I wrote about is in more detail on the NVIDIA user forums:

https://forums.developer.nvidia.com/t/random-numbers-on-device-generation/135748/2

Mat Colgrove
  • 5,441
  • 1
  • 10
  • 11