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: var nc = 30, maxCol = nc*3, cr,cg,cb; michael@0: michael@0: load(libdir + "util.js"); 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(c, r)); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: function computeParallel() { michael@0: return new ParallelArray([rows, cols], function(r, c) { michael@0: return computeSetByRow(c, r); michael@0: }).flatten(); michael@0: } michael@0: michael@0: function compare(arrs, pas) { michael@0: for (var r = 0; r < rows; r++) { michael@0: for (var c = 0; c < cols; c++) { michael@0: assertEq(seq[c + r * cols], par.get(r, c)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: var scale = 10000*300; michael@0: var rows = 1024; michael@0: var cols = 1024; michael@0: michael@0: // Experimentally, warmup doesn't seem to be necessary: michael@0: benchmark("MANDELBROT", 1, DEFAULT_MEASURE, michael@0: computeSequentially, computeParallel);