# JuMP and CPLEX - Setting number of threads

**URL:** https://discourse.julialang.org/t/jump-and-cplex-setting-number-of-threads/83747
**Category:** Optimization (Mathematical)
**Tags:** jump, cplex
**Created:** [July 4, 2022, 3:58pm UTC](https://discourse.julialang.org/t/jump-and-cplex-setting-number-of-threads/83747 "2022-07-04T15:58:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![vmk97](https://avatars.discourse-cdn.com/v4/letter/v/b3f665/32.png) [@vmk97](https://discourse.julialang.org/u/vmk97)
#### Post date: [July 4, 2022, 3:58pm UTC](https://discourse.julialang.org/t/jump-and-cplex-setting-number-of-threads/83747/1 "2022-07-04T15:58:27Z")

</div>

I am trying to solve a mixed integer linear program by branch-and-cut using JuMP and CPLEX.

I am having some trouble setting the number of threads for CPLEX.

I am trying to set the parameter CPXPARAM\_Threads using set\_optimizer\_attribute in the following way:

```julia
model = Model(CPLEX.Optimizer)
set_optimizer_attribute(model, "CPXPARAM_Threads", 4)

```

However, in the log I get the following:

```julia
Version identifier: 20.1.0.0 | 2020-11-10 | 9bedb6d68
CPXPARAM_Threads 1
...
Parallel mode: none, using 1 thread.
...

```

I don’t understand why the value of 4 that I provided turns into 1.

I would like to run CPLEX in parallel mode with more than one thread.

Thanks in advance.

**Obs** : I have CPLEX Optimization Studio v20.1 installed which I got through an academic license.  
I am running Julia on version 1.7.3, JuMP on version 0.21.10 and CPLEX on version 0.7.8.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [July 4, 2022, 5:58pm UTC](https://discourse.julialang.org/t/jump-and-cplex-setting-number-of-threads/83747/2 "2022-07-04T17:58:59Z")

</div>

Not sure what is going on here, but maybe you could try setting [this CPLEX parameter](https://www.ibm.com/docs/en/icos/12.8.0.0?topic=parameters-parallel-mode-switch) too?  
Did you launch Julia with a single thread or several?

---

<div class="post-metadata">

### Author: ![vmk97](https://avatars.discourse-cdn.com/v4/letter/v/b3f665/32.png) [@vmk97](https://discourse.julialang.org/u/vmk97)
#### Post date: [July 4, 2022, 9:34pm UTC](https://discourse.julialang.org/t/jump-and-cplex-setting-number-of-threads/83747/3 "2022-07-04T21:34:05Z")

</div>

Thanks, Guillaume.

Yes, I did try to set the CPXPARAM\_Parallel parameters as well, but it didn’t work either.

I think the answer is [here](https://github.com/jump-dev/JuMP.jl/issues/904).

I am passing a custom callback function to CPLEX for handling both lazy constraints and cuts.

According to the link, apparently CPLEX automatically turns off multi-threading when given callbacks.

---

<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: [July 4, 2022, 11:34pm UTC](https://discourse.julialang.org/t/jump-and-cplex-setting-number-of-threads/83747/4 "2022-07-04T23:34:01Z")

</div>

Did this warning trigger?

> <https://github.com/jump-dev/CPLEX.jl/blob/e2f15e06b767b33941dce62873a048d18e6a844f/src/MOI/MOI_callbacks.jl#L101-L108>

Perhaps CPLEX started automatically turning off multithreading with callbacks.

But yes, you cannot use threading if you have a callback. The underlying problem is that callbacks from Julia into C are not thread safe, so there is no way to implement this.

---

<div class="post-metadata">

### Author: ![vmk97](https://avatars.discourse-cdn.com/v4/letter/v/b3f665/32.png) [@vmk97](https://discourse.julialang.org/u/vmk97)
#### Post date: [July 5, 2022, 12:56pm UTC](https://discourse.julialang.org/t/jump-and-cplex-setting-number-of-threads/83747/5 "2022-07-05T12:56:37Z")

</div>

First of all, thank you, Guillaume and Oscar.

No, the warning is not triggered. I guess MOI gets the number of threads already forced to one and hence no warning.

Indeed when I run the model with no callback I am able to set the number of threads as I please.

> callbacks from Julia into C are not thread safe

Is this specific to Julia? Is it the same for other programming languages?
