Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Making Parallel Mandelbrot Future-Safe
#lang racket
(require racket/future racket/flonum "pict-helpers.rkt" future-visualizer)
(define W 256)
(define H 256)
(define bts (make-bytes (* 4 W H)))
(module+ main
(time
(begin
(mand-range 0 (quotient H 2))
(mand-range (quotient H 2) (- H 1))))
(display-bytes bts W H))
(define (mand-range start end)
(for ([y (in-range start end)])
(for ([x (in-range W)])
(define val (in-mandelbrot-set? x y))
(bytes-set! bts
(* 4 (+ (* y H) x))
val))))
;;in-mandelbrot-set? : int int int int -> int
(define (in-mandelbrot-set? x y)
(define c (make-rectangular
(- (/ (* 2.0 (->fl x)) W) 1.5)
(- (/ (* 2.0 (->fl y)) W) 1.0)))
(let loop ([i 0] [z 0.0+0.0i] [iters ITERS])
(cond
[(> i iters) 255]
[else
(define zq (* z z))
(cond
[(> (magnitude zq) 2.0) i]
[else
(loop (add1 i)
(+ zq c)
iters)])])))
(define ITERS 255)
#lang racket
(require racket/future racket/flonum "pict-helpers.rkt" future-visualizer)
(define W 256)
(define H 256)
(define bts (make-bytes (* 4 W H)))
(module+ main
(time
(let ([fa (future (λ () (mand-range 0 (quotient H 2))))]
[fb (future (λ () (mand-range (quotient H 2) (- H 1))))])
(touch fa)
(touch fb)))
(display-bytes bts W H))
(define (mand-range start end)
(for ([y (in-range start end)])
(for ([x (in-range W)])
(define val (in-mandelbrot-set? x y))
(bytes-set! bts
(* 4 (+ (* y H) x))
val))))
;;in-mandelbrot-set? : int int int int -> int
(define (in-mandelbrot-set? x y)
(define c (make-rectangular
(- (/ (* 2.0 (->fl x)) W) 1.5)
(- (/ (* 2.0 (->fl y)) W) 1.0)))
(let loop ([i 0] [z 0.0+0.0i] [iters ITERS])
(cond
[(> i iters) 255]
[else
(define zq (* z z))
(cond
[(> (magnitude zq) 2.0) i]
[else
(loop (add1 i)
(+ zq c)
iters)])])))
(define ITERS 255)
#lang racket
(require racket/future racket/flonum "pict-helpers.rkt" future-visualizer)
(define W 256)
(define H 256)
(define bts (make-bytes (* 4 W H)))
(module+ main
(visualize-futures
(let ([fa (future (λ () (mand-range 0 (quotient H 2))))]
[fb (future (λ () (mand-range (quotient H 2) (- H 1))))])
(touch fa)
(touch fb)))
(display-bytes bts W H))
(define (mand-range start end)
(for ([y (in-range start end)])
(for ([x (in-range W)])
(define val (in-mandelbrot-set? x y))
(bytes-set! bts
(* 4 (+ (* y H) x))
val))))
;;in-mandelbrot-set? : int int int int -> int
(define (in-mandelbrot-set? x y)
(define c (make-rectangular
(- (/ (* 2.0 (->fl x)) W) 1.5)
(- (/ (* 2.0 (->fl y)) W) 1.0)))
(let loop ([i 0] [z 0.0+0.0i] [iters ITERS])
(cond
[(> i iters) 255]
[else
(define zq (* z z))
(cond
[(> (magnitude zq) 2.0) i]
[else
(loop (add1 i)
(+ zq c)
iters)])])))
(define ITERS 255)
#lang typed/racket
(require racket/future racket/flonum future-visualizer/typed)
(require/typed "pict-helpers.rkt" [display-bytes (Bytes Integer Integer -> Void)])
(define W 256)
(define H 256)
(define bts (make-bytes (* 4 W H)))
(module+ main
(visualize-futures-thunk
(λ ()
(let ([fa (future (λ () (mand-range 0 (quotient H 2))))]
[fb (future (λ () (mand-range (quotient H 2) (- H 1))))])
(touch fa)
(touch fb))))
(display-bytes bts W H))
(: mand-range (Integer Integer -> Void))
(define (mand-range start end)
(for ([y (- end start)])
(for ([x (in-range W)])
(define val (in-mandelbrot-set? x (+ y start)))
(bytes-set! bts
(* 4 (+ (* (+ y start) H) x))
val))))
(: in-mandelbrot-set? (Integer Integer -> Integer))
(define (in-mandelbrot-set? x y)
(define c (make-rectangular
(- (/ (* 2.0 (->fl x)) W) 1.5)
(- (/ (* 2.0 (->fl y)) W) 1.0)))
(let loop ([i 0] [z 0.0+0.0i] [ITERS ITERS])
(cond
[(> i ITERS) 255]
[else
(define zq (* z z))
(cond
[(> (magnitude zq) 2.0) i]
[else
(loop (add1 i)
(+ zq c)
ITERS)])])))
(: ITERS Byte)
(define ITERS 255)
#lang typed/racket
(require racket/future racket/flonum future-visualizer/typed)
(require/typed "pict-helpers.rkt" [display-bytes (Bytes Integer Integer -> Void)])
(define W 256)
(define H 256)
(define bts (make-bytes (* 4 W H)))
(module+ main
(time
(let ([fa (future (λ () (mand-range 0 (quotient H 2))))]
[fb (future (λ () (mand-range (quotient H 2) (- H 1))))])
(touch fa)
(touch fb)))
(display-bytes bts W H))
(: mand-range (Integer Integer -> Void))
(define (mand-range start end)
(for ([y (- end start)])
(for ([x (in-range W)])
(define val (in-mandelbrot-set? x (+ y start)))
(bytes-set! bts
(* 4 (+ (* (+ y start) H) x))
val))))
(: in-mandelbrot-set? (Integer Integer -> Integer))
(define (in-mandelbrot-set? x y)
(define c (make-rectangular
(- (/ (* 2.0 (->fl x)) W) 1.5)
(- (/ (* 2.0 (->fl y)) W) 1.0)))
(let loop ([i 0] [z 0.0+0.0i] [ITERS ITERS])
(cond
[(> i ITERS) 255]
[else
(define zq (* z z))
(cond
[(> (magnitude zq) 2.0) i]
[else
(loop (add1 i)
(+ zq c)
ITERS)])])))
(: ITERS Byte)
(define ITERS 255)
#lang typed/racket
(require racket/future racket/flonum future-visualizer/typed)
(require/typed "pict-helpers.rkt" [display-bytes (Bytes Integer Integer -> Void)])
(define W 1024)
(define H 768)
(define bts (make-bytes (* 4 W H)))
(module+ main
(time
(let ([fa (future (λ () (mand-range 0 (quotient H 2))))]
[fb (future (λ () (mand-range (quotient H 2) (- H 1))))])
(touch fa)
(touch fb)))
(display-bytes bts W H))
(: mand-range (Integer Integer -> Void))
(define (mand-range start end)
(for ([y (- end start)])
(for ([x (in-range W)])
(define val (in-mandelbrot-set? x (+ y start)))
(bytes-set! bts
(* 4 (+ (* (+ y start) W) x))
val))))
(: in-mandelbrot-set? (Integer Integer -> Integer))
(define (in-mandelbrot-set? x y)
(define c (make-rectangular
(- (/ (* 2.0 (->fl x)) W) 1.5)
(- (/ (* 2.0 (->fl y)) H) 1.0)))
(let loop ([i 0] [z 0.0+0.0i] [ITERS ITERS])
(cond
[(> i ITERS) 255]
[else
(define zq (* z z))
(cond
[(> (magnitude zq) 2.0) i]
[else
(loop (add1 i)
(+ zq c)
ITERS)])])))
(: ITERS Byte)
(define ITERS 255)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment