我原本以为下面的代码会在所有可用的cpu(在此情况下为12个)上并行运行,但实际上它只有一个运行。我错在哪里了?
(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})