Why the result with openacc is different without openacc in the following code

I write a following example to test acc routine declared with ‘!$acc routine seq’. But I found the result is different between openacc and non-openacc. I am confused to find the reason. Could anyone help me? Thank you very much!

Code example:
test.f90:

program test
use mod_a
 integer :: i,j,k
 call fun_up()
 k=100
 !$acc update host(a)
 write(*,*) "1st, a=",a
 !$acc parallel loop seq present(a) copyin(k)
 do i=1,10
     do j=1,10
         a(j,i)=a(j,i)+k
         k=k+1
     end do
 end do
 !$acc update host(a)
 write(*,*) "2nd, a=",a
end program

module mod_a
module mod_a
integer :: a(10,10)
!$acc declare create(a)
contains
subroutine fun_up()
 !$acc routine seq
 integer :: i,j 
 k=1 
 do i=1,10
     do j=1,10
         a(j,i)=k
         k=k+1
     end do
 end do
 call fun_down()
end subroutine
subroutine fun_down()
 !$acc routine seq
 integer :: i,j 
 k=10
 do i=1,10
     do j=1,10
         a(j,i)=a(j,i)+k
         k=k+1
     end do
 end do
end subroutine
end module

Results without openacc (nvfortran -o test.exe test.f90 mod_a.f90)
1st, a= 11 13 15 17 19
21 23 25 27 29 31
33 35 37 39 41 43
45 47 49 51 53 55
57 59 61 63 65 67
69 71 73 75 77 79
81 83 85 87 89 91
93 95 97 99 101 103
105 107 109 111 113 115
117 119 121 123 125 127
129 131 133 135 137 139
141 143 145 147 149 151
153 155 157 159 161 163
165 167 169 171 173 175
177 179 181 183 185 187
189 191 193 195 197 199
201 203 205 207 209
2nd, a= 111 114 117 120 123
126 129 132 135 138 141
144 147 150 153 156 159
162 165 168 171 174 177
180 183 186 189 192 195
198 201 204 207 210 213
216 219 222 225 228 231
234 237 240 243 246 249
252 255 258 261 264 267
270 273 276 279 282 285
288 291 294 297 300 303
306 309 312 315 318 321
324 327 330 333 336 339
342 345 348 351 354 357
360 363 366 369 372 375
378 381 384 387 390 393
396 399 402 405 408

Results with openacc (nvfortran -acc -Minfo=all -o test.exe test.f90 mod_a.f90)
1st, a= 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0
2nd, a= 100 101 102 103 104
105 106 107 108 109 110
111 112 113 114 115 116
117 118 119 120 121 122
123 124 125 126 127 128
129 130 131 132 133 134
135 136 137 138 139 140
141 142 143 144 145 146
147 148 149 150 151 152
153 154 155 156 157 158
159 160 161 162 163 164
165 166 167 168 169 170
171 172 173 174 175 176
177 178 179 180 181 182
183 184 185 186 187 188
189 190 191 192 193 194
195 196 197 198 199

Hi Victor,

I noticed this post but moved it over the the NVHPC forums since the other forum wouldn’t be able to answer OpenACC questions.

I think you have an misunderstanding of what “acc routine” does. It creates a device callable version of the routine but does not offload the code to the GPU. That’s done with the “parallel” or “kernels” directives.

Given “fun_up” is called from host code, it’s being executed on the host. Hence when you use “!$acc update host(a)”, you’re over writing the host copy of “a” with the values from the device copy of “a”. Since the device wasn’t initialized, it’s values are zero.

Instead, you want to update the device copy of “a”, i.e. “!$acc update device(a)”.

% cat test.f90
module mod_a
integer :: a(10,10)
!$acc declare create(a)
contains
subroutine fun_up()
!$acc routine seq
integer :: i,j
k=1
do i=1,10
do j=1,10
a(j,i)=k
k=k+1
end do
end do
call fun_down()
end subroutine
subroutine fun_down()
!$acc routine seq
integer :: i,j
k=10
do i=1,10
do j=1,10
a(j,i)=a(j,i)+k
k=k+1
end do
end do
end subroutine
end module

program test
use mod_a
integer :: i,j,k
call fun_up()
k=100
!acc update host(a)
!$acc update device(a)
write(*,*) "1st, a=",a
!$acc parallel loop seq present(a) copyin(k)
do i=1,10
do j=1,10
a(j,i)=a(j,i)+k
k=k+1
end do
end do
!$acc update host(a)
write(*,*) "2nd, a=",a
end program

% nvfortran test.f90 ; a.out
 1st, a=           11           13           15           17           19
           21           23           25           27           29           31
           33           35           37           39           41           43
           45           47           49           51           53           55
           57           59           61           63           65           67
           69           71           73           75           77           79
           81           83           85           87           89           91
           93           95           97           99          101          103
          105          107          109          111          113          115
          117          119          121          123          125          127
          129          131          133          135          137          139
          141          143          145          147          149          151
          153          155          157          159          161          163
          165          167          169          171          173          175
          177          179          181          183          185          187
          189          191          193          195          197          199
          201          203          205          207          209
 2nd, a=          111          114          117          120          123
          126          129          132          135          138          141
          144          147          150          153          156          159
          162          165          168          171          174          177
          180          183          186          189          192          195
          198          201          204          207          210          213
          216          219          222          225          228          231
          234          237          240          243          246          249
          252          255          258          261          264          267
          270          273          276          279          282          285
          288          291          294          297          300          303
          306          309          312          315          318          321
          324          327          330          333          336          339
          342          345          348          351          354          357
          360          363          366          369          372          375
          378          381          384          387          390          393
          396          399          402          405          408
% nvfortran test.f90 -acc ; a.out
 1st, a=           11           13           15           17           19
           21           23           25           27           29           31
           33           35           37           39           41           43
           45           47           49           51           53           55
           57           59           61           63           65           67
           69           71           73           75           77           79
           81           83           85           87           89           91
           93           95           97           99          101          103
          105          107          109          111          113          115
          117          119          121          123          125          127
          129          131          133          135          137          139
          141          143          145          147          149          151
          153          155          157          159          161          163
          165          167          169          171          173          175
          177          179          181          183          185          187
          189          191          193          195          197          199
          201          203          205          207          209
 2nd, a=          111          114          117          120          123
          126          129          132          135          138          141
          144          147          150          153          156          159
          162          165          168          171          174          177
          180          183          186          189          192          195
          198          201          204          207          210          213
          216          219          222          225          228          231
          234          237          240          243          246          249
          252          255          258          261          264          267
          270          273          276          279          282          285
          288          291          294          297          300          303
          306          309          312          315          318          321
          324          327          330          333          336          339
          342          345          348          351          354          357
          360          363          366          369          372          375
          378          381          384          387          390          393
          396          399          402          405          408

Hope this helps,
Mat