Skip to content

Commit 8b82dac

Browse files
committed
rework buffer interaction
1 parent d9a989b commit 8b82dac

2 files changed

Lines changed: 8 additions & 36 deletions

File tree

‎src/tensors/indexmanipulations.jl‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,6 @@ function add_transform_kernel!(
592592
# Non-Abelian fusion: trees sharing the same set of uncoupled (external) sectors
593593
# form a *fusion block* and mix under the transformation via a recoupling matrix U
594594
# (rows = destination trees, columns = source trees). We iterate over blocks.
595-
tl_buffers = OhMyThreads.TaskLocalValue(() -> allocate_buffers(tdst, tsrc, transformer, allocator))
596595
tforeach(fusionblocks(tsrc); scheduler) do src
597596
dst, U = transformer(src)
598597
if length(src) == 1
@@ -611,26 +610,27 @@ function add_transform_kernel!(
611610
# 2. Recoupling: buffer_dst = buffer_src * U^T (blocksize × rows)
612611
# 3. Insert: scatter columns of buffer_dst to destination blocks,
613612
# applying the actual permutation p in the same step.
614-
buffer1, buffer2 = tl_buffers[]
615613
rows, cols = size(U)
616614
sz_src = size(tsrc[first(fusiontrees(src))...])
617615
blocksize = prod(sz_src)
618616
ptriv = (ntuple(identity, length(sz_src)), ())
619-
buffer_src = StridedView(buffer2, (blocksize, cols), (1, blocksize), 0)
617+
buffer = TO.tensoralloc(storagetype(tdst), blocksize * (rows + cols), Val(true), allocator)
618+
buffer_dst = StridedView(buffer, (blocksize, rows), (1, blocksize), 0)
619+
buffer_src = StridedView(buffer, (blocksize, cols), (1, blocksize), blocksize * rows)
620620
@inbounds for (i, (f₁, f₂)) in enumerate(fusiontrees(src))
621621
TO.tensoradd!(
622622
sreshape(buffer_src[:, i], sz_src), tsrc[f₁, f₂],
623623
ptriv, false, One(), Zero(), backend, allocator
624624
)
625625
end
626-
buffer_dst = StridedView(buffer1, (blocksize, rows), (1, blocksize), 0)
627626
mul!(buffer_dst, buffer_src, transpose(StridedView(U)))
628627
@inbounds for (i, (f₃, f₄)) in enumerate(fusiontrees(dst))
629628
TO.tensoradd!(
630629
tdst[f₃, f₄], sreshape(buffer_dst[:, i], sz_src),
631630
p, false, α, β, backend, allocator
632631
)
633632
end
633+
TO.tensorfree!(buffer, allocator)
634634
end
635635
end
636636
end
@@ -663,7 +663,6 @@ function add_transform_kernel!(
663663
# U — recoupling matrix (rows = dst trees, cols = src trees)
664664
# sz_{dst,src} — array shape of each block (same for all trees in the block)
665665
# structs_{dst,src}[i] — (offset, strides) into the flat data vector for tree i
666-
tl_buffers = OhMyThreads.TaskLocalValue(() -> allocate_buffers(data_dst, data_src, transformer, allocator))
667666
tforeach(transformer.data; scheduler) do (U, (sz_dst, structs_dst), (sz_src, structs_src))
668667
if length(U) == 1
669668
# Degenerate block with a single tree: no matmul needed.
@@ -676,14 +675,15 @@ function add_transform_kernel!(
676675
else
677676
# Multi-tree block: pack → recoupling matmul → unpack.
678677
# buffer2 = source staging area, buffer1 = destination staging area.
679-
buffer1, buffer2 = tl_buffers[]
680678
rows, cols = size(U)
681679
blocksize = prod(sz_src)
682680
ptriv = (ntuple(identity, length(sz_src)), ())
681+
buffer = TO.tensoralloc(typeof(data_dst), blocksize * (rows + cols), Val(true), allocator)
682+
buffer_dst = StridedView(buffer, (blocksize, rows), (1, blocksize), 0)
683+
buffer_src = StridedView(buffer, (blocksize, cols), (1, blocksize), blocksize * rows)
683684

684685
# 1. Extract: copy each source block into column i of buffer_src as a flat vector,
685686
# using a trivial permutation so the layout is canonical before the matmul.
686-
buffer_src = StridedView(buffer2, (blocksize, cols), (1, blocksize), 0)
687687
@inbounds for (i, struct_src_i) in enumerate(structs_src)
688688
TO.tensoradd!(
689689
sreshape(buffer_src[:, i], sz_src), StridedView(data_src, sz_src, struct_src_i...),
@@ -693,7 +693,6 @@ function add_transform_kernel!(
693693

694694
# 2. Recoupling: buffer_dst = buffer_src * U^T (each output tree is a linear
695695
# combination of input trees weighted by the recoupling coefficients).
696-
buffer_dst = StridedView(buffer1, (blocksize, rows), (1, blocksize), 0)
697696
mul!(buffer_dst, buffer_src, transpose(StridedView(U)))
698697

699698
# 3. Insert: scatter column i of buffer_dst into the destination, applying the
@@ -704,6 +703,7 @@ function add_transform_kernel!(
704703
p, false, α, β, backend, allocator
705704
)
706705
end
706+
TO.tensorfree!(buffer, allocator)
707707
end
708708
end
709709
return nothing

‎src/tensors/treetransformers.jl‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -128,34 +128,6 @@ function repack_transformer_structure(structures::Dictionary, trees)
128128
return sz, strides_offsets
129129
end
130130

131-
function buffersize(transformer::GenericTreeTransformer)
132-
return maximum(transformer.data; init = 0) do (basistransform, structures_dst, _)
133-
return prod(structures_dst[1]) * size(basistransform, 1)
134-
end
135-
end
136-
137-
function allocate_buffers(
138-
tdst::TensorMap, tsrc::TensorMap, transformer::GenericTreeTransformer,
139-
allocator = TO.DefaultAllocator()
140-
)
141-
sz = buffersize(transformer)
142-
return similar(tdst.data, sz), similar(tsrc.data, sz)
143-
end
144-
function allocate_buffers(
145-
data_dst::DenseVector, data_src::DenseVector, transformer::GenericTreeTransformer,
146-
allocator = TO.DefaultAllocator()
147-
)
148-
sz = buffersize(transformer)
149-
return similar(data_dst, sz), similar(data_src, sz)
150-
end
151-
function allocate_buffers(
152-
tdst::AbstractTensorMap, tsrc::AbstractTensorMap, transformer,
153-
allocator = TO.DefaultAllocator()
154-
)
155-
# be pessimistic and assume the worst for now
156-
sz = dim(space(tsrc))
157-
return similar(storagetype(tdst), sz), similar(storagetype(tsrc), sz)
158-
end
159131

160132
function treetransformertype(Vdst, Vsrc)
161133
I = sectortype(Vdst)

0 commit comments

Comments
 (0)