/* * Sequential Mandelbrot program * * This program computes and displays all or part of the Mandelbrot * set. By default, it examines all points in the complex plane * that have both real and imaginary parts between -2 and 2. * Command-line parameters allow zooming in on a specific part of * this range. * * Usage: * mandelbrot maxiter [x0 y0 size] * where * maxiter denotes the maximum number of iterations at each point * x0, y0, and size specify the range to examine (a square * centered at x0 + iy0 of size 2*size by 2*size -- by default, * a square of size 4 by 4 centered at the origin) * * Input: none, except the optional command-line arguments * Output: a graphical display as described in Wilkinson & Allen, * displayed using the X Window system, plus text output to * standard output showing the above parameters. * * * Code originally obtained from Web site for Wilkinson and Allen's * text on parallel programming: * http://www.cs.uncc.edu/~abw/parallel/par_prog/ * * Reformatted and revised by B. Massingill, C. Parrot and F. Trahay */ #include "mandelbrot.h" #include #include #define NTHREADS 4 /* Boundaries of the Mandelbrot set to display */ double r_min = -N; double r_max = N; double i_min = -N; double i_max = N; uint width = NPIXELS; /* dimensions of display window */ uint height = NPIXELS; double scale_r, scale_i, scale_color; uint maxiter; ulong min_color, max_color; Display *display; GC gc; Window win; struct computation_info{ int row_min; int row_max; }; pthread_mutex_t mutex; /* Draw the mandelbrot set */ void* draw_mandelbrot(void*arg) { struct computation_info* p_info = (struct computation_info*)arg; uint col, row; ulong couleur; /* Calculate and draw points */ for (row = p_info->row_min; row < p_info->row_max; ++row) { for (col = 0; col < width; ++col) { /* Compute the color of point (row, col) */ couleur = compute(row, col); /* Apply the color to point (row, col) */ int ret = pthread_mutex_lock(&mutex); assert(ret == 0); /* Change the drawing color */ XSetForeground (display, gc, couleur); /* Color point (row, col) */ XDrawPoint (display, win, gc, col, row); /* Apply change */ XFlush (display); ret = pthread_mutex_unlock(&mutex); assert(ret == 0); } } return NULL; } /* ---- Main program ---- */ int main(int argc, char *argv[]) { struct timeval t1, t2; pthread_mutex_init(&mutex, NULL); /* Check command-line arguments */ if (argc != 2) { fprintf(stderr, "usage: %s maxiter\n", argv[0]); return EXIT_FAILURE; } /* Process command-line arguments */ maxiter = atoi(argv[1]); init_mandelbrot(); /* Boundaries for each thread */ struct computation_info info[NTHREADS]; int i; for(i=0; i