What happens if I set more threads than available?

My computer has 8 cores, so use set_optimizer_attribute(myModel, "threads", 8) before starting the optimization. What would happen if I use the exact same code on another computer that has, for example, 4 cores? The program would use all the cores available, or the option would “fail”, and it would stick to the default?

1 Like

Threading is solver-specific. You should consult the documentation of the solver you are using.

In this case, I assume from "threads" that it is Cbc… which makes things difficult since there isn’t really much documentation. Out of interest, I looked up what the documentation suggests:

julia> using Cbc_jll

julia> Cbc_jll.cbc() do exe
           run(`$(exe)`)
       end

Welcome to the CBC MILP Solver
Version: 2.10.5
Build Date: Apr 14 2021
CoinSolver takes input from arguments ( - switches to stdin)
Enter ? for list of commands or help
Coin:threads?
thread(s) : Number of threads to try and use
Coin:threads??
thread(s) : Number of threads to try and use
To use multiple threads, set threads to number wanted. It may be
better to use one or two more than number of cpus available. If 100+n
then n threads and search is repeatable (maybe be somewhat slower),
if 200+n use threads for root cuts, 400+n threads used in sub-trees.
<Range of values is -100 to 100000;
current 0>

I hope you don’t have more than 99 threads! I also don’t know what negative values mean, or why the upper limit is 100,000…

You can see how many threads a computer has using:

Sys.CPU_THREADS

For other solvers, you can sometimes expect better performance by limiting the number of threads. See, e.g., Gurobi:

2 Likes

It was CBC indeed! And thanks for the tip, I didn’t new there was such an easy way to get how many cores are in the computer. Now I’ll just use set_optimizer_attribute(myModel, "threads", Sys.CPU_THREADS)

1 Like