Skip to content

Commit

Permalink
Define append! (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored May 24, 2024
1 parent 94bdb43 commit ec9fe5b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
8 changes: 8 additions & 0 deletions lib/JLArrays/src/JLArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ end
Base.copyto!(dest::DenseJLArray{T}, source::DenseJLArray{T}) where {T} =
copyto!(dest, 1, source, 1, length(source))

function Base.resize!(a::DenseJLVector{T}, nl::Integer) where {T}
a_resized = JLVector{T}(undef, nl)
copyto!(a_resized, 1, a, 1, min(length(a), nl))
a.data = a_resized.data
a.offset = 0
a.dims = size(a_resized)
return a
end

## random number generation

Expand Down
16 changes: 16 additions & 0 deletions src/host/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,19 @@ Base.deepcopy(x::AbstractGPUArray) = copy(x)

# revert of JuliaLang/julia#31929
Base.filter(f, As::AbstractGPUArray) = As[map(f, As)::AbstractGPUArray{Bool}]

# appending

function Base.append!(a::AbstractGPUVector, items::AbstractVector)
n = length(items)
resize!(a, length(a) + n)
copyto!(a, length(a) - n + 1, items, firstindex(items), n)
return a
end

# this is needed because copyto! of most GPU arrays
# doesn't currently support Tuple sources
function Base.append!(a::AbstractGPUVector, items::Tuple)
append!(a, collect(items))
return a
end
2 changes: 1 addition & 1 deletion test/testsuite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ include("testsuite/construction.jl")
include("testsuite/gpuinterface.jl")
include("testsuite/indexing.jl")
include("testsuite/base.jl")
#include("testsuite/vector.jl")
include("testsuite/vector.jl")
include("testsuite/reductions.jl")
include("testsuite/broadcasting.jl")
include("testsuite/linalg.jl")
Expand Down
58 changes: 35 additions & 23 deletions test/testsuite/vector.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
@testsuite "vectors" (AT, eltypes)->begin
a = Float32[]
x = AT(a)
@test length(x) == 0
push!(x, 12f0)
@test length(x) == 1
@test x[1] == 12f0
## push! not defined for most GPU arrays,
## uncomment once it is.
# a = Float32[]
# x = AT(a)
# @test length(x) == 0
# @test push!(x, 12)
# @allowscalar begin
# @test x[1] == 12
# end

a = Float32[0]
x = AT(a)
@test length(x) == 1
@test length(GPUArrays.buffer(x)) == 1
push!(x, 12)
@test length(GPUArrays.buffer(x)) == GPUArrays.grow_dimensions(0, 1, 1)
resize!(x, 5)
@test length(x) == 5
@test length(GPUArrays.buffer(x)) == 5

resize!(x, 3)
@test length(x) == 3
# we don't shrink buffers yet... TODO shrink them... or should we?
@test length(GPUArrays.buffer(x)) == 5

x = AT(Array{Float32}(undef, 16))
reshape!(x, (2, 2, 2, 2))
@test size(x) == (2, 2, 2, 2)
x = AT(Array{Float32}(undef, 16))
y = AT(rand(Float32, 32))
update!(x, y)
@test size(x) == (32,)
a = Float32[0, 1, 2]
x = AT(a)
resize!(x, 2)
@test length(x) == 2
@allowscalar begin
@test x[1] == 0
@test x[2] == 1
end

a = Float32[0, 1, 2]
x = AT(a)
append!(x, [3, 4])
@test length(x) == 5
@allowscalar begin
@test x[4] == 3
@test x[5] == 4
end

a = Float32[0, 1, 2]
x = AT(a)
append!(x, (3, 4))
@test length(x) == 5
@allowscalar begin
@test x[4] == 3
@test x[5] == 4
end
end

0 comments on commit ec9fe5b

Please sign in to comment.