使用clojure我在序列中有非常大量的数据,我想并行处理它,核心数量相对较少(4到8).
最简单的方法是使用pmap而不是将map处理函数映射到数据序列上.但协调开销导致我的案件净亏损.
我认为原因是pmap假设跨数据映射的函数非常昂贵.查看pmap的源代码,它似乎future依次为序列的每个元素构造一个,因此函数的每次调用都发生在一个单独的线程上(在可用内核的数量上循环).
以下是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 & …Run Code Online (Sandbox Code Playgroud)