/* * 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 #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; }; struct line{ int row; ulong couleur[NPIXELS]; }; #define NPLACES 10 struct line* line_array[NPLACES]; int head; int tail; sem_t place_dispo; sem_t info_prete; pthread_mutex_t mutex_line; 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; /* Calculate and draw points */ for (row = p_info->row_min; row < p_info->row_max; ++row) { struct line* l = malloc(sizeof(struct line)); l->row = row; for (col = 0; col < width; ++col) { /* Compute the color of point (row, col) */ l->couleur[col] = compute(row, col); } sem_wait(&place_dispo); pthread_mutex_lock(&mutex_line); line_array[head] = l; head = (head+1) % NPLACES; pthread_mutex_unlock(&mutex_line); sem_post(&info_prete); } return NULL; } /* ---- Main program ---- */ int main(int argc, char *argv[]) { struct timeval t1, t2; /* 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(); pthread_mutex_init(&mutex, NULL); sem_init(&place_dispo, 0, NPLACES); sem_init(&info_prete, 0, 0); head = 0; tail = 0; /* Boundaries for each thread */ struct computation_info info[NTHREADS]; int i; for(i=0; irow; int col; for(col=0; colcouleur[col]); /* Color point (row, col) */ XDrawPoint (display, win, gc, col, row); /* Apply change */ XFlush (display); } free(l); tail = (tail+1)%NPLACES; sem_post(&place_dispo); } for(i=0; i