Skip to content

Commit

Permalink
Merge pull request #11 from Sebmaster/feature-transferable-arguments
Browse files Browse the repository at this point in the history
Enable parallel.js to transmit transferable objects
  • Loading branch information
adambom committed Feb 25, 2013
2 parents d310e61 + 32e0027 commit 5e59f62
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
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

0 comments on commit 5e59f62

Please sign in to comment.