|
1 // Adapted from |
|
2 // |
|
3 // https://github.com/RiverTrail/RiverTrail/blob/master/examples/mandelbrot/mandelbrot.js |
|
4 // |
|
5 // which in turn is adapted from a WebCL implementation available at |
|
6 // |
|
7 // http://www.ibiblio.org/e-notes/webcl/mandelbrot.html |
|
8 |
|
9 var nc = 30, maxCol = nc*3, cr,cg,cb; |
|
10 |
|
11 load(libdir + "util.js"); |
|
12 |
|
13 // this is the actual mandelbrot computation, ported to JavaScript |
|
14 // from the WebCL / OpenCL example at |
|
15 // http://www.ibiblio.org/e-notes/webcl/mandelbrot.html |
|
16 function computeSetByRow(x, y) { |
|
17 var Cr = (x - 256) / scale + 0.407476; |
|
18 var Ci = (y - 256) / scale + 0.234204; |
|
19 var I = 0, R = 0, I2 = 0, R2 = 0; |
|
20 var n = 0; |
|
21 while ((R2+I2 < 2.0) && (n < 512)) { |
|
22 I = (R+R)*I+Ci; |
|
23 R = R2-I2+Cr; |
|
24 R2 = R*R; |
|
25 I2 = I*I; |
|
26 n++; |
|
27 } |
|
28 return n; |
|
29 } |
|
30 |
|
31 function computeSequentially() { |
|
32 result = []; |
|
33 for (var r = 0; r < rows; r++) { |
|
34 for (var c = 0; c < cols; c++) { |
|
35 result.push(computeSetByRow(c, r)); |
|
36 } |
|
37 } |
|
38 return result; |
|
39 } |
|
40 |
|
41 function computeParallel() { |
|
42 return new ParallelArray([rows, cols], function(r, c) { |
|
43 return computeSetByRow(c, r); |
|
44 }).flatten(); |
|
45 } |
|
46 |
|
47 function compare(arrs, pas) { |
|
48 for (var r = 0; r < rows; r++) { |
|
49 for (var c = 0; c < cols; c++) { |
|
50 assertEq(seq[c + r * cols], par.get(r, c)); |
|
51 } |
|
52 } |
|
53 } |
|
54 |
|
55 var scale = 10000*300; |
|
56 var rows = 1024; |
|
57 var cols = 1024; |
|
58 |
|
59 // Experimentally, warmup doesn't seem to be necessary: |
|
60 benchmark("MANDELBROT", 1, DEFAULT_MEASURE, |
|
61 computeSequentially, computeParallel); |