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.

  1. 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-orient turns it the way the phone held it, and identify prints its size. Then, before changing anything, time your A9 blur on 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.ppm

    Done when loupe b.ppm shows your photo blurred, and notes.txt starts with the photo’s size and those two real times.

  2. blur_rows — a band of rows. In blur.c, the work of blur’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 rows y0 up to y1, not including y1, of px into out. Each row’s walk still starts at out + 3 * y * w. blur then makes out, calls blur_rows(px, out, w, h, 0, h) and hands out back, and the pragma is gone.

    Done when A9’s check on stripes.ppm prints 255 85 85 and, for row 66, 255 170 170 again.

  3. Threads of your own. Each thread blurs one band of rows, and what it needs goes in a struct:

    • struct band, with px, out, w, h, y0 and y1, each with a comment saying what it holds.
    • void * blur_band(void * arg): arg is a struct band *, so cast it, call blur_rows with its fields, and return NULL.
    • blur takes the number of threads, unsigned char * blur(unsigned char * px, int w, int h, int n). It asks malloc for n of pthread_t and n of struct band. Band t has the rows from t * h / n up to (t + 1) * h / n. For each t, struct band * b = bands + t;, its fields filled through b->, and pthread_create(threads + t, NULL, blur_band, b). Then each thread joined, pthread_join(*(threads + t), NULL), and only then both arrays freed.
    • main takes the number of threads from its command line: it is int main(int argc, char ** argv), and atoi(argv[1]), from stdlib.h, is the number after ./blur.

    Done when ./blur 1 < photo.ppm > one.ppm and ./blur 12 < photo.ppm > b.ppm give the same file, cmp one.ppm b.ppm printing nothing; ./blur 12 < stripes.ppm passes step 2’s check; and the sanitizer’s build runs ./blur 12 on stripes.ppm with nothing reported.

  4. The times. In notes.txt, a table: the real time of ./blur on 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; done

    Done when the table has its six times.

  5. notes.txt — three short answers, below the table.

    1. On 12 threads, is your blur faster 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?
    2. blur frees the bands only after the joins. What could go wrong if it freed them straight after the loop of pthread_create?
    3. How much faster are 12 threads than 6, and why not twice, with the lab machine’s six cores in mind?
  6. 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 for R of 10. The same average can be had from 2 * (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: R a #define, the square from -R to R, and the pixels within R of 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 row y, and each x from R to w - R - 1, the sums of the 2 * R + 1 pixels from x - R to x + R in that row, red with red, green with green, blue with blue, at sums + 3 * (y * w + x).
    • Pass 2, down: for each pixel not within R of an edge, the sums of the 2 * R + 1 entries of sums from row y - R to y + R in 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 for down. struct band gets sums.
    • Worked through on stripes.ppm, R of 1, row 65, column 5: pass 1 leaves 765 0 0 at rows 64 and 65, which are red, and 765 765 765 at 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, for R of 1, 3, 5 and 10, and one sentence: why do the two passes gain more as R grows?

    Done when, for R of 1, 3 and 5, the two passes give the same file as the one pass, cmp printing nothing, and the table has its eight times.