<<

CPS343: Homework on Computing the Mandelbrot using CUDA 1

Homework on Computing the Mandelbrot Set using CUDA

The Mandelbrot Set

The Mandelbrot Set is the set of all complex numbers c for which the 2 zk+1 = zk + c, z0 = 0 (1) remains bounded as k → ∞. It can be shown that if the magnitude of z ever exceeds 2 this iteration will diverge and the point c is not in the set. Identifying points that are in the set is more difficult. We usually assume that the point c is in the set if zk ≤ 2 for all 0 ≤ k ≤ M for some M. The set of points obtained in this manner approximates the Mandelbrot set and as M increases the quality of the approximations improves. The Mandelbrot set can be displayed by mapping the to the Cartesian plane; the real and imaginary axes correspond to the x and y-axes respectively. A rectangular region of the plane is selected and the iteration given by (1) is carried out for each point c in the region. If c is identified as belonging to the Mandelbrot Set it is assigned a certain color (black in the image above). There are many ways to color points that are not in the set; usually the color depends on the smallest k for which |zk| > 2.

There are many excellent programs for viewing the Mandelbrot set and the surrounding region; one is XaoS1, which produced the image above. 1http://wmi.math.u-szeged.hu/xaos/doku.php CPS343: Homework on Computing the Mandelbrot Set using CUDA 2

An X11 program for exploring the Mandelbrot set

As a starting point for this assignment I’ve provided a program called mandelbrot located in the mandelbrot folder of the class repository. The program can be compiled with either g++ -Wall -O2 -o mandelbrot mandelbrot.cc -lX11 -lm -lrt or you can just type smake mandelbrot.cc. Since this program using X as the windowing system, it must be run from a running an X server; any of the workstations will do. The program responds to the following commands: Action Response left button zoom in right button zoom out middle button reset viewport arrow keys move viewport up, down, left, and right + (or =) increase number of per point - (or ) decrease number of iterations per point z zoom in to particular point (for timing test) q quit After each action that causes the viewport to be recomputed and displayed some timing informations is sent to standard output (the terminal in this case) as two numbers. The first number is the seconds required to compute the displayed image while the second number is the cumulative time for all image computations since the program was started.

Assignment

Create a parallel version of the supplied Mandelbrot viewing program using CUDA. You will need to add one additional pointer to access the image in device memory. I suggest copying mandelbrot.cc to mandelbrot.cu and then a. Write a kernel to do the computation for a single pixel. The calculation for this is found in the innermost loop in the computeMandelbrotSet() function. b. Modify computeMandelbrotSet() to launch your kernel. It will need to accept another argument which is a pointer to the image in device memory. c. Modify reallocateImageArray() have the same additional argument and call cudaFree() and cudaMalloc() to deallocate and allocate device memory for the image. d. Make the necessary modifications to function calls in main(). Also, be sure to free the device image memory before exiting the program.