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

Negative signs in expressions #323

Open
joaquimg opened this issue Jan 23, 2025 · 1 comment · May be fixed by #325
Open

Negative signs in expressions #323

joaquimg opened this issue Jan 23, 2025 · 1 comment · May be fixed by #325

Comments

@joaquimg
Copy link
Member

Here is a test:

using JuMP
function test1()
    model = Model()
    N = 1_000_000
    @variable(model, x[1:N])
    GC.gc()
    GC.gc()
    @time for i in 1:5
        @expression(model, sum(-1.0 * x[i] for i in 1:N))
    end
    GC.gc()
    GC.gc()
    @time for i in 1:5
        @expression(model, sum(-x[i] for i in 1:N))
    end
    GC.gc()
    GC.gc()
    @time for i in 1:5
        @expression(model, -sum(x[i] for i in 1:N))
    end
    return
end
test1()

Here are results:

  1.144432 seconds (235 allocations: 223.388 MiB)
  3.954068 seconds (35.00 M allocations: 2.900 GiB, 10.81% gc time)
  2.423065 seconds (475 allocations: 446.777 MiB)

I am actually very surprised that the second options is the slowest.
I first write it thinking it was the right way to do the third.

I think the action here is:

Have option 2 as fast as option 1.
Option 3 is seems a bit harder to detect and work with in a first glance.

@odow odow transferred this issue from jump-dev/JuMP.jl Jan 23, 2025
@odow
Copy link
Member

odow commented Jan 23, 2025

Second case is fixable:

julia> @macroexpand @expression(model, sum(-x[i] for i in 1:3))
quote
    #= REPL[284]:1 =#
    JuMP._valid_model(model, :model)
    begin
        #= /Users/oscar/.julia/packages/JuMP/CU7H5/src/macros.jl:399 =#
        let model = model
            #= /Users/oscar/.julia/packages/JuMP/CU7H5/src/macros.jl:400 =#
            begin
                #= /Users/oscar/.julia/packages/JuMP/CU7H5/src/macros/@expression.jl:86 =#
                begin
                    #= /Users/oscar/.julia/packages/JuMP/CU7H5/src/macros.jl:264 =#
                    var"#511###405" = begin
                            #= /Users/oscar/.julia/packages/MutableArithmetics/bmgkg/src/rewrite.jl:374 =#
                            let
                                #= /Users/oscar/.julia/packages/MutableArithmetics/bmgkg/src/rewrite.jl:375 =#
                                begin
                                    #= /Users/oscar/.julia/packages/MutableArithmetics/bmgkg/src/rewrite.jl:371 =#
                                    var"#513###406" = MutableArithmetics.Zero()
                                    for i = 1:3
                                        #= /Users/oscar/.julia/packages/MutableArithmetics/bmgkg/src/rewrite_generic.jl:307 =#
                                        var"#512###407" = (MutableArithmetics.operate!!)(*, -1, x[i])
                                        var"#513###406" = (MutableArithmetics.operate!!)(MutableArithmetics.add_mul, var"#513###406", var"#512###407")
                                    end
                                end
                                #= /Users/oscar/.julia/packages/MutableArithmetics/bmgkg/src/rewrite.jl:376 =#
                                var"#513###406"
                            end
                        end
                    #= /Users/oscar/.julia/packages/JuMP/CU7H5/src/macros.jl:265 =#
                    var"#514###408" = (JuMP.flatten!)(var"#511###405")
                end
                #= /Users/oscar/.julia/packages/JuMP/CU7H5/src/macros/@expression.jl:89 =#
                JuMP._replace_zero(model, var"#514###408")
            end
        end
    end
end

@odow odow linked a pull request Jan 24, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants