linux-sysprog · advanced · ~15 min
Parallel reduction without shared mutation — pass each thread a unique partial-sum slot.
Implement long parallel_sum(const int *a, int n, int nthreads).
Split the array into nthreads roughly-equal contiguous chunks. Each thread sums its chunk into a partial result. Main joins all threads, adds the partial sums, and returns the total.
Each thread must NOT touch a shared accumulator (no mutex needed for this design). Return -1 on argument errors (nthreads <= 0 or > 64).
Parallel reduction — split work into chunks, each thread sums its chunk, main adds the partial sums.
#include <pthread.h>
long parallel_sum(const int *a, int n, int nthreads) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.