2010-12-16 12 views
6

私はclojureでpmapを使っていくつかのパフォーマンステストを行っています。私はpmapで使用されているスレッドの数を制御できるようにしたいと考えています。 OpenMPのようなものを使うと、omp_set_num_threads()を使ってスレッドの数を設定できることが分かります。私はclojureに似たものがあれば疑問に思っていた。pmapで使用されるスレッドの数を制御する方法はありますか?

答えて

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を書くことができます。

関連する問題