Different behaviour of the identical code

Dear all, I am using VS code to compile my Julia code. I have the following code, which is “test.jl”. I also have another file that hosts my_function, which is called “test_test.jl”. Basically, my_function takes into k and outputs 3*k

using Distributed
@everywhere include("test_test.jl")
addprocs(5)
wok=workers()
an2=Vector{Float64}(undef,5)
 
 @everywhere k=6
for ja=1:5
 local ans1= @spawnat wok[ja]  my_function(k)
 an2[ja]=fetch(ans1)
end

rmprocs(wok)

So when I execute “test.jl”, sometimes it works fine, it gives a five-element vector of 18. But sometimes the REPL says “my_function” is not defined on workers. So when it goes wrong, the error message is
“On worker 12:
UndefVarError: #my_function not defined”. But when it is right, I can just type in an2 in REPL, and it is a 5-element vector of 18. And by “executing”, I mean clicking the top right triangle in the VScode interface, which is “Execute active file in REPL”. My “test_test.jl” file is the following

function my_function(k)
    loop_variable=0
    for i in 1:3
        loop_variable += k
        println("Inside loop: $loop_variable")
    end
   return loop_variable
end

You should use addprocs before including your file with the function, so that the processes spawn before you tell them to include ‘test_test.jl’.

Also, consider using pmap instead for this kind of task:

using Distributed
addprocs(5)

@everywhere include("test_test.jl")

k=6
an2=pmap(my_function, ones(5).*k)
println(an2)
rmprocs(workers())
2 Likes