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.
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
# 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 pointx1
/y1
: Bottom right corner pointdy
/dx
: Increment to x and y pixelsiterations
: Maximum iterations per pixel986728006
#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); }