|
1 | 1 | using .Expose: expose |
2 | | -# TODO: This seems to be faster than the newer version using `Folds.jl` |
3 | | -# in `contract_folds.jl`, investigate why. |
4 | 2 | function contract_blocks!( |
5 | 3 | alg::Algorithm"threaded_threads", |
6 | 4 | contraction_plans, |
@@ -50,88 +48,3 @@ function contract_blocks!( |
50 | 48 | end |
51 | 49 | return nothing |
52 | 50 | end |
53 | | - |
54 | | -########################################################################### |
55 | | -# Old version |
56 | | -# TODO: DELETE, keeping around for testing/benchmarking. |
57 | | -function contract!( |
58 | | - ::Algorithm"threaded_threads", |
59 | | - R::BlockSparseTensor{ElR,NR}, |
60 | | - labelsR, |
61 | | - T1::BlockSparseTensor{ElT1,N1}, |
62 | | - labelsT1, |
63 | | - T2::BlockSparseTensor{ElT2,N2}, |
64 | | - labelsT2, |
65 | | - contraction_plan, |
66 | | -) where {ElR,ElT1,ElT2,N1,N2,NR} |
67 | | - # Sort the contraction plan by the output blocks |
68 | | - # This is to help determine which output blocks are the result |
69 | | - # of multiple contractions |
70 | | - sort!(contraction_plan; by=last) |
71 | | - |
72 | | - # Ranges of contractions to the same block |
73 | | - repeats = Vector{UnitRange{Int}}(undef, nnzblocks(R)) |
74 | | - ncontracted = 1 |
75 | | - posR = last(contraction_plan[1]) |
76 | | - posR_unique = posR |
77 | | - for n in 1:(nnzblocks(R) - 1) |
78 | | - start = ncontracted |
79 | | - while posR == posR_unique |
80 | | - ncontracted += 1 |
81 | | - posR = last(contraction_plan[ncontracted]) |
82 | | - end |
83 | | - posR_unique = posR |
84 | | - repeats[n] = start:(ncontracted - 1) |
85 | | - end |
86 | | - repeats[end] = ncontracted:length(contraction_plan) |
87 | | - |
88 | | - contraction_plan_blocks = Vector{Tuple{Tensor,Tensor,Tensor}}( |
89 | | - undef, length(contraction_plan) |
90 | | - ) |
91 | | - for ncontracted in 1:length(contraction_plan) |
92 | | - block1, block2, blockR = contraction_plan[ncontracted] |
93 | | - T1block = T1[block1] |
94 | | - T2block = T2[block2] |
95 | | - Rblock = R[blockR] |
96 | | - contraction_plan_blocks[ncontracted] = (T1block, T2block, Rblock) |
97 | | - end |
98 | | - |
99 | | - indsR = inds(R) |
100 | | - indsT1 = inds(T1) |
101 | | - indsT2 = inds(T2) |
102 | | - |
103 | | - α = one(ElR) |
104 | | - @sync for repeats_partition in |
105 | | - Iterators.partition(repeats, max(1, length(repeats) ÷ nthreads())) |
106 | | - @spawn for ncontracted_range in repeats_partition |
107 | | - # Overwrite the block since it hasn't been written to |
108 | | - # R .= α .* (T1 * T2) |
109 | | - β = zero(ElR) |
110 | | - for ncontracted in ncontracted_range |
111 | | - blockT1, blockT2, blockR = contraction_plan_blocks[ncontracted] |
112 | | - # R .= α .* (T1 * T2) .+ β .* R |
113 | | - |
114 | | - # <fermions>: |
115 | | - α = compute_alpha( |
116 | | - ElR, labelsR, blockR, indsR, labelsT1, blockT1, indsT1, labelsT2, blockT2, indsT2 |
117 | | - ) |
118 | | - |
119 | | - contract!( |
120 | | - expose(blockR), |
121 | | - labelsR, |
122 | | - expose(blockT1), |
123 | | - labelsT1, |
124 | | - expose(blockT2), |
125 | | - labelsT2, |
126 | | - α, |
127 | | - β, |
128 | | - ) |
129 | | - # Now keep adding to the block, since it has |
130 | | - # been written to |
131 | | - # R .= α .* (T1 * T2) .+ R |
132 | | - β = one(ElR) |
133 | | - end |
134 | | - end |
135 | | - end |
136 | | - return R |
137 | | -end |
0 commit comments