@@ -97,24 +97,129 @@ function similarblocktype(::Type{A}, ::Type{TT}) where {A, TT}
9797 return Core. Compiler. return_type (similar, Tuple{A, Type{TT}, NTuple{numind (TT), Int}})
9898end
9999
100+ # blocks are stored as the type the allocator hands out, e.g. `PtrArray`-backed temporaries
101+ function allocated_blocktype (:: Type{TT} , structure, istemp:: Val , allocator) where {TT}
102+ V = eltype (SumSpaceIndices (structure))
103+ TT′ = Base. promote_op (TO. tensoralloc, Type{TT}, V, typeof (istemp), typeof (allocator))
104+ TA = AbstractTensorMap{scalartype (TT), spacetype (TT), numout (TT), numin (TT)}
105+ return typeintersect (TT′, TA)
106+ end
107+
100108function TO. tensoralloc (
101109 :: Type{BT} , structure:: TensorMapSumSpace , istemp:: Val , allocator = TO. DefaultAllocator ()
102110 ) where {BT <: AbstractBlockTensorMap }
103- C = BT (undef_blocks, structure)
104- issparse (C) && return C # don't fill up sparse blocks
105- blockallocator (V) = TO. tensoralloc (eltype (C), V, istemp, allocator)
106- map! (blockallocator, parent (C), eachspace (C))
111+ TT = eltype (BT)
112+ C = BlockTensorMap {allocated_blocktype(TT, structure, istemp, allocator)} (
113+ undef_blocks, structure
114+ )
115+ map! (V -> TO. tensoralloc (TT, V, istemp, allocator), parent (C), eachspace (C))
107116 return C
108117end
109118
119+ # sparse tensors start out empty
120+ function TO. tensoralloc (
121+ :: Type{BT} , structure:: TensorMapSumSpace , istemp:: Val , allocator = TO. DefaultAllocator ()
122+ ) where {BT <: SparseBlockTensorMap }
123+ TT = allocated_blocktype (eltype (BT), structure, istemp, allocator)
124+ return SparseBlockTensorMap {TT} (
125+ undef_blocks, structure; istemp = istemp === Val (true )
126+ )
127+ end
128+
129+ # sparse results allocate the blocks that will be written up front
130+ const BlockOrAdjoint = Union{AbstractBlockTensorMap, AdjointBlockTensorMap}
131+
132+ _tensoralloc (ttype, structure, keys, istemp:: Val , allocator) =
133+ TO. tensoralloc (ttype, structure, istemp, allocator)
134+ function _tensoralloc (
135+ ttype:: Type{<:SparseBlockTensorMap} , structure, keys, istemp:: Val , allocator
136+ )
137+ C = TO. tensoralloc (ttype, structure, istemp, allocator)
138+ foreach (I -> haskey (C, I) || (C[I] = newblock (C, I, allocator)), keys)
139+ return C
140+ end
141+
142+ function contract_keys (A, pA:: Index2Tuple , B, pB:: Index2Tuple , pAB:: Index2Tuple )
143+ OB = NTuple{length (pB[2 ]), Int}
144+ openB = Dict {NTuple{length(pB[1]), Int}, Vector{OB}} ()
145+ for IB in nonzero_keys (B)
146+ push! (get! (Vector{OB}, openB, TT. getindices (IB. I, pB[1 ])), TT. getindices (IB. I, pB[2 ]))
147+ end
148+ p = TO. linearize (pAB)
149+ keys = Set {CartesianIndex{length(p)}} ()
150+ for IA in nonzero_keys (A)
151+ oA = TT. getindices (IA. I, pA[1 ])
152+ for oB in get (openB, TT. getindices (IA. I, pA[2 ]), ())
153+ push! (keys, CartesianIndex (TT. getindices ((oA... , oB... ), p)))
154+ end
155+ end
156+ return keys
157+ end
158+
159+ function TO. tensoralloc_add (
160+ TC, A:: BlockOrAdjoint , pA:: Index2Tuple , conjA:: Bool ,
161+ istemp:: Val = Val (false ), allocator = TO. DefaultAllocator ()
162+ )
163+ ttype = TO. tensoradd_type (TC, A, pA, conjA)
164+ structure = TO. tensoradd_structure (A, pA, conjA)
165+ p = TO. linearize (pA)
166+ keys = (CartesianIndex (TT. getindices (I. I, p)) for I in nonzero_keys (A))
167+ return _tensoralloc (ttype, structure, keys, istemp, allocator)
168+ end
169+
170+ function TO. tensoralloc_contract (
171+ TC, A:: BlockOrAdjoint , pA:: Index2Tuple , conjA:: Bool ,
172+ B:: BlockOrAdjoint , pB:: Index2Tuple , conjB:: Bool ,
173+ pAB:: Index2Tuple , istemp:: Val = Val (false ), allocator = TO. DefaultAllocator ()
174+ )
175+ ttype = TO. tensorcontract_type (TC, A, pA, conjA, B, pB, conjB, pAB)
176+ structure = TO. tensorcontract_structure (A, pA, conjA, B, pB, conjB, pAB)
177+ keys = contract_keys (A, pA, B, pB, pAB)
178+ return _tensoralloc (ttype, structure, keys, istemp, allocator)
179+ end
180+
181+ # contract directly into `C` when it is a valid BLAS destination, otherwise go through a
182+ # temporary holding only the product blocks instead of a copy of all blocks of `C`
183+ function TO. tensorcontract! (
184+ C:: SparseBlockTensorMap ,
185+ A:: BlockOrAdjoint , pA:: Index2Tuple , conjA:: Bool ,
186+ B:: BlockOrAdjoint , pB:: Index2Tuple , conjB:: Bool ,
187+ pAB:: Index2Tuple , α:: Number , β:: Number , backend, allocator
188+ )
189+ ipAB = TO. oindABinC (pAB, pA, pB)
190+ if TO. isblasdestination (C, ipAB) || TO. isblasdestination (C, reverse (ipAB))
191+ for I in contract_keys (A, pA, B, pB, pAB)
192+ haskey (C, I) || (C[I] = zerovector! (newblock (C, I, allocator)))
193+ end
194+ return _tensorcontract! (C, A, pA, conjA, B, pB, conjB, pAB, α, β, backend, allocator)
195+ end
196+ N₁ = length (pA[1 ])
197+ pAB′ = (ntuple (identity, N₁), ntuple (i -> N₁ + i, length (pB[2 ])))
198+ AB = TO. tensoralloc_contract (
199+ scalartype (C), A, pA, conjA, B, pB, conjB, pAB′, Val (true ), allocator
200+ )
201+ _tensorcontract! (AB, A, pA, conjA, B, pB, conjB, pAB′, One (), Zero (), backend, allocator)
202+ TO. tensoradd! (C, AB, pAB, false , α, β, backend, allocator)
203+ TO. tensorfree! (AB, allocator)
204+ return C
205+ end
206+ function _tensorcontract! (C, A, pA, conjA, B, pB, conjB, pAB, α, β, backend, allocator)
207+ return @invoke TO. tensorcontract! (
208+ C:: AbstractTensorMap ,
209+ A:: AbstractTensorMap , pA:: Index2Tuple , conjA:: Bool ,
210+ B:: AbstractTensorMap , pB:: Index2Tuple , conjB:: Bool ,
211+ pAB:: Index2Tuple , α:: Number , β:: Number , backend:: Any , allocator:: Any
212+ )
213+ end
214+
110215# tensorfree!
111216# -----------
112217function TO. tensorfree! (t:: BlockTensorMap , allocator = TO. DefaultAllocator ())
113218 foreach (Base. Fix2 (TO. tensorfree!, allocator), parent (t))
114219 return nothing
115220end
116221function TO. tensorfree! (t:: SparseBlockTensorMap , allocator = TO. DefaultAllocator ())
117- foreach (Base. Fix2 (TO. tensorfree!, allocator), nonzero_values (t))
222+ istemp (t) && foreach (Base. Fix2 (TO. tensorfree!, allocator), nonzero_values (t))
118223 return nothing
119224end
120225
0 commit comments