I have a function that uses a for
loop to modify the elements of a matrix. The element-wise function is not huge, so I am unsure about the best way to deal with this. I want to have one thread for each core of the machine (automatically detected) to just execute all these. Ideally, it could also balance execution based on core workload. I have looked into creating custom thread pools for receiving workers, but not sure about if that will work. pmap
seems like it might be useful, but I saw in another thread that the coordination overhead would be signficant if every single operation was a future. What are your thoughts about how to approach this? Code below, with some links afterwards.
(defn alter-matrix! [matrix]
(let [row-count (nrow matrix)
col-count (ncol matrix)]
; Iterate through each element by coordinates.
(for [x (range row-count)
y (range col-count)]
; Calculate whether the element will be altered.
(let [event (rand)]
; 50% chance of alteration.
(if (< event 0.50)
; Unsafely set the new element to a altered version.
(.setQuick matrix x y
; Return an altered form of the element.
(alter-element (.getQuick matrix x y))))))))
(defn alter-element [element] (let [selector (rand-int 1 4)]
(cond (= 1 selector) 1.0 (= 2 selector) (* 2.0 element) (= 3 selector) (* 0.5 element))))
Parallel doseq for Clojure http://clojure.com/blog/2012/05/15/anatomy-of-reducer.html Better alternative to pmap in Clojure for parallelizing moderately inexpensive functions over big data?