I have an OpenACC Fortran code that uses two modules. If the modules are in the same file, it works fine. But if the modules are in separate files, it yields the wrong answer. I show the original code below.
There are two modules, heatx_mod and test_mod. Both are in the file par.f90. If you put test_mod in a different file, say test.f90, then the code yields the wrong answer. And if you put everything in submodules, it fails too:
heatx.f90
!--------------------------------------
! |
!--------------------------------------
program heatx
use heatx_mod
use test_mod
!$acc routine(calc) seq
allocate(x(n,n,n))
x=4.0
xtot=0.0
!$acc update device(x,xtot)
call test()
!$acc update host(x,xtot)
print*,'+------------------------------------------+'
print*,'| |'
print*,'+------------------------------------------+'
print'(1x,a,i1,2f15.7)','%heatx, avg 10^',int(log10(float(size(x)))),sum(x)/size(x),xtot/size(x)
print*
end program
par.f90
!--------------------------------------
! |
!--------------------------------------
module heatx_mod
integer, parameter :: n=100
real , allocatable :: x(:,:,:)
real :: xtot
!$acc declare create(x,xtot)
contains
!--------------------------------------
! |
!--------------------------------------
real function sqab(a)
!$acc routine seq
real :: a
sqab = sqrt(abs(a))
end function
!--------------------------------------
! |
!--------------------------------------
subroutine calc(i,j,k)
integer :: i,j,k
!$acc routine(sqab) seq
!$acc routine seq
x(i,j,k) = sqab(x(i,j,k))
xtot = xtot + x(i,j,k)
end subroutine
end module
!--------------------------------------
! |
!--------------------------------------
module test_mod
contains
subroutine test()
use heatx_mod
!$acc routine(calc) seq
!$acc parallel loop reduction(+:xtot)
do k=1,n
!$acc loop reduction(+:xtot)
do j=1,n
!$acc loop reduction(+:xtot)
do i=1,n
call calc(i,j,k)
end do
end do
end do
end subroutine
end module