Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable parallel.js to transmit transferable objects #11

Merged
merged 1 commit into from
Feb 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ <h4>Examples</h4>
// Spawn a remote reference
var r = Parallel.spawn(slowSquare, 100000);

// Call back when done
r.fetch(yourCallBack);
</pre>

<button class="btn btn-primary">Try it</button>
<span class="result"></span>
</div>
<p>You can even transmit transferable objects:</p>

<div class="example" id="example-5">
<pre class="prettyprint linenums">
var sum = function (n) {
var total = 0;
for (var i=0; i < n.length; ++i) {
total += n[i];
}
return total;
};

// Spawn a remote reference
var r = Parallel.spawn(sum, new Uint8Array([1,2,3]));

// Call back when done
r.fetch(yourCallBack);
</pre>
Expand Down
28 changes: 28 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ $(function () {
r.fetch(check());
});


$('#example-5 .btn').click(function () {
var that = this;

var sum = function (n) {
var total = 0;
for (var i=0; i < n.length; ++i) {
total += n[i];
}
return total;
};

var check = function () {
if (!r.fetch()) {
$(that).siblings('.result').html('Computation started: ' + Math.floor((Date.now() - start) / 1000) + 's ago');
} else {
return $(that).siblings('.result').html('Result is: ' + r.fetch() + '. Computed in: ' + Math.floor((Date.now() - start) / 1000) + ' seconds.');
}

setTimeout(check, 10);
}

var start = Date.now();
var r = Parallel.spawn(sum, new Uint8Array([1,2,3]));

r.fetch(check());
});

$('#example-3 .btn').click(function () {
var that = this;

Expand Down
13 changes: 9 additions & 4 deletions parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var Parallel = (function () {

return isNode ?
'process.on("message", function (m) { process.send({ data : JSON.stringify((' + op + ').apply(process, JSON.parse(m))) }); });' :
'self.onmessage = function (e) { self.postMessage(JSON.stringify((' + op + ').apply(self, JSON.parse(e.data)))); };';
'self.onmessage = function (e) { self.postMessage((' + op + ').apply(self, e.data)); };';
};

var wrapFiles = function (str) {
Expand Down Expand Up @@ -73,10 +73,11 @@ var Parallel = (function () {
};

RemoteRef.prototype.onWorkerMsg = function (e) {
this.ref.data = JSON.parse(e.data);

if (isNode) {
this.ref.data = JSON.parse(e.data);
this.ref.worker.terminate();
} else {
this.ref.data = e.data;
}
};

Expand All @@ -97,7 +98,11 @@ var Parallel = (function () {

return function (fn, args) {
var r = new RemoteRef(fn);
r.worker.postMessage(JSON.stringify([].concat(args)));
if (isNode) {
r.worker.postMessage(JSON.stringify([].concat(args)));
} else {
r.worker.postMessage([].concat(args));
}

return r;
};
Expand Down