-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench1.f90
70 lines (54 loc) · 1.63 KB
/
bench1.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
program main
use auto_diff
implicit none
integer, parameter :: n = 1000
type(tree_t) :: x1(n, n), x2(n, n)
type(tree_t) :: y(n, n)
real(rk) :: r1(n, n), r2(n, n)
real(rk) :: z(n, n)
real(rk) :: t1, t2
x1 = randu(n, n)
x2 = randu(n, n)
r1 = randu(n, n)
r2 = randu(n, n)
print *, "Forward"
call cpu_time(t1)
y = (x1 + sigmoid(x2))/(sigmoid(x1) + (x1 + x2)**2)
call cpu_time(t2)
print *, "Elapsed time (seconds):", t2 - t1
print *, "Ordinary arithmetic"
call cpu_time(t1)
z = (r1 + sigmoid_local(r2))/(sigmoid_local(r1) + (r1 + r2)**2)
call cpu_time(t2)
print *, "Elapsed time (seconds):", t2 - t1
print *, "Backward"
call cpu_time(t1)
call y%backward()
call cpu_time(t2)
print *, "Elapsed time (seconds):", t2 - t1
contains
function randu(m, n) result(out)
integer, intent(in) :: m, n
real(rk) :: out(m, n)
call random_number(out)
end function randu
elemental function sigmoid_local(x) result(y)
real(rk), intent(in) :: x
real(rk) :: y
y = 1.0_rk/(1.0_rk + exp(-x))
end function sigmoid_local
end program main
! fpm run --example bench1 --profile debug:
! Forward
! Elapsed time (seconds): 1.9218750000000000
! Ordinary arithmetic
! Elapsed time (seconds): 0.15625000000000000
! Backward
! Elapsed time (seconds): 0.29687500000000000
! fpm run --example bench1 --profile release:
! Forward
! Elapsed time (seconds): 1.6093750000000000
! Ordinary arithmetic
! Elapsed time (seconds): 0.0000000000000000
! Backward
! Elapsed time (seconds): 0.21875000000000000