Uneven jobs and work stealing
When job lengths vary, a fixed split leaves threads idle. Work stealing fixes it — and the measurements match the arithmetic.
- Predict the makespan of a static split from the job lengths
- Explain how work stealing rebalances without knowing the job lengths in advance
- Recognise when a benchmark's simulated work hides the effect being measured
The programs in 1_uneven_job_length/ keep the 262,144 jobs but give them four different length ranges, appended one after another:
std::uniform_int_distribution bin_1(1, 25);
std::uniform_int_distribution bin_2(25, 50);
std::uniform_int_distribution bin_3(50, 75);
std::uniform_int_distribution bin_4(75, 100);
std::generate_n(std::back_inserter(work_items), elements_per_bin, [&] { return bin_1(mt); });
// ... bin_2, bin_3, bin_4
So the job lengths rise along the vector. That is the worst case for static_partitioner, which cuts the vector into contiguous blocks: with four threads, thread 0 gets every short job and thread 3 every long one.
Predicting the makespan
The total work is \(N \cdot \bar{t}\), where the mean job length is \(\bar{t} = (13 + 37.5 + 62.5 + 87.5)/4 \approx 50\) µs. A perfect split over \(T\) threads takes \(N\bar{t}/T\). A static split takes as long as its slowest block: for \(T = 4\), that is thread 3’s 65,536 jobs at 87.5 µs each — 1.75 times the ideal.
Work stealing
The default partitioner never needs to know the job lengths. A thread starts with the whole range, splits it in half, keeps the left half and puts the right half on its own deque; it repeats until the piece is small. An idle thread takes the oldest entry from someone else’s deque — the biggest piece left — and splits that. Threads that drew short jobs run out sooner, so they steal more.
The grain size sets how small pieces get. Too large, and the last piece leaves threads idle at the end; too small, and the bookkeeping of splitting and stealing starts to cost more than the work. Try the grain selector above.
Measuring it properly
Running the repository’s programs pinned to 4 CPUs gives 11.6 s for static and 9.7 s for the default — a ratio of only 1.19, not 1.75. The reason is the simulated work. On this machine, sleep_for(d) actually sleeps for about \(d + 80\) µs: a 1 µs request takes 76 µs, a 100 µs request 184 µs. That fixed overhead is the same for every job, so it dilutes the difference between short and long ones.
Replacing the sleep with a busy-wait,
auto t0 = std::chrono::steady_clock::now();
while (std::chrono::steady_clock::now() - t0 < std::chrono::microseconds(work_items[i])) {}
makes each job take as long as requested. One thread then needs 13.3 s, which is \(262{,}144 \times 50\) µs plus a few percent, and the prediction holds:
Beyond 8 threads both curves flatten here. With 16 busy-waiting threads on a 22-thread CPU that is also running the operating system, threads begin to share physical cores, and the measurement says more about the machine than about the partitioner.
Try it yourself. Shuffle work_items with std::shuffle before the loop and re-time the static version. Predict the result first: once the long jobs are spread evenly along the vector, what does a contiguous split cost compared with work stealing?