CPU/GPU Multiprecision Mandelbrot Set

Eric Bainville - Dec 2009

Simple C implementation

The color of a pixel p=(IX,IY) usually corresponds to the number of iterations required to exit from the (O,2) disc for the corresponding value C(p) of the parameter. This algorithm is trivially implemented as follows:

for (int iy=0;iy<height;iy++) for (int ix=0;ix<width;ix++)
  double cx = leftX + ix * stepX;
  double cy = topY - iy * stepY;
  // Z = X+I*Y
  double x = 0;
  double y = 0;
  int it;
  for (it=0;it<maxIt;it++)
    double x2 = x*x;
    double y2 = y*y;
    // Stop iterations when |Z| > 2
    if (x2 + y2 > 4.0) break;
    double twoxy = 2.0*x*y;
    // Z = Z^2 + C
    x = x2 - y2 + cx;
    y = twoxy + cy;
  // Here IT is in 0..ITMAX, get the corresponding COLOR
  unsigned int color = getColor(it,maxIt);
  // Store the color in the A array
  a[ix+iy*width] = color;

width,height are the dimensions of the output image. leftX,topY are the coordinates of the top-left pixel of the image in the complex plane, and stepX,stepY are the increments between horizontal and vertical pixels. a is an array of width*height unsigned integers receiving the 32-bit RGBA colors. maxIt is the max number of iterations.

In the next page, we see how this code translates to an OpenCL kernel.