Skip to content

Commit 6a6f354

Browse files
committed
include more tests, fix vcat/hcat
1 parent 7e224d8 commit 6a6f354

File tree

5 files changed

+158
-148
lines changed

5 files changed

+158
-148
lines changed

src/base.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@ map!(f, y::GPUArray, x1::GPUArray, x2::GPUArray) =
2121
invoke(map!, Tuple{Any,GPUArray, Vararg{GPUArray}}, f, y, x1, x2)
2222

2323

24-
@generated function nindex(i::Int, ls::NTuple{N}) where N
24+
@generated function nindex(i::T, ls::NTuple{N}) where {T, N}
2525
quote
2626
Base.@_inline_meta
27-
$(foldr((n, els) -> :(i ls[$n] ? ($n, i) : (i -= ls[$n]; $els)), :(-1, -1), 1:N))
27+
$(foldr(:($T(0), $T(0)), T(1):T(N)) do n, els
28+
:(i ls[$n] ? ($T($n), i) : (i -= $T(ls[$n]); $els))
29+
end)
2830
end
2931
end
30-
31-
function catindex(dim, I::NTuple{N}, shapes) where N
32-
@inbounds x, i = nindex(I[dim], getindex.(shapes, dim))
33-
x, ntuple(n -> n == dim ? Cuint(i) : I[n], Val{N})
32+
function catindex(dim, I::NTuple{N, T}, shapes) where {T, N}
33+
xi = nindex(I[dim], map(s-> s[dim], shapes))
34+
x = xi[1]; i = xi[2]
35+
x, ntuple(n -> n == dim ? i : I[n], Val{N})
3436
end
3537

3638
function _cat(dim, dest, xs...)
37-
gpu_call(kernel, dest, (dim, dest, xs)) do state, dim, dest, xs
39+
gpu_call(dest, (Cuint(dim), dest, xs)) do state, dim, dest, xs
3840
I = @cartesianidx dest state
39-
n, I′ = catindex(dim, I, size.(xs))
41+
nI = catindex(dim, I, size.(xs))
42+
n = nI[1]; I′ = nI[2]
4043
@inbounds dest[I...] = xs[n][I′...]
4144
return
4245
end

src/testsuite/base.jl

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
using Base.Test, GPUArrays
2+
using GPUArrays: mapidx, gpu_sub2ind
3+
using GPUArrays.TestSuite
4+
5+
function cartesian_iter(state, A, res, Asize)
6+
for i in CartesianRange(CartesianIndex(Asize))
7+
idx = gpu_sub2ind(Asize, i.I)
8+
res[idx] = A[idx]
9+
end
10+
return
11+
end
12+
13+
function clmap!(state, f, out, b)
14+
i = linear_index(state) # get the kernel index it gets scheduled on
15+
out[i] = f(b[i])
16+
return
17+
end
18+
19+
function ntuple_test(state, result, ::Val{N}) where N
20+
result[1] = ntuple(Val{N}) do i
21+
Float32(i) * 77f0
22+
end
23+
return
24+
end
25+
26+
function ntuple_closure(state, result, ::Val{N}, testval) where N
27+
result[1] = ntuple(Val{N}) do i
28+
Float32(i) * testval
29+
end
30+
return
31+
end
32+
33+
function run_base(Typ)
34+
@testset "base functionality" begin
35+
@testset "mapidx" begin
36+
a = rand(Complex64, 77)
37+
b = rand(Complex64, 77)
38+
A = Typ(a)
39+
B = Typ(b)
40+
off = Cuint(1)
41+
mapidx(A, (B, off, Cuint(length(A)))) do i, a, b, off, len
42+
x = b[i]
43+
x2 = b[min(i+off, len)]
44+
a[i] = x * x2
45+
end
46+
foreach(1:length(a)) do i
47+
x = b[i]
48+
x2 = b[min(i+off, length(a))]
49+
a[i] = x * x2
50+
end
51+
@test Array(A) a
52+
end
53+
54+
@testset "muladd & abs" begin
55+
a = rand(Float32, 32) - 0.5f0
56+
A = Typ(a)
57+
x = abs.(A)
58+
@test Array(x) == abs.(a)
59+
y = muladd.(A, 2f0, x)
60+
@test Array(y) == muladd(a, 2f0, abs.(a))
61+
end
62+
63+
@testset "copy!" begin
64+
x = zeros(Float32, 10, 10)
65+
y = rand(Float32, 20, 10)
66+
a = Typ(x)
67+
b = Typ(y)
68+
r1 = CartesianRange(CartesianIndex(1, 3), CartesianIndex(7, 8))
69+
r2 = CartesianRange(CartesianIndex(4, 3), CartesianIndex(10, 8))
70+
copy!(x, r1, y, r2)
71+
copy!(a, r1, b, r2)
72+
@test x == Array(a)
73+
74+
x2 = zeros(Float32, 10, 10)
75+
copy!(x2, r1, b, r2)
76+
@test x2 == x
77+
78+
fill!(a, 0f0)
79+
copy!(a, r1, y, r2)
80+
@test Array(a) == x
81+
end
82+
83+
@testset "vcat + hcat" begin
84+
x = zeros(Float32, 10, 10)
85+
y = rand(Float32, 20, 10)
86+
a = Typ(x)
87+
b = Typ(y)
88+
@test vcat(x, y) == Array(vcat(a, b))
89+
z = rand(Float32, 10, 10)
90+
c = Typ(z)
91+
@test hcat(x, z) == Array(hcat(a, c))
92+
93+
against_base(hcat, Typ{Float32}, (3, 3), (3, 3))
94+
against_base(vcat, Typ{Float32}, (3, 3), (3, 3))
95+
end
96+
97+
@testset "reinterpret" begin
98+
a = rand(Complex64, 22)
99+
A = Typ(a)
100+
af0 = reinterpret(Float32, a)
101+
Af0 = reinterpret(Float32, A)
102+
@test Array(Af0) == af0
103+
104+
a = rand(Complex64, 10 * 10)
105+
A = Typ(a)
106+
af0 = reinterpret(Float32, a, (20, 10))
107+
Af0 = reinterpret(Float32, A, (20, 10))
108+
@test Array(Af0) == af0
109+
end
110+
111+
@testset "ntuple test" begin
112+
result = Typ(Vector{NTuple{3, Float32}}(1))
113+
gpu_call(ntuple_test, result, (result, Val{3}()))
114+
@test result[1] == (77, 2*77, 3*77)
115+
x = 88f0
116+
gpu_call(ntuple_closure, result, (result, Val{3}(), x))
117+
@test result[1] == (x, 2*x, 3*x)
118+
end
119+
120+
@testset "cartesian iteration" begin
121+
Ac = rand(Float32, 32, 32)
122+
A = Typ(Ac)
123+
result = zeros(A)
124+
gpu_call(cartesian_iter, result, (A, result, size(A)))
125+
Array(result) == Ac
126+
end
127+
128+
@testset "Custom kernel from Julia function" begin
129+
x = Typ(rand(Float32, 100))
130+
y = Typ(rand(Float32, 100))
131+
gpu_call(clmap!, x, (sin, x, y))
132+
jy = Array(y)
133+
@test map!(sin, jy, jy) Array(x)
134+
end
135+
T = Typ{Float32}
136+
@testset "map" begin
137+
against_base((a, b)-> map(+, a, b), T, (10,), (10,))
138+
against_base((a, b)-> map!(-, a, b), T, (10,), (10,))
139+
against_base((a, b, c, d)-> map!(*, a, b, c, d), T, (10,), (10,), (10,), (10,))
140+
end
141+
end
142+
end

src/testsuite/general.jl

Lines changed: 0 additions & 135 deletions
This file was deleted.

test/indexing.jl renamed to src/testsuite/indexing.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
module Indexing
2-
31
using GPUArrays, StaticArrays
42
using Base.Test, GPUArrays.TestSuite
53

6-
function main(Typ)
4+
function run_indexing(Typ)
75
@testset "indexing" begin
86
for T in (Float32, Int32, SVector{3, Float32})
97
@testset "Indexing with $T" begin
@@ -44,5 +42,3 @@ function main(Typ)
4442
end
4543
end
4644
end
47-
48-
end

src/testsuite/testsuite.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ include("fft.jl")
3939
include("gpuinterface.jl")
4040
include("linalg.jl")
4141
include("mapreduce.jl")
42+
include("base.jl")
43+
include("indexing.jl")
4244
# include("vector.jl")
4345

4446
function supported_eltypes()
@@ -50,12 +52,14 @@ Runs the test suite on array type `Typ`
5052
"""
5153
function run_tests(Typ)
5254
run_gpuinterface(Typ)
55+
run_base(Typ)
5356
run_blas(Typ)
5457
run_broadcasting(Typ)
5558
run_construction(Typ)
5659
run_fft(Typ)
5760
run_linalg(Typ)
5861
run_mapreduce(Typ)
62+
run_indexing(Typ)
5963
end
6064

6165
export against_base, run_tests, supported_eltypes

0 commit comments

Comments
 (0)