OpenACC與CUDA Fortran交互(1)

先上代碼:
  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導語。
發佈了40 篇原創文章 · 獲贊 7 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章