Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns assign3.core
- (:require [clojure.string :as str])
- (:require [clojure.java.io :as io])
- (:require [clojure.core.reducers :as r])
- (:require [com.climate.claypoole :as cp])
- (:require [iota])
- (:require [tesser.core :as t])
- (:require [tesser.math :as m]))
- ;; PROBLEM 1
- (defn sleepy-sqrt [x]
- (Thread/sleep 2000)
- (Math/sqrt x))
- (comment
- (def ncpus (cp/ncpus)) ;; get this ahead of time so it doesn't influence benchmark
- ;; PARTS 1.1 - 1.3
- (time (doall (map sleepy-sqrt (repeat 3 20)))) ;; 6007.972807 msecs
- (time (doall (pmap sleepy-sqrt (repeat 3 20)))) ;; 2000.279526 msecs
- (time (doall (cp/pmap ncpus sleepy-sqrt (repeat 3 20)))) ;; 2006.387321 msecs
- ;; For each additional thread, we drop ~2s of runtime down to a minimum of ~2s:
- (let [task #(time (doall (cp/pmap % sleepy-sqrt (repeat 3 20))))
- values (range 1 (inc ncpus))]
- (map task values)))
- ;; PROBLEM 2
- (def fname "data/soi.csv")
- (comment (-> (slurp fname)
- (str/split #"\n")
- (first)))
- ;; PART 2.1
- (defn get-lines [fname]
- (with-open [f (io/reader fname)]
- (line-seq f)))
- (defn get-header [fname]
- (first (get-lines fname)))
- (comment
- (get-header fname)
- ;; PART 2.2
- (with-open [f (io/reader fname)] ;; with-open to ensure we close the file
- (let [func (fn ([] 0) ([i _] (inc i)))]
- (r/fold func (line-seq f))))
- ;; PART 2.3
- (first (iota/seq fname)))
- ;; PART 2.4
- (defn spy [x & others]
- (do
- (printf x)
- x))
- (defn parse-value [text]
- (let [matches (re-matches #"0*(\d+)(.\d+)?" text)]
- (if matches
- (double (read-string (second matches)))
- text)))
- (defn parse-line-vec [line]
- (map parse-value (str/split line #",")))
- (comment
- (->> (iota/seq fname)
- (rest)
- (r/map parse-line-vec)
- (r/take 5)
- (into [])))
- ;; PART 2.5
- (defn parse-line-map [header line]
- (let [entries (str/split header #",")]
- (zipmap (map keyword entries) (parse-line-vec line))))
- (comment
- (->> (iota/seq fname)
- (rest)
- (r/map (partial parse-line-map (get-header fname)))
- (r/take 5)
- (into [])))
- (def records
- (->> (iota/seq fname)
- (rest)
- (r/map (partial parse-line-map (get-header fname)))
- (r/foldcat)
- (lazy-seq)))
- ;; Note: the next line takes around 20 seconds, because we have to evaluate
- ;; all of our reducers to populate the Cat to fulfill the lazy-seq's request.
- ;; I'm not sure if it's possible to do a cleaner version of this that goes
- ;; directly from reducer to lazy sequence, but if there is, I'd love to learn
- ;; about it!
- (comment
- (take 5 records))
- ;; PART 2.6
- (->> (m/covariance :A02300 :A00200)
- (t/tesser (t/chunk 512 records)))
- (->> (m/correlation :A02300 :A00200)
- (t/tesser (t/chunk 512 records)))
- ;; there appears to be a noticable connection
- ;; between salary and unemployment compensation!
- ;; PROBLEM 3 (unfinished)
- (def forks (for [i (range 5)]
- (ref :meta (hash-map :order i) nil)))
- (defn philosopher [index]
- (let [left (nth forks index)
- right (nth forks (mod (inc index) 5))
- left-order (:order (meta left))
- right-order (:order (meta right))
- fork-order (if (< left-order right-order)
- [left right]
- [right left])]
- (dosync
- (ref-set (first fork-order) index)
- (ref-set (second fork-order) index))))
Add Comment
Please, Sign In to add comment