在Clojure中使用pmap来并行化合并排序。程序在结束前挂起约1分钟,然后终止

问题描述 投票:1回答:1

我正在Clojure中编写一个程序,它接受来自文本文件10000.txt的输入(这有10k,无符号整数。)然后我将该列表传递给我的合并排序函数(一个sigle线程,2,4,8,16,32)线程)。

当我运行程序时,通过键入“clojure test.clj”,它输出每个函数的已用时间,但程序不会终止。

它挂起就像它正在等待输入或即将输出其他内容。大约1分钟后,程序终于终止了。幕后肯定会发生一些事情。关于在输入后终止程序需要做什么/我需要做什么的任何想法?

程序输出(终止前)这是它挂起约1分钟的地方

(use 'clojure.java.io)
(require '[clojure.string :as str])

;Read file and store into numbers, as a string
(def numbers (slurp "10000.txt"))

;Parse the string 'numbers', ignore the spaces 
;and save the result into x1 (a lazy-seq of integers)
(def x1 (map #(Integer/parseInt %) (str/split numbers #"\s+")))

;Function that performs the operation of merge sort algorithm
(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))))))

;The other merge-sort functions use pmap to run merge sort in parallel
;Using 1,2,4,8,16,32 threads
(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)))))

;Run each of the merge-sort functions and output their time
(time (naive-merge-sort x1))
(time (parallel-merge-sort-2 x1))
(time (parallel-merge-sort-4 x1))
(time (parallel-merge-sort-8 x1))
(time (parallel-merge-sort-16 x1))
(time (parallel-merge-sort-32 x1))

这是我的10000.txt文件:https://pastebin.com/5vKXUk1u

我的预期结果是程序在最后一次打印后终止,而不是花1分钟终止。

谢谢大家的时间和帮助!

multithreading concurrency clojure terminate pmap
1个回答
7
投票

你需要在最后调用shutdown-agents来阻止Clojure的线程池。

另见clojure.org上的Agents and Asynchronous Actions

请注意,使用代理会启动一个非守护程序后台线程池,以防止关闭JVM。使用shutdown-agents终止这些线程并允许关闭。

© www.soinside.com 2019 - 2024. All rights reserved.