# Is NLopt thread safe?

**URL:** <https://discourse.julialang.org/t/is-nlopt-thread-safe/71398>\
**Category:** General Usage\
**Tags:** nlopt\
**Created:** [November 12, 2021, 6:28pm UTC](https://discourse.julialang.org/t/is-nlopt-thread-safe/71398 "2021-11-12T18:28:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 12, 2021, 6:28pm UTC](https://discourse.julialang.org/t/is-nlopt-thread-safe/71398/1 "2021-11-12T18:28:44Z")

</div>

I’ve got a constrained optimization situation where restarting NLopt from multiple initial points tends to be a good idea. I’d like to simply do `@threads` and split them over several threads, but I don’t know if this is safe. I haven’t actually tried it yet, but thought maybe someone would be able to address this. @stevengj or @mlubin perhaps?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 12, 2021, 7:55pm UTC](https://discourse.julialang.org/t/is-nlopt-thread-safe/71398/2 "2021-11-12T19:55:41Z")

</div>

Well, I did try running it in multiple threads from different initial points, and it in fact didn’t work and then segfaulted… so I guess the answer is no unless there’s a specific methodology that works.

```julia
starting optimization... 1
starting optimization... 3
starting optimization... 5
starting optimization... 7
starting optimization... 9
finished with value: -0.1660709144581413, due to ROUNDOFF_LIMITED
starting optimization... 2
finished with value: 0.16607091445814082, due to ROUNDOFF_LIMITED
starting optimization... 6
finished with value: -0.16608205405870535, due to ROUNDOFF_LIMITED
starting optimization... 4
finished with value: -0.16607091445814082, due to ROUNDOFF_LIMITED
starting optimization... 8
finished with value: -0.16607091445814082, due to ROUNDOFF_LIMITED
starting optimization... 10
finished with value: 0.16229485881648792, due to ROUNDOFF_LIMITED

signal (11): Segmentation fault

```

Compared with serially:

```julia
starting optimization... 1
finished with value: 0.3555415144855803, due to ROUNDOFF_LIMITED
starting optimization... 2
finished with value: 0.328531673996751, due to ROUNDOFF_LIMITED
starting optimization... 3
....

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 12, 2021, 8:03pm UTC](https://discourse.julialang.org/t/is-nlopt-thread-safe/71398/3 "2021-11-12T20:03:50Z")

</div>

Are you creating independent problems for each task? I would find strange that there was any interaction between them.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 12, 2021, 8:07pm UTC](https://discourse.julialang.org/t/is-nlopt-thread-safe/71398/4 "2021-11-12T20:07:28Z")

</div>

I create a single “problem” then an array of initial values then call

```julia
initv = initval[i]
optimize!(opt,initv)

```

which might be the problem right there… as it’s maybe modifying the opt problem?

I’m trying it with an array of opt problems now.

---

<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:** [November 12, 2021, 8:10pm UTC](https://discourse.julialang.org/t/is-nlopt-thread-safe/71398/5 "2021-11-12T20:10:52Z")

</div>

> including many more than just the removal of SingleVariable.

Yes, you’ll probably need to create a separate problem for each call.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 12, 2021, 8:32pm UTC](https://discourse.julialang.org/t/is-nlopt-thread-safe/71398/6 "2021-11-12T20:32:31Z")

</div>

Ok, it seems it did work if I used a vector of opt problems. This is fantastic as it’s cutting my time to do 10 optimizations by a lot!

the general pattern is

```julia
optprob = [...]
init = [...]
result = [...]

Threads.@threads for i in 1:10
   ...some setup
   result[i] = optimize!(optprob[i],init[i])
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 12, 2021, 10:25pm UTC](https://discourse.julialang.org/t/is-nlopt-thread-safe/71398/7 "2021-11-12T22:25:33Z")

</div>

Yes, it should be thread safe if you have different optimization objects for each thread. (For the most part. There might be some problems with individual algorithms in NLopt, I’m not 100% sure without looking through them again in detail.)
