Skip to content

Instantly share code, notes, and snippets.

@liuliu
Created December 8, 2010 00:20
Show Gist options
  • Save liuliu/732693 to your computer and use it in GitHub Desktop.
Save liuliu/732693 to your computer and use it in GitHub Desktop.
sum with parallable
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 };
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment