Skip to content

2-arg show for GPUArray #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/host/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ Base.print_array(io::IO, X::AnyGPUArray) =
Base.print_array(io, adapt(ToArray(), X))

# show
Base._show_nonempty(io::IO, X::AnyGPUArray, prefix::String) =
function Base._show_nonempty(io::IO, X::AnyGPUArray, prefix::String)
print(io, typeof(X).name.name, "(")
Base._show_nonempty(io, adapt(ToArray(), X), prefix)
Base._show_empty(io::IO, X::AnyGPUArray) =
print(io, ")")
end
function Base._show_empty(io::IO, X::AnyGPUArray)
print(io, typeof(X).name.name, "(")
Base._show_empty(io, adapt(ToArray(), X))
Base.show_vector(io::IO, v::AnyGPUArray, args...) =
print(io, ")")
end
function Base.show_vector(io::IO, v::AnyGPUArray, args...)
print(io, typeof(v).name.name, "(")
Base.show_vector(io, adapt(ToArray(), v), args...)
print(io, ")")
end

## collect to CPU (discarding wrapper type)

Expand Down
4 changes: 2 additions & 2 deletions test/testsuite/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,13 @@ end
# due to different definition of `Int` type
# print([1]) shows as [1] on 64bit but Int64[1] on 32bit
msg = showstr(A)
@test msg == "[1]" || msg == "Int64[1]"
@test occursin("[1]", msg) || occursin("Int64[1]", msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple occursins with strings that are substrings of each other isn't very useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true. Maybe these should detect the brackets which this PR adds, at least this will fail before:

Suggested change
@test occursin("[1]", msg) || occursin("Int64[1]", msg)
@test occursin("([1])", msg) || occursin("(Int64[1])", msg)

Copy link
Contributor Author

@mcabbott mcabbott Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests fail on CI, and running the package's tests locally, on Julia <= 1.8.

But what they aim to test seems to work fine in isolation, and with just this @testset. Any idea what's wrong?

julia> using JLArrays

julia> jl([1]), 2  # this works fine:
(JLArray([1]), 2)

julia> # from tests:
       replstr(x, kv::Pair...) = sprint((io,x) -> show(IOContext(io, :compact => false, :limit => true, :displaysize => (24, 80), kv...), MIME("text/plain"), x), x)
       showstr(x, kv::Pair...) = sprint((io,x) -> show(IOContext(io, :limit => true, :displaysize => (24, 80), kv...), x), x);

julia> replstr(jl([1]))
"1-element JLArray{Int64, 1}:\n 1"

julia> showstr(jl([1]))  # still fine
"JLArray([1])"

julia> let AT = JLArray
               @testset "showing" begin
                   # vectors and non-vector arrays showing
                   # are handled differently in base/arrayshow.jl
                   A = AT(Int64[1])
                   B = AT(Int64[1 2;3 4])
                   msg = replstr(A)
                   @test occursin(Regex("^1-element $AT{Int64,\\s?1.*}:\n 1\$"), msg)
                   # # result of e.g. `print` differs on 32bit and 64bit machines
                    # due to different definition of `Int` type
                    # print([1]) shows as [1] on 64bit but Int64[1] on 32bit
                    msg = showstr(A)
                    @test occursin("([1])", msg) || occursin("(Int64[1])", msg)

                    msg = replstr(B)
                    @test occursin(Regex("^2×2 $AT{Int64,\\s?2.*}:\n 1  2\n 3  4\$"), msg)

                    msg = showstr(B)
                    @test occursin("([1 2; 3 4])", msg) || occursin("(Int64[1 2; 3 4])", msg)

                    # the printing of Adjoint depends on global state
                    msg = replstr(A')
                   @test occursin(Regex("^1×1 Adjoint{Int64,\\s?$AT{Int64,\\s?1.*}}:\n 1\$"), msg) ||
                       occursin(Regex("^1×1 LinearAlgebra.Adjoint{Int64,\\s?$AT{Int64,\\s?1.*}}:\n 1\$"), msg) ||
                       occursin(Regex("^1×1 adjoint\\(::$AT{Int64,\\s?1.*}\\) with eltype Int64:\n 1\$"), msg)
               end
           end
Test Summary: | Pass  Total
showing       |    5      5
Test.DefaultTestSet("showing", Any[], 5, false, false)

julia> VERSION
v"1.6.0"


msg = replstr(B)
@test occursin(Regex("^2×2 $AT{Int64,\\s?2.*}:\n 1 2\n 3 4\$"), msg)

msg = showstr(B)
@test msg == "[1 2; 3 4]" || msg == "Int64[1 2; 3 4]"
@test occursin("[1 2; 3 4]", msg) || occursin("Int64[1 2; 3 4]", msg)

# the printing of Adjoint depends on global state
msg = replstr(A')
Expand Down