User Tools

Site Tools


study:data_analysis:parallel_data

<markdown> Improving Performance with Parallel Programming

# Parallel processing with pmap

![!mandelbrot.png](http://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Mandel_zoom_00_mandelbrot_set.jpg/800px-Mandel_zoom_00_mandelbrot_set.jpg)

* Mandelbrot Set </markdown>

 Z0 = 0 (단, Z은 복소수.)
 Z' = Z^2 + C
 를 만족하는 복소수 C의 집합을 Mandelbrot set 이라고 한다.
 
 Z = X + Yi
 C = A + Bi 이라고 하면,
 Z' = Z^2 + C = (X+Yi)(X+Yi) + (A+Bi)
 Z' = X^2 - Y^2 + A + i(2XY + B)
 
 Z'의 절대값이 2를 넘지 않아야 발산하지 않는다.
 A : -2.5 ~ 1.0
 B : -1.0 ~ 1.0
 범위내에서 발산하지 않는 값을 찾아낼 수 있다.
 
 Mandeldelbrot set을 image로 표현하기 위해서는
 각 좌표값을 A, B범위에 맞춰서 scaling 해서 발산여부를 확인한다. 

<markdown>

</markdown>

(defn get-escape-point
  ([scaled-x scaled-y max-iterations]
   (loop [x 0, y 0, iteration 0]
     (let [x2 (* x x)
           y2 (* y y)]
       (if (and (< (+ x2 y2) 4)
                (< iteration max-iterations))
         (recur (+ (- x2 y2) scaled-x)
                (+ (* 2 x y) scaled-y)
                (inc iteration))
         iteration)))))
 
(defn scale-to
  ([pixel maximum [lower upper]]
   (+ (* (/ pixel maximum) (Math/abs (- upper lower))) lower)))
 
(defn scale-point
  ([pixel-x pixel-y max-x max-y set-range]
   [(scale-to pixel-x max-x (:x set-range))
    (scale-to pixel-y max-y (:y set-range))]))
 
(defn output-points
  ([max-x max-y]
   (let [range-y (range max-y)]
     (mapcat (fn [x] (map #(vector x %) range-y))
             (range max-x)))))
 
(defn mandelbrot-pixel
  ([max-x max-y max-iterations set-range]
   (partial mandelbrot-pixel
            max-x max-y max-iterations set-range))
  ([max-x max-y max-iterations set-range [pixel-x pixel-y]]
   (let [[x y] (scale-point pixel-x pixel-y max-x max-y
                            set-range)]
     (get-escape-point x y max-iterations))))
 
(defn mandelbrot
  ([mapper max-iterations max-x max-y set-range]
   (doall
     (mapper (mandelbrot-pixel
               max-x max-y max-iterations set-range)
             (output-points max-x max-y)))))
 
 
(def mandelbrot-range
  {:x [-2.5, 1.0], :y [-1.0, 1.0]} )

<markdown>

</markdown>

(def iteration 500)
(def resolution 500)

(def m (time (mandelbrot map iteration resolution resolution mandelbrot-range)))
;;"Elapsed time: 17991.922 msecs"

(def m (time (mandelbrot pmap iteration resolution resolution mandelbrot-range)))
;;"Elapsed time: 11562.502 msecs"

(def m (time (mandelbrot map (* 2 iteration) resolution resolution mandelbrot-range)))
;;"Elapsed time: 26766.022 msecs"

(def m (time (mandelbrot pmap (* 2 iteration) resolution resolution mandelbrot-range)))
;;"Elapsed time: 17194.2 msecs"

<markdown>

</markdown>

study/data_analysis/parallel_data.txt · Last modified: 2019/02/04 14:26 (external edit)