|
1 | 1 | using LinearAlgebra |
2 | 2 | import Adapt |
3 | 3 |
|
4 | | -@testset "CLArray" begin |
5 | | - @testset "constructors" begin |
6 | | - xs = CLArray{Int, 2, cl.Buffer}(undef, 2, 3) |
7 | | - @test collect(CLArray([1 2; 3 4])) == [1 2; 3 4] |
8 | | - @test testf(vec, rand(Float32, 5, 3)) |
9 | | - @test Base.elsize(xs) == sizeof(Int) |
10 | | - @test CLArray{Int, 2}(xs) === xs |
11 | | - |
12 | | - @test device_accessible(xs) |
13 | | - @test !host_accessible(xs) |
14 | | - @test_throws ArgumentError Base.unsafe_convert(Ptr{Int}, xs) |
15 | | - @test_throws ArgumentError Base.unsafe_convert(Ptr{Float32}, xs) |
16 | | - |
17 | | - @test collect(OpenCL.zeros(Float32, 2, 2)) == zeros(Float32, 2, 2) |
18 | | - @test collect(OpenCL.ones(Float32, 2, 2)) == ones(Float32, 2, 2) |
19 | | - |
20 | | - @test collect(OpenCL.fill(0, 2, 2)) == zeros(Int, 2, 2) |
21 | | - @test collect(OpenCL.fill(1, 2, 2)) == ones(Int, 2, 2) |
22 | | - end |
| 4 | +@testset "constructors" begin |
| 5 | + xs = CLArray{Int, 2, cl.Buffer}(undef, 2, 3) |
| 6 | + @test collect(CLArray([1 2; 3 4])) == [1 2; 3 4] |
| 7 | + @test testf(vec, rand(Float32, 5, 3)) |
| 8 | + @test Base.elsize(xs) == sizeof(Int) |
| 9 | + @test CLArray{Int, 2}(xs) === xs |
| 10 | + |
| 11 | + @test device_accessible(xs) |
| 12 | + @test !host_accessible(xs) |
| 13 | + @test_throws ArgumentError Base.unsafe_convert(Ptr{Int}, xs) |
| 14 | + @test_throws ArgumentError Base.unsafe_convert(Ptr{Float32}, xs) |
| 15 | + |
| 16 | + @test collect(OpenCL.zeros(Float32, 2, 2)) == zeros(Float32, 2, 2) |
| 17 | + @test collect(OpenCL.ones(Float32, 2, 2)) == ones(Float32, 2, 2) |
| 18 | + |
| 19 | + @test collect(OpenCL.fill(0, 2, 2)) == zeros(Int, 2, 2) |
| 20 | + @test collect(OpenCL.fill(1, 2, 2)) == ones(Int, 2, 2) |
| 21 | +end |
23 | 22 |
|
24 | | - @testset "adapt" begin |
25 | | - A = rand(Float32, 3, 3) |
26 | | - dA = CLArray(A) |
27 | | - @test Adapt.adapt(Array, dA) == A |
28 | | - @test Adapt.adapt(CLArray, A) isa CLArray |
29 | | - @test Array(Adapt.adapt(CLArray, A)) == A |
30 | | - end |
| 23 | +@testset "adapt" begin |
| 24 | + A = rand(Float32, 3, 3) |
| 25 | + dA = CLArray(A) |
| 26 | + @test Adapt.adapt(Array, dA) == A |
| 27 | + @test Adapt.adapt(CLArray, A) isa CLArray |
| 28 | + @test Array(Adapt.adapt(CLArray, A)) == A |
| 29 | +end |
31 | 30 |
|
32 | | - @testset "reshape" begin |
33 | | - A = [ |
34 | | - 1 2 3 4 |
35 | | - 5 6 7 8 |
36 | | - ] |
37 | | - gA = reshape(CLArray(A), 1, 8) |
38 | | - _A = reshape(A, 1, 8) |
39 | | - _gA = Array(gA) |
40 | | - @test all(_A .== _gA) |
41 | | - A = [1, 2, 3, 4] |
42 | | - gA = reshape(CLArray(A), 4) |
43 | | - end |
| 31 | +@testset "reshape" begin |
| 32 | + A = [ |
| 33 | + 1 2 3 4 |
| 34 | + 5 6 7 8 |
| 35 | + ] |
| 36 | + gA = reshape(CLArray(A), 1, 8) |
| 37 | + _A = reshape(A, 1, 8) |
| 38 | + _gA = Array(gA) |
| 39 | + @test all(_A .== _gA) |
| 40 | + A = [1, 2, 3, 4] |
| 41 | + gA = reshape(CLArray(A), 4) |
| 42 | +end |
44 | 43 |
|
45 | | - @testset "fill(::SubArray)" begin |
46 | | - xs = OpenCL.zeros(Float32, 3) |
47 | | - fill!(view(xs, 2:2), 1) |
48 | | - @test Array(xs) == [0, 1, 0] |
| 44 | +@testset "fill(::SubArray)" begin |
| 45 | + xs = OpenCL.zeros(Float32, 3) |
| 46 | + fill!(view(xs, 2:2), 1) |
| 47 | + @test Array(xs) == [0, 1, 0] |
| 48 | +end |
| 49 | +# TODO: Look into how to port the @sync |
| 50 | + |
| 51 | +if cl.memory_backend() isa cl.USMBackend |
| 52 | + @testset "shared buffers & unsafe_wrap" begin |
| 53 | + a = CLVector{Int, cl.UnifiedSharedMemory}(undef, 2) |
| 54 | + |
| 55 | + # check that basic operations work on arrays backed by shared memory |
| 56 | + fill!(a, 40) |
| 57 | + a .+= 2 |
| 58 | + @test Array(a) == [42, 42] |
| 59 | + |
| 60 | + # derive an Array object and test that the memory keeps in sync |
| 61 | + b = unsafe_wrap(Array, a) |
| 62 | + b[1] = 100 |
| 63 | + @test Array(a) == [100, 42] |
| 64 | + copyto!(a, 2, [200], 1, 1) |
| 65 | + cl.finish(cl.queue()) |
| 66 | + @test b == [100, 200] |
49 | 67 | end |
50 | | - # TODO: Look into how to port the @sync |
51 | | - |
52 | | - if cl.memory_backend() isa cl.USMBackend |
53 | | - @testset "shared buffers & unsafe_wrap" begin |
54 | | - a = CLVector{Int, cl.UnifiedSharedMemory}(undef, 2) |
55 | | - |
56 | | - # check that basic operations work on arrays backed by shared memory |
57 | | - fill!(a, 40) |
58 | | - a .+= 2 |
59 | | - @test Array(a) == [42, 42] |
60 | | - |
61 | | - # derive an Array object and test that the memory keeps in sync |
62 | | - b = unsafe_wrap(Array, a) |
63 | | - b[1] = 100 |
64 | | - @test Array(a) == [100, 42] |
65 | | - copyto!(a, 2, [200], 1, 1) |
66 | | - cl.finish(cl.queue()) |
67 | | - @test b == [100, 200] |
68 | | - end |
69 | | - |
70 | | - # https://github.com/JuliaGPU/CUDA.jl/issues/2191 |
71 | | - @testset "preserving memory types" begin |
72 | | - a = CLVector{Int, cl.UnifiedSharedMemory}([1]) |
73 | | - @test OpenCL.memtype(a) == cl.UnifiedSharedMemory |
74 | | - |
75 | | - # unified-ness should be preserved |
76 | | - b = a .+ 1 |
77 | | - @test OpenCL.memtype(b) == cl.UnifiedSharedMemory |
78 | | - |
79 | | - # when there's a conflict, we should defer to unified memory |
80 | | - c = CLVector{Int, cl.UnifiedSharedMemory}([1]) |
81 | | - d = CLVector{Int, cl.UnifiedDeviceMemory}([1]) |
82 | | - e = c .+ d |
83 | | - @test OpenCL.memtype(e) == cl.UnifiedSharedMemory |
84 | | - end |
| 68 | + |
| 69 | + # https://github.com/JuliaGPU/CUDA.jl/issues/2191 |
| 70 | + @testset "preserving memory types" begin |
| 71 | + a = CLVector{Int, cl.UnifiedSharedMemory}([1]) |
| 72 | + @test OpenCL.memtype(a) == cl.UnifiedSharedMemory |
| 73 | + |
| 74 | + # unified-ness should be preserved |
| 75 | + b = a .+ 1 |
| 76 | + @test OpenCL.memtype(b) == cl.UnifiedSharedMemory |
| 77 | + |
| 78 | + # when there's a conflict, we should defer to unified memory |
| 79 | + c = CLVector{Int, cl.UnifiedSharedMemory}([1]) |
| 80 | + d = CLVector{Int, cl.UnifiedDeviceMemory}([1]) |
| 81 | + e = c .+ d |
| 82 | + @test OpenCL.memtype(e) == cl.UnifiedSharedMemory |
85 | 83 | end |
| 84 | +end |
86 | 85 |
|
87 | | - @testset "resizing" begin |
88 | | - a = CLArray([1, 2, 3]) |
| 86 | +@testset "resizing" begin |
| 87 | + a = CLArray([1, 2, 3]) |
89 | 88 |
|
90 | | - resize!(a, 3) |
91 | | - @test length(a) == 3 |
92 | | - @test Array(a) == [1, 2, 3] |
| 89 | + resize!(a, 3) |
| 90 | + @test length(a) == 3 |
| 91 | + @test Array(a) == [1, 2, 3] |
93 | 92 |
|
94 | | - resize!(a, 5) |
95 | | - @test length(a) == 5 |
96 | | - @test Array(a)[1:3] == [1, 2, 3] |
| 93 | + resize!(a, 5) |
| 94 | + @test length(a) == 5 |
| 95 | + @test Array(a)[1:3] == [1, 2, 3] |
97 | 96 |
|
98 | | - resize!(a, 2) |
99 | | - @test length(a) == 2 |
100 | | - @test Array(a)[1:2] == [1, 2] |
| 97 | + resize!(a, 2) |
| 98 | + @test length(a) == 2 |
| 99 | + @test Array(a)[1:2] == [1, 2] |
101 | 100 |
|
102 | | - b = CLArray{Int}(undef, 0) |
103 | | - @test length(b) == 0 |
104 | | - resize!(b, 1) |
105 | | - @test length(b) == 1 |
106 | | - end |
| 101 | + b = CLArray{Int}(undef, 0) |
| 102 | + @test length(b) == 0 |
| 103 | + resize!(b, 1) |
| 104 | + @test length(b) == 1 |
107 | 105 | end |
0 commit comments