我本来期望下述代码能在所有可用的 cpu 上进行并行化(在这个案例中为 12 个 CPU),但它只在一个 CPU 上运行。我错在哪里了?
(ns csv2summap.core
(:require [clojure.data.csv :as csv]
[clojure.java.io :as io]
[clojure.core.reducers :as r])))
(with-open [writer (io/writer "numbers.csv")]
(csv/write-csv
writer
(take 10000000
(repeatedly #(vector (char (+ 65 (rand-int 26))) (rand-int 1000))))))
(defn sum-vals
([] {})
([m [k v]]
(update m k (fnil + 0) (Integer/parseInt v))))
(defn merge-sums
([] {})
([& m] (apply merge-with + m)))
(time
(with-open [reader (io/reader "numbers.csv")]
(doall
(r/fold
(/ 10000000 12)
merge-sums
sum-vals
(csv/read-csv reader)))))
(def n-cpu (.availableProcessors (Runtime/getRuntime)))
=>12
(ns csv2summap.core
(:require [clojure.data.csv :as csv]
[clojure.java.io :as io]
[clojure.core.reducers :as r])))
-
(defproject csv2summap "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.1"]
[org.clojure/data.csv "1.0.0"]]
:repl-options {:init-ns csv2summap.core})