NLP parameters inside a multithreaded loop

I am not sure if this is a JuMP or NLopt issue but whenever I try to do a loop with a NLP inside defining a parameter in each iteration, it only works if I am using a single thread.
For example, the code below does not work:

using JuMP, NLopt

model = Model(NLopt.Optimizer)
set_optimizer_attribute(model, "algorithm",:LD_MMA )
@variable(model, z)
@NLparameter(model, x == 1.0)
@NLobjective(model, Min, (z - x)^2)

A = rand(100)

Threads.@threads for x0 in A
   set_value(x, x0)
   JuMP.optimize!(model)
   @show value(x) 
end

While the code below works:

Threads.@threads for x0 in A
   model = Model(NLopt.Optimizer)
   set_optimizer_attribute(model, "algorithm",:LD_MMA )
   @variable(model, z)
   @NLparameter(model, x == x0)
   @NLobjective(model, Min, (z - x)^2)
   JuMP.optimize!(model)
   @show value(x) 
end

However, I believe the point of parameters is not having to define the whole model in each iteration.

The only other NLP solver I have is Ipopt with MUMPS which I believe cannot be multithreaded anyway.

1 Like

You cannot solve JuMP models in a thread like this because the JuMP model maintains internal mutable state. Parameters are useful in sequential solves, not parallel ones.

1 Like

Ok thanks!
I guess I’ll have to create a new model in each parallel iteration

1 Like