Skip to content

Commit 11295de

Browse files
authored
Cache common subexpressions in IndexMap (#2802)
* Cache common subexpressions in IndexMap * Address review: tests, benchmark and experimental comment * Document that the `nl_cache` field is experimental and must only be accessed through `map_indices`. * Use an `IdDict` since common subexpressions are identified by object identity. * Skip the cache when the variable map is the identity (rebased on #3118). * Also use the cache for `VectorNonlinearFunction`. * Keep the `IndexMap(var_map, con_map)` constructor. * Add tests. Benchmark of `map_indices` on `f_{k+1} = sin(f_k) * f_k + f_k` with `f_0 = sin(x)`, whose tree has O(3^n) nodes but whose DAG has O(n) nodes, comparing `map_indices(Base.Fix1(getindex, map), f)` (no cache) with `map_indices(map, f)` (cache): | n | no cache (μs) | cache (μs) | no cache (bytes) | cache (bytes) | |----|---------------|------------|------------------|---------------| | 1 | 0.136 | 0.322 | 896 | 1120 | | 4 | 4.1 | 1.15 | 25008 | 3728 | | 8 | 349 | 2.88 | 1994928 | 7216 | | 12 | 35900 | 5.97 | 161559936 | 12736 | * Fix Vale wording and Julia nightly display assertion Replace the Latin abbreviation rejected by Vale. Build the unary-plus expectation from an Expr so the NL reader test follows Julia's display syntax across versions. Validation: test/FileFormats/NL/test_read.jl passes on Julia 1.13.3 and installed nightly 1.15.0-DEV.244; coverage markers confirm the new lines execute. Vale reports no errors on the changed files. Repository-wide JuliaFormatter 2.14.7 leaves no unstaged diff.
1 parent ccef35f commit 11295de

4 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎src/Utilities/copy/index_map.jl‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ struct IndexMap <: AbstractDict{MOI.Index,MOI.Index}
2020
typeof(CleverDicts.index_to_key),
2121
}
2222
con_map::DoubleDicts.IndexDoubleDict
23+
# This field is experimental and may be removed in a future release. Do not
24+
# access it directly; use `map_indices` instead.
25+
nl_cache::IdDict{MOI.ScalarNonlinearFunction,MOI.ScalarNonlinearFunction}
2326
end
2427

2528
"""
@@ -33,6 +36,11 @@ function IndexMap()
3336
return IndexMap(var_map, con_map)
3437
end
3538

39+
function IndexMap(var_map, con_map)
40+
nl_cache = IdDict{MOI.ScalarNonlinearFunction,MOI.ScalarNonlinearFunction}()
41+
return IndexMap(var_map, con_map, nl_cache)
42+
end
43+
3644
function _identity_constraints_map(
3745
model,
3846
map::DoubleDicts.IndexDoubleDictInner{F,S},
@@ -121,3 +129,21 @@ function map_indices(map::IndexMap, x::X)::X where {X<:_OnlyVariableIndices}
121129
end
122130
return map_indices(Base.Fix1(getindex, map), x)
123131
end
132+
133+
# Common subexpressions, that is, `MOI.ScalarNonlinearFunction` objects that are
134+
# shared between functions, are mapped only once, and the mapped functions
135+
# share the corresponding mapped subexpressions.
136+
function map_indices(map::IndexMap, f::MOI.ScalarNonlinearFunction)
137+
if CleverDicts.is_identity(map.var_map)
138+
return f
139+
end
140+
return map_indices(Base.Fix1(getindex, map), f, map.nl_cache)
141+
end
142+
143+
function map_indices(map::IndexMap, f::MOI.VectorNonlinearFunction)
144+
if CleverDicts.is_identity(map.var_map)
145+
return f
146+
end
147+
rows = MOI.ScalarNonlinearFunction[map_indices(map, row) for row in f.rows]
148+
return MOI.VectorNonlinearFunction(rows)
149+
end

‎src/Utilities/functions.jl‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,11 @@ end
367367
function map_indices(
368368
index_map::F,
369369
f::MOI.ScalarNonlinearFunction,
370+
nl_cache = nothing,
370371
) where {F<:Function}
372+
if !isnothing(nl_cache) && haskey(nl_cache, f)
373+
return nl_cache[f]
374+
end
371375
root = MOI.ScalarNonlinearFunction(f.head, similar(f.args))
372376
stack = Tuple{MOI.ScalarNonlinearFunction,Int,MOI.ScalarNonlinearFunction}[]
373377
for (i, fi) in enumerate(f.args)
@@ -380,6 +384,10 @@ function map_indices(
380384
while !isempty(stack)
381385
parent, i, arg = pop!(stack)
382386
if arg isa MOI.ScalarNonlinearFunction
387+
if !isnothing(nl_cache) && haskey(nl_cache, arg)
388+
parent.args[i] = nl_cache[arg]
389+
continue
390+
end
383391
child = MOI.ScalarNonlinearFunction(arg.head, similar(arg.args))
384392
for (j, argj) in enumerate(arg.args)
385393
if argj isa MOI.ScalarNonlinearFunction
@@ -389,10 +397,16 @@ function map_indices(
389397
end
390398
end
391399
parent.args[i] = child
400+
if !isnothing(nl_cache)
401+
nl_cache[arg] = child
402+
end
392403
else
393404
parent.args[i] = MOI.Utilities.map_indices(index_map, arg)
394405
end
395406
end
407+
if !isnothing(nl_cache)
408+
nl_cache[f] = root
409+
end
396410
return root
397411
end
398412

‎test/FileFormats/NL/test_read.jl‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,10 @@ function test_hs071()
648648
end
649649
model_print = sprint(print, model)
650650
_in_ = @static Sys.iswindows() ? "in" : "∈"
651+
# Julia versions differ in how they print unary operators.
652+
unary_plus = Expr(:call, :+, 2.0)
651653
for line in [
652-
"(1.1 * v[1] + 1.4 * v[2] + 1.2 * v[3] + 1.3 * v[4]) + +2.0",
654+
"(1.1 * v[1] + 1.4 * v[2] + 1.2 * v[3] + 1.3 * v[4]) + $unary_plus",
653655
"v[1] >= 1.1",
654656
"v[2] >= 1.4",
655657
"v[3] >= 1.2",

‎test/Utilities/test_copy.jl‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,40 @@ function test_map_indices_identity_index_map()
11441144
return
11451145
end
11461146

1147+
function test_map_indices_common_subexpressions()
1148+
x = MOI.VariableIndex.(1:2)
1149+
index_map = MOI.Utilities.IndexMap()
1150+
index_map[x[1]] = x[2]
1151+
index_map[x[2]] = x[1]
1152+
sin_x = MOI.ScalarNonlinearFunction(:sin, Any[x[1]])
1153+
f = MOI.ScalarNonlinearFunction(:+, Any[sin_x, sin_x, x[2]])
1154+
g = MOI.ScalarNonlinearFunction(:*, Any[2.0, sin_x])
1155+
f_dest = MOI.Utilities.map_indices(index_map, f)
1156+
@test f_dest ≈ MOI.ScalarNonlinearFunction(
1157+
:+,
1158+
Any[
1159+
MOI.ScalarNonlinearFunction(:sin, Any[x[2]]),
1160+
MOI.ScalarNonlinearFunction(:sin, Any[x[2]]),
1161+
x[1],
1162+
],
1163+
)
1164+
@test f_dest.args[1] === f_dest.args[2]
1165+
g_dest = MOI.Utilities.map_indices(index_map, g)
1166+
@test g_dest.args[2] === f_dest.args[1]
1167+
@test MOI.Utilities.map_indices(index_map, f) === f_dest
1168+
h = MOI.VectorNonlinearFunction([f, g])
1169+
h_dest = MOI.Utilities.map_indices(index_map, h)
1170+
@test h_dest.rows[1] === f_dest
1171+
@test h_dest.rows[2] === g_dest
1172+
# The cache is not used if the variable map is the identity.
1173+
identity_map = MOI.Utilities.IndexMap()
1174+
identity_map[x[1]] = x[1]
1175+
identity_map[x[2]] = x[2]
1176+
@test MOI.Utilities.map_indices(identity_map, f) === f
1177+
@test MOI.Utilities.map_indices(identity_map, h) === h
1178+
return
1179+
end
1180+
11471181
end # module
11481182

11491183
TestCopy.runtests()

0 commit comments

Comments
 (0)