linux-sysprog · advanced · ~15 min
Parallel reduction without shared mutation — pass each thread a unique partial-sum slot.
Sum an integer array in parallel: split it into contiguous chunks, let each thread total its own chunk into a private slot, then add the partial sums. No shared accumulator, so no mutex is needed.
Implement long parallel_sum(const int *a, int n, int nthreads) that returns the sum of a[0..n-1] computed across nthreads threads.
a, n: the array and its length.nthreads: how many threads to split the work across.Divides the array into nthreads roughly-equal contiguous chunks, has each thread sum its chunk into its own partial-result slot, joins all threads, and returns the sum of the partials. Returns -1 if nthreads <= 0 or nthreads > 64.
a = [0,1,2,...,99] (sum = 4950)
parallel_sum(a, 100, 4) -> 4950
parallel_sum(a, 100, 1) -> 4950
parallel_sum(a, 100, 7) -> 4950 (uneven split still totals correctly)
parallel_sum(a, 0, 4) -> 0
parallel_sum(a, 100, 0) -> -1 (invalid thread count)
n == 0: returns 0.nthreads <= 0 or > 64: returns -1.n doesn't divide evenly.Parallel reduction — split work into chunks, each thread sums its chunk, main adds the partial sums.
An int array a of length n, plus nthreads (the number of worker threads).
The total sum of the array, or -1 if nthreads <= 0 or > 64.
Split into nthreads contiguous chunks; each thread sums into its own slot. n == 0 gives 0.
#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.