e.54.2.f90 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ! { dg-do run }
  2. function dotprod_ref (B, C, N) result (sum)
  3. implicit none
  4. real :: B(N), C(N), sum
  5. integer :: N, i
  6. sum = 0.0e0
  7. do i = 1, N
  8. sum = sum + B(i) * C(i)
  9. end do
  10. end function
  11. function dotprod (B, C, N, block_size, num_teams, block_threads) result (sum)
  12. implicit none
  13. real :: B(N), C(N), sum
  14. integer :: N, block_size, num_teams, block_threads, i, i0
  15. sum = 0.0e0
  16. !$omp target map(to: B, C, block_size, num_teams, block_threads)
  17. !$omp teams num_teams(num_teams) thread_limit(block_threads) &
  18. !$omp& reduction(+:sum)
  19. !$omp distribute
  20. do i0 = 1, N, block_size
  21. !$omp parallel do reduction(+:sum)
  22. do i = i0, min (i0 + block_size - 1, N)
  23. sum = sum + B(i) * C(i)
  24. end do
  25. end do
  26. !$omp end teams
  27. !$omp end target
  28. end function
  29. subroutine init (B, C, N)
  30. real :: B(N), C(N)
  31. integer :: N, i
  32. do i = 1, N
  33. B(i) = 0.0001 * i
  34. C(i) = 0.000001 * i * i
  35. end do
  36. end subroutine
  37. subroutine check (a, b)
  38. real :: a, b, err
  39. real, parameter :: EPS = 0.0001
  40. if (b == 0.0) then
  41. err = a
  42. else if (a == 0.0) then
  43. err = b
  44. else
  45. err = (a - b) / b
  46. end if
  47. if (err > EPS .or. err < -EPS) call abort
  48. end subroutine
  49. program e_54_1
  50. integer :: n
  51. real :: ref, d
  52. real, pointer, dimension(:) :: B, C
  53. n = 1024 * 1024
  54. allocate (B(n), C(n))
  55. call init (B, C, n)
  56. ref = dotprod_ref (B, C, n)
  57. d = dotprod (B, C, n, n / 8, 2, 8)
  58. call check (ref, d)
  59. deallocate (B, C)
  60. end program