You're viewing version 1.2.0 of pmap. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16
  • (pmap f coll)
  • (pmap f coll & colls)
Like map, except f is applied in parallel. Semi-lazy in that the
parallel computation stays ahead of the consumption, but doesn't
realize the entire result unless required. Only useful for
computationally intensive functions where the time of f dominates
the coordination overhead.

3 Examples top

  • ;; This function operates just like map.  See
    ;; clojure.core/map for more details.
    user=> (pmap inc [1 2 3 4 5])
    (2 3 4 5 6)
  • ;; create a function that simulates a long running process using Thread/sleep
    (defn long-running-job [n]
        (Thread/sleep 3000) ; wait for 3 seconds
        (+ n 10))
    
    ;; used `doall` to eagerly evaluate `map`, which evaluates lazily by default
    
    ;; notice that the total elapse time is almost 3 secs * 4
    user=> (time (doall (map long-running-job (range 4))))
    "Elapsed time: 11999.235098 msecs"
    (10 11 12 13)
    
    ;; notice that the total elapse time is almost 3 secs only
    user=> (time (doall (pmap long-running-job (range 4))))
    "Elapsed time: 3200.001117 msecs"
    (10 11 12 13)
  • ;; pmap is implemented using Clojure futures.  See examples for 'future'
    ;; for discussion of an undesirable 1-minute wait that can occur before
    ;; your standalone Clojure program exits if you do not use shutdown-agents.
Log in to add / edit an example.

See Also top

  • 1
    clojure.core/map

    Returns a lazy sequence consisting of the result of applying f to the

  • 0
    clojure.core/future

    Takes a body of expressions and yields a future object that will in

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:5426 top

(defn pmap
  "Like map, except f is applied in parallel. Semi-lazy in that the
  parallel computation stays ahead of the consumption, but doesn't
  realize the entire result unless required. Only useful for
  computationally intensive functions where the time of f dominates
  the coordination overhead."
  {:added "1.0"}
  ([f coll]
   (let [n (+ 2 (.. Runtime getRuntime availableProcessors))
         rets (map #(future (f %)) coll)
         step (fn step [[x & xs :as vs] fs]
                (lazy-seq
                 (if-let [s (seq fs)]
                   (cons (deref x) (step xs (rest s)))
                   (map deref vs))))]
     (step rets (drop n rets))))
  ([f coll & colls]
   (let [step (fn step [cs]
                (lazy-seq
                 (let [ss (map seq cs)]
                   (when (every? identity ss)
                     (cons (map first ss) (step (map rest ss)))))))]
     (pmap #(apply f %) (step (cons coll colls))))))
Vars in clojure.core/pmap: apply cons defn drop let
Used in 4 other vars bean pcalls preduce *test-statistic-map*

Comments top

1 comment(s) for pmap.

for insight into how pmap does stuff see this presentation: "From Concurrency to Parallelism", by David Edgar Liebke @ http://incanter.org/downloads/fjclj.pdf

Log in to add a comment.