UndefVarError when initializing variable inside function with @everywhere

Note that @everywhere executes the statement under module Main on the child process; this is causing the error when you move the code to module run_h. More on this here and here.

In general, it is better to keep the functions self sufficient, and not have them rely on a global variable that is initialised from elsewhere. If you really insist on having a module-scoped global matrix A (which I am not at all sure you need, btw), it can be initialised lazily. Smth. like this:

# my_mod.jl
module MyMod

using Distributed, LinearAlgebra, Random

A = zeros(0,0)

function h!(x)
    global A
    if length(A) == 0
        A = zeros(100, 100)
    end

    return eigvals(rand!(MersenneTwister(x), A))

end

function run_h(iter)
    if nprocs() > 1
        @everywhere include("my_mod.jl")  # See more details below
    end
    return pmap(MyMod.h!, iter) 
end

export run_h


end

And then in REPL:

julia> include("my_mod.jl")

julia> using .MyMod, Distributed

julia> run_h(1:20) # works

julia> addprocs(4)

julia> run_h(1:20) # also works

See also this SO post on how to load the code on the workers with @everywhere using ..., if your module code is distributed via the package manager (apparently you need to mess with macros to achieve that). As per addprocs doc, the workers inherit the working directory and julia binary of the master process, unless explicitly specified otherwise. JULIA_PROJECT is specified via the shell environment.

Hope this helps.