liveBook · Manning

Work stealing

As explained in Clojure: The Essential Reference

Work-stealing is a parallel scheduling technique in which an idle worker takes unprocessed tasks from another worker's queue.

Work stealing helps keep workers busy during a parallel fold. A fold splits a supported collection into chunks, reduces the chunks in parallel, and combines their results. Workers may split chunks further; when a worker becomes free, it can take unfinished work from another worker. section 7.1.1

This matters when work takes unpredictable amounts of time: one busy worker need not leave others idle. It improves on basic thread pooling in that situation, but parallelism is not automatically faster for small jobs. section 7.1.1

A fold with parallel work available

After `(require '[clojure.core.reducers :as r])`, `(r/fold + (into [] (range 1000000)))` returns `499999500000`. For a 1,000-element vector, the supplied benchmark instead found `r/fold` slower than `reduce`; use the extra scheduling work where the workload warrants it. section 7.1.1

How Clojure: The Essential Reference covers it

How deep it goes
Explains chunk splitting and idle-worker stealing, but does not detail the underlying scheduling algorithm.
Examples
Uses a million-element fold sum and a benchmark where folding 1,000 elements is slower than reducing them.
What you can do after
Evaluate/understand when work stealing can help a fold and why a small fold may still be slower.
How it connects
Connects work stealing to parallel reduction, divide and conquer, Java fork-join, and thread pooling.

Also explained in

Compare how 5 books explain work stealing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.