From 2cea761a1fa2763b1c71f2ea4add6932f116dccd Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Thu, 13 Feb 2025 07:53:17 -0500 Subject: [PATCH 1/4] Simplify show implementation --- .../wrappedabstractblocksparsearray.jl | 64 +++---------------- test/test_basics.jl | 4 +- 2 files changed, 12 insertions(+), 56 deletions(-) diff --git a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl index 9ce0bd2a..0217aaa2 100644 --- a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl @@ -337,60 +337,16 @@ function Base.Array(a::AnyAbstractBlockSparseArray) return Array{eltype(a)}(a) end -using SparseArraysBase: ReplacedUnstoredSparseArray - -# Wraps a block sparse array but replaces the unstored values. -# This is used in printing in order to customize printing -# of zero/unstored values. -struct ReplacedUnstoredBlockSparseArray{T,N,F,Parent<:AbstractArray{T,N}} <: - AbstractBlockSparseArray{T,N} - parent::Parent - getunstoredblock::F -end -Base.parent(a::ReplacedUnstoredBlockSparseArray) = a.parent -Base.axes(a::ReplacedUnstoredBlockSparseArray) = axes(parent(a)) -function BlockArrays.blocks(a::ReplacedUnstoredBlockSparseArray) - return ReplacedUnstoredSparseArray(blocks(parent(a)), a.getunstoredblock) -end - -# This is copied from `SparseArraysBase.jl` since it is not part -# of the public interface. -# Like `Char` but prints without quotes. -struct UnquotedChar <: AbstractChar - char::Char +function SparseArraysBase.isstored( + A::AnyAbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N} +) where {N} + bI = BlockIndex(findblockindex.(axes(A), I)) + bA = blocks(A) + return isstored(bA, bI.I...) && isstored(bA[bI.I...], bI.α...) end -Base.show(io::IO, c::UnquotedChar) = print(io, c.char) -Base.show(io::IO, ::MIME"text/plain", c::UnquotedChar) = show(io, c) -using FillArrays: Fill -struct GetUnstoredBlockShow{Axes} - axes::Axes -end -@inline function (f::GetUnstoredBlockShow)( - a::AbstractArray{<:Any,N}, I::Vararg{Int,N} -) where {N} - # TODO: Make sure this works for sparse or block sparse blocks, immutable - # blocks, diagonal blocks, etc.! - b_size = ntuple(ndims(a)) do d - return length(f.axes[d][Block(I[d])]) - end - return Fill(UnquotedChar('.'), b_size) -end -# TODO: Use `Base.to_indices`. -@inline function (f::GetUnstoredBlockShow)( - a::AbstractArray{<:Any,N}, I::CartesianIndex{N} -) where {N} - return f(a, Tuple(I)...) -end - -# TODO: Make this an `@interface ::AbstractBlockSparseArrayInterface` function -# once we delete the hacky `Base.show` definitions in `BlockSparseArraysTensorAlgebraExt`. -function Base.show(io::IO, mime::MIME"text/plain", a::AnyAbstractBlockSparseArray) - summary(io, a) - isempty(a) && return nothing - print(io, ":") - println(io) - a′ = ReplacedUnstoredBlockSparseArray(a, GetUnstoredBlockShow(axes(a))) - @allowscalar Base.print_array(io, a′) - return nothing +function Base.replace_in_print_matrix( + A::AnyAbstractBlockSparseArray{<:Any,2}, i::Integer, j::Integer, s::AbstractString +) + return isstored(A, i, j) ? s : Base.replace_with_centered_mark(s) end diff --git a/test/test_basics.jl b/test/test_basics.jl index de982d6b..fca4668d 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -1124,8 +1124,8 @@ arrayts = (Array, JLArray) # spacing so it isn't easy to make the test general. a = BlockSparseMatrix{elt,arrayt{elt,2}}([2, 2], [2, 2]) @allowscalar a[1, 2] = 12 - @test sprint(show, "text/plain", a) == - "$(summary(a)):\n $(zero(eltype(a))) $(eltype(a)(12)) │ . .\n $(zero(eltype(a))) $(zero(eltype(a))) │ . .\n ───────────┼──────\n . . │ . .\n . . │ . ." + @test @allowscalar(sprint(show, "text/plain", a)) == + "$(summary(a)):\n $(zero(eltype(a))) $(eltype(a)(12)) │ ⋅ ⋅ \n $(zero(eltype(a))) $(zero(eltype(a))) │ ⋅ ⋅ \n ───────────┼──────────\n ⋅ ⋅ │ ⋅ ⋅ \n ⋅ ⋅ │ ⋅ ⋅ " end end @testset "TypeParameterAccessors.position" begin From 63692e905cb236aa4add908e5a50f363562d17ca Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Thu, 13 Feb 2025 12:17:12 -0500 Subject: [PATCH 2/4] Ensure GPU compatibility --- .../wrappedabstractblocksparsearray.jl | 15 ++++++++++++++- test/test_basics.jl | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl index 0217aaa2..39225d1a 100644 --- a/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl +++ b/src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl @@ -1,4 +1,4 @@ -using Adapt: Adapt, WrappedArray +using Adapt: Adapt, WrappedArray, adapt using ArrayLayouts: zero! using BlockArrays: BlockArrays, @@ -350,3 +350,16 @@ function Base.replace_in_print_matrix( ) return isstored(A, i, j) ? s : Base.replace_with_centered_mark(s) end + +# attempt to catch things that wrap GPU arrays +function Base.print_array(io::IO, X::AnyAbstractBlockSparseArray) + X_cpu = adapt(Array, X) + if typeof(X_cpu) === typeof(X) # prevent infinite recursion + # need to specify ndims to allow specialized code for vector/matrix + @allowscalar @invoke Base.print_array( + io, X_cpu::AbstractArray{eltype(X_cpu),ndims(X_cpu)} + ) + else + Base.print_array(io, X_cpu) + end +end diff --git a/test/test_basics.jl b/test/test_basics.jl index fca4668d..6bcbded4 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -1124,7 +1124,7 @@ arrayts = (Array, JLArray) # spacing so it isn't easy to make the test general. a = BlockSparseMatrix{elt,arrayt{elt,2}}([2, 2], [2, 2]) @allowscalar a[1, 2] = 12 - @test @allowscalar(sprint(show, "text/plain", a)) == + @test sprint(show, "text/plain", a) == "$(summary(a)):\n $(zero(eltype(a))) $(eltype(a)(12)) │ ⋅ ⋅ \n $(zero(eltype(a))) $(zero(eltype(a))) │ ⋅ ⋅ \n ───────────┼──────────\n ⋅ ⋅ │ ⋅ ⋅ \n ⋅ ⋅ │ ⋅ ⋅ " end end From c5107fabe1fd422b0c521c3a03564cc910f25134 Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Thu, 13 Feb 2025 15:15:17 -0500 Subject: [PATCH 3/4] Bump v0.2.18 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 82ccb111..1944cd5c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BlockSparseArrays" uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" authors = ["ITensor developers and contributors"] -version = "0.2.17" +version = "0.2.18" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" From 62b49713e06ff401959e04ad117932771564074b Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Mon, 3 Mar 2025 08:14:11 -0500 Subject: [PATCH 4/4] Format Project.toml --- Project.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Project.toml b/Project.toml index c82496f1..a62c6a87 100644 --- a/Project.toml +++ b/Project.toml @@ -3,7 +3,6 @@ uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" authors = ["ITensor developers and contributors"] version = "0.2.26" - [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"