forked from microsoft/napajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimate-pi-in-parallel.js
71 lines (58 loc) · 2.11 KB
/
estimate-pi-in-parallel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
var napa = require("napajs");
// Change this value to control number of napa workers initialized.
const NUMBER_OF_WORKERS = 4;
// Create a napa zone with number_of_workers napa workers.
var zone = napa.zone.create('zone', { workers: NUMBER_OF_WORKERS });
/*
Estimate the value of π by using a Monte Carlo method.
Take `points` samples of random x and y values on a
[0,1][0,1] plane. Calculating the length of the diagonal
tells us whether the point lies inside, or outside a
quarter circle running from 0,1 to 1,0. The ratio of the
number of points inside to outside gives us an
approximation of π/4.
See https://en.wikipedia.org/wiki/File:Pi_30K.gif
for a visualization of how this works.
*/
function estimatePI(points) {
var i = points;
var inside = 0;
while (i-- > 0) {
var x = Math.random();
var y = Math.random();
if ((x * x) + (y * y) <= 1) {
inside++;
}
}
return inside / points * 4;
}
function run(points, batches) {
var start = Date.now();
var promises = [];
for (var i = 0; i < batches; i++) {
promises[i] = zone.execute(estimatePI, [points / batches]);
}
return Promise.all(promises).then(values => {
var aggregate = 0;
values.forEach(result => aggregate += result.value);
printResult(points, batches, aggregate / batches, Date.now() - start);
});
}
function printResult(points, batches, pi, ms) {
console.log('\t' + points
+ '\t\t' + batches
+ '\t\t' + NUMBER_OF_WORKERS
+ '\t\t' + ms
+ '\t\t' + pi.toPrecision(7)
+ '\t' + Math.abs(pi - Math.PI).toPrecision(7));
}
console.log();
console.log('\t# of points\t# of batches\t# of workers\tlatency in MS\testimated π\tdeviation');
console.log('\t---------------------------------------------------------------------------------------');
// Run with different # of points and batches in sequence.
run(4000000, 1)
.then(result => run(4000000, 2))
.then(result => run(4000000, 4))
.then(result => run(4000000, 8))