I am trying to solve a problem in which I have to evaluate a function, f(x), for several different values of an argument x. I want to write a function that depending on the number of available processes it uses either map (if nprocs()==1
) or pmap (if nprocs()>1
). The function f(x) I want to evaluate requires a preallocated matrix and this is where I find a behaviour I do not understand.
This minimal example shows the problem I am facing:
using Distributed
addprocs(4)
@everywhere using LinearAlgebra
function test1()
if nprocs()>1
@everywhere A = zeros(100, 100)
return eigvals(A)
else
B = zeros(100, 100)
return eigvals(B)
end
end
function test2()
if nprocs()>1
@everywhere A = zeros(100, 100)
return eigvals(A)
else
A = zeros(100, 100)
return eigvals(A)
end
end
If I run test1()
, it works without problems. However, when I run test2()
, I get the error: UndefVarError: A not defined
If I run the code without adding any procs, both test1()
and test2()
work. Of course I can just write my code in the style of test1()
, but I would like to understand why test2()
fails. Thanks in advance for any suggestion!
EDIT: Changed the title of the topic to reflect the fact that the issue seems to be unrelated to the use of and if
clause