multithreading - Multithreaded merge-sort algorithm in Clojure -
i need write implementation of merge-sort algorithm in clojure in single thread , using parallelism options 2, 4, 8, 16, , 32 threads.
program read large collection of integers (1 million) text file , put them list sorting.
newbie on clojure , functional programming @ all.
i've written code reading file ...
(use 'clojure.java.io) (defn get-lines [fname] (with-open [r (reader fname)] (doall (map read-string (line-seq r))))) (def numbers (get-lines "numbers.dat"))
... , found single thread implementation.
can't realize parallel algorithm. seems beyond me.
me?
if looking for...
(use 'clojure.java.io) (defn get-lines [fname] (with-open [r (reader fname)] (doall (map read-string (line-seq r))))) (def numbers (get-lines "numbers.dat")) (defn merge-lists [left right] (loop [head [] l left r right] (if (empty? l) (concat head r) (if (empty? r) (concat head l) (if (> (first l) (first r)) (recur (conj head (first r)) l (rest r)) (recur (conj head (first l)) (rest l) r)))))) (defn naive-merge-sort [list] (if (< (count list) 2) list (apply merge-lists (map naive-merge-sort (split-at (/ (count list) 2) list))))) (defn parallel-merge-sort-2 [list] (if (< (count list) 2) list (apply merge-lists (pmap naive-merge-sort (split-at (/ (count list) 2) list))))) (defn parallel-merge-sort-4 [list] (if (< (count list) 2) list (apply merge-lists (pmap parallel-merge-sort-2 (split-at (/ (count list) 2) list))))) (defn parallel-merge-sort-8 [list] (if (< (count list) 2) list (apply merge-lists (pmap parallel-merge-sort-4 (split-at (/ (count list) 2) list))))) (defn parallel-merge-sort-16 [list] (if (< (count list) 2) list (apply merge-lists (pmap parallel-merge-sort-8 (split-at (/ (count list) 2) list))))) (defn parallel-merge-sort-32 [list] (if (< (count list) 2) list (apply merge-lists (pmap parallel-merge-sort-16 (split-at (/ (count list) 2) list)))))
naive-merge-sort single threaded implementation. parallel implementations breaks input list 2-32 chunks sorted using naive-merge-sort.
Comments
Post a Comment