|
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 load(libdir + "parallelarray-helpers.js"); |
|
10 |
|
11 var nc = 30, maxCol = nc*3; |
|
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(r, c)); |
|
36 } |
|
37 } |
|
38 return result; |
|
39 } |
|
40 |
|
41 var scale = 10000*300; |
|
42 var rows = 4; |
|
43 var cols = 4; |
|
44 |
|
45 // check that we get correct result |
|
46 if (getBuildConfiguration().parallelJS) { |
|
47 var expected = computeSequentially(); |
|
48 assertParallelExecSucceeds( |
|
49 function (m) Array.buildPar(rows * cols, function (xy) { |
|
50 return computeSetByRow((xy/cols)|0,(xy%cols)) |
|
51 }, m), |
|
52 function (r) assertStructuralEq(expected, r)); |
|
53 } |