Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

init commit

  • Loading branch information...
commit f3b04c1a59c1c7f69d384de3a8913d3255ff9ba6 0 parents
@liuliu authored
Showing with 141 additions and 0 deletions.
  1. +33 −0 sum.html
  2. +108 −0 sum.js
33 sum.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<title>Parallable Sum</title>
+</head>
+<body>
+<input style="margin-left:16px" type="button" value="Sum" onclick="sumArray()" /><input style="margin-left:16px" type="button" value="Sum with Web Worker (BETA)" onclick="sumArrayAsync()" />
+<div style="margin-top:20px">One single run will take 10 seconds or so, please be patient</div>
+<div id="elapsed_time" style="margin-top:20px"></div>
+<div style="margin-top:20px" id="output"></div>
+<script type="text/javascript" src="sum.js"></script>
+<script type="text/javascript">
+var some = [];
+for (var i = 0; i < 98573; i++)
+ some[i] = i;
+
+function sumArray() {
+ var elapsed_time = (new Date()).getTime();
+ document.getElementById("output").innerHTML = sum(some);
+ document.getElementById("elapsed_time").innerHTML = ((new Date()).getTime() - elapsed_time) + "ms";
+}
+
+function sumArrayAsync() {
+ var elapsed_time = (new Date()).getTime();
+ sum(some, true, 8)(function (result) {
+ document.getElementById("output").innerHTML = result;
+ document.getElementById("elapsed_time").innerHTML = ((new Date()).getTime() - elapsed_time) + "ms";
+ });
+}
+</script>
+</body>
+</html>
108 sum.js
@@ -0,0 +1,108 @@
+if (parallable === undefined) {
+ var parallable = function (file, funct) {
+ parallable.core[funct.toString()] = funct().core;
+ return function () {
+ var i;
+ var async, worker_num, params;
+ if (arguments.length > 1) {
+ async = arguments[arguments.length - 2];
+ worker_num = arguments[arguments.length - 1];
+ params = new Array(arguments.length - 2);
+ for (i = 0; i < arguments.length - 2; i++)
+ params[i] = arguments[i];
+ } else {
+ async = arguments[0].async;
+ worker_num = arguments[0].worker;
+ params = arguments[0];
+ delete params["async"];
+ delete params["worker"];
+ params = [params];
+ }
+ var scope = { "shared" : {} };
+ var ctrl = funct.apply(scope, params);
+ if (async) {
+ var executed = 0;
+ var outputs = new Array(worker_num);
+ var inputs = ctrl.pre.apply(scope, [worker_num]);
+ /* sanitize scope shared because for Chrome/WebKit, worker only support JSONable data */
+ for (i in scope.shared)
+ /* delete function, if any */
+ if (typeof scope.shared[i] == "function")
+ delete scope.shared[i];
+ /* delete DOM object, if any */
+ else if (scope.shared[i].tagName !== undefined)
+ delete scope.shared[i];
+ return function (complete, error) {
+ for (i = 0; i < worker_num; i++) {
+ var worker = new Worker(file);
+ worker.onmessage = (function (i) {
+ return function (event) {
+ outputs[i] = (typeof event.data == "string") ? JSON.parse(event.data) : event.data;
+ executed++;
+ if (executed == worker_num)
+ complete(ctrl.post.apply(scope, [outputs]));
+ }
+ })(i);
+ var msg = { "input" : inputs[i],
+ "name" : funct.toString(),
+ "shared" : scope.shared,
+ "id" : i,
+ "worker" : params.worker_num };
+ try {
+ worker.postMessage(msg);
+ } catch (e) {
+ worker.postMessage(JSON.stringify(msg));
+ }
+ }
+ }
+ } else {
+ return ctrl.post.apply(scope, [[ctrl.core.apply(scope, [ctrl.pre.apply(scope, [1])[0], 0, 1])]]);
+ }
+ }
+ };
+ parallable.core = {};
+}
+
+var sum = parallable("sum.js", function (list) {
+ if (this.shared !== undefined) {
+ this.shared.list = list;
+ }
+ var pre = function (worker_num) {
+ var list = this.shared.list;
+ var part = new Array(worker_num);
+ var i;
+ var len = Math.ceil(list.length / worker_num);
+ for (i = 0; i < worker_num; i++)
+ part[i] = new Array(len);
+ for (i = 0; i < list.length; i++)
+ part[Math.floor(i / len)][i % len] = list[i];
+ return part;
+ };
+ var core = function (data, id, worker_num) {
+ var i, j;
+ var sum = 0;
+ for (i = 0; i < data.length; i++)
+ for (j = 0; j < 5000; j++)
+ sum += Math.cos(Math.sin(data[i]));
+ return sum;
+ };
+ var post = function (seq) {
+ var i;
+ var sum = 0;
+ for (i = 0; i < seq.length; i++)
+ sum += seq[i];
+ return sum;
+ };
+ return { "pre" : pre, "core" : core, "post" : post };
+});
+
+onmessage = function (event) {
+ var data = (typeof event.data == "string") ? JSON.parse(event.data) : event.data;
+ var scope = { "shared" : data.shared };
+ var result = parallable.core[data.name].apply(scope, [data.input, data.id, data.worker]);
+ try {
+ postMessage(result);
+ } catch (e) {
+ postMessage(JSON.stringify(result));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.