Assignment 10 — A blur on threads you start
A9’s blur on threads started by hand with pthreads, each with a struct of its own and a band of rows, timed on a photo of your own.
Due Nov 18, 2026
In Class 23, pthread_create started threads by hand, and each was handed a struct with its share of the work. This assignment does that for A9’s blur: the pragma comes out, and the threads you start yourself go in. It runs on a photo of your own.
You will hand in five files: image.h, image.c, Makefile, blur.c and notes.txt. The photo stays with you: notes.txt has its size and your times, and the picture itself is not handed in.
Every program builds with make and no warnings: the Makefile’s CFLAGS is -Wall -g -O2 -pthread, with -pthread where A9 had -fopenmp. Check the program once with the sanitizer, make -B blur CFLAGS="-Wall -g -pthread -fsanitize=address", and run it with nothing reported. Every function you write gets the design recipe’s one-line comment. Every program frees all the memory it asked for. As in A9, no [ or ] in any function but main.
A photo of your own, as a PPM. Any photo you like, and one you took is best, at least 2000 pixels wide; a phone’s are about 4000. Get it onto the lab machine, for instance by emailing it to yourself and saving it from the browser. A JPEG, a PNG or an iPhone’s HEIC all work:
magick photo.jpg -auto-orient photo.ppm identify photo.ppm-auto-orientturns it the way the phone held it, andidentifyprints its size. Then, before changing anything, time your A9bluron it, on 1 thread and on 12:time OMP_NUM_THREADS=1 ./blur < photo.ppm > b.ppm time OMP_NUM_THREADS=12 ./blur < photo.ppm > b.ppmDone when
loupe b.ppmshows your photo blurred, andnotes.txtstarts with the photo’s size and those tworealtimes.blur_rows— a band of rows. Inblur.c, the work ofblur’s row loop moves into a function of its own,void blur_rows(unsigned char * px, unsigned char * out, int w, int h, int y0, int y1), which blurs rowsy0up toy1, not includingy1, ofpxintoout. Each row’s walk still starts atout + 3 * y * w.blurthen makesout, callsblur_rows(px, out, w, h, 0, h)and handsoutback, and the pragma is gone.Done when A9’s check on
stripes.ppmprints255 85 85and, for row 66,255 170 170again.Threads of your own. Each thread blurs one band of rows, and what it needs goes in a
struct:struct band, withpx,out,w,h,y0andy1, each with a comment saying what it holds.void * blur_band(void * arg):argis astruct band *, so cast it, callblur_rowswith its fields, and returnNULL.blurtakes the number of threads,unsigned char * blur(unsigned char * px, int w, int h, int n). It asksmallocfornofpthread_tandnofstruct band. Bandthas the rows fromt * h / nup to(t + 1) * h / n. For eacht,struct band * b = bands + t;, its fields filled throughb->, andpthread_create(threads + t, NULL, blur_band, b). Then each thread joined,pthread_join(*(threads + t), NULL), and only then both arrays freed.maintakes the number of threads from its command line: it isint main(int argc, char ** argv), andatoi(argv[1]), fromstdlib.h, is the number after./blur.
Done when
./blur 1 < photo.ppm > one.ppmand./blur 12 < photo.ppm > b.ppmgive the same file,cmp one.ppm b.ppmprinting nothing;./blur 12 < stripes.ppmpasses step 2’s check; and the sanitizer’s build runs./blur 12onstripes.ppmwith nothing reported.The times. In
notes.txt, a table: therealtime of./bluron your photo with 1, 2, 4, 6, 8 and 12 threads. One loop runs all six:for t in 1 2 4 6 8 12; do echo "threads $t"; time ./blur $t < photo.ppm > b.ppm; doneDone when the table has its six times.
notes.txt— three short answers, below the table.- On 12 threads, is your
blurfaster than A9’s on the same photo, slower, or about the same? What does A9’s pragma do with the rows that your bands do too? blurfrees the bands only after the joins. What could go wrong if it freed them straight after the loop ofpthread_create?- How much faster are 12 threads than 6, and why not twice, with the lab machine’s six cores in mind?
- On 12 threads, is your
The same blur, in two passes — optional. This step is optional and not needed for full marks. A9’s wider blur, with radius
R, reads(2 * R + 1) * (2 * R + 1)pixels for each one it writes: 441 forRof 10. The same average can be had from2 * (2 * R + 1)reads, 42, in two passes: first the sums across each row, then the sums of those sums down each column. The picture comes out exactly the same.- If you did not do A9’s step 5, do it first, in
blur_rows:Ra#define, the square from-RtoR, and the pixels withinRof an edge copied. It is the picture the two passes must match. - The sums need more room than a byte:
int * sums = malloc(3 * w * h * sizeof(int)), three to a pixel, like the picture. - Pass 1,
across: for each rowy, and eachxfromRtow - R - 1, the sums of the2 * R + 1pixels fromx - Rtox + Rin that row, red with red, green with green, blue with blue, atsums + 3 * (y * w + x). - Pass 2,
down: for each pixel not withinRof an edge, the sums of the2 * R + 1entries ofsumsfrom rowy - Rtoy + Rin its column, each divided by(2 * R + 1) * (2 * R + 1). The others are copied, as before. - Pass 2 reads sums from rows above and below its own band, which another thread’s pass 1 wrote. So every thread finishes pass 1 before any thread starts pass 2: two rounds, the threads started and joined for
across, and then started and joined again fordown.struct bandgetssums. - Worked through on
stripes.ppm,Rof 1, row 65, column 5: pass 1 leaves765 0 0at rows 64 and 65, which are red, and765 765 765at row 66, which is white. Pass 2 adds the three,2295 765 765, and divides by 9:255 85 85, as step 2’s check.
Then, in
notes.txt, a table: one pass and two passes on 12 threads, forRof 1, 3, 5 and 10, and one sentence: why do the two passes gain more asRgrows?Done when, for
Rof 1, 3 and 5, the two passes give the same file as the one pass,cmpprinting nothing, and the table has its eight times.- If you did not do A9’s step 5, do it first, in