Assignment 9 — Pictures in parallel

The Mandelbrot set and a blur, on twelve threads: one line of OpenMP each, a table of how long each takes on 1 to 12 threads, and why neither gets twelve times faster.

Due Nov 13, 2026

In Class 21, one line, #pragma omp parallel for, put a lab machine’s twelve threads, two on each of six cores, to work on the rows of the Mandelbrot set. This assignment finishes that, and then does the same for a blur, which reads each pixel’s neighbours and so has to write its answer somewhere new.

You will hand in six files: image.h, image.c, Makefile, mandel.c, blur.c and notes.txt. The library is the one from class: image.h and image.c.

Every program builds with make and no warnings. Class 21’s Makefile builds with -O2 -fopenmp and leaves the sanitizer out, since it would be part of every time you take; check each program once with it, as the Makefile’s comment shows, and run it with nothing reported. Every function you write gets the design recipe’s one-line comment, and one that hands back new memory says the caller frees it. Every program frees every picture it asked for. No [ or ] in any function but main: a walk moves its pointer, and a pixel reached by its column and row starts at px + 3 * (y * w + x).

  1. mandel.c — the set, on twelve threads. Class 21’s mandel.c with #pragma omp parallel for schedule(dynamic) above the row loop, built with Class 21’s Makefile. In notes.txt, a table: the real time of time ./mandel > m.ppm on 1, 2, 3, 4, 8 and 12 threads, once without schedule(dynamic) and once with it. Class 21’s loop runs all six:

    for t in 1 2 3 4 8 12; do echo "threads $t"; time OMP_NUM_THREADS=$t ./mandel > m.ppm; done

    Done when the table has its twelve times, and OMP_NUM_THREADS=1 ./mandel > one.ppm and OMP_NUM_THREADS=12 ./mandel > m.ppm give the same file: cmp one.ppm m.ppm prints nothing.

  2. blur.c — every pixel the average of its square. Write unsigned char * blur(unsigned char * px, int w, int h), which hands back a new w by h picture: each pixel the average of the 3 by 3 square around it in px, itself and its eight neighbours, red with red, green with green, blue with blue. A pixel on the picture’s edge has no full square, so it is copied as it is. Its comment ends the caller frees it.

    It has to be a new picture: the average reads the old neighbours, and written into px the pixels below would average ones already blurred. In order:

    • new_image(w, h) for the answer, and a pointer q that walks it, one row at a time from out + 3 * y * w, with put_pixel.
    • For the pixel at column x, row y, p = px + 3 * (y * w + x). The square is dy from -1 to 1 and dx from -1 to 1, and its pixel s = p + 3 * (dy * w + dx): one row up is 3 * w bytes back.
    • Three sums, r, g and b, over the nine; each divided by 9.
    • The edge: x == 0, y == 0, x == w - 1 or y == h - 1, and then put_pixel(&q, *p, *(p + 1), *(p + 2)).

    main reads a picture, blurs it, writes the blurred one, and frees both. In the Makefile, blur joins the line that adds the library, mandel gray blur: image.c, and its clean.

    Done when, on stripes.ppm, red over white over blue, the border between red and white comes out pink. Exactly: row 65 is the last red row, and its square reaches one white row, so column 5 of it is 255 85 85: 6 red pixels and 3 white, red 255, green 3 * 255 / 9. The byte where it starts is 15 + 3 * (65 * 320 + 5), the header being 15 bytes, so

    ./blur < stripes.ppm > b.ppm
    od -An -tu1 -j 62430 -N 3 b.ppm

    prints 255 85 85, and the line below it, row 66, 255 170 170.

  3. blur in parallel. The line above blur’s row loop. Then the same check as mandel’s: on 1 thread and on 12, ./blur < m.ppm gives the same file. In notes.txt, a second table: the real time of time ./blur < m.ppm > b.ppm on 1, 2, 4, 8 and 12 threads, with and without schedule(dynamic). Done when the two files are the same, and the table has its ten times.

  4. notes.txt — three short answers, below the two tables.

    1. Without schedule(dynamic), mandel on 3 threads is slower than on
      1. Why, with the picture in mind?
    2. Does schedule(dynamic) help blur? Why, or why not?
    3. On 12 threads, which comes closer to twelve times faster, mandel or blur? What part of blur’s run do the threads not touch?
  5. A wider blur — optional. This step is optional and not needed for full marks. Make the square’s size a #define, R, the radius: each pixel the average of the (2 * R + 1) by (2 * R + 1) square around it, and the pixels within R of an edge copied. R of 1 is step 2’s blur.

    • dy and dx run from -R to R, and the average divides by (2 * R + 1) * (2 * R + 1): 25 for R of 2.
    • The edge is x < R, y < R, x >= w - R or y >= h - R.
    • With R of 1 the program must still pass step 2’s check.

    Then a third table: blur on m.ppm, 1 thread and 12, for R of 1, 3 and 5, and one sentence: why does the gain from twelve threads grow with R?

    Done when the table has its six times, and R of 1 still prints 255 85 85.