Commit 196e9114 authored by Neil Gershenfeld's avatar Neil Gershenfeld
Browse files

wip

parent 87994340
Pipeline #2844 passed with stage
in 0 seconds
......@@ -6,21 +6,23 @@
//
const points = 1e8
const { fork } = require('child_process')
var processes = 1
var processes = parseInt(process.argv[2])
var pi = 0
var results = 0
var tstart = Date.now()/1000
const forked = fork('child.js','1')
forked.on('message',(result) => {
for (let i = 0; i < processes; ++i) {
id = fork('forkspi.js',[i,points])
id.on('message',(result) => {
pi += result
results += 1
if (results == processes) {
var tend = Date.now()/1000
var mflops = (processes*points)*5.0*1e-6/(tend-tstart)
console.log('pi: '+pi)
console.log('time: '+(tend-tstart).toFixed(1)+'s')
console.log('processes: '+processes)
console.log('estimated MFlops: '+mflops.toFixed(1))
process.exit()
}
})
if (results == processes) {
var tend = Date.now()/1000
var mflops = (processes*points)*5.0*1e-6/(tend-tstart)
console.log('pi: '+pi)
console.log('time: '+(tend-tstart).toFixed(1)+'s')
console.log('processes: '+processes)
console.log('estimated MFlops: '+mflops.toFixed(1))
process.exit()
}
})
}
//
// clusterpi.js
// forkspi.js
// Neil Gershenfeld 11/23/18
// pi calculation benchmark
// pi = 3.14159265358979323846
//
const points = 1e8
const { fork } = require('child_process');
const forked = fork('child.js');
forked.on('message', (msg) => {
console.log('Message from child', msg);
});
forked.send({ hello: 'world' });
process.on('message', (msg) => {
console.log('Message from parent:', msg);
});
let counter = 0;
setInterval(() => {
process.send({ counter: counter++ });
}, 1000);
function master() {
var processes = require('os').cpus().length
var tstart = Date.now()/1000
for (var i = 0; i < processes; i++)
cluster.fork()
var index = 0
var pi = 0
var results = 0
for (const id in cluster.workers) {
var worker = cluster.workers[id]
worker.on('message',(result) => {
pi += result
results += 1
if (results == processes) {
var tend = Date.now()/1000
var mflops = (processes*points)*5.0*1e-6/(tend-tstart)
console.log('pi: '+pi)
console.log('time: '+(tend-tstart).toFixed(1)+'s')
console.log('processes: '+processes)
console.log('estimated MFlops: '+mflops.toFixed(1))
process.exit()
}
})
function send(index) {
return function() {
worker.send(index)
}
}
worker.on('online',send(index))
index += 1
}
}
function child() {
process.on('message',(index) => {
var a = 0.5
var b = 0.75
var c = 0.25
var sum = 0
var start = 1+points*index
var end = points*(index+1)
for (var i = start; i <= end; ++i)
sum += a/((i-b)*(i-c))
process.send(sum)
})
}
var index = parseInt(process.argv[2])
var points = parseInt(process.argv[3])
var a = 0.5
var b = 0.75
var c = 0.25
var sum = 0
var start = 1+points*index
var end = points*(index+1)
for (var i = start; i <= end; ++i)
sum += a/((i-b)*(i-c))
process.send(sum)
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