Commit 721dfd48 authored by Neil Gershenfeld's avatar Neil Gershenfeld
Browse files

wip

parent 196e9114
Pipeline #2845 passed with stage
in 0 seconds
// //
// clusterpi.js // clusterpi.js
// Neil Gershenfeld 11/23/18 // Neil Gershenfeld 12/8/18
// pi calculation benchmark // pi calculation benchmark
// pi = 3.14159265358979323846 // pi = 3.14159265358979323846
// //
const cluster = require('cluster') const cluster = require('cluster')
const points = 1e8
if (cluster.isMaster) if (cluster.isMaster)
master() master()
else else
child() child()
function master() { function master() {
var processes = require('os').cpus().length var processes = parseInt(process.argv[2])
var tstart = Date.now()/1000 var points = 1e9
for (var i = 0; i < processes; i++)
cluster.fork()
var index = 0
var pi = 0 var pi = 0
var results = 0 var results = 0
for (const id in cluster.workers) { var tstart = Date.now()/1000
var worker = cluster.workers[id] for (var i = 0; i < processes; i++) {
var worker = cluster.fork({index:i,points:points})
worker.on('message',(result) => { worker.on('message',(result) => {
pi += result pi += result
results += 1 results += 1
...@@ -33,25 +30,18 @@ function master() { ...@@ -33,25 +30,18 @@ function master() {
process.exit() process.exit()
} }
}) })
function send(index) {
return function() {
worker.send(index)
}
}
worker.on('online',send(index))
index += 1
} }
} }
function child() { function child() {
process.on('message',(index) => { var index = parseInt(process.env.index)
var a = 0.5 var points = parseInt(process.env.points)
var b = 0.75 var a = 0.5
var c = 0.25 var b = 0.75
var sum = 0 var c = 0.25
var start = 1+points*index var sum = 0
var end = points*(index+1) var start = 1+points*index
for (var i = start; i <= end; ++i) var end = points*(index+1)
sum += a/((i-b)*(i-c)) for (var i = start; i <= end; ++i)
process.send(sum) sum += a/((i-b)*(i-c))
}) process.send(sum)
} }
...@@ -11,8 +11,8 @@ var pi = 0 ...@@ -11,8 +11,8 @@ var pi = 0
var results = 0 var results = 0
var tstart = Date.now()/1000 var tstart = Date.now()/1000
for (let i = 0; i < processes; ++i) { for (let i = 0; i < processes; ++i) {
id = fork('forkspi.js',[i,points]) const child = fork('forkspi.js',[i,points])
id.on('message',(result) => { child.on('message',(result) => {
pi += result pi += result
results += 1 results += 1
if (results == processes) { if (results == processes) {
......
// //
// forkspi.js // forkspi.js
// Neil Gershenfeld 11/23/18 // Neil Gershenfeld 12/8/18
// pi calculation benchmark // pi calculation benchmark
// pi = 3.14159265358979323846 // pi = 3.14159265358979323846
// //
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
// pi calculation benchmark // pi calculation benchmark
// pi = 3.14159265358979323846 // pi = 3.14159265358979323846
// //
var points = 1e8 var points = 1e9
var a = 0.5 var a = 0.5
var b = 0.75 var b = 0.75
var c = 0.25 var c = 0.25
......
...@@ -6,10 +6,12 @@ ...@@ -6,10 +6,12 @@
|71.46|[pi.html](https://pub.pages.cba.mit.edu/pi/JavaScript/pi.html)|JavaScript, 56 workers|Intel 2x E5-2680|Nov 19, 2018| |71.46|[pi.html](https://pub.pages.cba.mit.edu/pi/JavaScript/pi.html)|JavaScript, 56 workers|Intel 2x E5-2680|Nov 19, 2018|
|46.96|[mpipi.c](MPI/mpipi.c)|C, MPI<br>mpicc mpipi.c -o mpipi -O3 -ffast-math <br> mpirun -np 6 mpipi|Intel i7-8700T|Nov 17, 2018| |46.96|[mpipi.c](MPI/mpipi.c)|C, MPI<br>mpicc mpipi.c -o mpipi -O3 -ffast-math <br> mpirun -np 6 mpipi|Intel i7-8700T|Nov 17, 2018|
|16.16|[pi.html](https://pub.pages.cba.mit.edu/pi/JavaScript/pi.html)|JavaScript, 6 workers|Intel i7-8700T|Nov 17, 2018| |16.16|[pi.html](https://pub.pages.cba.mit.edu/pi/JavaScript/pi.html)|JavaScript, 6 workers|Intel i7-8700T|Nov 17, 2018|
|14.34|[clusterpi.js](Node/clusterpi.js)|Node, 6 workers|Intel i7-8700T|Dec 8, 2018|
|9.371|[pi.c](C/pi.c)|C<br>gcc pi.c -o pi -lm -O3 -ffast-math|Intel i7-8700T|Nov 17, 2018| |9.371|[pi.c](C/pi.c)|C<br>gcc pi.c -o pi -lm -O3 -ffast-math|Intel i7-8700T|Nov 17, 2018|
|3.817|[pi.js](Node/pi.js)|Node|Intel 2x E5-2680|Nov 24, 2018|
|3.734|[pi.html](https://pub.pages.cba.mit.edu/pi/JavaScript/pi.html)|JavaScript, 1 worker|Intel i7-8700T|Nov 17, 2018| |3.734|[pi.html](https://pub.pages.cba.mit.edu/pi/JavaScript/pi.html)|JavaScript, 1 worker|Intel i7-8700T|Nov 17, 2018|
|3.472|[pi.html](https://pub.pages.cba.mit.edu/pi/JavaScript/pi.html)|JavaScript, 1 worker|Intel 2x E5-2680|Nov 19, 2018| |3.472|[pi.html](https://pub.pages.cba.mit.edu/pi/JavaScript/pi.html)|JavaScript, 1 worker|Intel 2x E5-2680|Nov 19, 2018|
|3.817|[pi.js](Node/pi.js)|Node|Intel 2x E5-2680|Nov 24, 2018| |3.184|[clusterpi.js](Node/clusterpi.js)|Node, 1 worker|Intel i7-8700T|Dec 8, 2018|
|0.573|[pi.c](C/pi.c)|C<br>gcc pi.c -o pi -lm|Intel i7-8700T|Nov 17, 2018| |0.573|[pi.c](C/pi.c)|C<br>gcc pi.c -o pi -lm|Intel i7-8700T|Nov 17, 2018|
|0.470|[numpi.py](Python/numpi.py)|Python, NumPy|Intel i7-8700T|Nov 17, 2018| |0.470|[numpi.py](Python/numpi.py)|Python, NumPy|Intel i7-8700T|Nov 17, 2018|
|0.029|[pi.py](Python/pi.py)|Python|Intel i7-8700T|Nov 17, 2018| |0.029|[pi.py](Python/pi.py)|Python|Intel i7-8700T|Nov 17, 2018|
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment