有没有办法控制pmap使用的线程数?

时间:2010-12-16 01:30:57

标签: clojure

我正在使用pmap对clojure进行一些性能测试,我希望能够控制与pmap一起使用的线程数。我知道在使用像OpenMP这样的东西时,可以使用omp_set_num_threads()来设置线程数。我想知道在clojure中是否会有类似的东西。

1 个答案:

答案 0 :(得分:7)

以下是pmap的代码:

(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."
  ([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))))

如您所见,pmap使用所有可用的处理器并循环使用它们。所以,不,没有办法设置线程数...但你总是可以编写自己的pmap,这将提供这样的功能。

相关问题