Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UndefRefError: access to undefined reference when running PRIMA.jl in parallel #37

Open
soldasim opened this issue Jan 22, 2025 · 0 comments

Comments

@soldasim
Copy link

Hi, I have encountered an error, which I believe is caused by a bug in PRIMA.jl.

When I run multiple PRIMA.jl optimization runs in parallel, sometimes I get the following error;

Stacktrace
UndefRefError: access to undefined reference
Stacktrace:
  [1] getindex
    @ ./essentials.jl:917 [inlined]
  [2] _get_objfun_stack()
    @ PRIMA ~/.julia/packages/PRIMA/0LIGn/src/PRIMA.jl:825
  [3] _push_objfun
    @ ~/.julia/packages/PRIMA/0LIGn/src/PRIMA.jl:835 [inlined]
  [4] newuoa!(f::OptimizationPRIMA.var"#3#7"{OptimizationBase.OptimizationCache{SciMLBase.OptimizationFunction{true, ADTypes.AutoForwardDiff{nothing, Nothing}, BOSS.var"#111#113"{BOSS.var"#103#107"{BOSS.var"#101#105"{Vector{Float64}, Vector{Bool}, Vector{Bool}, GaussianProcess{Nothing, Vector{InverseGamma{Float64}}, Vector{Product{Continuous, InverseGamma{Float64}, Vector{InverseGamma{Float64}}}}, Vector{InverseGamma{Float64}}}}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, OptimizationBase.ReInitCache{Vector{Float64}, Nothing}, Nothing, Nothing, Nothing, Nothing, Nothing, NEWUOA, Bool, OptimizationBase.NullCallback, Nothing}}, x::Vector{Float64}; scale::Nothing, rhobeg::Float64, rhoend::Float64, ftarget::Float64, maxfun::Int64, npt::Int64, iprint::PRIMA.Message)
    @ PRIMA ~/.julia/packages/PRIMA/0LIGn/src/PRIMA.jl:446
  [5] newuoa!
    @ ~/.julia/packages/PRIMA/0LIGn/src/PRIMA.jl:425 [inlined]
  [6] #newuoa#6
    @ ~/.julia/packages/PRIMA/0LIGn/src/PRIMA.jl:101 [inlined]
  [7] newuoa
    @ ~/.julia/packages/PRIMA/0LIGn/src/PRIMA.jl:99 [inlined]
  [8] __solve(cache::OptimizationBase.OptimizationCache{SciMLBase.OptimizationFunction{true, ADTypes.AutoForwardDiff{nothing, Nothing}, BOSS.var"#111#113"{BOSS.var"#103#107"{BOSS.var"#101#105"{Vector{Float64}, Vector{Bool}, Vector{Bool}, GaussianProcess{Nothing, Vector{InverseGamma{Float64}}, Vector{Product{Continuous, InverseGamma{Float64}, Vector{InverseGamma{Float64}}}}, Vector{InverseGamma{Float64}}}}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, OptimizationBase.ReInitCache{Vector{Float64}, Nothing}, Nothing, Nothing, Nothing, Nothing, Nothing, NEWUOA, Bool, OptimizationBase.NullCallback, Nothing})
    @ OptimizationPRIMA ~/.julia/packages/OptimizationPRIMA/WIB3T/src/OptimizationPRIMA.jl:189
  [9] solve!(cache::OptimizationBase.OptimizationCache{SciMLBase.OptimizationFunction{true, ADTypes.AutoForwardDiff{nothing, Nothing}, BOSS.var"#111#113"{BOSS.var"#103#107"{BOSS.var"#101#105"{Vector{Float64}, Vector{Bool}, Vector{Bool}, GaussianProcess{Nothing, Vector{InverseGamma{Float64}}, Vector{Product{Continuous, InverseGamma{Float64}, Vector{InverseGamma{Float64}}}}, Vector{InverseGamma{Float64}}}}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, OptimizationBase.ReInitCache{Vector{Float64}, Nothing}, Nothing, Nothing, Nothing, Nothing, Nothing, NEWUOA, Bool, OptimizationBase.NullCallback, Nothing})
    @ SciMLBase ~/.julia/packages/SciMLBase/AkGLd/src/solve.jl:187
 [10] solve(::SciMLBase.OptimizationProblem{true, SciMLBase.OptimizationFunction{true, ADTypes.AutoForwardDiff{nothing, Nothing}, BOSS.var"#111#113"{BOSS.var"#103#107"{BOSS.var"#101#105"{Vector{Float64}, Vector{Bool}, Vector{Bool}, GaussianProcess{Nothing, Vector{InverseGamma{Float64}}, Vector{Product{Continuous, InverseGamma{Float64}, Vector{InverseGamma{Float64}}}}, Vector{InverseGamma{Float64}}}}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Vector{Float64}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, @Kwargs{}}, ::NEWUOA; kwargs::@Kwargs{})
    @ SciMLBase ~/.julia/packages/SciMLBase/AkGLd/src/solve.jl:95
 [11] solve
    @ ~/.julia/packages/SciMLBase/AkGLd/src/solve.jl:92 [inlined]
 [12] (::BOSS.var"#optimize#115"{OptimizationMAP{NEWUOA, Int64}, BOSS.var"#103#107"{BOSS.var"#101#105"{Vector{Float64}, Vector{Bool}, Vector{Bool}, GaussianProcess{Nothing, Vector{InverseGamma{Float64}}, Vector{Product{Continuous, InverseGamma{Float64}, Vector{InverseGamma{Float64}}}}, Vector{InverseGamma{Float64}}}}}, BOSS.var"#112#114"{SciMLBase.OptimizationFunction{true, ADTypes.AutoForwardDiff{nothing, Nothing}, BOSS.var"#111#113"{BOSS.var"#103#107"{BOSS.var"#101#105"{Vector{Float64}, Vector{Bool}, Vector{Bool}, GaussianProcess{Nothing, Vector{InverseGamma{Float64}}, Vector{Product{Continuous, InverseGamma{Float64}, Vector{InverseGamma{Float64}}}}, Vector{InverseGamma{Float64}}}}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}}})(start::Vector{Float64})
    @ BOSS ~/.julia/packages/BOSS/TWXPv/src/model_fitter/optimization.jl:142
 [13] macro expansion
    @ ~/.julia/packages/BOSS/TWXPv/src/utils/optim_utils.jl:40 [inlined]
 [14] (::BOSS.var"#269#threadsfor_fun#160"{BOSS.var"#269#threadsfor_fun#159#161"{BOSS.var"#optimize#115"{OptimizationMAP{NEWUOA, Int64}, BOSS.var"#103#107"{BOSS.var"#101#105"{Vector{Float64}, Vector{Bool}, Vector{Bool}, GaussianProcess{Nothing, Vector{InverseGamma{Float64}}, Vector{Product{Continuous, InverseGamma{Float64}, Vector{InverseGamma{Float64}}}}, Vector{InverseGamma{Float64}}}}}, BOSS.var"#112#114"{SciMLBase.OptimizationFunction{true, ADTypes.AutoForwardDiff{nothing, Nothing}, BOSS.var"#111#113"{BOSS.var"#103#107"{BOSS.var"#101#105"{Vector{Float64}, Vector{Bool}, Vector{Bool}, GaussianProcess{Nothing, Vector{InverseGamma{Float64}}, Vector{Product{Continuous, InverseGamma{Float64}, Vector{InverseGamma{Float64}}}}, Vector{InverseGamma{Float64}}}}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}}}, Matrix{Float64}, BossOptions{NoCallback}, Base.Threads.SpinLock, Base.Threads.Atomic{Int64}, Vector{Float64}, Vector{Vector{Float64}}, UnitRange{Int64}}})(tid::Int64; onethread::Bool)
    @ BOSS ./threadingconstructs.jl:253
 [15] #269#threadsfor_fun
    @ ./threadingconstructs.jl:220 [inlined]
 [16] (::Base.Threads.var"#1#2"{BOSS.var"#269#threadsfor_fun#160"{BOSS.var"#269#threadsfor_fun#159#161"{BOSS.var"#optimize#115"{OptimizationMAP{NEWUOA, Int64}, BOSS.var"#103#107"{BOSS.var"#101#105"{Vector{Float64}, Vector{Bool}, Vector{Bool}, GaussianProcess{Nothing, Vector{InverseGamma{Float64}}, Vector{Product{Continuous, InverseGamma{Float64}, Vector{InverseGamma{Float64}}}}, Vector{InverseGamma{Float64}}}}}, BOSS.var"#112#114"{SciMLBase.OptimizationFunction{true, ADTypes.AutoForwardDiff{nothing, Nothing}, BOSS.var"#111#113"{BOSS.var"#103#107"{BOSS.var"#101#105"{Vector{Float64}, Vector{Bool}, Vector{Bool}, GaussianProcess{Nothing, Vector{InverseGamma{Float64}}, Vector{Product{Continuous, InverseGamma{Float64}, Vector{InverseGamma{Float64}}}}, Vector{InverseGamma{Float64}}}}}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}}}, Matrix{Float64}, BossOptions{NoCallback}, Base.Threads.SpinLock, Base.Threads.Atomic{Int64}, Vector{Float64}, Vector{Vector{Float64}}, UnitRange{Int64}}}, Int64})()
    @ Base.Threads ./threadingconstructs.jl:154

Unfortunately, I was not able to create a MRE. The error seems to only occur with more complex code (as is often the case with parallelization-related errors).

I believe it is caused by incorrect use of Threads.threadid() to index into an array here;

PRIMA.jl/src/PRIMA.jl

Lines 818 to 826 in 8502a75

# Private function `_get_objfun_stack` yields the stack of objective functions
# for the caller thread.
function _get_objfun_stack()
i = Threads.threadid()
while length(_objfun_stack) < i
push!(_objfun_stack, Vector{ObjFun}(undef, 0))
end
return _objfun_stack[i]
end

One should not use Threads.threadid() to index into an array, because the parallel tasks can migrate between threads at any moment in Julia. See the note at https://docs.julialang.org/en/v1/base/multi-threading/#Base.Threads.threadid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant