js/src/parjs-benchmarks/mandelbrot.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/parjs-benchmarks/mandelbrot.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,61 @@
     1.4 +// Adapted from
     1.5 +//
     1.6 +// https://github.com/RiverTrail/RiverTrail/blob/master/examples/mandelbrot/mandelbrot.js
     1.7 +//
     1.8 +// which in turn is adapted from a WebCL implementation available at
     1.9 +//
    1.10 +// http://www.ibiblio.org/e-notes/webcl/mandelbrot.html
    1.11 +
    1.12 +var nc = 30, maxCol = nc*3, cr,cg,cb;
    1.13 +
    1.14 +load(libdir + "util.js");
    1.15 +
    1.16 +// this is the actual mandelbrot computation, ported to JavaScript
    1.17 +// from the WebCL / OpenCL example at
    1.18 +// http://www.ibiblio.org/e-notes/webcl/mandelbrot.html
    1.19 +function computeSetByRow(x, y) {
    1.20 +  var Cr = (x - 256) / scale + 0.407476;
    1.21 +  var Ci = (y - 256) / scale + 0.234204;
    1.22 +  var I = 0, R = 0, I2 = 0, R2 = 0;
    1.23 +  var n = 0;
    1.24 +  while ((R2+I2 < 2.0) && (n < 512)) {
    1.25 +    I = (R+R)*I+Ci;
    1.26 +    R = R2-I2+Cr;
    1.27 +    R2 = R*R;
    1.28 +    I2 = I*I;
    1.29 +    n++;
    1.30 +  }
    1.31 +  return n;
    1.32 +}
    1.33 +
    1.34 +function computeSequentially() {
    1.35 +  result = [];
    1.36 +  for (var r = 0; r < rows; r++) {
    1.37 +    for (var c = 0; c < cols; c++) {
    1.38 +      result.push(computeSetByRow(c, r));
    1.39 +    }
    1.40 +  }
    1.41 +  return result;
    1.42 +}
    1.43 +
    1.44 +function computeParallel() {
    1.45 +  return new ParallelArray([rows, cols], function(r, c) {
    1.46 +    return computeSetByRow(c, r);
    1.47 +  }).flatten();
    1.48 +}
    1.49 +
    1.50 +function compare(arrs, pas) {
    1.51 +  for (var r = 0; r < rows; r++) {
    1.52 +    for (var c = 0; c < cols; c++) {
    1.53 +      assertEq(seq[c + r * cols], par.get(r, c));
    1.54 +    }
    1.55 +  }
    1.56 +}
    1.57 +
    1.58 +var scale = 10000*300;
    1.59 +var rows = 1024;
    1.60 +var cols = 1024;
    1.61 +
    1.62 +// Experimentally, warmup doesn't seem to be necessary:
    1.63 +benchmark("MANDELBROT", 1, DEFAULT_MEASURE,
    1.64 +          computeSequentially, computeParallel);

mercurial