forked from inducer/pycuda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'gert' into try-merge-gert
Conflicts: Makefile.in src/python/gpuarray.py
- Loading branch information
Showing
16 changed files
with
1,828 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
.pydevproject | ||
.project | ||
.settings | ||
*~ | ||
.*.sw[po] | ||
*.dat | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#simple module to show the ploting of random data | ||
|
||
import pycuda.gpuarray as cuda | ||
from matplotlib.pylab import * | ||
|
||
size = 1000 | ||
|
||
#random data generated on gpu | ||
a = cuda.array(size).randn() | ||
|
||
|
||
subplot(211) | ||
plot(a) | ||
grid(True) | ||
ylabel('plot - gpu') | ||
|
||
subplot(212) | ||
hist(a, 100) | ||
grid(True) | ||
ylabel('histogram - gpu') | ||
|
||
#and save it | ||
savefig('plot-random-data') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#! /usr/bin/env python | ||
import pycuda.driver as drv | ||
import numpy | ||
import numpy.linalg as la | ||
from pytools import Table | ||
|
||
|
||
|
||
|
||
def main(): | ||
drv.init() | ||
assert drv.Device.count() >= 1 | ||
ctx = drv.Device(0).make_context() | ||
|
||
import pycuda.gpuarray as gpuarray | ||
|
||
# make sure all the kernels are compiled | ||
gpuarray.GPUArray.compile_kernels() | ||
print "done compiling" | ||
|
||
sizes = [] | ||
times = [] | ||
flops = [] | ||
flopsCPU = [] | ||
timesCPU = [] | ||
|
||
for power in range(10, 25): # 24 | ||
size = 1<<power | ||
print size | ||
sizes.append(size) | ||
a = gpuarray.zeros((size,), dtype=numpy.float32) | ||
|
||
if power > 20: | ||
count = 100 | ||
else: | ||
count = 1000 | ||
|
||
#start timer | ||
start = drv.Event() | ||
end = drv.Event() | ||
start.record() | ||
|
||
#cuda operation which fills the array with random numbers | ||
for i in range(count): | ||
a.randn() | ||
|
||
#stop timer | ||
end.record() | ||
end.synchronize() | ||
|
||
#calculate used time | ||
secs = start.time_till(end)*1e-3 | ||
|
||
times.append(secs/count) | ||
flops.append(size) | ||
|
||
#cpu operations which fills teh array with random data | ||
a = numpy.array((size,), dtype=numpy.float32) | ||
|
||
#start timer | ||
start = drv.Event() | ||
end = drv.Event() | ||
start.record() | ||
|
||
#cpu operation which fills the array with random data | ||
for i in range(count): | ||
numpy.random.randn(size).astype(numpy.float32) | ||
|
||
#stop timer | ||
end.record() | ||
end.synchronize() | ||
|
||
#calculate used time | ||
secs = start.time_till(end)*1e-3 | ||
|
||
#add results to variable | ||
timesCPU.append(secs/count) | ||
flopsCPU.append(size) | ||
|
||
|
||
#calculate pseudo flops | ||
flops = [f/t for f, t in zip(flops,times)] | ||
flopsCPU = [f/t for f, t in zip(flopsCPU,timesCPU)] | ||
|
||
#print the data out | ||
tbl = Table() | ||
tbl.add_row(("Size", "Time GPU", "Size/Time GPU", "Time CPU","Size/Time CPU","GPU vs CPU speedup")) | ||
for s, t, f,tCpu,fCpu in zip(sizes, times, flops,timesCPU,flopsCPU): | ||
tbl.add_row((s,t,f,tCpu,fCpu,f/fCpu)) | ||
print tbl | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#! /usr/bin/env python | ||
import pycuda.cumath as cuma | ||
import pycuda._kernel as kernel | ||
import pycuda.gpuarray as cuda | ||
import pycuda.driver as drv | ||
import types | ||
import numpy as numpy | ||
from pytools import Table | ||
|
||
runs = 10 | ||
|
||
drv.init() | ||
assert drv.Device.count() >= 1 | ||
ctx = drv.Device(0).make_context() | ||
|
||
|
||
def time_cpu_execution(size,method,argumentCount): | ||
"""times the execution time on the cpu""" | ||
|
||
start = drv.Event() | ||
end = drv.Event() | ||
start.record() | ||
|
||
a = numpy.zeros(size,numpy.float32)+1 | ||
|
||
for x in range(runs): | ||
for i in range(size): | ||
if argumentCount == 1: | ||
method(a[i]) | ||
if argumentCount == 2: | ||
method(a[i],2) | ||
|
||
#stop timer | ||
end.record() | ||
end.synchronize() | ||
|
||
#calculate used time | ||
secs = start.time_till(end) | ||
|
||
return secs | ||
|
||
def time_gpu_execution(size,method,argumentCount): | ||
"""times the execution time on the gpu""" | ||
start = drv.Event() | ||
end = drv.Event() | ||
start.record() | ||
|
||
a = cuda.array(size)+1 | ||
|
||
for x in range(runs): | ||
if argumentCount == 1: | ||
method(a) | ||
if argumentCount == 2: | ||
method(a,2) | ||
|
||
#stop timer | ||
end.record() | ||
end.synchronize() | ||
|
||
#calculate used time | ||
secs = start.time_till(end) | ||
|
||
return secs | ||
|
||
#iterate over all methods and time the execution time with different array sizes | ||
print "compile kernels" | ||
kernel._compile_kernels(kernel) | ||
|
||
#generate our output table, one for gpu, one for cpu | ||
tblCPU = Table() | ||
tblGPU = Table() | ||
tblSPD = Table() | ||
|
||
#contains all the method names | ||
methods = ["size"] | ||
|
||
for name in dir(cuma): | ||
if (name.startswith("__") and name.endswith("__")) == False: | ||
method = getattr(cuma, name) | ||
|
||
if type(method) == types.FunctionType: | ||
methods.append(name) | ||
|
||
tblCPU.add_row(methods) | ||
tblGPU.add_row(methods) | ||
tblSPD.add_row(methods) | ||
|
||
#generate arrays with differnt sizes | ||
for power in range(1,20): | ||
size = 1<<power | ||
|
||
#temp variables | ||
rowCPU = [size] | ||
rowGPU = [size] | ||
rowSPD = [size] | ||
|
||
print "calculating: ", size | ||
|
||
for name in dir(cuma): | ||
if (name.startswith("__") and name.endswith("__")) == False: | ||
|
||
method = getattr(cuma, name) | ||
|
||
if type(method) == types.FunctionType: | ||
code = method.func_code | ||
argCount = code.co_argcount | ||
|
||
gpu_time = time_gpu_execution(size,method,argCount) | ||
cpu_time = time_cpu_execution(size,method,argCount) | ||
|
||
rowCPU.append(str(cpu_time/runs)[0:7]) | ||
rowGPU.append(str(gpu_time/runs)[0:7]) | ||
|
||
speed_cpu = size/(cpu_time/runs) | ||
speed_gpu = size/(gpu_time/runs) | ||
rowSPD.append(str(speed_gpu/speed_cpu)[0:7]) | ||
|
||
tblCPU.add_row(rowCPU) | ||
tblGPU.add_row(rowGPU) | ||
tblSPD.add_row(rowSPD) | ||
|
||
print "" | ||
|
||
print "GPU Times (ms)" | ||
|
||
print "" | ||
|
||
print tblGPU | ||
|
||
print "" | ||
|
||
print "CPU Times (ms)" | ||
|
||
print "" | ||
print tblCPU | ||
|
||
|
||
print "" | ||
|
||
print "GPU VS CPU" | ||
|
||
print "" | ||
print tblSPD | ||
|
Oops, something went wrong.