重い関数 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))
let
を plet
にするだけであら不思議、ニスレッド並列で計算してくれます。
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
書き捨て御免!