ERROR ON PREV
Mandelbrot In CUDA
  1. How to Program, Part I
  2. How to Program, Part II
  3. How to Program, Part III
  4. How to Program, Part IV
  5. How to Program, Part V
  6. How to Program, Part VI
  7. exercises
  8. pyMPI tutorial
  9. Calculating PI, Part I
  10. Calculating PI, Part II
  11. Calculating PI, Part III
  12. Dividing Work
  13. More MPI
  14. Poogle - Web Search
  15. Mandelbrot Sets
  16. Mandelbrot, The Code
  17. Mandelbrot, The Images
  18. Mandelbrot In CUDA
  19. Conway's Life, Part I
  20. Life Code Listing
  21. Conway's Life, Part II
  22. MPI Life Code Listing

Mandelbrot In CUDA

It is possible to use Python for serious scientific codes by using tools like Numpy, Scipy, or Pycuda. Here we have an example of computing a Mandelbrot image in Pycuda. Most of the computational load is handled by CUDA.

mandelbrot_cuda.py
1####################################################
2#  Calculate Mandelbrot set and save it as a bmp image
3#
4#  Data parallel version using Pycuda
5#  Create string with cuda code and let the graphics
6#  card farm out the work to each warp.
7#
8####################################################
9 
10import bmp
11import pycuda.driver as drv
12import pycuda.tools
13import pycuda.autoinit
14from pycuda.compiler import SourceModule
15import pycuda.gpuarray as gpuarray
16import numpy as nm
17 
18# maximal number of iterations to compute a pixel
19MAX_ITER = 256
20 
21# image dimensions
22from sizes import nx,ny
23 
24from pycuda.elementwise import ElementwiseKernel
25complex_gpu = ElementwiseKernel(
26        "int nx, int ny, int maxiter, int *iteration",
27        """
28        float zr, zi, z2;
29        float jf = 1.0f*(i%ny);
30        float rowf = 1.0f*(i/ny);
31        float nxf = 1.0f*nx;
32        float nyf = 1.0f*ny;
33        float qif = 4.0f*rowf/nyf-2.0f;
34        float qrf = 4.0f*jf/nxf-2.0f;
35        iteration[i] = maxiter;
36        zr = 0.0f;
37        zi = 0.0f;
38        for(int n=0;n < maxiter;n++) {
39            float nzr = zr*zr - zi*zi + qrf;
40            float nzi =   2.0*zr*zi   + qif;
41            zi = nzi;
42            zr = nzr;
43            z2 = zr*zr+zi*zi;
44            if(z2 > 4.0f) {
45                iteration[i] = n;
46                break;
47            }
48        }
49        """,
50        "mandlebrot_gpu",)
51 
52# allocate a numpy array
53iterations = nm.zeros(nx*ny).astype(nm.int32)
54 
55# allocate a gpu array
56iterations_gpu = gpuarray.to_gpu(iterations)
57 
58# perform the gpu calculation
59complex_gpu(nm.int16(nx), nm.int16(ny), nm.int16(MAX_ITER), iterations_gpu)
60 
61# copy data from the gpu array to the numpy array
62iterations_gpu.get(iterations)
63 
64# reshape the array to look the way we want
65image = iterations.reshape(nx,ny)
66 
67bmp.write_image('image.bmp', nx, ny, image, MAX_ITER)