Skip to content

Commit a787cea

Browse files
committed
Merge pull request #100 from JuliaParallel/eschnett/montecarlo
Clean up Monte Carlo example
2 parents 98746f0 + 46061ca commit a787cea

File tree

3 files changed

+71
-79
lines changed

3 files changed

+71
-79
lines changed

examples/07-pi-impl.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import MPI
2+
function montecarlo(mc_eval::Function, mc_monitor::Function,
3+
reps::Integer, n_returns::Integer, pooled::Integer=1,
4+
sleeptime::Real=0)
5+
# set up the MPI communicator
6+
MPI.Init()
7+
comm = MPI.COMM_WORLD
8+
rank = MPI.Comm_rank(comm)
9+
commsize = MPI.Comm_size(comm)
10+
11+
# containers and bookkeeping
12+
contrib = zeros(pooled, n_returns)
13+
results = zeros(reps, n_returns)
14+
pernode = div(reps, commsize - 1)
15+
if mod(reps, commsize - 1) != 0
16+
if rank == 0
17+
println("Error (montecarlo):")
18+
println(" reps=$reps, commsize=$commsize")
19+
println("Choose commsize so that reps/(commsize-1) is integer")
20+
end
21+
MPI.Finalize()
22+
exit(1)
23+
end
24+
25+
if mod(pernode, pooled) != 0
26+
pooled = div(pernode, commsize-1)
27+
end
28+
29+
if rank > 0
30+
# workers
31+
@inbounds for i = 1:div(pernode, pooled)
32+
# do work
33+
for j = 1:pooled
34+
contrib[j,:] = mc_eval()
35+
end
36+
MPI.Isend(contrib, 0, rank, comm)
37+
end
38+
39+
else
40+
# manager
41+
sofar = 0 # results collected so far
42+
while sofar < div(reps, pooled)
43+
sleep(sleeptime) # flood control if job is costly
44+
@inbounds for node = 1:commsize-1
45+
# check for results
46+
ready, _ = MPI.Iprobe(node, node, comm)
47+
if ready # get them if they're ready
48+
sofar +=1
49+
if sofar*pooled <= reps
50+
MPI.Recv!(contrib, node, node, comm)
51+
results[sofar*pooled-pooled+1:sofar*pooled,:] = contrib
52+
mc_monitor(sofar*pooled, results)
53+
end
54+
if sofar == div(reps, pooled)
55+
break
56+
end
57+
end
58+
end
59+
end
60+
end
61+
62+
MPI.Finalize()
63+
end

examples/07-pi.jl

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,26 @@ like to use, subject to X-1 being an even divisor
77
of 1e6, e.g., set X=5.
88
=#
99

10-
include("montecarlo.jl")
10+
include("07-pi-impl.jl")
1111

1212
function pi_wrapper()
13-
pihat = 4.*float((norm(rand(2,1)) .< 1.))
13+
4.0 * (norm(rand(2)) < 1)
1414
end
1515

1616
# this function reports intermediate results during MC runs
1717
function pi_monitor(sofar, results)
18-
# examine results every 2.5*10^5 draws
19-
if mod(sofar,2.5e5)==0.
18+
# Examine results every 12.5*10^5 draws
19+
if mod(sofar, 10^6) == 0
2020
m = mean(results[1:sofar,:],1)
21-
println("reps so far: ", sofar)
22-
println("pihat: ", m)
23-
println()
24-
#if sofar == size(results,1)
25-
# writedlm("mcresults.out", results)
26-
#end
21+
println("reps: $sofar, pihat: $m")
2722
end
28-
end
23+
end
2924

3025
# do the monte carlo: 10^6 reps of single draws
3126
function main()
32-
reps = Int(1e6) # desired number of MC reps
27+
reps = 10^7 # desired number of MC reps
3328
nreturns = 1
34-
pooled = Int(50000)
29+
pooled = 10^5
3530
montecarlo(pi_wrapper, pi_monitor, reps, nreturns, pooled)
3631
end
3732

examples/montecarlo.jl

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)