|
1 |
| -struct Reduction{O <: CodeOptimizer} <: CodeOptimizer |
| 1 | +struct Reduction{S, O <: CodeOptimizer} <: CodeOptimizer |
2 | 2 | inner::O
|
3 | 3 | end
|
4 | 4 |
|
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 |
6 | 14 | inner::O
|
7 | 15 | end
|
8 | 16 |
|
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}) |
11 | 29 | # reduce graph
|
12 | 30 | 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) |
14 | 32 |
|
15 | 33 | # 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)) |
18 | 36 | 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 |
24 | 37 |
|
| 38 | + # compute ordering |
| 39 | + append!(stack, label[eincode2order(opt)]) |
25 | 40 | return stack, invperm(stack)
|
26 | 41 | end
|
27 |
| -=# |
28 | 42 |
|
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}) |
30 | 53 | # 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) |
32 | 56 |
|
33 | 57 | # 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)) |
36 | 60 | opt = optimize_code(code, sizes, alg.inner).eins
|
37 | 61 |
|
38 | 62 | # compute ordering
|
39 | 63 | append!(stack, label[eincode2order(opt)])
|
40 | 64 | return stack, invperm(stack)
|
41 | 65 | end
|
42 | 66 |
|
| 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 | + |
43 | 95 | function eincode2order(code::NestedEinsum{L}) where {L}
|
44 | 96 | elimination_order = Vector{L}()
|
45 | 97 | isleaf(code) && return elimination_order
|
|
0 commit comments