Skip to content

Commit 5383601

Browse files
committed
Merge branch 'gpu-support' of https://github.com/darsnack/CircularArrayBuffers.jl into gpu-support
2 parents 02e5067 + 2802016 commit 5383601

File tree

2 files changed

+164
-162
lines changed

2 files changed

+164
-162
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
version:
13-
- '1.0'
13+
# - '1.0' # incompatible with CUDA.jl
1414
- '1'
1515
- 'nightly'
1616
os:

test/runtests.jl

Lines changed: 163 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -169,175 +169,177 @@ CUDA.allowscalar(false)
169169
end
170170
end
171171

172-
@testset "CircularArrayBuffers (CuArray)" begin
173-
A = CUDA.ones(2, 2)
174-
Ac = adapt(Array, A)
175-
C = CUDA.ones(Float32, 2, 2)
176-
177-
@testset "Adapt" begin
178-
X = CircularArrayBuffer(rand(2, 3))
179-
Xc = adapt(CuArray, X)
180-
@test Xc isa CircularArrayBuffer{Float64, 2, <:CuArray}
181-
@test adapt(Array, Xc) == X
182-
end
183-
184-
# https://github.com/JuliaReinforcementLearning/ReinforcementLearning.jl/issues/551
185-
@testset "1D with 0d data" begin
186-
b = adapt(CuArray, CircularArrayBuffer{Int}(3))
187-
CUDA.@allowscalar push!(b, CUDA.zeros(Int, ()))
188-
@test length(b) == 1
189-
@test CUDA.@allowscalar b[1] == 0
190-
end
191-
192-
@testset "1D Int" begin
193-
b = adapt(CuArray, CircularArrayBuffer{Int}(3))
194-
195-
@test eltype(b) == Int
196-
@test capacity(b) == 3
197-
@test isfull(b) == false
198-
@test isempty(b) == true
199-
@test length(b) == 0
200-
@test size(b) == (0,)
201-
# element must has the exact same length with the element of buffer
202-
@test_throws Exception push!(b, [1, 2])
203-
204-
for x in 1:3
205-
push!(b, x)
172+
if CUDA.functional()
173+
@testset "CircularArrayBuffers (CuArray)" begin
174+
A = CUDA.ones(2, 2)
175+
Ac = adapt(Array, A)
176+
C = CUDA.ones(Float32, 2, 2)
177+
178+
@testset "Adapt" begin
179+
X = CircularArrayBuffer(rand(2, 3))
180+
Xc = adapt(CuArray, X)
181+
@test Xc isa CircularArrayBuffer{Float64,2,<:CuArray}
182+
@test adapt(Array, Xc) == X
206183
end
207184

208-
@test capacity(b) == 3
209-
@test isfull(b) == true
210-
@test length(b) == 3
211-
@test size(b) == (3,)
212-
# scalar indexing is not allowed
213-
@test_throws ErrorException b[1]
214-
@test_throws ErrorException b[end]
215-
@test CUDA.@allowscalar b[1:end] == cu([1, 2, 3])
216-
217-
for x in 4:5
218-
push!(b, x)
185+
# https://github.com/JuliaReinforcementLearning/ReinforcementLearning.jl/issues/551
186+
@testset "1D with 0d data" begin
187+
b = adapt(CuArray, CircularArrayBuffer{Int}(3))
188+
CUDA.@allowscalar push!(b, CUDA.zeros(Int, ()))
189+
@test length(b) == 1
190+
@test CUDA.@allowscalar b[1] == 0
219191
end
220192

221-
@test capacity(b) == 3
222-
@test length(b) == 3
223-
@test size(b) == (3,)
224-
@test CUDA.@allowscalar b[1:end] == [3, 4, 5]
225-
226-
empty!(b)
227-
@test isfull(b) == false
228-
@test isempty(b) == true
229-
@test length(b) == 0
230-
@test size(b) == (0,)
231-
232-
push!(b, 6)
233-
@test isfull(b) == false
234-
@test isempty(b) == false
235-
@test length(b) == 1
236-
@test size(b) == (1,)
237-
@test CUDA.@allowscalar b[1] == 6
238-
239-
push!(b, 7)
240-
push!(b, 8)
241-
@test isfull(b) == true
242-
@test isempty(b) == false
243-
@test length(b) == 3
244-
@test size(b) == (3,)
245-
@test CUDA.@allowscalar b[1:3] == cu([6, 7, 8])
246-
247-
push!(b, 9)
248-
@test isfull(b) == true
249-
@test isempty(b) == false
250-
@test length(b) == 3
251-
@test size(b) == (3,)
252-
@test CUDA.@allowscalar b[1:3] == cu([7, 8, 9])
253-
254-
x = CUDA.@allowscalar pop!(b)
255-
@test x == 9
256-
@test length(b) == 2
257-
@test CUDA.@allowscalar b[1:2] == cu([7, 8])
258-
259-
x = CUDA.@allowscalar popfirst!(b)
260-
@test x == 7
261-
@test length(b) == 1
262-
@test CUDA.@allowscalar b[1] == 8
263-
264-
x = CUDA.@allowscalar pop!(b)
265-
@test x == 8
266-
@test length(b) == 0
267-
268-
@test_throws ArgumentError pop!(b)
269-
@test_throws ArgumentError popfirst!(b)
270-
end
271-
272-
@testset "2D Float64" begin
273-
b = adapt(CuArray, CircularArrayBuffer{Float64}(2, 2, 3))
274-
275-
@test eltype(b) == Float64
276-
@test capacity(b) == 3
277-
@test isfull(b) == false
278-
@test length(b) == 0
279-
@test size(b) == (2, 2, 0)
280-
281-
for x in 1:3
282-
push!(b, x * A)
193+
@testset "1D Int" begin
194+
b = adapt(CuArray, CircularArrayBuffer{Int}(3))
195+
196+
@test eltype(b) == Int
197+
@test capacity(b) == 3
198+
@test isfull(b) == false
199+
@test isempty(b) == true
200+
@test length(b) == 0
201+
@test size(b) == (0,)
202+
# element must has the exact same length with the element of buffer
203+
@test_throws Exception push!(b, [1, 2])
204+
205+
for x in 1:3
206+
push!(b, x)
207+
end
208+
209+
@test capacity(b) == 3
210+
@test isfull(b) == true
211+
@test length(b) == 3
212+
@test size(b) == (3,)
213+
# scalar indexing is not allowed
214+
@test_throws ErrorException b[1]
215+
@test_throws ErrorException b[end]
216+
@test CUDA.@allowscalar b[1:end] == cu([1, 2, 3])
217+
218+
for x in 4:5
219+
push!(b, x)
220+
end
221+
222+
@test capacity(b) == 3
223+
@test length(b) == 3
224+
@test size(b) == (3,)
225+
@test CUDA.@allowscalar b[1:end] == [3, 4, 5]
226+
227+
empty!(b)
228+
@test isfull(b) == false
229+
@test isempty(b) == true
230+
@test length(b) == 0
231+
@test size(b) == (0,)
232+
233+
push!(b, 6)
234+
@test isfull(b) == false
235+
@test isempty(b) == false
236+
@test length(b) == 1
237+
@test size(b) == (1,)
238+
@test CUDA.@allowscalar b[1] == 6
239+
240+
push!(b, 7)
241+
push!(b, 8)
242+
@test isfull(b) == true
243+
@test isempty(b) == false
244+
@test length(b) == 3
245+
@test size(b) == (3,)
246+
@test CUDA.@allowscalar b[1:3] == cu([6, 7, 8])
247+
248+
push!(b, 9)
249+
@test isfull(b) == true
250+
@test isempty(b) == false
251+
@test length(b) == 3
252+
@test size(b) == (3,)
253+
@test CUDA.@allowscalar b[1:3] == cu([7, 8, 9])
254+
255+
x = CUDA.@allowscalar pop!(b)
256+
@test x == 9
257+
@test length(b) == 2
258+
@test CUDA.@allowscalar b[1:2] == cu([7, 8])
259+
260+
x = CUDA.@allowscalar popfirst!(b)
261+
@test x == 7
262+
@test length(b) == 1
263+
@test CUDA.@allowscalar b[1] == 8
264+
265+
x = CUDA.@allowscalar pop!(b)
266+
@test x == 8
267+
@test length(b) == 0
268+
269+
@test_throws ArgumentError pop!(b)
270+
@test_throws ArgumentError popfirst!(b)
283271
end
284272

285-
@test capacity(b) == 3
286-
@test isfull(b) == true
287-
@test length(b) == 2 * 2 * 3
288-
@test size(b) == (2, 2, 3)
289-
for i in 1:3
290-
@test b[:, :, i] == i * A
273+
@testset "2D Float64" begin
274+
b = adapt(CuArray, CircularArrayBuffer{Float64}(2, 2, 3))
275+
276+
@test eltype(b) == Float64
277+
@test capacity(b) == 3
278+
@test isfull(b) == false
279+
@test length(b) == 0
280+
@test size(b) == (2, 2, 0)
281+
282+
for x in 1:3
283+
push!(b, x * A)
284+
end
285+
286+
@test capacity(b) == 3
287+
@test isfull(b) == true
288+
@test length(b) == 2 * 2 * 3
289+
@test size(b) == (2, 2, 3)
290+
for i in 1:3
291+
@test b[:, :, i] == i * A
292+
end
293+
@test b[:, :, end] == 3 * A
294+
295+
for x in 4:5
296+
push!(b, x * CUDA.ones(Float64, 2, 2))
297+
end
298+
299+
@test capacity(b) == 3
300+
@test length(b) == 2 * 2 * 3
301+
@test size(b) == (2, 2, 3)
302+
@test b[:, :, 1] == 3 * A
303+
@test b[:, :, end] == 5 * A
304+
305+
# doing b == ... triggers scalar indexing
306+
@test CUDA.@allowscalar b == cu(reshape([c for x in 3:5 for c in x * Ac], 2, 2, 3))
307+
308+
push!(b, 6 * CUDA.ones(Float32, 2, 2))
309+
push!(b, 7 * CUDA.ones(Int, 2, 2))
310+
@test CUDA.@allowscalar b == cu(reshape([c for x in 5:7 for c in x * Ac], 2, 2, 3))
311+
312+
x = pop!(b)
313+
@test x == 7 * CUDA.ones(Float64, 2, 2)
314+
@test CUDA.@allowscalar b == cu(reshape([c for x in 5:6 for c in x * Ac], 2, 2, 2))
291315
end
292-
@test b[:, :, end] == 3 * A
293316

294-
for x in 4:5
295-
push!(b, x * CUDA.ones(Float64, 2, 2))
317+
@testset "append!" begin
318+
b = adapt(CuArray, CircularArrayBuffer{Int}(2, 3))
319+
append!(b, CUDA.zeros(2))
320+
append!(b, 1:4)
321+
@test CUDA.@allowscalar b == cu([
322+
0 1 3
323+
0 2 4
324+
])
325+
326+
327+
b = adapt(CuArray, CircularArrayBuffer{Int}(2, 3))
328+
for i in 1:5
329+
push!(b, CUDA.fill(i, 2))
330+
end
331+
empty!(b)
332+
append!(b, 1:4)
333+
@test CUDA.@allowscalar b == cu([
334+
1 3
335+
2 4
336+
])
337+
338+
append!(b, 5:8)
339+
@test CUDA.@allowscalar b == cu([
340+
3 5 7
341+
4 6 8
342+
])
296343
end
297-
298-
@test capacity(b) == 3
299-
@test length(b) == 2 * 2 * 3
300-
@test size(b) == (2, 2, 3)
301-
@test b[:, :, 1] == 3 * A
302-
@test b[:, :, end] == 5 * A
303-
304-
# doing b == ... triggers scalar indexing
305-
@test CUDA.@allowscalar b == cu(reshape([c for x in 3:5 for c in x * Ac], 2, 2, 3))
306-
307-
push!(b, 6 * CUDA.ones(Float32, 2, 2))
308-
push!(b, 7 * CUDA.ones(Int, 2, 2))
309-
@test CUDA.@allowscalar b == cu(reshape([c for x in 5:7 for c in x * Ac], 2, 2, 3))
310-
311-
x = pop!(b)
312-
@test x == 7 * CUDA.ones(Float64, 2, 2)
313-
@test CUDA.@allowscalar b == cu(reshape([c for x in 5:6 for c in x * Ac], 2, 2, 2))
314-
end
315-
316-
@testset "append!" begin
317-
b = adapt(CuArray, CircularArrayBuffer{Int}(2, 3))
318-
append!(b, CUDA.zeros(2))
319-
append!(b, 1:4)
320-
@test CUDA.@allowscalar b == cu([
321-
0 1 3
322-
0 2 4
323-
])
324-
325-
326-
b = adapt(CuArray, CircularArrayBuffer{Int}(2, 3))
327-
for i in 1:5
328-
push!(b, CUDA.fill(i, 2))
329-
end
330-
empty!(b)
331-
append!(b, 1:4)
332-
@test CUDA.@allowscalar b == cu([
333-
1 3
334-
2 4
335-
])
336-
337-
append!(b, 5:8)
338-
@test CUDA.@allowscalar b == cu([
339-
3 5 7
340-
4 6 8
341-
])
342344
end
343345
end

0 commit comments

Comments
 (0)