在Fortran90内部函数上使用例程指令

时间:2020-09-23 14:30:01

标签: fortran openacc

我已授予OpenAcc访问名为“ lang_force”的子例程的权限,在该子例程中,我称为Fortran90固有的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

当使用最新版本的pdf90进行编译时,它告诉我它需要访问RANDOM_NUMBER()。如何为这样的fortran90内在子例程声明例程指令?

1 个答案:

答案 0 :(得分:2)

设备代码(包括RANDOM_NUMBER)中不支持所有Fortran内部函数。

尤其是RANDOM_NUMBER并不是线程安全的,因为所有线程将共享同一状态。取而代之的是,您需要使用cuRand,我们将其随附的示例与编译器一起运送到“ 2020 / examples / CUDA-Libraries / cuRAND / test_rand_oacc_ftn”目录下。

虽然这是C语言,但我在NVIDIA用户论坛上写的更详细:

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