Skip to content

Commit bbf87a7

Browse files
committed
various changes
1 parent f140479 commit bbf87a7

File tree

3 files changed

+72
-20
lines changed

3 files changed

+72
-20
lines changed

src/OMEinsumContractionOrders.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import CliqueTrees
1313
using CliqueTrees: cliquetree, residual, EliminationAlgorithm, MMW, BFS, MCS, LexBFS, RCMMD, RCMGL, MCSM, LexM, AMF, MF, MMD, MF, BT, SafeRules
1414

1515
export CodeOptimizer, CodeSimplifier,
16-
KaHyParBipartite, GreedyMethod, TreeSA, SABipartite, Treewidth, ExactTreewidth,
16+
KaHyParBipartite, GreedyMethod, TreeSA, SABipartite, Treewidth, ExactTreewidth, Reduction
1717
MergeGreedy, MergeVectors,
1818
uniformsize,
1919
simplify_code, optimize_code, optimize_permute,

src/interfaces.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function _optimize_code(code, size_dict, optimizer::TreeSA)
5555
sc_weight=optimizer.sc_weight, rw_weight=optimizer.rw_weight, initializer=optimizer.initializer,
5656
greedy_method=optimizer.greedy_config, fixed_slices=optimizer.fixed_slices)
5757
end
58-
function _optimize_code(code, size_dict, optimizer::Reduction)
59-
_optimizer = Treewidth(; alg=ReductionEA(optimizer.inner))
58+
function _optimize_code(code, size_dict, optimizer::Reduction{S}) where {S}
59+
_optimizer = Treewidth(; alg=ReductionEA{S}(optimizer.inner))
6060
_optimize_code(code, size_dict, _optimizer)
6161
end

src/reduction.jl

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,97 @@
1-
struct Reduction{O <: CodeOptimizer} <: CodeOptimizer
1+
struct Reduction{S, O <: CodeOptimizer} <: CodeOptimizer
22
inner::O
33
end
44

5-
struct ReductionEA{O <: CodeOptimizer} <: CliqueTrees.EliminationAlgorithm
5+
function Reduction{S}(inner::O) where {O <: CodeOptimizer}
6+
return Reduction{S, O}(inner)
7+
end
8+
9+
function Reduction(inner::CodeOptimizer)
10+
return Reduction{3}(inner)
11+
end
12+
13+
struct ReductionEA{S, O <: CodeOptimizer} <: CliqueTrees.EliminationAlgorithm
614
inner::O
715
end
816

9-
#=
10-
function CliqueTrees.permutation(weights::AbstractVector, graph, alg::ReductionEA)
17+
function ReductionEA{S}(inner::O) where {O <: CodeOptimizer}
18+
return ReductionEA{S, O}(inner)
19+
end
20+
21+
# rules applied:
22+
# - islet
23+
# - twig
24+
# - series
25+
# - triangle
26+
# - buddy
27+
# - cube
28+
function CliqueTrees.permutation(weights::AbstractVector, graph, alg::ReductionEA{1})
1129
# reduce graph
1230
width = CliqueTrees.lowerbound(weights, graph)
13-
weights, graph, stack, index, width = CliqueTrees.saferules(weights, graph, width)
31+
graph, stack, label, width = CliqueTrees.pr3(weights, graph, width)
1432

1533
# feed reduced graph back to OMEinsumContractionOrders
16-
code = EinCode(maximal_cliques(CliqueTrees.Graph(graph)), Int[])
17-
sizes = Dict{Int, Int}(v => round(Int, 2^weights[v]) for v in CliqueTrees.vertices(graph))
34+
code = EinCode(maximal_cliques(graph), Int[])
35+
sizes = Dict{Int, Int}(v => round(Int, 2^weights[label[v]]) for v in CliqueTrees.vertices(graph))
1836
opt = optimize_code(code, sizes, alg.inner).eins
19-
20-
# compute ordering
21-
for v in eincode2order(opt)
22-
append!(stack, CliqueTrees.neighbors(index, v))
23-
end
2437

38+
# compute ordering
39+
append!(stack, label[eincode2order(opt)])
2540
return stack, invperm(stack)
2641
end
27-
=#
2842

29-
function CliqueTrees.permutation(weights::AbstractVector, graph, alg::ReductionEA)
43+
# rules applied:
44+
# - islet
45+
# - twig
46+
# - series
47+
# - triangle
48+
# - buddy
49+
# - cube
50+
# - simplicial
51+
# - almost-simplicial
52+
function CliqueTrees.permutation(weights::AbstractVector, graph, alg::ReductionEA{2})
3053
# reduce graph
31-
kernel, stack, label, width = CliqueTrees.pr4(graph, CliqueTrees.lowerbound(graph))
54+
width = CliqueTrees.lowerbound(weights, graph)
55+
graph, stack, label, width = CliqueTrees.pr4(graph, weights)
3256

3357
# feed reduced graph back to OMEinsumContractionOrders
34-
code = EinCode(maximal_cliques(kernel), Int[])
35-
sizes = Dict{Int, Int}(v => round(Int, 2^weights[label[v]]) for v in CliqueTrees.vertices(kernel))
58+
code = EinCode(maximal_cliques(graph), Int[])
59+
sizes = Dict{Int, Int}(v => round(Int, 2^weights[label[v]]) for v in CliqueTrees.vertices(graph))
3660
opt = optimize_code(code, sizes, alg.inner).eins
3761

3862
# compute ordering
3963
append!(stack, label[eincode2order(opt)])
4064
return stack, invperm(stack)
4165
end
4266

67+
# rules applied:
68+
# - islet
69+
# - twig
70+
# - series
71+
# - triangle
72+
# - buddy
73+
# - cube
74+
# - simplicial
75+
# - almost-simplicial
76+
# - indistinguishable neighbors
77+
function CliqueTrees.permutation(weights::AbstractVector, graph, alg::ReductionEA{3})
78+
# reduce graph
79+
width = CliqueTrees.lowerbound(weights, graph)
80+
weights, graph, stack, index, width = CliqueTrees.saferules(weights, graph, width)
81+
82+
# feed reduced graph back to OMEinsumContractionOrders
83+
code = EinCode(maximal_cliques(CliqueTrees.Graph(graph)), Int[])
84+
sizes = Dict{Int, Int}(v => round(Int, 2^weights[v]) for v in CliqueTrees.vertices(graph))
85+
opt = optimize_code(code, sizes, alg.inner).eins
86+
87+
# compute ordering
88+
for v in eincode2order(opt)
89+
append!(stack, CliqueTrees.neighbors(index, v))
90+
end
91+
92+
return stack, invperm(stack)
93+
end
94+
4395
function eincode2order(code::NestedEinsum{L}) where {L}
4496
elimination_order = Vector{L}()
4597
isleaf(code) && return elimination_order

0 commit comments

Comments
 (0)