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

Refactor Utilities.default_copy_to #1531

Merged
merged 8 commits into from
Aug 14, 2021
Merged

Refactor Utilities.default_copy_to #1531

merged 8 commits into from
Aug 14, 2021

Conversation

odow
Copy link
Member

@odow odow commented Aug 12, 2021

Part of #1313, using that same script with GLPK.

Now that allocate-load is gone, we can refactor a lot of Utilities.default_copy_to.

Before

julia> tinf
InferenceTimingNode: 4.981569/10.378739 on InferenceFrameInfo for Core.Compiler.Timings.ROOT() with 79 direct children

image

After

julia> tinf
InferenceTimingNode: 3.927378/8.255136 on InferenceFrameInfo for Core.Compiler.Timings.ROOT() with 61 direct children

image

Takeaways

We now have 61 entries into runtime dispatch instead of 79, and time drops from ~10 seconds to ~8 seconds.

There are 29 methods remaining where this happens:

julia> mtrigs
29-element Vector{SnoopCompile.TaggedTriggers{Method}}:
 MathOptInterface.Utilities.CachingOptimizer(model_cache::MathOptInterface.ModelLike, optimizer::MathOptInterface.AbstractOptimizer) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/cachingoptimizer.jl:89 (1 callees from 1 callers)
 get(model::MathOptInterface.Utilities.StructOfConstraints, attr::MathOptInterface.ListOfConstraintIndices{F, S}) where {F, S} in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/struct_of_constraints.jl:127 (1 callees from 1 callers)
 (::MathOptInterface.Utilities.var"#180#181")(constrs) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/struct_of_constraints.jl:110 (1 callees from 1 callers)
 (::MathOptInterface.var"#instantiate##kw")(::Any, ::typeof(MathOptInterface.instantiate), optimizer_constructor) in MathOptInterface at /Users/oscar/.julia/dev/MathOptInterface/src/instantiate.jl:117 (1 callees from 1 callers)
 broadcastvcat(f::Function, model::MathOptInterface.Utilities.ModelScalarConstraints) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/struct_of_constraints.jl:284 (1 callees from 1 callers)
 _reverse_dict(dest::MathOptInterface.Utilities.DoubleDicts.IndexDoubleDict, src::MathOptInterface.Utilities.DoubleDicts.IndexDoubleDict) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/cachingoptimizer.jl:227 (1 callees from 1 callers)
 add_variables(m::MathOptInterface.Utilities.CachingOptimizer, n) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/cachingoptimizer.jl:335 (1 callees from 1 callers)
 (::MathOptInterface.Utilities.var"#138#140")(::Any) in MathOptInterface.Utilities (1 callees from 1 callers)
 _try_constrain_variables_on_creation(dest::MathOptInterface.ModelLike, src::MathOptInterface.ModelLike, index_map::MathOptInterface.Utilities.IndexMap, ::Type{S}) where S<:MathOptInterface.AbstractScalarSet in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/copy.jl:227 (1 callees from 1 callers)
 get(model::Union{MathOptInterface.Utilities.AbstractModelLike{T}, MathOptInterface.Utilities.AbstractOptimizer{T}} where T, ::MathOptInterface.ListOfVariableAttributesSet) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/model.jl:191 (1 callees from 1 callers)
 sort!(v::AbstractVector{T} where T, lo::Integer, hi::Integer, ::Base.Sort.InsertionSortAlg, o::Base.Order.Ordering) in Base.Sort at sort.jl:527 (1 callees from 1 callers)
 broadcastvcat(f::Function, model::MathOptInterface.Utilities.ModelFunctionConstraints) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/struct_of_constraints.jl:284 (1 callees from 1 callers)
 _cost_of_bridging(arg::Tuple) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/copy.jl:409 (2 callees from 1 callers)
 pass_attributes(dest::MathOptInterface.ModelLike, src::MathOptInterface.ModelLike, copy_names::Bool, index_map::MathOptInterface.Utilities.IndexMap) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/copy.jl:21 (2 callees from 1 callers)
 iterate(f::Base.Iterators.Filter, state...) in Base.Iterators at iterators.jl:448 (2 callees from 2 callers)
 get(uf::MathOptInterface.Utilities.UniversalFallback, attr::Union{MathOptInterface.ConstraintFunction, MathOptInterface.ConstraintSet}, ci::MathOptInterface.ConstraintIndex) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/universalfallback.jl:814 (2 callees from 2 callers)
 example_diet(optimizer, bridge) in Main at /Users/oscar/Documents/JuMP/performance/auto-cache/bench.jl:4 (2 callees from 1 callers)
 findnext(testf::Function, A, start) in Base at array.jl:1850 (2 callees from 2 callers)
 constraints(model::MathOptInterface.Utilities.ModelScalarConstraints{T, var"#453#C1", var"#454#C2", var"#455#C3", var"#456#C4", var"#457#C5", var"#458#C6", var"#459#C7", var"#460#C8"}, ::Type{var"#s261"} where var"#s261"<:MathOptInterface.AbstractFunction, ::Type{var"#s260"} where var"#s260"<:MathOptInterface.LessThan{T}) where {T, var"#453#C1", var"#454#C2", var"#455#C3", var"#456#C4", var"#457#C5", var"#458#C6", var"#459#C7", var"#460#C8"} in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/struct_of_constraints.jl:304 (2 callees from 1 callers)
 constraints(model::MathOptInterface.Utilities.ModelScalarConstraints{T, var"#433#C1", var"#434#C2", var"#435#C3", var"#436#C4", var"#437#C5", var"#438#C6", var"#439#C7", var"#440#C8"}, ::Type{var"#s291"} where var"#s291"<:MathOptInterface.AbstractFunction, ::Type{var"#s290"} where var"#s290"<:MathOptInterface.EqualTo{T}) where {T, var"#433#C1", var"#434#C2", var"#435#C3", var"#436#C4", var"#437#C5", var"#438#C6", var"#439#C7", var"#440#C8"} in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/struct_of_constraints.jl:304 (2 callees from 1 callers)
 promote_typeof(x, xs...) in Base at promotion.jl:272 (2 callees from 2 callers)
 _example_diet(model) in Main at /Users/oscar/Documents/JuMP/performance/auto-cache/bench.jl:17 (3 callees from 1 callers)
 var"#pass_nonvariable_constraints_fallback#131"(filter_constraints::Union{Nothing, Function}, ::typeof(MathOptInterface.Utilities.pass_nonvariable_constraints_fallback), dest::MathOptInterface.ModelLike, src::MathOptInterface.ModelLike, index_map::MathOptInterface.Utilities.IndexMap, constraint_types) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/copy.jl:287 (3 callees from 1 callers)
 get(uf::MathOptInterface.Utilities.UniversalFallback, listattr::MathOptInterface.ListOfConstraintTypesPresent) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/universalfallback.jl:389 (3 callees from 1 callers)
 _reverse_dict(src::D) where D<:Dict in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/cachingoptimizer.jl:237 (3 callees from 1 callers)
 var"#instantiate#21"(with_bridge_type::Union{Nothing, Type}, with_names::Bool, ::typeof(MathOptInterface.instantiate), optimizer_constructor) in MathOptInterface at /Users/oscar/.julia/dev/MathOptInterface/src/instantiate.jl:117 (3 callees from 1 callers)
 var"#default_copy_to#141"(copy_names::Bool, filter_constraints::Union{Nothing, Function}, ::typeof(MathOptInterface.Utilities.default_copy_to), dest::MathOptInterface.ModelLike, src::MathOptInterface.ModelLike) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/copy.jl:463 (4 callees from 1 callers)
 _pass_constraints(dest::MathOptInterface.ModelLike, src::MathOptInterface.ModelLike, copy_names::Bool, index_map::MathOptInterface.Utilities.IndexMap, variable_constraints_not_added::Vector{Any}, filter_constraints::Union{Nothing, Function}) in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/copy.jl:336 (5 callees from 1 callers)
 get(uf::MathOptInterface.Utilities.UniversalFallback, listattr::MathOptInterface.ListOfConstraintAttributesSet{F, S}) where {F, S} in MathOptInterface.Utilities at /Users/oscar/.julia/dev/MathOptInterface/src/Utilities/universalfallback.jl:440 (6 callees from 3 callers)

@odow odow changed the title WIP: refactor Utilities.default_copy_to Refactor Utilities.default_copy_to Aug 12, 2021
test/Utilities/copy.jl Outdated Show resolved Hide resolved
@odow odow requested a review from blegat August 13, 2021 03:25
Copy link
Member

@blegat blegat left a comment

Choose a reason for hiding this comment

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

More readable indeed! There might be some solvers that were using some functions that are now underscored but we will see that when we update the solvers.

src/Utilities/copy.jl Outdated Show resolved Hide resolved
src/Utilities/copy.jl Outdated Show resolved Hide resolved
@odow odow merged commit bd22950 into master Aug 14, 2021
@odow odow deleted the od/clean-copy branch August 14, 2021 03:11
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