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

并显示CPU上的期望值:

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...