Work on improving efficiency of mapping over arrays.
This commit is contained in:
		
							parent
							
								
									cd6b4ae10c
								
							
						
					
					
						commit
						f06febd7c6
					
				
					 3 changed files with 20 additions and 17 deletions
				
			
		| 
						 | 
					@ -11,8 +11,7 @@
 | 
				
			||||||
  :jvm-opts ["-Xmx4g"]
 | 
					  :jvm-opts ["-Xmx4g"]
 | 
				
			||||||
  :license {:name "GNU General Public License v2"
 | 
					  :license {:name "GNU General Public License v2"
 | 
				
			||||||
            :url "http://www.gnu.org/licenses/gpl-2.0.html"}
 | 
					            :url "http://www.gnu.org/licenses/gpl-2.0.html"}
 | 
				
			||||||
  :plugins [[lein-marginalia "0.7.1"]
 | 
					  :plugins [[lein-marginalia "0.7.1"]]
 | 
				
			||||||
            [lein-gorilla "0.3.2"]]
 | 
					 | 
				
			||||||
  :dependencies [[org.clojure/clojure "1.5.1"]
 | 
					  :dependencies [[org.clojure/clojure "1.5.1"]
 | 
				
			||||||
                 [org.clojure/math.combinatorics "0.0.7"]
 | 
					                 [org.clojure/math.combinatorics "0.0.7"]
 | 
				
			||||||
                 [org.clojure/tools.trace "0.7.8"]
 | 
					                 [org.clojure/tools.trace "0.7.8"]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,12 +28,13 @@
 | 
				
			||||||
                                                                  :altitude
 | 
					                                                                  :altitude
 | 
				
			||||||
                                                                  (or (:altitude cell) 0) >)))))
 | 
					                                                                  (or (:altitude cell) 0) >)))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defn flow
 | 
					(def flow
 | 
				
			||||||
  "Compute the total flow upstream of this `cell` in this `world`, and return a cell identical
 | 
					  "Compute the total flow upstream of this `cell` in this `world`, and return a cell identical
 | 
				
			||||||
   to this one but having a value of its flow property set from that computation.
 | 
					   to this one but having a value of its flow property set from that computation.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   Flow comes from a higher cell to a lower only if the lower is the lowest neighbour of the higher."
 | 
					   Flow comes from a higher cell to a lower only if the lower is the lowest neighbour of the higher."
 | 
				
			||||||
   [world cell]
 | 
					   (memoize 
 | 
				
			||||||
 | 
					     (fn [world cell]
 | 
				
			||||||
       (cond
 | 
					       (cond
 | 
				
			||||||
         (not (nil? (:flow cell))) cell
 | 
					         (not (nil? (:flow cell))) cell
 | 
				
			||||||
         (<= (or (:altitude cell) 0) *sealevel*) cell
 | 
					         (<= (or (:altitude cell) 0) *sealevel*) cell
 | 
				
			||||||
| 
						 | 
					@ -42,7 +43,7 @@
 | 
				
			||||||
                {:flow (+ (:rainfall cell)
 | 
					                {:flow (+ (:rainfall cell)
 | 
				
			||||||
                          (apply +
 | 
					                          (apply +
 | 
				
			||||||
                                 (map (fn [neighbour] (:flow (flow world neighbour)))
 | 
					                                 (map (fn [neighbour] (:flow (flow world neighbour)))
 | 
				
			||||||
                      (flow-contributors world cell))))})))
 | 
					                                      (flow-contributors world cell))))})))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defn flow-world
 | 
					(defn flow-world
 | 
				
			||||||
  "Return a world like this `world`, but with cells tagged with the amount of
 | 
					  "Return a world like this `world`, but with cells tagged with the amount of
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,7 +2,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(ns mw-engine.utils
 | 
					(ns mw-engine.utils
 | 
				
			||||||
  (:require
 | 
					  (:require
 | 
				
			||||||
    [clojure.core.reducers :as r]
 | 
					;;    [clojure.core.reducers :as r]
 | 
				
			||||||
    [clojure.math.combinatorics :as combo]))
 | 
					    [clojure.math.combinatorics :as combo]))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(defn abs
 | 
					(defn abs
 | 
				
			||||||
| 
						 | 
					@ -32,13 +32,16 @@
 | 
				
			||||||
(defn map-world
 | 
					(defn map-world
 | 
				
			||||||
  "Apply this `function` to each cell in this `world` to produce a new world.
 | 
					  "Apply this `function` to each cell in this `world` to produce a new world.
 | 
				
			||||||
   the arguments to the function will be the world, the cell, and any
 | 
					   the arguments to the function will be the world, the cell, and any
 | 
				
			||||||
   `additional-args` supplied"
 | 
					   `additional-args` supplied. Note that we parallel map over rows but
 | 
				
			||||||
 | 
					   just map over cells within a row. That's because it isn't worth starting
 | 
				
			||||||
 | 
					   a new thread for each cell, but there may be efficiency gains in 
 | 
				
			||||||
 | 
					   running rows in parallel."
 | 
				
			||||||
  ([world function]
 | 
					  ([world function]
 | 
				
			||||||
    (map-world world function nil))
 | 
					    (map-world world function nil))
 | 
				
			||||||
  ([world function additional-args]
 | 
					  ([world function additional-args]
 | 
				
			||||||
    (into []
 | 
					    (into []
 | 
				
			||||||
           (r/map (fn [row]
 | 
					           (pmap (fn [row]
 | 
				
			||||||
                    (into [] (r/map
 | 
					                    (into [] (map
 | 
				
			||||||
                             #(apply function
 | 
					                             #(apply function
 | 
				
			||||||
                                     (cons world (cons % additional-args)))
 | 
					                                     (cons world (cons % additional-args)))
 | 
				
			||||||
                             row)))
 | 
					                             row)))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue