-
Notifications
You must be signed in to change notification settings - Fork 5
/
gpuArrayExample.py
47 lines (36 loc) · 967 Bytes
/
gpuArrayExample.py
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
import pycuda.autoinit
import pycuda.driver as drv
import pycuda.gpuarray as gpuarray
import pycuda.cumath as cumath
import numpy as np
N = np.int32(134931456)
start = drv.Event()
end = drv.Event()
x = np.random.normal(size = N)
start.record()
dX = gpuarray.to_gpu(x)
end.record()
end.synchronize()
print "Transfer to GPU time: %fs" %(start.time_till(end)*1e-3)
print "Timing vectorized exponentiation:"
start.record()
dexpX = cumath.exp(dX)
end.record()
end.synchronize()
print "GPU array calc time: %fs" %(start.time_till(end)*1e-3)
start.record()
expX = np.exp(x)
end.record()
end.synchronize()
print "CPU calc time: %fs" %(start.time_till(end)*1e-3)
print "Timing vectorized dot product/sum of squares:"
start.record()
gpuarray.dot(dX,dX)
end.record()
end.synchronize()
print "GPU array calc time: %fs" %(start.time_till(end)*1e-3)
start.record()
np.dot(x, x)
end.record()
end.synchronize()
print "CPU calc time: %fs" %(start.time_till(end)*1e-3)