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!*1024
11 allocate(v1(length))
12 v1(1:length) = 0.0
13 !使用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,length
22 v1(idx) = v1(idx) + 10
23 enddo
24 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 data
35
36 deallocate(v1)
37
38 end program
39
40 subroutine add1(vec, length)
41 use openacc
42 implicit none
43 integer, intent(in)::length
44 real, intent(inout) :: vec(1:length)
45 integer idx
46 !$acc parallel loop present(vec(1:length))
47 do idx = 1, length
48 vec(idx) = vec(idx) + idx
49 enddo
50 end subroutine