1 program main 2 use omp_lib 3 use openacc 4 implicit none 5 6 real,allocatable:: v1(:) 7 integer length, idx 8 9 call acc_set_device_num(2,acc_device_nvidia)10 length = 1024!*1024!*102411 allocate(v1(length))12 v1(1:length) = 0.013 !使用data语句,将数据copy到device上14 !$acc data copy(v1(1:length))15 call add1(v1, length) !子程序在device上执行16 17 !print只能cpu实行,所以讲数据update到cpu上18 !$acc update host(v1(1:5))19 print*, "v1(1) = ", v1(1:5)20 !下面是在cpu上执行21 do idx = 1,length22 v1(idx) = v1(idx) + 1023 enddo24 print*, "v1(1) = ", v1(1:5) !直接打印出cpu上的结果25 26 !将cpu上的执行结果update到device上27 !$acc update device(v1(1:5))28 call add1(v1, length)29 30 !上面子程序在device上执行完,update到本地输出31 !$acc update host(v1(1:5))32 print*, "v1(1) = ", v1(1:5)33 !结束34 !$acc end data35 36 deallocate(v1)37 38 end program 39 40 subroutine add1(vec, length)41 use openacc42 implicit none43 integer, intent(in)::length44 real, intent(inout) :: vec(1:length)45 integer idx46 !$acc parallel loop present(vec(1:length))47 do idx = 1, length48 vec(idx) = vec(idx) + idx49 enddo50 end subroutine