]> git.itanic.dy.fi Git - mandelbrot/commitdiff
Parallelize with openmp
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Tue, 3 May 2011 16:00:02 +0000 (19:00 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Tue, 3 May 2011 16:00:02 +0000 (19:00 +0300)
Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
Makefile
mandelbrot.c

index cb2543bd4017ecf0dc25401eb017163184cf3c98..556c985f13a8cc6ee4e17163547c01601ca0aa6f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 SDL_CONFIG = $(shell sdl-config --cflags)
 SDL_LIBS = $(shell sdl-config --libs)
-CFLAGS = $(SDL_CONFIG) -Wall -O2 -g
-
+CFLAGS = $(SDL_CONFIG) -Wall -O2 -g -fopenmp
+LDFLAGS = -fopenmp
 LIBS = $(SDL_LIBS) -lm -lrt
 
 CC = gcc
@@ -22,10 +22,11 @@ else
 endif
 
 mandelbrot: $(MANDELBROT_OBJS)
-       $(QUIET_LINK)$(CC) $(LIBS) -o $@ $(MANDELBROT_OBJS)
+       $(QUIET_LINK)$(CC) $(LIBS) $(LDFLAGS) -o $@ $(MANDELBROT_OBJS)
 
 debug-mandelbrot: $(MANDELBROT_DEBUG_OBJS)
-       $(QUIET_LINK)$(CC) $(LIBS) -o $@ $(MANDELBROT_DEBUG_OBJS) -DDEBUG
+       $(QUIET_LINK)$(CC) $(LIBS) $(LDFLAGS) -o $@ $(MANDELBROT_DEBUG_OBJS) \
+               -DDEBUG
 
 .c.o:
        $(QUIET_CC)$(CC) -MMD -MF .$@.d $(CFLAGS) -c $< -o $@
index be706ebc1d15a01002c41f1943185afafabdd172..624fcf2ebda615e2275021d1d50aa59b91624a30 100644 (file)
@@ -45,8 +45,12 @@ int draw_mandelbrot(struct SDL_Surface *screen, double x1, double x2,
        xstep = xlen / (double)screen->w;
        ystep = ylen / (double)screen->h;
 
-       for (ys = 0, y0 = y1; ys < screen->h; y0 += ystep, ys++) {
-               for (xs = 0, x0 = x1; xs < screen->w; x0 += xstep, xs++) {
+       y0 = y1;
+#pragma omp parallel for private(xs, ys, x0, y0)
+       for (ys = 0; ys < screen->h; ys++) {
+               y0 = y1 + ystep * ys;
+               x0 = x1;
+               for (xs = 0; xs < screen->w; xs++) {
                        iteration = get_mandelbrot_iterations(x0, y0);
 
                        if (iteration == MAX_ITERATION)
@@ -55,11 +59,11 @@ int draw_mandelbrot(struct SDL_Surface *screen, double x1, double x2,
                                putpixel(screen, xs, ys,
                                        iteration * 8, iteration,
                                        iteration / 4);
+                       x0 += xstep;
                }
-
-               SDL_Flip(screen);
        }
 
+       SDL_Flip(screen);
        return 0;
 }