PyGPU - Python for the GPU

Haven't you ever dreamt of writing code in a very high level language and have that code execute at speeds rivaling that of lower-level languages? PyGPU is a compiler that lets you write image processing programs in Python that execute on the graphics processing unit (GPU) present in modern graphics cards. This enables image processing algorithms to take advantage of the performance of the GPU. In some applications, performance increases approach an order of magnitude, compared to CPUs.

Existing methods for programming the GPU uses either specialised languages (such as Cg, HLSL, or GLSL) or an embedded language in C++ (Sh) or C# (Accelerator). The specialised languages are generally very low-level (on the level of C with some simple C++ constructs). Furthermore, they require intimate knowledge of advanced graphics programming concepts, as well as quite a bit of glue-code to handle parameter passing and render pipeline setup.

The embedded approaches admit a higher level of abstraction (being embedded in somewhat higher level languages) and also does away with a lot of the glue code necessary with using specialised languages. However, neither C++ or C# come close to the simple, direct flexibility and expressive power of more dynamic languages such as Python or Ruby.

PyGPU is an embedded language in Python, that allow most of Python features (list-comprehensions, higher-order functions, iterators) to be used for constructing GPU algorithms. It uses a image abstraction to abstract away implementation details of the GPU, while still allowing translation to very efficient GPU native-code.



PyGPU is a research project for exploring the possibilities of using very high level languages for writing GPU programs.


Edge-detected Lena

Sobel edge-detection

def sobelEdgeDetect(im=DImage, p=Position):
    wX = outerproduct([1,2,1],[-1,0,1])
    wY = transpose(wX)

    Gx = convolve(wX,im,p)
    Gy = convolve(wY,im,p)

    return sqrt(Gx**2 + Gy**2)
F16 composite

Seamless cloning

Seamless cloning works by solving a Laplace equation to optimally match the edges of two images allowing seamless pasting of one image into another.

The multigrid Laplace solver and pasting operations are implemented in PyGPU and run exclusively on the GPU.

The example is based on the SIGGRAPH 2003 paper Poisson Image Editing by Pérez, Gangnet, and Blake. The test images are from
Fluid 0 Fluid 1
Fluid 2 Fluid 3

2D Fluid simulator

This example implements a PyGPU program that solves Navier-Stokes equations for incompressible flow. It executes on the GPU and the solver runs at interactive rates.

The example is based on the GPU Gems article available here (PDF).

Download and installation

PyGPU is written completely in Python but, in order to interface with the graphics card and GPU, requires a couple of extension packages. Links to the respective extension packages are provided below.

Installation instructions
  1. Install NumPy and Pygame. For instructions see their respective webpages.
  2. Install PyGLEW and PyCg.
    • Windows: Simply run the executable files provided below. The Windows distribution if PyGLEW and PyCg is for Python 2.4 only.
    • Linux: Download the tar.gz-archives, unpack them, and run python install as root, to install.
      Note: if you use a modern Linux distribution (Ubuntu 7.0 e.g.) you should use the Python 2.5 versions (indicated by the -py2.5 suffix). Otherwise, use the ordinary, Python 2.4 packages.
  3. Download PyGPU, unpack it and run the program to verify that your installation works correctly. Start it by either double-clicking on the icon, or by firing up a terminal and typing python while in the appropriate directory.

Download links

PyGPU runs on both Linux and Windows and under both NVIDIA and ATI graphics cards. Previous version have been reported to work on Mac OS as well. The current version is 0.2.0a-629. This is alpha software, you should expect missing functionality, performance issues, etc. If you find bugs please report them in the issue-tracker.

For direct SVN access to the source repository please see the Google code page.

NumPy is a library for array computing in Python. PyGPU has been developed and tested using NumPy version 1.0b1 and later.
Pygame provide Python bindings for SDL (the Simple Direct media Library) that is required to create an OpenGL context in which to run the examples. PyGPU has been developed and tested with version 1.7.1 of Pygame and version 1.2.9 of SDL. Note that if you run Windows, the Pygame installer includes all the necessary SDL dll:s as well. Also, most Linux distributions include a reasonable modern SDL. Generally it is only necessary to download and install pygame.
PyGLEW are Python bindings for the GLEW (OpenGL Extension Wrangler library). It wraps OpenGL versions 1.1 to 2.0, including a number of additional extensions critical to the operation of PyGPU. The current version is 0.1.2:
PyCg - Python bindings for NVIDIA's Cg library. The current version is 0.14.1 and is for Cg version 1.4.
Last modified: Thu Oct 18 12:50:21 2007