clojure.core/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.
Examples¶
Note by belun¶
for insight into how pmap does stuff see this presentation: "From Concurrency to Parallelism", by David Edgar Liebke @ http://incanter.org/downloads/fjclj.pdf
Note by reborg¶
The following can be used to understand how many threads pmap runs at once (assuming tasks are roughly the same computational cost). The min level correspond to the situation where the consumer is slower than the producer, while the max level is when the consumer is faster than the producer:
- When the sequence is not chunked (for example subvec) the min parallelism is 1 and the max parallelism is
(+ 2 N-cores). Example: with 12 cores,(doall (pmap #(Thread/sleep %) (subvec (into [] (range 1000)) 0 999)))keeps 12+2 threads busy. - In case of chunked sequences (vast majority are size 32), the min parallelism is
(min chunk-size (+ 2 n-cores)), while the max amount is equal to(+ chunk-size 2 N-cores). Example: with 12 cores,(doall (pmap #(Thread/sleep %) (range 1000)))keeps 12+2+32 threads busy.
See also¶
- clojure.core/map (13)
- clojure.core/future (1)
- clojure.core/pcalls (1)
- clojure.core/pvalues (1)
- clojure.core/partition (6)
Content from the matching ClojureDocs page, with authors credited on each contribution.