Just pre-allocate A
outside of the function. The @everywhere
macro executes just the statement that is passed to it, so A
will be in global scope on the child processes anyway.
The code below works, regardless of whether addprocs
were called or not.
using Distributed
addprocs(4)
@everywhere using LinearAlgebra, Random
@everywhere A = zeros(100, 100)
@everywhere function h!(x)
return eigvals(rand!(MersenneTwister(x), A))
end
function run_h(iter)
if nprocs()>1
return pmap(x->h!(x), iter)
else
return map(x->h!(x), iter)
end
end