Skip to content

Change ScaledPSDConeBridge to support ScaledPositiveSemidefiniteConeTriangle - #270

Merged
blegat merged 3 commits into
masterfrom
bl/scaled
Jun 6, 2023
Merged

blegat merged 3 commits into
masterfrom
bl/scaled

Conversation

@blegat

@blegat blegat commented May 22, 2023

Copy link
Copy Markdown
Member

No description provided.

@codecov

codecov Bot commented May 22, 2023 •

Copy link
Copy Markdown

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.15 🎉

Comparison is base (51842ad) 90.84% compared to head (f989f92) 90.99%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #270      +/-   ##
==========================================
+ Coverage   90.84%   90.99%   +0.15%     
==========================================
  Files           8        8              
  Lines         437      411      -26     
==========================================
- Hits          397      374      -23     
+ Misses         40       37       -3     
Impacted Files Coverage Δ
src/MOI_wrapper/scaled_psd_cone_bridge.jl 95.23% <100.00%> (+2.59%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@odow odow left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So (1): I approve adding a bridge like this.

But (2): is this not technically breaking if someone was explicitly using the bridge, rather than a LazyBridgeOptimizer?

@blegat

blegat commented May 25, 2023

Copy link
Copy Markdown
Member Author

But (2): is this not technically breaking if someone was explicitly using the bridge, rather than a LazyBridgeOptimizer?

Yes, it should be unlikely to break someone's code since I kept the same bridge name so if you use it with add_bridge, e.g., from JuMP it should still work. We could do SCS v2 but I think SCS v1.2 is probably enough

@odow

odow commented May 25, 2023 •

Copy link
Copy Markdown
Member
  • Benchmark this to see if the extra copy makes a difference

@odow odow changed the title Use MOI scaled PSD cone Change ScaledPSDConeBridge to support ScaledPositiveSemidefiniteConeTriangle Jun 6, 2023
@odow

odow commented Jun 6, 2023

Copy link
Copy Markdown
Member

Did some benchmarking. The extra copy is not a cause for concern, so I'm okay to merge this PR.

Previous performance benchmark

Code

julia> import MathOptInterface as MOI

julia> import Random

julia> import SCS

julia> import SparseArrays

julia> function main()
           Random.seed!(1234)
           m, n = 20, 800
           d = div(n * (n + 1), 2)
           A, b, c = SparseArrays.sprand(d, m, 0.2), rand(d), rand(m)
           model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
           x = MOI.add_variables(model, m)
           obj = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(c, x), 0.0)
           MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
           MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
           terms = map(zip(SparseArrays.findnz(A)...)) do (i, j, v)
               return MOI.VectorAffineTerm(i, MOI.ScalarAffineTerm(-v, x[j]))
           end
           MOI.add_constraint(
               model, 
               MOI.VectorAffineFunction(terms, b),
               MOI.PositiveSemidefiniteConeTriangle(n),
           )
           scs = MOI.instantiate(SCS.Optimizer; with_bridge_type = Float64)
           MOI.set(scs, MOI.RawOptimizerAttribute("max_iters"), 1)
           MOI.copy_to(scs, model)
           MOI.optimize!(scs)
           return
       end
main (generic function with 1 method)

This PR

julia> @time main()
------------------------------------------------------------------
	       SCS v3.2.3 - Splitting Conic Solver
	(c) Brendan O'Donoghue, Stanford University, 2012
------------------------------------------------------------------
problem:  variables n: 20, constraints m: 320400
cones: 	  s: psd vars: 320400, ssize: 1
settings: eps_abs: 1.0e-04, eps_rel: 1.0e-04, eps_infeas: 1.0e-07
	  alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1
	  max_iters: 1, normalize: 1, rho_x: 1.00e-06
	  acceleration_lookback: 10, acceleration_interval: 10
lin-sys:  sparse-direct-amd-qdldl
	  nnz(A): 1280310, nnz(P): 0
------------------------------------------------------------------
 iter | pri res | dua res |   gap   |   obj   |  scale  | time (s)
------------------------------------------------------------------
     0| 1.01e+01  4.69e+02  3.45e+03  1.73e+03  1.00e-01  1.32e+00 
     1| 1.01e+01  4.69e+02  3.45e+03  1.73e+03  1.00e-01  1.33e+00 
------------------------------------------------------------------
status:  solved (inaccurate - reached max_iters)
timings: total: 1.33e+00s = setup: 5.91e-01s + solve: 7.41e-01s
	 lin-sys: 1.02e-02s, cones: 6.75e-01s, accel: 2.50e-08s
------------------------------------------------------------------
objective = 1726.040603 (inaccurate)
------------------------------------------------------------------
  1.725265 seconds (3.03 k allocations: 313.449 MiB, 2.24% gc time)

julia> @time main()
------------------------------------------------------------------
	       SCS v3.2.3 - Splitting Conic Solver
	(c) Brendan O'Donoghue, Stanford University, 2012
------------------------------------------------------------------
problem:  variables n: 20, constraints m: 320400
cones: 	  s: psd vars: 320400, ssize: 1
settings: eps_abs: 1.0e-04, eps_rel: 1.0e-04, eps_infeas: 1.0e-07
	  alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1
	  max_iters: 1, normalize: 1, rho_x: 1.00e-06
	  acceleration_lookback: 10, acceleration_interval: 10
lin-sys:  sparse-direct-amd-qdldl
	  nnz(A): 1280310, nnz(P): 0
------------------------------------------------------------------
 iter | pri res | dua res |   gap   |   obj   |  scale  | time (s)
------------------------------------------------------------------
     0| 1.01e+01  4.69e+02  3.45e+03  1.73e+03  1.00e-01  1.31e+00 
     1| 1.01e+01  4.69e+02  3.45e+03  1.73e+03  1.00e-01  1.32e+00 
------------------------------------------------------------------
status:  solved (inaccurate - reached max_iters)
timings: total: 1.32e+00s = setup: 5.86e-01s + solve: 7.37e-01s
	 lin-sys: 1.01e-02s, cones: 6.71e-01s, accel: 2.40e-08s
------------------------------------------------------------------
objective = 1726.040603 (inaccurate)
------------------------------------------------------------------
  1.704311 seconds (3.03 k allocations: 313.449 MiB, 2.92% gc time)

master

julia> @time main()
------------------------------------------------------------------
	       SCS v3.2.3 - Splitting Conic Solver
	(c) Brendan O'Donoghue, Stanford University, 2012
------------------------------------------------------------------
problem:  variables n: 20, constraints m: 320400
cones: 	  s: psd vars: 320400, ssize: 1
settings: eps_abs: 1.0e-04, eps_rel: 1.0e-04, eps_infeas: 1.0e-07
	  alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1
	  max_iters: 1, normalize: 1, rho_x: 1.00e-06
	  acceleration_lookback: 10, acceleration_interval: 10
lin-sys:  sparse-direct-amd-qdldl
	  nnz(A): 1280310, nnz(P): 0
------------------------------------------------------------------
 iter | pri res | dua res |   gap   |   obj   |  scale  | time (s)
------------------------------------------------------------------
     0| 1.01e+01  4.69e+02  3.45e+03  1.73e+03  1.00e-01  1.30e+00 
     1| 1.01e+01  4.69e+02  3.45e+03  1.73e+03  1.00e-01  1.31e+00 
------------------------------------------------------------------
status:  solved (inaccurate - reached max_iters)
timings: total: 1.32e+00s = setup: 5.87e-01s + solve: 7.29e-01s
	 lin-sys: 8.94e-03s, cones: 6.67e-01s, accel: 3.20e-08s
------------------------------------------------------------------
objective = 1726.040603 (inaccurate)
------------------------------------------------------------------
  1.715707 seconds (3.00 k allocations: 271.931 MiB, 1.15% gc time)

julia> @time main()
------------------------------------------------------------------
	       SCS v3.2.3 - Splitting Conic Solver
	(c) Brendan O'Donoghue, Stanford University, 2012
------------------------------------------------------------------
problem:  variables n: 20, constraints m: 320400
cones: 	  s: psd vars: 320400, ssize: 1
settings: eps_abs: 1.0e-04, eps_rel: 1.0e-04, eps_infeas: 1.0e-07
	  alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1
	  max_iters: 1, normalize: 1, rho_x: 1.00e-06
	  acceleration_lookback: 10, acceleration_interval: 10
lin-sys:  sparse-direct-amd-qdldl
	  nnz(A): 1280310, nnz(P): 0
------------------------------------------------------------------
 iter | pri res | dua res |   gap   |   obj   |  scale  | time (s)
------------------------------------------------------------------
     0| 1.01e+01  4.69e+02  3.45e+03  1.73e+03  1.00e-01  1.31e+00 
     1| 1.01e+01  4.69e+02  3.45e+03  1.73e+03  1.00e-01  1.32e+00 
------------------------------------------------------------------
status:  solved (inaccurate - reached max_iters)
timings: total: 1.32e+00s = setup: 5.92e-01s + solve: 7.31e-01s
	 lin-sys: 1.02e-02s, cones: 6.65e-01s, accel: 3.20e-08s
------------------------------------------------------------------
objective = 1726.040603 (inaccurate)
------------------------------------------------------------------
  1.777191 seconds (3.00 k allocations: 271.931 MiB, 2.11% gc time)

Worst-case: lots of little PSD cones

Code

julia> import MathOptInterface as MOI

julia> import SCS

julia> function main(N)
           model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
           obj = zero(MOI.ScalarAffineFunction{Float64})
           MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
           for i in 1:N
               x = MOI.add_variables(model, 6)
               obj += MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, x), 0.0)
               MOI.add_constraint(
                   model, 
                   MOI.Utilities.operate(vcat, Float64, 1.0 .* x...),
                   MOI.PositiveSemidefiniteConeTriangle(6),
               )
           end
           MOI.set(model, MOI.ObjectiveFunction{typeof(obj)}(), obj)
           scs = MOI.instantiate(SCS.Optimizer; with_bridge_type = Float64)
           MOI.set(scs, MOI.Silent(), true)
           MOI.set(scs, MOI.RawOptimizerAttribute("max_iters"), 1)
           MOI.copy_to(scs, model)
           MOI.optimize!(scs)
           return
       end
main (generic function with 1 method)

This PR

julia> for N in (1, 10, 100, 1_000, 10_000)
           GC.gc()
           @time main(N)
       end
  0.001020 seconds (2.79 k allocations: 123.891 KiB)
  0.001285 seconds (4.46 k allocations: 234.672 KiB)
  0.004006 seconds (20.92 k allocations: 2.664 MiB)
  0.066691 seconds (201.96 k allocations: 104.580 MiB, 9.18% gc time)
  3.231644 seconds (2.02 M allocations: 9.050 GiB, 15.38% gc time)

master

julia> for N in (1, 10, 100, 1_000, 10_000)
           GC.gc()
           @time main(N)
       end
  0.000948 seconds (2.76 k allocations: 122.062 KiB)
  0.001238 seconds (4.30 k allocations: 225.844 KiB)
  0.003974 seconds (19.51 k allocations: 2.589 MiB)
  0.065522 seconds (187.94 k allocations: 103.846 MiB, 9.92% gc time)
  3.223676 seconds (1.88 M allocations: 9.043 GiB, 16.75% gc time)

@blegat

blegat commented Jun 6, 2023

Copy link
Copy Markdown
Member Author

Thanks for the benchmark! I hope we get a benchmark showing the opposite at some point so that it justifies doing jump-dev/MathOptInterface.jl#526 😇

@blegat
blegat merged commit 1160611 into master Jun 6, 2023
@odow
odow deleted the bl/scaled branch June 22, 2023 03:01
@odow odow mentioned this pull request Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants