Nvfortran OpenACC allocatable module variable


The following module does not compile with nvfortran (but it does with gfortran):

module xx_mod
implicit none
real, allocatable :: arr(:)
!$ACC declare create(arr)
subroutine init
implicit none
allocate( arr(9) )
end subroutine init
end module xx_mod

nvfortran -acc -c xx.f90
NVFORTRAN-S-0155-Module variables used in acc routine need to be in !$acc declare create() - arr$dev (xx.f90: 9)
0 inform, 0 warnings, 1 severes, 0 fatal for init

nvfortran --version
nvfortran 20.7-0 LLVM 64-bit target on x86-64 Linux -tp zen

Any suggestions are much appreciated.

The code isn’t valid since you shouldn’t allocate a host module array from the device. When allocating the array included in a declare create directive on host, the device copy of the array is implicitly created on the device as well. Hence, no need allocate it on the device. If you want a device only array, you would use the “device_resident” clause instead of “create”, but even in this case, the call to allocate should be done on the host.

Unclear why Gfortran 10.x accepts it since it will most likely cause runtime issues. The allocated memory would only be available for the lifetime of the kernel calling the “init” routine and not be persistent across kernels. Plus “arr” is shared by all the threads of a compute region so may cause a race condition.