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"]
 | 
			
		||||
  :license {:name "GNU General Public License v2"
 | 
			
		||||
            :url "http://www.gnu.org/licenses/gpl-2.0.html"}
 | 
			
		||||
  :plugins [[lein-marginalia "0.7.1"]
 | 
			
		||||
            [lein-gorilla "0.3.2"]]
 | 
			
		||||
  :plugins [[lein-marginalia "0.7.1"]]
 | 
			
		||||
  :dependencies [[org.clojure/clojure "1.5.1"]
 | 
			
		||||
                 [org.clojure/math.combinatorics "0.0.7"]
 | 
			
		||||
                 [org.clojure/tools.trace "0.7.8"]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,21 +28,22 @@
 | 
			
		|||
                                                                  :altitude
 | 
			
		||||
                                                                  (or (:altitude cell) 0) >)))))
 | 
			
		||||
 | 
			
		||||
(defn flow
 | 
			
		||||
(def flow
 | 
			
		||||
  "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.
 | 
			
		||||
 | 
			
		||||
   Flow comes from a higher cell to a lower only if the lower is the lowest neighbour of the higher."
 | 
			
		||||
   [world cell]
 | 
			
		||||
   (cond
 | 
			
		||||
     (not (nil? (:flow cell))) cell
 | 
			
		||||
     (<= (or (:altitude cell) 0) *sealevel*) cell
 | 
			
		||||
     true
 | 
			
		||||
      (merge cell
 | 
			
		||||
           {:flow (+ (:rainfall cell)
 | 
			
		||||
               (apply +
 | 
			
		||||
                 (map (fn [neighbour] (:flow (flow world neighbour)))
 | 
			
		||||
                      (flow-contributors world cell))))})))
 | 
			
		||||
   (memoize 
 | 
			
		||||
     (fn [world cell]
 | 
			
		||||
       (cond
 | 
			
		||||
         (not (nil? (:flow cell))) cell
 | 
			
		||||
         (<= (or (:altitude cell) 0) *sealevel*) cell
 | 
			
		||||
         true
 | 
			
		||||
         (merge cell
 | 
			
		||||
                {:flow (+ (:rainfall cell)
 | 
			
		||||
                          (apply +
 | 
			
		||||
                                 (map (fn [neighbour] (:flow (flow world neighbour)))
 | 
			
		||||
                                      (flow-contributors world cell))))})))))
 | 
			
		||||
 | 
			
		||||
(defn flow-world
 | 
			
		||||
  "Return a world like this `world`, but with cells tagged with the amount of
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,7 +2,7 @@
 | 
			
		|||
 | 
			
		||||
(ns mw-engine.utils
 | 
			
		||||
  (:require
 | 
			
		||||
    [clojure.core.reducers :as r]
 | 
			
		||||
;;    [clojure.core.reducers :as r]
 | 
			
		||||
    [clojure.math.combinatorics :as combo]))
 | 
			
		||||
 | 
			
		||||
(defn abs
 | 
			
		||||
| 
						 | 
				
			
			@ -32,13 +32,16 @@
 | 
			
		|||
(defn map-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
 | 
			
		||||
   `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]
 | 
			
		||||
    (map-world world function nil))
 | 
			
		||||
  ([world function additional-args]
 | 
			
		||||
    (into []
 | 
			
		||||
           (r/map (fn [row]
 | 
			
		||||
                    (into [] (r/map
 | 
			
		||||
           (pmap (fn [row]
 | 
			
		||||
                    (into [] (map
 | 
			
		||||
                             #(apply function
 | 
			
		||||
                                     (cons world (cons % additional-args)))
 | 
			
		||||
                             row)))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue