Skip to content

Conversation

@odow
Copy link
Member

@odow odow commented Mar 2, 2021

JuMP code to go with the MOI PR jump-dev/MathOptInterface.jl#1252.

If we lazily bridge the lazy bridges, we get much improved speed in the case that the user writes a model without bridges. (The first two benchmark calls.) This comes from avoiding a copy, and from not having to infer the bridging call-chain.

Importantly, because JuMP already has a MOI.AbstractOptimizer backend, there are no performance implications when bridges are used.

For now I've just added a new function, but we could add this as opt-in to the standard Model constructor, and change it to the default in the next breaking JuMP release.

julia> using BenchmarkTools

julia> @btime old_foo()
  570.573 μs (2429 allocations: 175.06 KiB)

julia> @btime new_foo()
  176.522 μs (962 allocations: 82.81 KiB)

julia> @btime old_foo_obj()
  581.474 μs (2438 allocations: 175.31 KiB)

julia> @btime new_foo_obj()
  581.940 μs (2437 allocations: 175.23 KiB)

julia> @btime old_foo_var()
  1.002 ms (2801 allocations: 203.28 KiB)

julia> @btime new_foo_var()
  1.002 ms (2801 allocations: 203.22 KiB)

julia> @btime old_foo_con()
  915.958 μs (2628 allocations: 188.42 KiB)

julia> @btime new_foo_con()
  921.349 μs (2628 allocations: 188.38 KiB)

Code

using JuMP, Clp

### No bridging

function new_foo()
    model = AutoModel(Clp.Optimizer)
    set_silent(model)
    @variable(model, x >= 0)
    @constraint(model, 2x + 1 <= 1)
    @objective(model, Max, 1.0 * x)
    optimize!(model)
end

function old_foo()
    model = Model(Clp.Optimizer)
    set_silent(model)
    @variable(model, x >= 0)
    @constraint(model, 2x + 1 <= 1)
    @objective(model, Max, 1.0 * x)
    optimize!(model)
end

### ObjectiveFunction bridging

function new_foo_obj()
    model = AutoModel(Clp.Optimizer)
    set_silent(model)
    @variable(model, x >= 0)
    @constraint(model, 2x + 1 <= 1)
    @objective(model, Max, x)
    optimize!(model)
end

function old_foo_obj()
    model = Model(Clp.Optimizer)
    set_silent(model)
    @variable(model, x >= 0)
    @constraint(model, 2x + 1 <= 1)
    @objective(model, Max, x)
    optimize!(model)
end

### variable bridging

function old_foo_var()
    model = Model(Clp.Optimizer)
    set_silent(model)
    @variable(model, x[1:1] in MOI.Nonnegatives(1))
    @constraint(model, 2x[1] + 1 <= 1)
    @objective(model, Max, 1.0 * x[1])
    optimize!(model)
end

function new_foo_var()
    model = AutoModel(Clp.Optimizer)
    set_silent(model)
    @variable(model, x[1:1] in MOI.Nonnegatives(1))
    @constraint(model, 2x[1] + 1 <= 1)
    @objective(model, Max, 1.0 * x[1])
    optimize!(model)
end

### constraint bridging

function old_foo_con()
    model = Model(Clp.Optimizer)
    set_silent(model)
    @variable(model, x[1:2])
    @constraint(model, x in MOI.Nonpositives(2))
    @objective(model, Max, 1.0 * x[1])
    optimize!(model)
end

function new_foo_con()
    model = AutoModel(Clp.Optimizer)
    set_silent(model)
    @variable(model, x[1:2])
    @constraint(model, x in MOI.Nonpositives(2))
    @objective(model, Max, 1.0 * x[1])
    optimize!(model)
end

@codecov
Copy link

codecov bot commented Mar 2, 2021

Codecov Report

Merging #2513 (142b8cf) into master (0aa6ad1) will decrease coverage by 1.04%.
The diff coverage is 0.00%.

❗ Current head 142b8cf differs from pull request most recent head c17fefb. Consider uploading reports for the commit c17fefb to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2513      +/-   ##
==========================================
- Coverage   93.37%   92.32%   -1.05%     
==========================================
  Files          44       43       -1     
  Lines        5511     4693     -818     
==========================================
- Hits         5146     4333     -813     
+ Misses        365      360       -5     
Impacted Files Coverage Δ
src/JuMP.jl 76.43% <0.00%> (-6.60%) ⬇️
src/Containers/vectorized_product_iterator.jl 46.66% <0.00%> (-29.53%) ⬇️
src/Containers/DenseAxisArray.jl 77.77% <0.00%> (-10.56%) ⬇️
src/Containers/nested_iterator.jl 92.00% <0.00%> (-8.00%) ⬇️
src/Containers/SparseAxisArray.jl 84.21% <0.00%> (-7.56%) ⬇️
src/parse_nlp.jl 90.56% <0.00%> (-5.37%) ⬇️
src/copy.jl 91.11% <0.00%> (-3.89%) ⬇️
src/mutable_arithmetics.jl 82.08% <0.00%> (-3.80%) ⬇️
src/operators.jl 91.89% <0.00%> (-3.67%) ⬇️
... and 34 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0aa6ad1...c17fefb. Read the comment docs.

@odow odow force-pushed the od/autobridge branch from baeb02f to b723e7b Compare May 3, 2021 20:59
@odow
Copy link
Member Author

odow commented May 21, 2021

Closing in favor of #2610

@odow odow closed this May 21, 2021
@odow odow deleted the od/autobridge branch May 21, 2021 04:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants