LoginSignup
6
6

More than 5 years have passed since last update.

Lisp in parallel : スレッド並列計算に lparallel

Posted at

重い関数 heavy-function がありました。

(defun heavy-function (n)
  (sleep n)
  (random n))

下を実行するととても時間がかかってしまいます・・・。

(let ((a (heavy-function 100))
       (b (heavy-function 100)))
  (+ a b))

plet

そこで登場、 lparallel (4コアあるマシンだと思ってください)

(setf *kernel* (make-kernel 4))

(plet ((a (heavy-function 100))
       (b (heavy-function 100)))
  (+ a b))

letplet にするだけであら不思議、ニスレッド並列で計算してくれます。

pmap, preduce, pmap-reduce

こんな重い計算も

(defvar vector (make-array 300))
(iter (for i below 300)
      (setf (aref vector i) (random 300)))

(reduce #'+ (map 'vector #'sin vector))

こうするだけで、計算時間は1/4に!

(preduce #'+ (pmap 'vector #'sin vector))

こうするともっといい。

(pmap-reduce #'sin #'+ vector))

詳しい評価は http://lparallel.org/benchmarks/ へ。

ptree

makefile のような依存関係を一瞬で並列に解決。

(ptree ((area   (width height) (* width height))
        (width  (border)       (+ 7 (* 2 border)))
        (height (border)       (+ 5 (* 2 border)))
        (border ()             1))
  area)

; => 63

ホスト間通信

lfarm で、同じAPIのままホスト間通信。 https://github.com/lmj/lfarm/blob/master/README.md

書き捨て御免!

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6