Share object between master/workers


#1

Is there a way to share an instance of a non-bits type between the master process and one or more workers? I’d like to do something like the following:

addprocs(1)
@everywhere MyType
   x
end
@everywhere function print_x_twice(mt::MyType)
    println(mt.x)
    sleep(5)
    println(mt.x)
end

mt = MyType(1)
shared_ref = SharedRef(mt)

@spawnat 2 begin
    mt = get(SharedRef)
    print_x_twice(mt)
end

sleep(.1)
mt.x = 100

and see output of

From worker 2: 1
From worker 2: 100

ie., I’d like changes made to mt on the master process to be visible on the worker. Of course, SharedRef isn’t a real type. Is there a way to do this? I’d think RemoteChannel, but I’ve read through the parallelism documentation half a dozen times and still don’t really understand how to use remote channels; certainly I haven’t been able to get the above working with a remote channel.


#2

Self-reply: I am still curious about how to do this because it seems like it could be useful. In the mean time, passing messages via RemoteChannel seems to work. Something like

rr = RemoteChannel()
x = ObjectWithRemoteChannel(rr)
@spawn dostuff(x)

push!(rr, message) # visible on the worker

#3

I’m not familiar with sharing objects, but for passing objects see also ParallelDataTransfer.