Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusing Gurobi environment doesn't work inside package #388

Closed
pearlzli opened this issue Jan 10, 2021 · 5 comments
Closed

Reusing Gurobi environment doesn't work inside package #388

pearlzli opened this issue Jan 10, 2021 · 5 comments

Comments

@pearlzli
Copy link

pearlzli commented Jan 10, 2021

In the past week, reusing a Gurobi environment has stopped working for me inside packages but not modules. I'm using Julia v1.5.3, Gurobi v9.1.1, and Gurobi.jl v0.9.7. As a MWE, I create a package using pkg> generate TestGurobi, add Gurobi as a dependency, and edit src/TestGurobi.jl to be

module TestGurobi

using Gurobi
const GRB_ENV = Gurobi.Env()

end

Then, in the REPL, the Gurobi.Optimizer constructor works with a new Gurobi environment but not with GRB_ENV:

julia> using Gurobi, TestGurobi
[ Info: Precompiling TestGurobi [11fe4acc-459e-4469-9efc-38a933d93789]
Academic license - for non-commercial use only

(TestGurobi) pkg> st Gurobi
Project TestGurobi v0.1.0
Status `~/Desktop/TestGurobi/Project.toml`
  [2e9cd046] Gurobi v0.9.7

julia> Gurobi.Optimizer(Gurobi.Env())
Academic license - for non-commercial use only
    sense  : minimize
    number of variables             = 0
    number of linear constraints    = 0
    number of quadratic constraints = 0
    number of sos constraints       = 0
    number of non-zero coeffs       = 0
    number of non-zero qp objective terms  = 0
    number of non-zero qp constraint terms = 0

julia> Gurobi.Optimizer(TestGurobi.GRB_ENV)
ERROR: Gurobi Error 10002:
Stacktrace:
 [1] _check_ret at /Users/pearl/.julia/packages/Gurobi/Lci9Q/src/MOI_wrapper.jl:277 [inlined]
 [2] empty!(::Gurobi.Optimizer) at /Users/pearl/.julia/packages/Gurobi/Lci9Q/src/MOI_wrapper.jl:329
 [3] Gurobi.Optimizer(::Gurobi.Env; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /Users/pearl/.julia/packages/Gurobi/Lci9Q/src/MOI_wrapper.jl:252
 [4] Gurobi.Optimizer(::Gurobi.Env) at /Users/pearl/.julia/packages/Gurobi/Lci9Q/src/MOI_wrapper.jl:225
 [5] top-level scope at REPL[2]:1

However, curiously (in a new REPL session) if I include the module file instead of using the package, GRB_ENV works fine:

julia> using Gurobi; include("src/TestGurobi.jl")
Academic license - for non-commercial use only
Main.TestGurobi

julia> Gurobi.Optimizer(TestGurobi.GRB_ENV)
    sense  : minimize
    number of variables             = 0
    number of linear constraints    = 0
    number of quadratic constraints = 0
    number of sos constraints       = 0
    number of non-zero coeffs       = 0
    number of non-zero qp objective terms  = 0
    number of non-zero qp constraint terms = 0

Defining the constant GRB_ENV in the REPL (again in a new session) directly also works:

julia> using Gurobi

julia> const GRB_ENV = Gurobi.Env()
Academic license - for non-commercial use only
Gurobi.Env(Ptr{Nothing} @0x00007fa5435df800, false, 0)

julia> Gurobi.Optimizer(GRB_ENV)
    sense  : minimize
    number of variables             = 0
    number of linear constraints    = 0
    number of quadratic constraints = 0
    number of sos constraints       = 0
    number of non-zero coeffs       = 0
    number of non-zero qp objective terms  = 0
    number of non-zero qp constraint terms = 0

Am I doing something wrong? I had been using the constant GRB_ENV inside a package for months now without any problems, so I'm not sure what could have changed. I tried updating Gurobi.jl and Gurobi itself, both to the newest versions, to no avail.

@pearlzli
Copy link
Author

pearlzli commented Jan 10, 2021

It seems like there's a weird interaction with Revise.jl (v3.1.11) going on. After executing the first set of REPL commands in my previous post (before "However, curiously..."), if I add an additional line const GRB_ENV_2 = Gurobi.Env() to src/TestGurobi.jl, then I can use GRB_ENV_2 but still not GRB_ENV:

julia> Gurobi.Optimizer(TestGurobi.GRB_ENV_2)
    sense  : minimize
    number of variables             = 0
    number of linear constraints    = 0
    number of quadratic constraints = 0
    number of sos constraints       = 0
    number of non-zero coeffs       = 0
    number of non-zero qp objective terms  = 0
    number of non-zero qp constraint terms = 0


julia> Gurobi.Optimizer(TestGurobi.GRB_ENV)
ERROR: Gurobi Error 10002:
Stacktrace:
 [1] _check_ret at /Users/pearl/.julia/packages/Gurobi/Lci9Q/src/MOI_wrapper.jl:277 [inlined]
 [2] empty!(::Gurobi.Optimizer) at /Users/pearl/.julia/packages/Gurobi/Lci9Q/src/MOI_wrapper.jl:329
 [3] Gurobi.Optimizer(::Gurobi.Env; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /Users/pearl/.julia/packages/Gurobi/Lci9Q/src/MOI_wrapper.jl:252
 [4] Gurobi.Optimizer(::Gurobi.Env) at /Users/pearl/.julia/packages/Gurobi/Lci9Q/src/MOI_wrapper.jl:225
 [5] top-level scope at REPL[4]:1

Alternatively, if I rename the constant GRB_ENV to GRB_ENV_3, call Gurobi.Optimizer(TestGurobi.GUROBI_ENV_3), then rename it back to GRB_ENV, then suddenly Gurobi.Optimizer(TestGurobi.GUROBI_ENV) works after all!

@odow
Copy link
Member

odow commented Jan 11, 2021

I can reproduce this. I'll take a look.

@odow
Copy link
Member

odow commented Jan 11, 2021

You can't initialize C variables like this. You need to use an __init__ function like follows:

module TestGurobi

using Gurobi

const GRB_ENV = Ref{Gurobi.Env}()

function __init__()
    global GRB_ENV[] = Gurobi.Env()
end

end

Then

using TestGurobi
env = TestGurobi.GRB_ENV[]

Here are the docs: https://docs.julialang.org/en/v1/manual/modules/#Module-initialization-and-precompilation

@odow
Copy link
Member

odow commented Jan 11, 2021

Closing because this is not an issue with Gurobi.jl.

@odow odow closed this as completed Jan 11, 2021
@pearlzli
Copy link
Author

Thanks for the quick response! This worked great for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants