OpenACC与CUDA Fortran交互(1)

来源:互联网 发布:淘宝青岛双星专卖店 编辑:程序博客网 时间:2024/06/02 23:41
先上代码:
  1. ! openacc_main.f90
  2. program main
  3.   use saxpy_mod
  4.   integer, parameter :: N = 2**20
  5.   real, dimension(N) :: X, Y

  6.   X(:) = 1.0
  7.   Y(:) = 0.0

  8.   !$acc data copy(y) copyin(x)
  9.   call saxpy(N, 2.0, x, y)
  10.   !$acc end data

  11.   print *, y(1)
  12. end program
  1. ! kernels.cuf
  2. module saxpy_mod
  3.   contains
  4.   attributes(global) &
  5.   subroutine saxpy_kernel(n, a, x, y)
  6.     real :: x(:), y(:), a
  7.     integer :: n,i
  8.     attributes(value) :: a,n
  9.     i = threadIdx%x+(blockIdx%x-1)*blockDim%x
  10.     if (i<=n) y(i) = y(i) + a*x(i)
  11.   end subroutine
  12.   subroutine saxpy (n, a, x, y)
  13.     use cudafor
  14.     real, device :: x(:), y(:)
  15.     real :: a
  16.     integer :: n
  17.     call saxpy_kernel<<<4096,256>>>(n, a, x, y)
  18.   end subroutine
  19. end module saxpy_mod
对于函数saxpy_kernel来说,变量x,y 有device属性,编译器会知道传过来的是设备数组,故不需要host_data导语。
原创粉丝点击