-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum.py
More file actions
45 lines (27 loc) · 879 Bytes
/
sum.py
File metadata and controls
45 lines (27 loc) · 879 Bytes
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
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import pycuda.reduction as reduct
import pycuda.gpuarray as gpuarray
import numpy
import time
n = 1000
gpuSum = reduct.ReductionKernel(numpy.int32, neutral="0", reduce_expr="a+b",map_expr="1 << x[i]", arguments="long* x")
a_gpu = gpuarray.to_gpu(numpy.asarray(range(n), dtype = numpy.int64))
t0 = time.time()
krnl = reduct.ReductionKernel(numpy.int64, neutral="0",
reduce_expr="x[i] + x[i+1]", map_expr="x[i]",
arguments="long *x")
t1 = time.time()
#res = krnl(a_gpu).get()
t5 = time.time()
print gpuarray.max(a_gpu).get()
t6 = time.time()
print '%0.6f' % ((t6 - t5)*1000)
print '%0.6f' % ((t1 - t0)*1000)
t2 = time.time()
sum = sum(range(n))
#maxcpu = max(range(n))
t3 = time.time()
print sum
print '%0.6f' % ((t3 - t2)*1000)