如何解决OpenACC声明构造
我通过PGI编译器了解了OpenACC 2.6支持的功能,并且遇到了cpu和GPU之间的内存管理问题。
以下Fortran代码是official document的修改版本:
module data
integer,parameter :: maxl = 100000
real,dimension(maxl) :: xstat
real,dimension(:),allocatable :: yalloc
!$acc declare create(xstat,yalloc)
end module
module useit
use data
contains
subroutine compute(n)
integer :: n
integer :: i
!$acc parallel loop present(yalloc)
do i = 1,n
yalloc(i) = iprocess(i)
enddo
end subroutine
real function iprocess(i)
!$acc routine seq
integer :: i
iprocess = yalloc(i) + 2*xstat(i)
end function
end module
program main
use data
use useit
implicit none
integer :: nSize = 100
!---------------------------------------------------------------------------
call alLocit(nSize)
call initialize
call compute(nSize)
!$acc update self(yalloc)
write(*,*) "yalloc(10)=",yalloc(10) ! should be 3
call finalize
contains
subroutine alLocit(n)
integer :: n
allocate(yalloc(n))
end subroutine alLocit
subroutine initialize
xstat = 1.0
yalloc = 1.0
!$acc enter data copyin(xstat,yalloc)
end subroutine initialize
subroutine finalize
deallocate(yalloc)
end subroutine finalize
end program main
可以使用nvfortran
编译此代码:
nvfortran -Minfo test.f90
yalloc(10)= 3.000000
但是,使用OpenACC编译时:
nvfortran -add -Minfo test.f90
upload CUDA data device=0 threadid=1 variable=descriptor bytes=128
upload CUDA data device=0 threadid=1 variable=.attach. bytes=8
upload CUDA data file=/home/yang/GPU-Collection/openacc/basics/globalArray.f90 function=initialize line=55 device=0 threadid=1 variable=.attach. bytes=8
launch CUDA kernel file=/home/yang/GPU-Collection/openacc/basics/globalArray.f90 function=compute line=14 device=0 threadid=1 num_gangs=1 num_workers=1 vector_length=128 grid=1 block=128
download CUDA data file=/home/yang/GPU-Collection/openacc/basics/globalArray.f90 function=main line=41 device=0 threadid=1 variable=yalloc bytes=400
yalloc(10)= 0.000000
我试图在几个地方添加一些显式的内存移动,但是没有任何帮助。这真让我感到困惑。
解决方法
问题出在您的初始化例程中
subroutine initialize
xstat = 1.0
yalloc = 1.0
!acc enter data copyin(xstat,yalloc)
!$acc update device(xstat,yalloc)
end subroutine initialize
由于xstat和yalloc已经在数据区域(declare指令)中,因此第二个数据区域(“ enter data copyin”)实际上被忽略(尽管参考计数器已更新)。相反,您需要使用更新指令来同步数据。
通过此更改,代码将获得正确的答案:
% nvfortran test.f90 -acc -Minfo=accel; a.out
compute:
14,Generating Tesla code
15,!$acc loop gang,vector(128) ! blockidx%x threadidx%x
iprocess:
19,Generating acc routine seq
Generating Tesla code
main:
41,Generating update self(yalloc(:))
initialize:
56,Generating update device(yalloc(:),xstat(:))
yalloc(10)= 3.000000
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。