michael@0: // Adapted from michael@0: // michael@0: // https://github.com/RiverTrail/RiverTrail/blob/master/examples/mandelbrot/mandelbrot.js michael@0: // michael@0: // which in turn is adapted from a WebCL implementation available at michael@0: // michael@0: // http://www.ibiblio.org/e-notes/webcl/mandelbrot.html michael@0: michael@0: load(libdir + "parallelarray-helpers.js"); michael@0: michael@0: var nc = 30, maxCol = nc*3; michael@0: michael@0: // this is the actual mandelbrot computation, ported to JavaScript michael@0: // from the WebCL / OpenCL example at michael@0: // http://www.ibiblio.org/e-notes/webcl/mandelbrot.html michael@0: function computeSetByRow(x, y) { michael@0: var Cr = (x - 256) / scale + 0.407476; michael@0: var Ci = (y - 256) / scale + 0.234204; michael@0: var I = 0, R = 0, I2 = 0, R2 = 0; michael@0: var n = 0; michael@0: while ((R2+I2 < 2.0) && (n < 512)) { michael@0: I = (R+R)*I+Ci; michael@0: R = R2-I2+Cr; michael@0: R2 = R*R; michael@0: I2 = I*I; michael@0: n++; michael@0: } michael@0: return n; michael@0: } michael@0: michael@0: function computeSequentially() { michael@0: result = []; michael@0: for (var r = 0; r < rows; r++) { michael@0: for (var c = 0; c < cols; c++) { michael@0: result.push(computeSetByRow(r, c)); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: var scale = 10000*300; michael@0: var rows = 4; michael@0: var cols = 4; michael@0: michael@0: // check that we get correct result michael@0: if (getBuildConfiguration().parallelJS) { michael@0: var expected = computeSequentially(); michael@0: assertParallelExecSucceeds( michael@0: function (m) Array.buildPar(rows * cols, function (xy) { michael@0: return computeSetByRow((xy/cols)|0,(xy%cols)) michael@0: }, m), michael@0: function (r) assertStructuralEq(expected, r)); michael@0: }