Commit 68d1de0b authored by Neil Gershenfeld's avatar Neil Gershenfeld
Browse files

Merge branch 'master' of fab2neilg:pub/pi

parents 6007d0b7 5d0f37f5
...@@ -11,7 +11,7 @@ else ...@@ -11,7 +11,7 @@ else
child() child()
function master() { function master() {
var processes = parseInt(process.argv[2]) var processes = parseInt(process.argv[2])
var points = 1e10 var points = 1e9
var pi = 0 var pi = 0
var results = 0 var results = 0
var tstart = Date.now()/1000 var tstart = Date.now()/1000
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
// pi calculation benchmark // pi calculation benchmark
// pi = 3.14159265358979323846 // pi = 3.14159265358979323846
// //
const points = 1e8 const points = 1e9
const { fork } = require('child_process') const { fork } = require('child_process')
var processes = parseInt(process.argv[2]) var processes = parseInt(process.argv[2])
var pi = 0 var pi = 0
......
//
// workerpi.js
// Neil Gershenfeld 12/11/18
// pi calculation benchmark
// pi = 3.14159265358979323846
//
const {Worker} = require('worker_threads')
var points = 1e9
var thread =
`
const Worker = require('worker_threads')
Worker.parentPort.on('message',(msg) => {
var index = msg.index
var points = msg.points
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))
Worker.parentPort.postMessage(sum)
})
`
var workers = parseInt(process.argv[2])
var results = 0
var pi = 0
var tstart = Date.now()/1000
for (let i = 0; i < workers; ++i) {
var worker = new Worker(thread,{eval:true})
worker.on('message',(sum) => {
results += 1
pi += sum
if (results == workers) {
var tend = Date.now()/1000
var mflops = (workers*points)*5.0*1e-6/(tend-tstart)
console.log('pi: '+pi)
console.log('time: '+(tend-tstart).toFixed(1)+'s')
console.log('workers: '+workers)
console.log('estimated MFlops: '+mflops.toFixed(1))
process.exit()
}
})
worker.postMessage({index:i,points:points})
}
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