# NLP parameters inside a multithreaded loop

**URL:** https://discourse.julialang.org/t/nlp-parameters-inside-a-multithreaded-loop/72622
**Category:** Optimization (Mathematical)
**Tags:** jump, bug, nlopt
**Created:** [December 5, 2021, 11:29pm UTC](https://discourse.julialang.org/t/nlp-parameters-inside-a-multithreaded-loop/72622 "2021-12-05T23:29:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Joao\_Guilherme\_Santo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joao_guilherme_santo/32/8227_2.png) [@Joao\_Guilherme\_Santo](https://discourse.julialang.org/u/Joao_Guilherme_Santo)
#### Post date: [December 5, 2021, 11:29pm UTC](https://discourse.julialang.org/t/nlp-parameters-inside-a-multithreaded-loop/72622/1 "2021-12-05T23:29:20Z")

</div>

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:

```julia
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:

```julia
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.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [December 5, 2021, 11:47pm UTC](https://discourse.julialang.org/t/nlp-parameters-inside-a-multithreaded-loop/72622/2 "2021-12-05T23:47:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Joao\_Guilherme\_Santo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joao_guilherme_santo/32/8227_2.png) [@Joao\_Guilherme\_Santo](https://discourse.julialang.org/u/Joao_Guilherme_Santo)
#### Post date: [December 6, 2021, 12:06am UTC](https://discourse.julialang.org/t/nlp-parameters-inside-a-multithreaded-loop/72622/3 "2021-12-06T00:06:51Z")

</div>

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