I would like to take an arbitrary function at runtime that may or may not be defined with the @everywhere
macro, and run it in a distributed way.
My first, naive attempt was to simply try and use the function inside of pmap
@assert nprocs() > 1
function g(f)
pmap(1:5) do x
f(x)
end
end
addone(x) = x+1
subone(x) = x-1
g(addone)
g(subone)
This, of course, did not work and resulted in.
On worker 2:
UndefVarError: `#addone` not defined
Next I tried passing the function f as an argument of pmap
@assert nprocs() > 1
function g(f)
pmap(zip(1:5, Iterators.repeated(f))) do (x,f)
f(x)
end
end
addone(x) = x+1
subone(x) = x-1
g(addone)
g(subone)
This also did not work, it also threw
On worker 2:
UndefVarError: `#addone` not defined
Now I am at a loss, surely something like this must be possible in Julia.