70 Points

Embarrassingly Parallel

Welcome back to COMP1917. Late one night, you're browsing wikipedia and discover that the Mandelbrot set is an Embarrassingly parallel problem. All those years ago you could have written a parallelized Mandelbrot set generator.

Fortunately you do not have to generate an image. Instead, you need to calculate the total number of iterations that occurred if you were to generate an image.

Each point is independent, so you should be able to calculate the work set with complete parallelism.

Additional points will be awarded for the fastest program, determined using time on a (controlled) Linux system.

Example point iteration function:

int iterations_at_point( double x, double y, int max )
{
    double x0 = x;
    double y0 = y;
    int iter = 0;

    while( (x*x + y*y <= 4) && iter < max ) {

        double xt = x*x - y*y + x0;
        double yt = 2*x*y + y0;

        x = xt;
        y = yt;

        iter++;
    }

    return iter;
}
Source

Hints

  • Don't spawn too many threads. One thread for every pixel may seem like a good idea, but consider the overhead.

Restrictions

  • CPU Only. You cannot use a GPU (or alike)

Input

# We will use command line arguments to provide parameters to your program.
# ./mandelbrot x0 y0 x1 y1 dy dx iterations
./mandelbrot -2.25 -1.5 0.75 1.5 0.01 0.01 65536

We will provide 7 arguments:

  • x0/y0: Top left corner point
  • x1/y1: Bottom right corner point
  • dy/dx: Increment to x and y pixels
  • iterations: Maximum iterations per pixel

Output

986728006

Reference (Single-threaded, C implementation)

#include <stdio.h>
#include <stdlib.h>

int iterations_at_point( double x, double y, int max )
{
    double x0 = x;
    double y0 = y;
    int iter = 0;

    while( (x*x + y*y <= 4) && iter < max ) {

        double xt = x*x - y*y + x0;
        double yt = 2*x*y + y0;

        x = xt;
        y = yt;

        iter++;
    }

    return iter;
}

void mandelbrot(double x0, double y0, double x1, double y1, double dx, 
    double dy, int maxIter) {

    // printf("Expected Points: %f x %f = %f\n", (x1-x0)/dx, (y1-y0)/dy, (x1-x0)/dx*(y1-y0)/dy);

    int numIters = 0;

    for (double y = y0; y < y1; y += dy) {
        for (double x = x0; x < x1; x += dx) {
            numIters += iterations_at_point(x, y, maxIter);
        }
    }

    printf("%d\n", numIters);

}