-
-
Save femto113/5202935 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cluster = require('cluster'); | |
var numCPUs = parseInt(process.argv[2]) || 1; | |
var MESSAGE_BURST = 10; // how many messages are sent by master in each round | |
var WORK_COST = 128; // factor to slow down average message handling cost in each worker | |
var REPORTABLE_WORK = 100; // how often worker reports its accomplishments | |
var ready_workers = []; | |
if (cluster.isMaster) { | |
var total_work = 0; | |
console.log("Master started"); | |
for (var i = 0; i < numCPUs; i++) { | |
cluster.fork(); | |
} | |
cluster.on('online', function (worker) { | |
ready_workers.push(worker); | |
worker.on("message", function (msg) { | |
total_work = total_work + parseInt(msg.work); | |
}); | |
if (ready_workers.length === numCPUs) { | |
process.emit('burst'); | |
} | |
}); | |
var scount = 0; | |
var time = process.hrtime(); | |
var i = 0; | |
process.on('burst', function () { | |
// send 5000 then give the event loop a look-in. | |
for (var n = 0; n < MESSAGE_BURST; n++) { | |
var worker = ready_workers[i++]; | |
worker.send('get to work'); | |
scount++; | |
if (i === numCPUs) i = 0; | |
} | |
process.nextTick(function () { process.emit('burst'); }); | |
}); | |
process.on('done', function (id) { | |
console.log("%s is done", id); | |
}); | |
setInterval(function () { | |
var diff = process.hrtime(time); | |
time = process.hrtime(); | |
var secs = (diff[0] * 1e9 + diff[1]) / 1e9; | |
process.stdout.write("Message Rate: " + parseInt(scount/secs) + " Work Rate: " + parseInt(total_work/secs) + " \r"); | |
scount = 0; | |
total_work = 0; | |
}, 1000); | |
} else { | |
var cordic = require("./cordic"); | |
console.log("Worker %s started", cluster.worker.id); | |
var work = 0; | |
process.on("message", function () { | |
var W = Math.floor(Math.random() * (WORK_COST * 2)) >>> 0; | |
for (var i = 0; i < W; i++) { | |
cordic.sincos(); // does some CPU intensive trig stuff | |
work++; | |
} | |
if (work >= REPORTABLE_WORK) { | |
process.send({ id: cluster.worker.id, work: work }); | |
work = 0; | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AG_CONST = 0.6072529350; | |
function FIXED(X) | |
{ | |
return X * 65536.0; | |
} | |
function FLOAT(X) | |
{ | |
return X / 65536.0; | |
} | |
function DEG2RAD(X) | |
{ | |
return 0.017453 * (X); | |
} | |
var Angles = [ | |
FIXED(45.0), FIXED(26.565), FIXED(14.0362), FIXED(7.12502), | |
FIXED(3.57633), FIXED(1.78991), FIXED(0.895174), FIXED(0.447614), | |
FIXED(0.223811), FIXED(0.111906), FIXED(0.055953), | |
FIXED(0.027977) | |
]; | |
function cordicsincos() { | |
var X; | |
var Y; | |
var TargetAngle; | |
var CurrAngle; | |
var Step; | |
X = FIXED(AG_CONST); /* AG_CONST * cos(0) */ | |
Y = 0; /* AG_CONST * sin(0) */ | |
TargetAngle = FIXED(28.027); | |
CurrAngle = 0; | |
for (Step = 0; Step < 12; Step++) { | |
var NewX; | |
if (TargetAngle > CurrAngle) { | |
NewX = X - (Y >> Step); | |
Y = (X >> Step) + Y; | |
X = NewX; | |
CurrAngle += Angles[Step]; | |
} else { | |
NewX = X + (Y >> Step); | |
Y = -(X >> Step) + Y; | |
X = NewX; | |
CurrAngle -= Angles[Step]; | |
} | |
} | |
} | |
if (exports) { exports.sincos = cordicsincos; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment