Compiling the code and interfacing with Ctypes

Now let's compile the code we just wrote into a DLL or .so binary. This is actually fairly painless: if you are a Linux user, type the following into the command line to compile this file into mandelbrot.so:

nvcc -Xcompiler -fPIC -shared -o mandelbrot.so mandelbrot.cu

If you are a Windows user, type the following into the command line to compile the file into mandelbrot.dll:

nvcc -shared -o mandelbrot.dll mandelbrot.cu

Now we can write our Python interface. We will start with the appropriate import statements, excluding PyCUDA completely and using just Ctypes. For ease of use, we'll just import all of the classes and functions from Ctypes directly into the default Python namespace, like so: ...

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.