我本以为以下操作将会并行化运行在所有可用的CPU(在本例中是12个)上,但它只在其中一个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})