Skip to content

Commit d38695e

Browse files
committed
[NDTensors] Avoid using threadid
1 parent 323b58f commit d38695e

File tree

3 files changed

+0
-134
lines changed

3 files changed

+0
-134
lines changed

NDTensors/src/blocksparse/contract.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ function contract_blockoffsets(
4646
alg = Algorithm"sequential"()
4747
if using_threaded_blocksparse() && nthreads() > 1
4848
alg = Algorithm"threaded_threads"()
49-
# This code is a bit cleaner but slower:
50-
# alg = Algorithm"threaded_folds"()
5149
end
5250
return contract_blockoffsets(
5351
alg, boffs1, inds1, labels1, boffs2, inds2, labels2, indsR, labelsR

NDTensors/src/blocksparse/contract_folds.jl

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,3 @@
1-
# Function barrier to improve type stability,
2-
# since `Folds`/`FLoops` is not type stable:
3-
# https://discourse.julialang.org/t/type-instability-in-floop-reduction/68598
4-
function contract_blocks!(
5-
alg::Algorithm"threaded_folds",
6-
contraction_plans,
7-
boffs1,
8-
boffs2,
9-
labels1_to_labels2,
10-
labels1_to_labelsR,
11-
labels2_to_labelsR,
12-
ValNR,
13-
)
14-
if nnzblocks(boffs1) > nnzblocks(boffs2)
15-
Folds.foreach(eachnzblock(boffs1).values, ThreadedEx()) do block1
16-
for block2 in eachnzblock(boffs2)
17-
maybe_contract_blocks!(
18-
contraction_plans[threadid()],
19-
block1,
20-
block2,
21-
labels1_to_labels2,
22-
labels1_to_labelsR,
23-
labels2_to_labelsR,
24-
ValNR,
25-
)
26-
end
27-
end
28-
else
29-
Folds.foreach(eachnzblock(boffs2).values, ThreadedEx()) do block2
30-
for block1 in eachnzblock(boffs1)
31-
maybe_contract_blocks!(
32-
contraction_plans[threadid()],
33-
block1,
34-
block2,
35-
labels1_to_labels2,
36-
labels1_to_labelsR,
37-
labels2_to_labelsR,
38-
ValNR,
39-
)
40-
end
41-
end
42-
end
43-
return nothing
44-
end
45-
461
function contract!(
472
::Algorithm"threaded_folds",
483
R::BlockSparseTensor,
Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using .Expose: expose
2-
# TODO: This seems to be faster than the newer version using `Folds.jl`
3-
# in `contract_folds.jl`, investigate why.
42
function contract_blocks!(
53
alg::Algorithm"threaded_threads",
64
contraction_plans,
@@ -50,88 +48,3 @@ function contract_blocks!(
5048
end
5149
return nothing
5250
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

Comments
 (0)