forked from csc-training/openmp-offload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi.F90
93 lines (63 loc) · 1.83 KB
/
pi.F90
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
program pi_test
use iso_fortran_env, only : INT64
implicit none
integer(kind=INT64) :: nsamples
character(len=85) :: arg
real :: pi1, pi2
if (command_argument_count() /= 1) then
STOP 'Usage pi N where N is the number of samples'
end if
call get_command_argument(1, arg)
read(arg, *) nsamples
pi1 = cpu_pi(nsamples)
write(*,*) 'Pi calculated with CPU', pi1
pi2 = gpu_pi(nsamples)
write(*,*) 'Pi calculated with GPU', pi2
contains
real function cpu_pi(n)
implicit none
integer(kind=INT64) :: n
integer :: i, inside
real, dimension(n) :: x, y
call random_number(x)
call random_number(y)
inside = 0
do i = 1, n
if (x(i)**2 + y(i)**2 < 1.0) then
inside = inside + 1
end if
end do
cpu_pi = 4.0 * real(inside) / real(n)
end function cpu_pi
real function gpu_pi(n)
use, intrinsic :: iso_c_binding
use omp_lib
use curand
implicit none
integer(kind=INT64) :: n
integer :: i, inside
integer(kind=c_size_t) :: stat
integer(kind=c_size_t) :: gen
! real, dimension(:), allocatable :: x, y
real :: x(n), y(n)
inside = 0
! TODO start: allocate x and y in the device with OpenMP enter data
! TODO end
stat = curandCreateGenerator(gen, CURAND_RNG_PSEUDO_DEFAULT)
! TODO start: use device pointer for CUDA random generator calls
stat = curandGenerateUniform(gen, x, n)
stat = curandGenerateUniform(gen, y, n)
! TODO end
! TODO start: execute the loop in parallel in device
do i = 1, n
if (x(i)**2 + y(i)**2 < 1.0) then
inside = inside + 1
end if
end do
! TODO end
! TODO start deallocate x and y in the device with OpenMP exit data
! TODO end
gpu_pi = 4.0 * real(inside) / real(n)
! deallocate(x, y)
end function gpu_pi
end program pi_test