forked from arrayfire/arrayfire-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathafjit.py
More file actions
54 lines (40 loc) · 1.05 KB
/
afjit.py
File metadata and controls
54 lines (40 loc) · 1.05 KB
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
# [jit-snippet]
# As JIT is automatically enabled in ArrayFire, this version of the function
# forces each expression to be evaluated. If the eval() function calls are
# removed, then the execution of this code would be equivalent to the
# following function.
import time
import arrayfire as af
samples = int(9e8)
x = af.randu((samples))
y = af.randu((samples))
def pi_no_jit(x, y, samples):
temp = x * x
af.eval(temp)
temp += y * y
af.eval(temp)
temp = af.sqrt(temp)
af.eval(temp)
temp = temp < 1
af.eval(temp)
return 4.0 * af.sum(temp) / samples
def pi_jit(x, y, samples):
temp = af.sqrt(x * x + y * y) < 1
af.eval(temp)
return 4.0 * af.sum(temp) / samples
# Print device info
af.info()
# Time JIT code
start = time.perf_counter()
res = pi_jit(x, y, samples)
af.sync()
end = time.perf_counter()
print("jit:", end - start, res)
af.device_gc()
# Time no JIT code
start = time.perf_counter()
res = pi_no_jit(x, y, samples)
af.sync()
end = time.perf_counter()
print("no jit:", end - start, res)
# [jit-endsnippet]