Guest User

Untitled

a guest
Mar 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. (ns assign3.core
  2. (:require [clojure.string :as str])
  3. (:require [clojure.java.io :as io])
  4. (:require [clojure.core.reducers :as r])
  5. (:require [com.climate.claypoole :as cp])
  6. (:require [iota])
  7. (:require [tesser.core :as t])
  8. (:require [tesser.math :as m]))
  9.  
  10.  
  11. ;; PROBLEM 1
  12.  
  13. (defn sleepy-sqrt [x]
  14. (Thread/sleep 2000)
  15. (Math/sqrt x))
  16.  
  17. (comment
  18. (def ncpus (cp/ncpus)) ;; get this ahead of time so it doesn't influence benchmark
  19.  
  20. ;; PARTS 1.1 - 1.3
  21.  
  22. (time (doall (map sleepy-sqrt (repeat 3 20)))) ;; 6007.972807 msecs
  23. (time (doall (pmap sleepy-sqrt (repeat 3 20)))) ;; 2000.279526 msecs
  24. (time (doall (cp/pmap ncpus sleepy-sqrt (repeat 3 20)))) ;; 2006.387321 msecs
  25.  
  26. ;; For each additional thread, we drop ~2s of runtime down to a minimum of ~2s:
  27.  
  28. (let [task #(time (doall (cp/pmap % sleepy-sqrt (repeat 3 20))))
  29. values (range 1 (inc ncpus))]
  30. (map task values)))
  31.  
  32.  
  33. ;; PROBLEM 2
  34.  
  35.  
  36. (def fname "data/soi.csv")
  37.  
  38. (comment (-> (slurp fname)
  39. (str/split #"\n")
  40. (first)))
  41.  
  42. ;; PART 2.1
  43.  
  44. (defn get-lines [fname]
  45. (with-open [f (io/reader fname)]
  46. (line-seq f)))
  47.  
  48. (defn get-header [fname]
  49. (first (get-lines fname)))
  50.  
  51. (comment
  52. (get-header fname)
  53.  
  54. ;; PART 2.2
  55.  
  56. (with-open [f (io/reader fname)] ;; with-open to ensure we close the file
  57. (let [func (fn ([] 0) ([i _] (inc i)))]
  58. (r/fold func (line-seq f))))
  59.  
  60. ;; PART 2.3
  61.  
  62. (first (iota/seq fname)))
  63.  
  64. ;; PART 2.4
  65.  
  66. (defn spy [x & others]
  67. (do
  68. (printf x)
  69. x))
  70.  
  71. (defn parse-value [text]
  72. (let [matches (re-matches #"0*(\d+)(.\d+)?" text)]
  73. (if matches
  74. (double (read-string (second matches)))
  75. text)))
  76.  
  77. (defn parse-line-vec [line]
  78. (map parse-value (str/split line #",")))
  79.  
  80. (comment
  81. (->> (iota/seq fname)
  82. (rest)
  83. (r/map parse-line-vec)
  84. (r/take 5)
  85. (into [])))
  86.  
  87. ;; PART 2.5
  88.  
  89. (defn parse-line-map [header line]
  90. (let [entries (str/split header #",")]
  91. (zipmap (map keyword entries) (parse-line-vec line))))
  92.  
  93. (comment
  94. (->> (iota/seq fname)
  95. (rest)
  96. (r/map (partial parse-line-map (get-header fname)))
  97. (r/take 5)
  98. (into [])))
  99.  
  100. (def records
  101. (->> (iota/seq fname)
  102. (rest)
  103. (r/map (partial parse-line-map (get-header fname)))
  104. (r/foldcat)
  105. (lazy-seq)))
  106.  
  107. ;; Note: the next line takes around 20 seconds, because we have to evaluate
  108. ;; all of our reducers to populate the Cat to fulfill the lazy-seq's request.
  109.  
  110. ;; I'm not sure if it's possible to do a cleaner version of this that goes
  111. ;; directly from reducer to lazy sequence, but if there is, I'd love to learn
  112. ;; about it!
  113.  
  114. (comment
  115. (take 5 records))
  116.  
  117. ;; PART 2.6
  118.  
  119. (->> (m/covariance :A02300 :A00200)
  120. (t/tesser (t/chunk 512 records)))
  121.  
  122. (->> (m/correlation :A02300 :A00200)
  123. (t/tesser (t/chunk 512 records)))
  124.  
  125. ;; there appears to be a noticable connection
  126. ;; between salary and unemployment compensation!
  127.  
  128.  
  129. ;; PROBLEM 3 (unfinished)
  130.  
  131. (def forks (for [i (range 5)]
  132. (ref :meta (hash-map :order i) nil)))
  133.  
  134. (defn philosopher [index]
  135. (let [left (nth forks index)
  136. right (nth forks (mod (inc index) 5))
  137. left-order (:order (meta left))
  138. right-order (:order (meta right))
  139. fork-order (if (< left-order right-order)
  140. [left right]
  141. [right left])]
  142. (dosync
  143. (ref-set (first fork-order) index)
  144. (ref-set (second fork-order) index))))
Add Comment
Please, Sign In to add comment