Querying your GPU with PyCUDA

Now, finally, we will begin our foray into the world of GPU programming by writing our own version of deviceQuery in Python. Here, we will primarily concern ourselves with only the amount of available memory on the device, the compute capability, the number of multiprocessors, and the total number of CUDA cores.

We will begin by initializing CUDA as follows:

import pycuda.driver as drvdrv.init()
Note that we will always have to initialize PyCUDA with pycuda.driver.init() or by importing the PyCUDA autoinit submodule with import pycuda.autoinit!

We can now immediately check how many GPU devices we have on our host computer with this line:

print 'Detected {} CUDA Capable device(s)'.format(drv.Device.count())

Get Hands-On GPU Programming with Python and CUDA now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.