-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu_testing.jl
53 lines (52 loc) · 1.58 KB
/
gpu_testing.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using CuArrays, FileIO, Colors, GPUArrays, BenchmarkTools, ImageShow
using CuArrays: CuArray
"""
The function calculating the Julia set
"""
function juliaset(z0, maxiter)
c = ComplexF32(-0.5, 0.75)
z = z0
for i in 1:maxiter
abs2(z) > 4f0 && return (i - 1) % UInt8
z = z * z + c
end
return maxiter % UInt8 # % is used to convert without overflow check
end
range = 100:50:2^12
cutimes, jltimes = Float64[], Float64[]
function run_bench(in, out)
# use dot syntax to apply `juliaset` to each elemt of q_converted
# and write the output to result
out .= juliaset.(in, 16)
# all calls to the GPU are scheduled asynchronous,
# so we need to synchronize
GPUArrays.synchronize(out)
end
# store a reference to the last results for plotting
last_jl, last_cu = nothing, nothing
for N in range
w, h = N, N
q = [ComplexF32(r, i) for i=1:-(2.0/w):-1, r=-1.5:(3.0/h):1.5]
for (times, Typ) in ((cutimes, CuArray), (jltimes, Array))
# convert to Array or CuArray - moving the calculation to CPU/GPU
q_converted = Typ(q)
result = Typ(zeros(UInt8, size(q)))
for i in 1:10 # 5 samples per size
# benchmarking macro, all variables need to be prefixed with $
t = Base.@elapsed begin
run_bench(q_converted, result)
end
global last_jl, last_cu # we're in local scope
if result isa CuArray
last_cu = result
else
last_jl = result
end
push!(times, t)
end
end
end
cu_jl = hcat(Array(last_cu), last_jl)
cmap = colormap("Blues", 16 + 1)
color_lookup(val, cmap) = cmap[val + 1]
color_lookup.(cu_jl, (cmap,))