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

Speed up test/operator.jl, fix bug on Julia 1.0 with Base.warn_one. #1443

Merged
merged 3 commits into from Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/JuMP.jl
Expand Up @@ -724,14 +724,16 @@ function operator_warn(::AbstractModel) end
function operator_warn(model::Model)
model.operator_counter += 1
if model.operator_counter > 20000
Base.warn_once(
Compat.@warn(
"The addition operator has been used on JuMP expressions a large " *
"number of times. This warning is safe to ignore but may " *
"indicate that model generation is slower than necessary. For " *
"performance reasons, you should not add expressions in a loop. " *
"Instead of x += y, use add_to_expression!(x,y) to modify x in " *
"place. If y is a single variable, you may also use " *
"add_to_expression!(x, coef, y) for x += coef*y.")
"add_to_expression!(x, coef, y) for x += coef*y.", maxlog=1)
IainNZ marked this conversation as resolved.
Show resolved Hide resolved
# NOTE: On Julia 1.0 (at least), maxlog=1 does not work correctly.
# See https://github.com/JuliaLang/julia/issues/28786.
end
end

Expand Down
29 changes: 20 additions & 9 deletions test/operator.jl
Expand Up @@ -318,15 +318,26 @@ function operators_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType::
@test dot(floats, anys) == 10 + 40 + 2x
end

@testset "JuMP PR #943" begin
pull943 = ModelType()
@variable(pull943, x[1 : 10^6]);
JuMP.set_start_value.(x, 1 : 10^6)
@expression(pull943, testsum, sum(x[i] * i for i = 1 : 10^6))
@expression(pull943, testdot1, dot(x, 1 : 10^6))
@expression(pull943, testdot2, dot(1 : 10^6, x))
@test JuMP.value(testsum, JuMP.start_value) ≈ JuMP.value(testdot1, JuMP.start_value)
@test JuMP.value(testsum, JuMP.start_value) ≈ JuMP.value(testdot2, JuMP.start_value)
if ModelType <: Model
# Only `Model` is guaranteed to have `operator_counter`, so
# only test for that case.
@testset "dot doesn't trigger operator_counter" begin
# Check that dot is not falling back to default, inefficient
# addition (JuMP PR #943).
model = ModelType()
@test model.operator_counter == 0
@variable(model, x[1:100])
JuMP.set_start_value.(x, 1:100)
@expression(model, test_sum, sum(x[i] * i for i in 1:100))
@expression(model, test_dot1, dot(x, 1:100))
@expression(model, test_dot2, dot(1:100, x))
@test model.operator_counter == 0
test_add = test_dot1 + test_dot2
@test model.operator_counter == 1 # Check triggerable.
test_sum_value = JuMP.value(test_sum, JuMP.start_value)
@test test_sum_value ≈ JuMP.value(test_dot1, JuMP.start_value)
@test test_sum_value ≈ JuMP.value(test_dot2, JuMP.start_value)
end
end
end
end
Expand Down