Created
March 23, 2014 22:43
-
-
Save IainNZ/9730991 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# solver.jl | |
module Solver | |
export solve | |
function solve_sub() | |
y = rand(1000,1000) | |
for busy = 1:100 | |
y *= 10 | |
y /= 10 | |
end | |
return mean(y) | |
end | |
function solve() | |
remote_busy = false | |
remote_result = nothing | |
for iter = 1:100 | |
x = rand(1000,1000) | |
x = 10*x | |
x = 10*x | |
x = 10*x | |
x = 10*x | |
x = 10*x | |
x /= 100000 | |
avg = mean(x) | |
print(iter, " ", avg, " ", remote_busy) | |
if avg < 0.5 && !remote_busy | |
println(" starting sub") | |
remote_result = @spawnat 2 solve_sub() | |
remote_busy = true | |
end | |
@time ready = remote_busy && isready(remote_result) | |
if ready | |
res = take!(remote_result) | |
print(" got result $res") | |
remote_busy = false | |
end | |
println() | |
end | |
end | |
end | |
# Then another file test.jl | |
using Solver | |
solve() | |
# julia -p 2 test.jl | |
1 0.499949244417727 false starting sub | |
elapsed time: 1.256024145 seconds (1540584 bytes allocated) | |
got result 0.4998042450174787 | |
2 0.5005155793905703 falseelapsed time: 1.49e-7 seconds (0 bytes allocated) | |
3 0.49979916948467273 false starting sub | |
elapsed time: 1.165724098 seconds (10376 bytes allocated) | |
got result 0.5000743191124121 | |
4 0.49987534612626455 false starting sub | |
elapsed time: 1.162399819 seconds (8088 bytes allocated) | |
got result 0.5000267495200585 | |
5 0.5003024117948347 falseelapsed time: 1.48e-7 seconds (0 bytes allocated) | |
6 0.5001057455369299 falseelapsed time: 1.91e-7 seconds (0 bytes allocated) | |
7 0.49977373772910283 false starting sub | |
elapsed time: 1.168619742 seconds (8088 bytes allocated) | |
got result 0.5002357213152845 | |
8 0.4999847640256721 false starting sub | |
elapsed time: 1.165707928 seconds (9704 bytes allocated) | |
got result 0.5002197985300247 | |
9 0.4997749590974831 false starting sub | |
elapsed time: 1.162779257 seconds (9704 bytes allocated) | |
got result 0.4999976924032774 | |
10 0.4996215802466504 false starting sub | |
# which shows how it blocks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try wait(remote_result) at L38