liveBook · Manning

Parallel map

As explained in Clojure: The Essential Reference

pmap is a Clojure function that applies a function to one or more collections in parallel and returns a lazy sequence of results in input order.

Parallel map applies a function to collection elements concurrently while preserving the ordered, lazy-sequence interface of map. In Clojure, pmap is useful for independent, sufficiently expensive transformations, but parallelism does not automatically improve every workload. section 9.1.5 section 9.1

Its contract and practical behavior are:

  • Accepts a function and one required sequential collection, plus optional collections; the function arity must match the number of collections. section 9.1.5
  • Returns a lazy sequence in input order, even though transformations execute in parallel through futures. section 9.1.5
  • Raises ArityException without a collection and has no transducer version. section 9.1.5
  • Can process work in chunks; chunking may reduce overhead, while cheap functions or poorly balanced tasks can make pmap slower than map. section 9.1.5

Ordered results with parallel evaluation

(pmap + (range 10) (range 10)) ;; (0 2 4 6 8 10 12 14 16 18) (defn f [x] (Thread/sleep (+ (* 10 x) 500)) (println (str "done-" x)) x) (def s (pmap f (range 100))) (first s) ;; 0 (take 2 s) ;; (0 1) The side-effect messages can appear as work completes, while the realized sequence remains ordered. section 9.1.5

Choosing granularity

Benchmarks show that pmap can lose to map for cheap operations such as inc, while partitioning work into larger groups can improve a costly computation. Another timing example shows uneven sleep durations completing far faster after sorting the work before pmap.

Listing 9.5. → Parallel computation, Lazy sequences
(pmap
  ([f coll])
  ([f coll & colls]))

(pcalls [& fns])

(pvalues [& exprs])

Browse the compact signatures for pmap, pcalls, and pvalues. section 9.1.5

How Clojure: The Essential Reference covers it

How deep it goes
Explains contracts, laziness, ordering, chunking, exceptions, and performance tradeoffs, but does not provide a transducer form.
Examples
Uses elementwise addition, delayed printing, uneven sleeps, chunked evaluation, and Criterium benchmarks.
What it assumes
Assumes familiarity with Clojure functions, collections, sequences, and map-like arity; requires a Seqable input collection.
What you can do after
Apply pmap to suitable independent workloads and evaluate whether chunking or sequential map better fits the cost profile.
How it connects
Connects pmap with map, futures, lazy sequences, sequential collections, pcalls, and pvalues.

Also explained in

Compare how 2 books explain parallel map →

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.