Skip to content

Commit 1048ff1

Browse files
authored
blockwise _transpose! with extra adjoint destination support and inbounds (#95)
1 parent 34a00be commit 1048ff1

2 files changed

Lines changed: 47 additions & 21 deletions

File tree

‎src/tensors/indexmanipulations.jl‎

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const BlockTensorOrAdjoint = Union{AbstractBlockTensorMap, AdjointBlockTensorMap}
2-
31
# permute!, braid!, tensoradd!, permute_twist!
42
# --------------------------------------------
53
# these all unwrap adjoints and then call `_braid!`
@@ -59,46 +57,61 @@ end
5957

6058
# transpose!
6159
# ----------
62-
function TK.transpose!(
63-
tdst::BlockTensorMap, tsrc::BlockTensorMap, p::Index2Tuple,
64-
α::Number, β::Number, backend::AbstractBackend, allocator
60+
# this unwraps adjoints and then calls `_transpose!`
61+
62+
@propagate_inbounds function _transpose_block!(tdst, tsrc, p, conjsrc, α, β, backend, allocator)
63+
tsrc′, p′, _, conjsrc′ = TK._unwrap_source(tsrc, p, nothing, conjsrc)
64+
return TK._transpose!(tdst, tsrc′, p′, conjsrc′, α, β, backend, allocator)
65+
end
66+
# no `twistmask` here, so the blocks of a `tdst'` can be unwrapped too, returning the block itself
67+
@propagate_inbounds function _transpose_block!(
68+
tdst::AdjointTensorMap, tsrc, p, conjsrc, α, β, backend, allocator
6569
)
66-
@boundscheck TK.spacecheck_transform(TK.transpose, space(tdst), space(tsrc), p)
70+
tdst′, p′, conjsrc′, α′, β′ = TK._unwrap_destination(tdst, p, conjsrc, α, β)
71+
_transpose_block!(tdst′, tsrc, p′, conjsrc′, α′, β′, backend, allocator)
72+
return tdst
73+
end
6774

75+
@propagate_inbounds function TK._transpose!(
76+
tdst::BlockTensorMap, tsrc::BlockTensorMap, p::Index2Tuple, conjsrc::Bool,
77+
α, β, backend, allocator
78+
)
79+
@boundscheck TK.spacecheck_transform(TK.permute, space(tdst), space(tsrc), p, conjsrc)
6880
dstdata = parent(tdst)
6981
srcdata = permutedims(StridedView(parent(tsrc)), (p[1]..., p[2]...))
70-
7182
@inbounds for I in eachindex(dstdata, srcdata)
72-
dstdata[I] = TK.transpose!(dstdata[I], srcdata[I], p, α, β, backend, allocator)
83+
dstdata[I] = _transpose_block!(dstdata[I], srcdata[I], p, conjsrc, α, β, backend, allocator)
7384
end
7485
return tdst
7586
end
76-
function TK.transpose!(
77-
tdst::AbstractBlockTensorMap, tsrc::BlockTensorOrAdjoint,
78-
p::Index2Tuple, α::Number, β::Number, backend::AbstractBackend, allocator
87+
@propagate_inbounds function TK._transpose!(
88+
tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap, p::Index2Tuple, conjsrc::Bool,
89+
α, β, backend, allocator
7990
)
80-
@boundscheck TK.spacecheck_transform(TK.transpose, space(tdst), space(tsrc), p)
91+
@boundscheck TK.spacecheck_transform(TK.permute, space(tdst), space(tsrc), p, conjsrc)
8192
p_lin = (p[1]..., p[2]...)
8293
_scale_untouched!(tdst, tsrc, β, p_lin)
8394
@inbounds for (I, v) in nonzero_pairs(tsrc)
8495
I′ = CartesianIndex(TT.getindices(I.I, p_lin))
8596
_addblock!(tdst, I′, β, allocator) do c, γ
86-
TK.transpose!(c, v, p, α, γ, backend, allocator)
97+
# `@inbounds` doesn't reach into this closure
98+
@inbounds _transpose_block!(c, v, p, conjsrc, α, γ, backend, allocator)
8799
end
88100
end
89101
return tdst
90102
end
91-
function TK.transpose!(
92-
tdst::TensorMap, tsrc::BlockTensorMap, p::Index2Tuple,
93-
α::Number, β::Number, backend::AbstractBackend, allocator
103+
# a block tensor holding a single block is interchangeable with that block
104+
@propagate_inbounds function TK._transpose!(
105+
tdst::TensorMap, tsrc::BlockTensorMap, p::Index2Tuple, conjsrc::Bool,
106+
α, β, backend, allocator
94107
)
95-
return TK.transpose!(tdst, only(tsrc), p, α, β, backend, allocator)
108+
return _transpose_block!(tdst, only(tsrc), p, conjsrc, α, β, backend, allocator)
96109
end
97-
function TK.transpose!(
98-
tdst::BlockTensorMap, tsrc::TensorMap, p::Index2Tuple,
99-
α::Number, β::Number, backend::AbstractBackend, allocator
110+
@propagate_inbounds function TK._transpose!(
111+
tdst::BlockTensorMap, tsrc::TensorMap, p::Index2Tuple, conjsrc::Bool,
112+
α, β, backend, allocator
100113
)
101-
TK.transpose!(only(tdst), tsrc, p, α, β, backend, allocator)
114+
TK._transpose!(only(tdst), tsrc, p, conjsrc, α, β, backend, allocator)
102115
return tdst
103116
end
104117

‎test/linalg/indexmanipulations.jl‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ transform!(f, C, A, p, α, β) = f === braid! ? f(C, A, p, (1, 2, 3, 4, 5), α,
4141
@test dense(transform!(f, C, A, p, One(), Zero())) ≈ transform!(f, zerovector(dense(C)), Ad, p, One(), Zero())
4242
end
4343

44+
# TensorKit unwraps a destination wrapped as `AdjointTensorMap` before calling the kernels
45+
@testset "$f into AdjointTensorMap ($(sectortype(V[1])), sparse = $sparse)" for V in (Vtr, Vf),
46+
f in (permute!, braid!, transpose!), sparse in (false, true)
47+
T = ComplexF64
48+
A = maketensor(sparse, T, V[1] ⊗ V[2] ⊗ V[3] ← V[4] ⊗ V[5])
49+
p = perms(f)
50+
D = maketensor(sparse, T, permute(space(A), p)')
51+
α, β = randn(T), randn(T)
52+
Cd = transform!(f, TensorKit.AdjointTensorMap(dense(D)), dense(A), p, α, β)
53+
C = transform!(f, TensorKit.AdjointTensorMap(D), A, p, α, β)
54+
@test dense(parent(C)) ≈ parent(Cd)
55+
end
56+
4457
# `twist` and `twist!(tdst, tsrc, ...)` go through `_twist!`, unlike the in-place `twist!(t, inds)`
4558
@testset "twist ($(sectortype(V[1])), C sparse = $Csparse, A sparse = $Asparse, adjoint = $adj)" for V in (Vtr, Vf),
4659
Csparse in (false, true), Asparse in (false, true), adj in (false, true)

0 commit comments

Comments
 (0)