aboutsummaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.fortran/examples-4/e.54.2.f90
blob: 6a830184ded3355b20dfe3f01e2857f23b4ccb85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
! { dg-do run }

function dotprod_ref (B, C, N) result (sum)
  implicit none
  real :: B(N), C(N), sum
  integer :: N, i
  sum = 0.0e0
  do i = 1, N
    sum = sum + B(i) * C(i)
  end do
end function

function dotprod (B, C, N, block_size, num_teams, block_threads) result (sum)
  implicit none
  real :: B(N), C(N), sum
  integer :: N, block_size, num_teams, block_threads, i, i0
  sum = 0.0e0
  !$omp target map(to: B, C, block_size, num_teams, block_threads)
    !$omp teams num_teams(num_teams) thread_limit(block_threads) &
    !$omp& reduction(+:sum)
      !$omp distribute
      do i0 = 1, N, block_size
        !$omp parallel do reduction(+:sum)
        do i = i0, min (i0 + block_size - 1, N)
          sum = sum + B(i) * C(i)
        end do
      end do
    !$omp end teams
  !$omp end target
end function

subroutine init (B, C, N)
  real :: B(N), C(N)
  integer :: N, i
  do i = 1, N
    B(i) = 0.0001 * i
    C(i) = 0.000001 * i * i
  end do
end subroutine

subroutine check (a, b)
  real :: a, b, err
  real, parameter :: EPS = 0.0001
  if (b == 0.0) then
    err = a
  else if (a == 0.0) then
    err = b
  else
    err = (a - b) / b
  end if
  if (err > EPS .or. err < -EPS) call abort
end subroutine

program e_54_1
  integer :: n
  real :: ref, d
  real, pointer, dimension(:) :: B, C
  n = 1024 * 1024
  allocate (B(n), C(n))
  call init (B, C, n)
  ref = dotprod_ref (B, C, n)
  d = dotprod (B, C, n, n / 8, 2, 8)
  call check (ref, d)
  deallocate (B, C)
end program