google / or-tools

Google's Operations Research tools:
https://developers.google.com/optimization/
Apache License 2.0
11.32k stars 2.14k forks source link

`SetNumThreads` has no effect on Gurobi? #2815

Open gdowdy3 opened 3 years ago

gdowdy3 commented 3 years ago

What version of OR-Tools and what language are you using? Version: v9.0.9048 Language: Python

Which solver are you using (e.g. CP-SAT, Routing Solver, GLOP, BOP, Gurobi) Gurobi

What operating system (Linux, Windows, ...) and version? macOS Big Sur v11.6

What did you do? Steps to reproduce the behavior:

  1. Install ortools with pip3 install ortools
  2. Install a Gurobi license.
  3. Save the following script as MWE.py. This script is very similar to the example shown here: https://developers.google.com/optimization/lp/lp_example. However, there are a few changes:
    • Gurobi is specified as the solver.
    • The solver output is enabled.
    • The decision variables are integer-valued.
    • The number of threads is specified as 4.
from ortools.linear_solver import pywraplp

def LinearProgrammingExample():
    """Linear programming sample."""
    # Instantiate a Glop solver, naming it LinearExample.
    solver = pywraplp.Solver.CreateSolver('GUROBI')

    # Create the two variables and let them take on any non-negative value.
    x = solver.IntVar(0, solver.infinity(), 'x')
    y = solver.IntVar(0, solver.infinity(), 'y')

    print('Number of variables =', solver.NumVariables())

    # Constraint 0: x + 2y <= 14.
    solver.Add(x + 2 * y <= 14.0)

    # Constraint 1: 3x - y >= 0.
    solver.Add(3 * x - y >= 0.0)

    # Constraint 2: x - y <= 2.
    solver.Add(x - y <= 2.0)

    print('Number of constraints =', solver.NumConstraints())

    # Objective function: 3x + 4y.
    solver.Maximize(3 * x + 4 * y)

    # Set the number of threads
    solver.SetNumThreads(4)

    # Enable solver output
    solver.EnableOutput()

    # Solve the system.
    status = solver.Solve()

    if status == pywraplp.Solver.OPTIMAL:
        print('Solution:')
        print('Objective value =', solver.Objective().Value())
        print('x =', x.solution_value())
        print('y =', y.solution_value())
    else:
        print('The problem does not have an optimal solution.')

    print('\nAdvanced usage:')
    print('Problem solved in %f milliseconds' % solver.wall_time())
    print('Problem solved in %d iterations' % solver.iterations())

LinearProgrammingExample()
  1. Run python3 MWE.py
  2. Observe output.

What did you expect to see Thread count: 8 physical cores, 16 logical processors, using up to 4 threads Thread count was 4 (of 16 available processors)

What did you see instead? Thread count: 8 physical cores, 16 logical processors, using up to 16 threads Thread count was 16 (of 16 available processors)

Anything else we should know about your project / environment When I was initially trying to set up ortools to use Gurobi on this machine, I had some trouble getting ortools to find Gurobi. In my efforts to solve the problem, I installed a fresh copy of Gurobi from https://www.gurobi.com. I also manually set up the GUROBI_HOME variable in my .zshenv. I believe this is the reason that I see the following whenever I run the above code.

I0430 18:18:55.265136     1 environment.cc:737] Found the Gurobi library in '/Library/gurobi912/mac64/lib/libgurobi91.dylib.

I'm not sure whether or not this is relevant.

lperron commented 3 years ago

Indeed. You should be able to work around by setting the solver specific parameter as string using the Gurobi names.

gdowdy3 commented 3 years ago

Thanks for the quick reply!

Just to make sure I understand, is it the case that SetNumThreads has no effect on Gurobi by design? Or am I only experiencing this problem because I installed Gurobi myself?

gdowdy3 commented 3 years ago

For the benefit of future generations, the work-around mentioned above is to replace

solver.SetNumThreads(4)

with

solver.SetSolverSpecificParametersAsString('Threads 4')

I have confirmed that this works. Thanks, @lperron!

Mizux commented 3 years ago

For the record: up to v9.1 gurobi_interface.cc doesn't implement SetNumThread() but have a flag https://github.com/google/or-tools/blob/86d4c543f717fa8716a9d66c25143660668bf825/ortools/linear_solver/gurobi_interface.cc#L68-L69 Which is only use at instantiation (in the ctor) https://github.com/google/or-tools/blob/86d4c543f717fa8716a9d66c25143660668bf825/ortools/linear_solver/gurobi_interface.cc#L617-L618 While we should see (IMHO) something like in sat_interface.cc: https://github.com/google/or-tools/blob/86d4c543f717fa8716a9d66c25143660668bf825/ortools/linear_solver/sat_interface.cc#L99 https://github.com/google/or-tools/blob/86d4c543f717fa8716a9d66c25143660668bf825/ortools/linear_solver/sat_interface.cc#L294-L297 https://github.com/google/or-tools/blob/86d4c543f717fa8716a9d66c25143660668bf825/ortools/linear_solver/sat_interface.cc#L106

https://github.com/google/or-tools/blob/86d4c543f717fa8716a9d66c25143660668bf825/ortools/linear_solver/sat_interface.cc#L287-L288 and then setup during the solve() call https://github.com/google/or-tools/blob/86d4c543f717fa8716a9d66c25143660668bf825/ortools/linear_solver/sat_interface.cc#L142-L143