Here is some very simple speed testing code - maybe it is useful to another
beginner.  I've used it to convince myself that GPUs really do go faster
than CPUs (this is useful here in the office to show my physics colleagues).

The code was adapted from hello_gpu.py.  It has two halves, first it does a
simple calculation many times on the GPU and then it does the same
calculation on the CPU.  Both times it uses dev.Event to count how long the
operations took.

Roughly speaking on my WinXP Intel Core2 Duo 2.66GHz CPU (1 CPU used) the
9800GT GPU comes out 20-55* faster than the CPU.

In the code below a value for sin is calculated 2,000,000 times in a 400
element array.  A 20-30* speedup holds for tan, sin, addition, sqrt, exp.
The pow function shows a 55* speedup.  If you want to do your own testing
then replace the two references to 'sin' with your chosen function.
Remember that 2,000,000 is also written twice so change it in both places to
alter the number of iterations.  Extra note - the final result for 'tan'
diverges quickly, 'sin' and others seem to be mostly stable.

By lowering the iterations from 2,000,000 to 200 (in both places) then both
the CPU and GPU complete their tasks in roughly the same time.

I did a variation where 'dest' is removed and 'float *a' is referenced by
drv.InOut(a) (so a is the input parameter and is also used for the output
result) - I didn't observe any obvious speed difference.

Side note - I'm also using the NVIDIA System Monitor, I've selected all the
GPU outputs along with CPU outputs so they hover as transparent displays at
the top of the screen.  Whenever the GPU is invoked you see the GPU Usage,
Cooler and Temp change.

HTHs another newbie,
Ian.

======

# based on hello_gpu.py

import pycuda.driver as drv
import pycuda.tools
import pycuda.autoinit
import numpy
import numpy.linalg as la
from pycuda.compiler import SourceModule

nbr_values = 400

#############
# GPU SECTION

mod = SourceModule("""
__global__ void addone(float *dest, float *a)
{
  const int i = threadIdx.x;
  for(int n = 0; n < 2000000; n++) {
    a[i] = sin(a[i]);
  }
  dest[i] = a[i];
}
""")

addone = mod.get_function("addone")

a = numpy.ones(nbr_values).astype(numpy.float32)
a += 1 # a is now an array of 2s

dest = numpy.zeros_like(a)

start = drv.Event()
end = drv.Event()
start.record()

addone(drv.Out(dest), drv.In(a), block=(nbr_values,1,1))

#stop timer
end.record()
end.synchronize()
secs = start.time_till(end)*1e-3
print "GPU time:", secs
print "GPU result starts with...", dest[:3]


#############
# CPU SECTION

a = numpy.ones(nbr_values).astype(numpy.float32)
a += 1 # a is now an array of 2s
start.record()

for i in range(2000000):
    a = numpy.sin(a)

#stop timer
end.record()
end.synchronize()
secs = start.time_till(end)*1e-3
print "CPU time:", secs
print "CPU result starts with...", a[:3]

======


-- 
Ian Ozsvald (Professional Screencaster)
[email protected]

http://ProCasts.co.uk/examples.html
http://TheScreencastingHandbook.com
http://IanOzsvald.com + http://ShowMeDo.com
http://twitter.com/ianozsvald
_______________________________________________
PyCUDA mailing list
[email protected]
http://tiker.net/mailman/listinfo/pycuda_tiker.net

Reply via email to