| 1 |
module procedures |
| 2 |
use iso_c_binding, only: c_ptr, c_f_pointer |
| 3 |
use omp_lib |
| 4 |
implicit none |
| 5 |
|
| 6 |
contains |
| 7 |
|
| 8 |
function foo(bv, av, n) result(res) |
| 9 |
implicit none |
| 10 |
integer :: res, n, i |
| 11 |
type(c_ptr) :: bv |
| 12 |
type(c_ptr) :: av |
| 13 |
real(8), pointer :: fp_bv(:), fp_av(:) ! Fortran pointers for array access |
| 14 |
!$omp declare variant(bar) match(construct={dispatch}) adjust_args(need_device_ptr: bv, av) |
| 15 |
!$omp declare variant(baz) match(implementation={vendor(gnu)}) |
| 16 |
|
| 17 |
! Associate C pointers with Fortran pointers |
| 18 |
call c_f_pointer(bv, fp_bv, [n]) |
| 19 |
call c_f_pointer(av, fp_av, [n]) |
| 20 |
|
| 21 |
! Perform operations using Fortran pointers |
| 22 |
do i = 1, n |
| 23 |
fp_bv(i) = fp_av(i) * i |
| 24 |
end do |
| 25 |
res = -1 |
| 26 |
end function foo |
| 27 |
|
| 28 |
function baz(d_bv, d_av, n) result(res) |
| 29 |
implicit none |
| 30 |
integer :: res, n, i |
| 31 |
type(c_ptr) :: d_bv |
| 32 |
type(c_ptr) :: d_av |
| 33 |
real(8), pointer :: fp_bv(:), fp_av(:) ! Fortran pointers for array access |
| 34 |
|
| 35 |
! Associate C pointers with Fortran pointers |
| 36 |
call c_f_pointer(d_bv, fp_bv, [n]) |
| 37 |
call c_f_pointer(d_av, fp_av, [n]) |
| 38 |
|
| 39 |
!$omp distribute parallel do |
| 40 |
do i = 1, n |
| 41 |
fp_bv(i) = fp_av(i) * i |
| 42 |
end do |
| 43 |
res = -3 |
| 44 |
end function baz |
| 45 |
|
| 46 |
function bar(d_bv, d_av, n) result(res) |
| 47 |
implicit none |
| 48 |
integer :: res, n, i |
| 49 |
type(c_ptr) :: d_bv |
| 50 |
type(c_ptr) :: d_av |
| 51 |
|
| 52 |
!$omp target is_device_ptr(d_bv, d_av) |
| 53 |
block |
| 54 |
real(8), pointer :: fp_bv(:), fp_av(:) ! Fortran pointers for array access |
| 55 |
|
| 56 |
! Associate C pointers with Fortran pointers |
| 57 |
call c_f_pointer(d_bv, fp_bv, [n]) |
| 58 |
call c_f_pointer(d_av, fp_av, [n]) |
| 59 |
|
| 60 |
! Perform operations on target |
| 61 |
do i = 1, n |
| 62 |
fp_bv(i) = fp_av(i) * i |
| 63 |
end do |
| 64 |
end block |
| 65 |
|
| 66 |
res = -2 |
| 67 |
end function bar |
| 68 |
|
| 69 |
function test(n) result(res) |
| 70 |
use iso_c_binding, only: c_ptr, c_loc |
| 71 |
implicit none |
| 72 |
integer :: n, res, i, f, ff, last_dev |
| 73 |
real(8), allocatable, target :: av(:), bv(:), d_bv(:) |
| 74 |
real(8), parameter :: e = 2.71828d0 |
| 75 |
type(c_ptr) :: c_av, c_bv, c_d_bv |
| 76 |
|
| 77 |
allocate(av(n), bv(n), d_bv(n)) |
| 78 |
|
| 79 |
! Initialize arrays |
| 80 |
do i = 1, n |
| 81 |
av(i) = e * i |
| 82 |
bv(i) = 0.0d0 |
| 83 |
d_bv(i) = 0.0d0 |
| 84 |
end do |
| 85 |
|
| 86 |
last_dev = omp_get_num_devices() - 1 |
| 87 |
|
| 88 |
c_av = c_loc(av) |
| 89 |
c_d_bv = c_loc(d_bv) |
| 90 |
!$omp target data map(to: av(:n)) map(from: d_bv(:n)) device(last_dev) if(n == 1024) |
| 91 |
!$omp dispatch nocontext(n > 1024) novariants(n < 1024) device(last_dev) |
| 92 |
f = foo(c_d_bv, c_av, n) |
| 93 |
!$omp end target data |
| 94 |
|
| 95 |
c_bv = c_loc(bv) |
| 96 |
ff = foo(c_bv, c_loc(av), n) |
| 97 |
|
| 98 |
! Verify results |
| 99 |
do i = 1, n |
| 100 |
if (d_bv(i) /= bv(i)) then |
| 101 |
write(0,*) 'ERROR at ', i, ': ', d_bv(i), ' (act) != ', bv(i), ' (exp)' |
| 102 |
res = 1 |
| 103 |
return |
| 104 |
end if |
| 105 |
end do |
| 106 |
|
| 107 |
res = f |
| 108 |
deallocate(av, bv, d_bv) |
| 109 |
end function test |
| 110 |
end module procedures |
| 111 |
|
| 112 |
program main |
| 113 |
use procedures |
| 114 |
implicit none |
| 115 |
integer :: ret |
| 116 |
|
| 117 |
ret = test(1023) |
| 118 |
if (ret /= -1) stop 1 |
| 119 |
|
| 120 |
ret = test(1024) |
| 121 |
if (ret /= -2) stop 1 |
| 122 |
|
| 123 |
ret = test(1025) |
| 124 |
if (ret /= -3) stop 1 |
| 125 |
end program main |