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).
mandel.c— the set, on twelve threads. Class 21’smandel.cwith#pragma omp parallel for schedule(dynamic)above the row loop, built with Class 21’sMakefile. Innotes.txt, a table: therealtime oftime ./mandel > m.ppmon 1, 2, 3, 4, 8 and 12 threads, once withoutschedule(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; doneDone when the table has its twelve times, and
OMP_NUM_THREADS=1 ./mandel > one.ppmandOMP_NUM_THREADS=12 ./mandel > m.ppmgive the same file:cmp one.ppm m.ppmprints nothing.blur.c— every pixel the average of its square. Writeunsigned char * blur(unsigned char * px, int w, int h), which hands back a newwbyhpicture: each pixel the average of the 3 by 3 square around it inpx, 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
pxthe pixels below would average ones already blurred. In order:new_image(w, h)for the answer, and a pointerqthat walks it, one row at a time fromout + 3 * y * w, withput_pixel.- For the pixel at column
x, rowy,p = px + 3 * (y * w + x). The square isdyfrom-1to1anddxfrom-1to1, and its pixels = p + 3 * (dy * w + dx): one row up is3 * wbytes back. - Three sums,
r,gandb, over the nine; each divided by 9. - The edge:
x == 0,y == 0,x == w - 1ory == h - 1, and thenput_pixel(&q, *p, *(p + 1), *(p + 2)).
mainreads a picture, blurs it, writes the blurred one, and frees both. In theMakefile,blurjoins the line that adds the library,mandel gray blur: image.c, and itsclean.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 is255 85 85: 6 red pixels and 3 white, red255, green3 * 255 / 9. The byte where it starts is15 + 3 * (65 * 320 + 5), the header being 15 bytes, so./blur < stripes.ppm > b.ppm od -An -tu1 -j 62430 -N 3 b.ppmprints
255 85 85, and the line below it, row 66,255 170 170.blurin parallel. The line aboveblur’s row loop. Then the same check asmandel’s: on 1 thread and on 12,./blur < m.ppmgives the same file. Innotes.txt, a second table: therealtime oftime ./blur < m.ppm > b.ppmon 1, 2, 4, 8 and 12 threads, with and withoutschedule(dynamic). Done when the two files are the same, and the table has its ten times.notes.txt— three short answers, below the two tables.- Without
schedule(dynamic),mandelon 3 threads is slower than on- Why, with the picture in mind?
- Does
schedule(dynamic)helpblur? Why, or why not? - On 12 threads, which comes closer to twelve times faster,
mandelorblur? What part ofblur’s run do the threads not touch?
- Without
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 withinRof an edge copied.Rof 1 is step 2’s blur.dyanddxrun from-RtoR, and the average divides by(2 * R + 1) * (2 * R + 1): 25 forRof 2.- The edge is
x < R,y < R,x >= w - Rory >= h - R. - With
Rof 1 the program must still pass step 2’s check.
Then a third table:
bluronm.ppm, 1 thread and 12, forRof 1, 3 and 5, and one sentence: why does the gain from twelve threads grow withR?Done when the table has its six times, and
Rof 1 still prints255 85 85.