using ArrayFire
ArrayFire v3.6.0 (CUDA, 64-bit Linux, build 2858662) Platform: CUDA Toolkit 9.1, Driver: 396.26 [0] GeForce GTX 980, 4041 MB, CUDA Compute 5.2
import Plots: frame, gif, mp4
include("SimpleAnimation.jl")
using SimpleAnimation
import Colors: Gray
const MB = UInt64(1024*1024);
gpu32(a) = AFArray([Float32(a)])
gpu32 (generic function with 1 method)
# generate Laplacian with free edge
# 1D usage: Δ*u
# 2D usage: Δ*u + u*Δ
function genLaplacian(N::Int)
Δ = AFArray(Array(Float32.(SymTridiagonal(-2*ones(N), ones(N-1)))))
Δ[1, 1] = Δ[end, end] = -1.
return Δ
end
genLaplacian (generic function with 1 method)
du_react(u, v) = a - b*u + u*u/(1+c*u*u)/v
dv_react(u, v) = u*u - v
a, b, c = 0.f0, 1.2f0, 0.4f0
Du, Dv = 4.f0, 80.f0
;
N = 256
u = rand(AFArray{Float32}, N, N)
v = rand(AFArray{Float32}, N, N)
Δ = genLaplacian(N)
dt = 0.001f0
@time for count in 1:10
u, v = (
u + dt * du_react.(u, v) + dt * Du * (Δ*u + u*Δ),
v + dt * dv_react.(u, v) + dt * Dv * (Δ*v + v*Δ)
)
afgc(3200MB)
end
0.004319 seconds (1.15 k allocations: 24.188 KiB)
N = 256
#u = rand(AFArray{Float32}, N, N)
#v = rand(AFArray{Float32}, N, N)
u = AFArray(repmat(Float32.(1-cospi.((1:N)/N*20)), 1, N))
v = AFArray(Float32.(0.5*ones(N, N)))
Δ = genLaplacian(N)
dt = 0.001f0
anim = SimpleAnim()
@time for count in 1:1000
u, v = (
u + dt * du_react.(u, v) + dt * Du * (Δ*u + u*Δ),
v + dt * dv_react.(u, v) + dt * Dv * (Δ*v + v*Δ)
)
if mod(count, 10) == 0
#frame(anim, Gray.(clamp.(Array(sync(u)), 0, 1)))
gc(false)
afgc(3200MB)
end
end
#@time gif(anim, "zebra.gif")
Gray.(clamp.(Array(sync(u)), 0, 1))
0.569079 seconds (111.05 k allocations: 2.305 MiB, 11.69% gc time)
u[51:200, 51:200] = rand(AFArray{Float32}, 150, 150)
v[51:200, 51:200] = rand(AFArray{Float32}, 150, 150)
Gray.(clamp.(Array(sync(u)), 0, 1))
@time for count in 1:60000
u, v = (
u + dt * du_react.(u, v) + dt * Du * (Δ*u + u*Δ),
v + dt * dv_react.(u, v) + dt * Dv * (Δ*v + v*Δ)
)
if mod(count, 2000) == 0
frame(anim, Gray.(clamp.(Array(sync(u)), 0, 1)))
gc(false)
afgc(3200MB)
end
end
@time mp4(anim, "zebra.mp4")
86.694858 seconds (6.65 M allocations: 157.291 MiB, 3.34% gc time) 0.172122 seconds (831 allocations: 49.781 KiB)
@time gif(anim, "zebra.gif")
0.224544 seconds (188 allocations: 10.141 KiB)
fn = "zebra.gif"
run(`ffmpeg -v 0 -framerate 10 -loop 0 -i $(anim.dir)/%06d.bmp -i "$(anim.dir)/palette.bmp" -lavfi "paletteuse=dither=sierra2_4a" -y $fn`)
Gray.(clamp.(Array(sync(u)), 0, 1))