Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
pub
pi
Commits
87994340
Commit
87994340
authored
Dec 08, 2018
by
Neil Gershenfeld
Browse files
wip
parent
c3a41aa3
Pipeline
#2843
passed with stage
in 1 second
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Node/forkpi.js
0 → 100644
View file @
87994340
//
// forkpi.js
// Neil Gershenfeld 12/8/18
// pi calculation benchmark
// pi = 3.14159265358979323846
//
const
points
=
1
e8
const
{
fork
}
=
require
(
'
child_process
'
)
var
processes
=
1
var
pi
=
0
var
results
=
0
var
tstart
=
Date
.
now
()
/
1000
const
forked
=
fork
(
'
child.js
'
,
'
1
'
)
forked
.
on
(
'
message
'
,(
result
)
=>
{
pi
+=
result
results
+=
1
if
(
results
==
processes
)
{
var
tend
=
Date
.
now
()
/
1000
var
mflops
=
(
processes
*
points
)
*
5.0
*
1
e
-
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
()
}
})
Node/forkspi.js
0 → 100644
View file @
87994340
//
// clusterpi.js
// Neil Gershenfeld 11/23/18
// pi calculation benchmark
// pi = 3.14159265358979323846
//
const
points
=
1
e8
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
*
1
e
-
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
)
})
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment