I am trying to let Monte Carlo Code run until a time constraint is met. A 
simplified version of the code is below. I have two issues

1) I am trying to wait until a computation finishes but wait does not work 
so I currently use sleep which is crazy. What is wrong with wait.
2) I try to work with Array{RemoteRef,nw} which does not seem to work and I 
don't understand why.
3) How do I do a read only variable that can be read by all workers 

 Thank you,


Sebastian


@everywhere function dowork(res,nZs)

    global toStop

    global steps

    while !toStop

        for j=1:steps

            localpart(res)[1]+=rand()

            localpart(nZs)[1]+=1

        end

        sleep(0.1);

    end

end


secs=2.0

@everywhere steps=10 # this should be constant readable by all processes

@everywhere toStop=false

res2=dzeros((1,nw), workers(), [1,nw])

nZs2=dzeros((1,nw), workers(), [1,nw])

s = @spawnat 2 dowork(res2,nZs2)

sleep(secs)

@everywhere toStop=true

sleep(1)

# wait(s) if I remove sleep and do this the program does not stop



@everywhere toStop=false

nw=length(workers())

res=dzeros((1,nw), workers(), [1,nw])

nZs=dzeros((1,nw), workers(), [1,nw])

refs=Array{RemoteRef,nw}

#for i in workers()

#    refs[i]=@spawnat i dowork(res,nZs)  #does not work

#end

for i in workers()

    @spawnat i dowork(res,nZs)

end

sleep(secs)

@everywhere toStop=true


sleep(2.0) #buggy to make sure that all computations have finished before 
starting the next

#would like to do

#for i in workers()

#    wait(refs[i]) #does not work

#end

sum(nZs)/sum(nZs2)



Reply via email to