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.

What you'll learn
  • 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.

The same jobs, statically split (orange line: the makespan) and dynamically scheduled. With uneven jobs the static split leaves three threads idle while the last one works through the longest bin.

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.

Four threads working through 64 jobs whose lengths rise along the range. Each step jumps to the next job completion. The readout lists every steal and each thread's deque; at the end, the threads that drew short jobs have run more of them.
Top: static_partitioner cuts the range into four contiguous blocks, one per thread, so thread 3 receives all jobs of 75 to 100 microseconds and finishes last while the others sit idle. Bottom: the default auto_partitioner splits the range in halves; idle threads 1, 2 and 3 steal pieces, which are split further into small chunks. Top: static_partitioner cuts the range into four contiguous blocks, one per thread, so thread 3 receives all jobs of 75 to 100 microseconds and finishes last while the others sit idle. Bottom: the default auto_partitioner splits the range in halves; idle threads 1, 2 and 3 steal pieces, which are split further into small chunks.
Schematic of the two partitioners. Static decides everything before the loop starts; the default keeps splitting and lets idle threads take the largest remaining piece.

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:

Speedup over one thread with the busy-wait. Dynamic stays close to the ideal line up to 8 threads (7.5× on 8); static reaches only 2.2× on 4 threads — 6.08 s, against a predicted 65,536 × 87.5 µs = 5.73 s.
On 8 CPUs the sleep-based programs report a 1.24× gap between the partitioners; the busy-wait versions show the real 1.72×.

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?

View source on GitHub →