I have a simulation where I pass functions as parameters by putting them in a parameter object. However, when I try to run the simulation using Distributed
, the functions do not run in the workers due to their world age being too new.
For example, this MWE:
using Distributed
num = 2
addprocs(num)
@everywhere begin
struct testParams
f::Function
end
function worker(input, output)
while true
f = take!(input).f
put!(output, (myid(), f(1.0)))
end
end
end
# channels to send and receive data from workers
input = RemoteChannel(()->Channel{testParams}(num))
output = RemoteChannel(()->Channel{Tuple}(num))
for i in 1:num
put!(input, testParams((x)->2*x))
end
# run workers
for w in workers()
remote_do(worker, w, input, output)
end
# collect results
for i in 1:num
w, ans = take!(output)
println("worker $w: $ans")
end
produces the following errors:
From worker 3: (::Distributed.var"#113#115"{Distributed.RemoteDoMsg})() at ./task.jl:356MethodError: no method matching (::Serialization.__deserialized_types__.var"#27#28")(::Float64)
From worker 3: The applicable method may be too new: running in world age 27823, while current world is 27824.
From worker 3: Closest candidates are:
From worker 3: #27(::Any) at /Users/vancleve/science/projects/food_sharing_odwyer/eees/julia/popsim.jl:63 (method too new to be called from this world context.)
From worker 3: worker(::RemoteChannel{Channel{testParams}}, ::RemoteChannel{Channel{Tuple}}) at /Users/vancleve/science/projects/food_sharing_odwyer/eees/julia/popsim.jl:53
From worker 3: (::Distributed.var"#114#116"{Distributed.RemoteDoMsg})() at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Distributed/src/process_messages.jl:315
From worker 3: run_work_thunk(::Distributed.var"#114#116"{Distributed.RemoteDoMsg}, ::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Distributed/src/process_messages.jl:79
From worker 5: MethodError: no method matching (::Serialization.__deserialized_types__.var"#27#28")(::Float64)
From worker 5: The applicable method may be too new: running in world age 27817, while current world is 27818.
From worker 5: Closest candidates are:
From worker 5: #27(::Any) at /Users/vancleve/science/projects/food_sharing_odwyer/eees/julia/popsim.jl:63 (method too new to be called from this world context.)
From worker 5: worker(::RemoteChannel{Channel{testParams}}, ::RemoteChannel{Channel{Tuple}}) at /Users/vancleve/science/projects/food_sharing_odwyer/eees/julia/popsim.jl:53
From worker 5: (::Distributed.var"#114#116"{Distributed.RemoteDoMsg})() at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Distributed/src/process_messages.jl:315
From worker 5: run_work_thunk(::Distributed.var"#114#116"{Distributed.RemoteDoMsg}, ::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Distributed/src/process_messages.jl:79
Is there any solution to this other than invoking the functions in the workers with Base.invokelatest
?