Skip to content

Commit fe37211

Browse files
committed
Incremental fixes for MPSKit
1 parent 5a66126 commit fe37211

6 files changed

Lines changed: 28 additions & 8 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ MatrixAlgebraKit = "0.6"
3636
Random = "1"
3737
SafeTestsets = "0.1"
3838
Strided = "2.3.3"
39-
TensorKit = "0.16.4"
39+
TensorKit = "0.17"
4040
TensorOperations = "5"
4141
Test = "1"
4242
TestExtras = "0.2, 0.3"

‎ext/BlockTensorKitGPUArraysExt.jl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,15 @@ function KernelAbstractions.get_backend(BA::BlockArrays.BlockArray{T, N, A}) whe
88
return KernelAbstractions.get_backend(first(BA.blocks))
99
end
1010

11+
function Base.copyto!(dest::BM, src::TA) where {T <: Number, TA <: AnyGPUMatrix{T}, BM <: BlockMatrix{T, Matrix{TA}}}
12+
# TODO -- should we use Threads here to parallelize these
13+
# transfers in streams if possible?
14+
for block_index in Iterators.product(blockaxes(dest)...)
15+
indices = getindex.(axes(dest), block_index)
16+
dest_view = @view dest[block_index...]
17+
dest_view .= src[indices...]
18+
end
19+
return dest
20+
end
21+
1122
end

‎src/linalg/factorizations.jl‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ for f! in (
2525
)
2626
@eval function MAK.$f!(t::AbstractBlockTensorMap, F, alg::AbstractAlgorithm)
2727
TensorKit.foreachblock(t, F...) do _, (tblock, Fblocks...)
28-
dense_block = similar_dense(tblock)
29-
Fblocks′ = MAK.$f!(copy_dense!(dense_block, tblock), alg)
28+
Fblocks′ = MAK.$f!(copy_dense!(similar_dense(tblock), tblock), alg)
3029
# deal with the case where the output is not in-place
3130
for (b′, b) in zip(Fblocks′, Fblocks)
32-
b === b′ || copy!(b, b′)
31+
b === b′ || copyto!(b, b′)
3332
end
3433
return nothing
3534
end
@@ -45,10 +44,9 @@ for f! in (
4544
)
4645
@eval function MAK.$f!(t::AbstractBlockTensorMap, N, alg::AbstractAlgorithm)
4746
TensorKit.foreachblock(t, N) do _, (tblock, Nblock)
48-
dense_block = similar_dense(tblock)
49-
Nblock′ = MAK.$f!(copy_dense!(dense_block, tblock), alg)
47+
Nblock′ = MAK.$f!(copy_dense!(similar_dense(tblock), tblock), alg)
5048
# deal with the case where the output is not the same as the input
51-
Nblock === Nblock′ || copy!(Nblock, Nblock′)
49+
Nblock === Nblock′ || copyto!(Nblock, Nblock′)
5250
return nothing
5351
end
5452
return N

‎src/tensors/blocktensor.jl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ end
161161
Base.eltype(::Type{<:BlockTensorMap{TT}}) where {TT} = TT
162162
Base.parent(t::BlockTensorMap) = t.data
163163

164+
# handle this separately because the storagetype of `AbstractTensorMap` is
165+
# *always* Vector no matter the actual data storage type
166+
TK.storagetype(t::BlockTensorMap{AbstractTensorMap{E, S, N₁, N₂}}) where {E, S, N₁, N₂} = TK.promote_storagetype(values(t.data)...)
167+
164168
function Base.copyto!(
165169
dest::BlockTensorMap, Rdest::CartesianIndices,
166170
src::BlockTensorMap, Rsrc::CartesianIndices,

‎src/tensors/sparseblocktensor.jl‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ VI.scalartype(::Type{<:SparseBlockTensorMap{TT}}) where {TT} = scalartype(TT)
168168
Base.parent(t::SparseBlockTensorMap) = SparseTensorArray(t.data, space(t))
169169
Base.eltype(::Type{<:SparseBlockTensorMap{TT}}) where {TT} = TT
170170

171+
# handle this separately because the storagetype of `AbstractTensorMap` is
172+
# *always* Vector no matter the actual data storage type
173+
TK.storagetype(t::SparseBlockTensorMap{AbstractTensorMap{E, S, N₁, N₂}}) where {E, S, N₁, N₂} = TK.promote_storagetype(values(t.data)...)
174+
171175
issparse(::SparseBlockTensorMap) = true
172176
nonzero_keys(t::SparseBlockTensorMap) = keys(t.data)
173177
nonzero_values(t::SparseBlockTensorMap) = values(t.data)

‎src/tensors/tensoroperations.jl‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ for TTA in (:AbstractTensorMap, :AbstractBlockTensorMap), TTB in (:AbstractTenso
3131
) where {N₁, N₂}
3232
S = TK.check_spacetype(A, B)
3333
TC′ = TK.promote_permute(TC, sectortype(S))
34-
M = TK.promote_storagetype(TK.similarstoragetype(A, TC′), TK.similarstoragetype(B, TC′))
34+
# explicitly compute storagetype here to work around eltype of AbstractTensorMap
35+
MA = TK.similarstoragetype(TK.storagetype(A), TC′)
36+
MB = TK.similarstoragetype(TK.storagetype(B), TC′)
37+
M = TK.promote_storagetype(MA, MB)
3538
return if issparse(A) && issparse(B)
3639
sparseblocktensormaptype(S, N₁, N₂, M)
3740
else

0 commit comments

Comments
 (0)