Skip to content
This repository was archived by the owner on May 27, 2021. It is now read-only.

Commit 345956c

Browse files
committed
Add tests for all those complex functions (and a bunch of real ones)
1 parent 323aa8f commit 345956c

File tree

2 files changed

+125
-5
lines changed

2 files changed

+125
-5
lines changed

src/device/cuda/math.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
@inline atan2(x::Float64, y::Float64) = @wrap __nv_atan2(x::double, y::double)::double
3737
@inline atan2(x::Float32, y::Float32) = @wrap __nv_atan2f(x::float, y::float)::float
3838

39-
@inline angle(x::ComplexF64) = atan2(x.re, y.im)
40-
@inline angle(x::ComplexF32) = atan2(x.re, y.im)
41-
@inline angle(x::Float64) = 0.0
42-
@inline angle(x::Float32) = 0.0
39+
@inline angle(x::ComplexF64) = atan2(x.im, x.re)
40+
@inline angle(x::ComplexF32) = atan2(x.im, x.re)
41+
@inline angle(x::Float64) = signbit(x) * -3.141592653589793
42+
@inline angle(x::Float32) = signbit(x) * -3.1415927f0
4343

4444
## hyperbolic
4545

test/device/cuda.jl

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,129 @@ end
9999
val = Array(buf)
100100
@test val[] x^y
101101
end
102-
end
103102

104103

104+
@testset "angle" begin
105+
buf = CuTestArray(zeros(Float32))
106+
cbuf = CuTestArray(zeros(Float32))
107+
108+
function cuda_kernel(a, x)
109+
a[] = CUDAnative.angle(x)
110+
return
111+
end
112+
113+
#op(::Float32)
114+
x = rand(Float32)
115+
@cuda cuda_kernel(buf, x)
116+
val = Array(buf)
117+
@test val[] angle(x)
118+
@cuda cuda_kernel(buf, -x)
119+
val = Array(buf)
120+
@test val[] angle(-x)
121+
122+
#op(::ComplexF32)
123+
x = rand(ComplexF32)
124+
@cuda cuda_kernel(cbuf, x)
125+
val = Array(cbuf)
126+
@test val[] angle(x)
127+
@cuda cuda_kernel(cbuf, -x)
128+
val = Array(cbuf)
129+
@test val[] angle(-x)
130+
131+
#op(::Float64)
132+
x = rand(Float64)
133+
@cuda cuda_kernel(buf, x)
134+
val = Array(buf)
135+
@test val[] angle(x)
136+
@cuda cuda_kernel(buf, -x)
137+
val = Array(buf)
138+
@test val[] angle(-x)
139+
140+
#op(::ComplexF64)
141+
x = rand(ComplexF64)
142+
@cuda cuda_kernel(cbuf, x)
143+
val = Array(cbuf)
144+
@test val[] angle(x)
145+
@cuda cuda_kernel(cbuf, -x)
146+
val = Array(cbuf)
147+
@test val[] angle(-x)
148+
end
149+
150+
# dictionary of key=>tuple, where the tuple should
151+
# contain the cpu command and the cuda function to test.
152+
ops = Dict("exp"=>(exp, CUDAnative.exp),
153+
"angle"=>(angle, CUDAnative.angle),
154+
"exp2"=>(exp2, CUDAnative.exp2),
155+
"exp10"=>(exp10, CUDAnative.exp10),
156+
"expm1"=>(expm1, CUDAnative.expm1))
157+
158+
@testset "$key" for key=keys(ops)
159+
cpu_op, cuda_op = ops[key]
160+
161+
buf = CuTestArray(zeros(Float32))
162+
163+
function cuda_kernel(a, x)
164+
a[] = cuda_op(x)
165+
return
166+
end
167+
168+
#op(::Float32)
169+
x = rand(Float32)
170+
@cuda cuda_kernel(buf, x)
171+
val = Array(buf)
172+
@test val[] cpu_op(x)
173+
@cuda cuda_kernel(buf, -x)
174+
val = Array(buf)
175+
@test val[] cpu_op(-x)
176+
177+
#op(::Float64)
178+
x = rand(Float64)
179+
@cuda cuda_kernel(buf, x)
180+
val = Array(buf)
181+
@test val[] cpu_op(x)
182+
@cuda cuda_kernel(buf, -x)
183+
val = Array(buf)
184+
@test val[] cpu_op(-x)
185+
end
186+
187+
# dictionary of key=>tuple, where the tuple should
188+
# contain the cpu command and the cuda function to test.
189+
ops = Dict("exp"=>(exp, CUDAnative.exp),
190+
"abs"=>(abs, CUDAnative.abs),
191+
"abs2"=>(abs2, CUDAnative.abs2),
192+
"angle"=>(angle, CUDAnative.angle),
193+
"log"=>(log, CUDAnative.log))
194+
195+
@testset "Complex - $key" for key=keys(ops)
196+
cpu_op, cuda_op = ops[key]
197+
198+
buf = CuTestArray(zeros(Complex{Float32}))
199+
200+
function cuda_kernel(a, x)
201+
a[] = cuda_op(x)
202+
return
203+
end
204+
205+
#op(::ComplexF32, ::ComplexF32)
206+
x = rand(ComplexF32)
207+
@cuda cuda_kernel(buf, x)
208+
val = Array(buf)
209+
@test val[] cpu_op(x)
210+
@cuda cuda_kernel(buf, -x)
211+
val = Array(buf)
212+
@test val[] cpu_op(-x)
213+
214+
#op(::ComplexF64, ::ComplexF64)
215+
x = rand(ComplexF64)
216+
@cuda cuda_kernel(buf, x)
217+
val = Array(buf)
218+
@test val[] cpu_op(x)
219+
@cuda cuda_kernel(buf, -x)
220+
val = Array(buf)
221+
@test val[] cpu_op(-x)
222+
end
223+
end
224+
105225

106226
############################################################################################
107227

0 commit comments

Comments
 (0)