Skip to content

Commit ec9fe5b

Browse files
authored
Define append! (#533)
1 parent 94bdb43 commit ec9fe5b

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

lib/JLArrays/src/JLArrays.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ end
372372
Base.copyto!(dest::DenseJLArray{T}, source::DenseJLArray{T}) where {T} =
373373
copyto!(dest, 1, source, 1, length(source))
374374

375+
function Base.resize!(a::DenseJLVector{T}, nl::Integer) where {T}
376+
a_resized = JLVector{T}(undef, nl)
377+
copyto!(a_resized, 1, a, 1, min(length(a), nl))
378+
a.data = a_resized.data
379+
a.offset = 0
380+
a.dims = size(a_resized)
381+
return a
382+
end
375383

376384
## random number generation
377385

src/host/abstractarray.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,19 @@ Base.deepcopy(x::AbstractGPUArray) = copy(x)
318318

319319
# revert of JuliaLang/julia#31929
320320
Base.filter(f, As::AbstractGPUArray) = As[map(f, As)::AbstractGPUArray{Bool}]
321+
322+
# appending
323+
324+
function Base.append!(a::AbstractGPUVector, items::AbstractVector)
325+
n = length(items)
326+
resize!(a, length(a) + n)
327+
copyto!(a, length(a) - n + 1, items, firstindex(items), n)
328+
return a
329+
end
330+
331+
# this is needed because copyto! of most GPU arrays
332+
# doesn't currently support Tuple sources
333+
function Base.append!(a::AbstractGPUVector, items::Tuple)
334+
append!(a, collect(items))
335+
return a
336+
end

test/testsuite.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ include("testsuite/construction.jl")
8888
include("testsuite/gpuinterface.jl")
8989
include("testsuite/indexing.jl")
9090
include("testsuite/base.jl")
91-
#include("testsuite/vector.jl")
91+
include("testsuite/vector.jl")
9292
include("testsuite/reductions.jl")
9393
include("testsuite/broadcasting.jl")
9494
include("testsuite/linalg.jl")

test/testsuite/vector.jl

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
11
@testsuite "vectors" (AT, eltypes)->begin
2-
a = Float32[]
3-
x = AT(a)
4-
@test length(x) == 0
5-
push!(x, 12f0)
6-
@test length(x) == 1
7-
@test x[1] == 12f0
2+
## push! not defined for most GPU arrays,
3+
## uncomment once it is.
4+
# a = Float32[]
5+
# x = AT(a)
6+
# @test length(x) == 0
7+
# @test push!(x, 12)
8+
# @allowscalar begin
9+
# @test x[1] == 12
10+
# end
811

912
a = Float32[0]
1013
x = AT(a)
11-
@test length(x) == 1
12-
@test length(GPUArrays.buffer(x)) == 1
13-
push!(x, 12)
14-
@test length(GPUArrays.buffer(x)) == GPUArrays.grow_dimensions(0, 1, 1)
15-
resize!(x, 5)
16-
@test length(x) == 5
17-
@test length(GPUArrays.buffer(x)) == 5
18-
1914
resize!(x, 3)
2015
@test length(x) == 3
21-
# we don't shrink buffers yet... TODO shrink them... or should we?
22-
@test length(GPUArrays.buffer(x)) == 5
2316

24-
x = AT(Array{Float32}(undef, 16))
25-
reshape!(x, (2, 2, 2, 2))
26-
@test size(x) == (2, 2, 2, 2)
27-
x = AT(Array{Float32}(undef, 16))
28-
y = AT(rand(Float32, 32))
29-
update!(x, y)
30-
@test size(x) == (32,)
17+
a = Float32[0, 1, 2]
18+
x = AT(a)
19+
resize!(x, 2)
20+
@test length(x) == 2
21+
@allowscalar begin
22+
@test x[1] == 0
23+
@test x[2] == 1
24+
end
25+
26+
a = Float32[0, 1, 2]
27+
x = AT(a)
28+
append!(x, [3, 4])
29+
@test length(x) == 5
30+
@allowscalar begin
31+
@test x[4] == 3
32+
@test x[5] == 4
33+
end
34+
35+
a = Float32[0, 1, 2]
36+
x = AT(a)
37+
append!(x, (3, 4))
38+
@test length(x) == 5
39+
@allowscalar begin
40+
@test x[4] == 3
41+
@test x[5] == 4
42+
end
3143
end

0 commit comments

Comments
 (0)