MIT License
Copyright (C) 2018 Lirimy
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 Colors: Gray
#import Images: display
import Plots: gif, mp4, frame
include("SimpleAnimation.jl")
using SimpleAnimation
#using Plots
const MB = UInt64(2^20);
gpu32(a) = AFArray([Float32(a)])
rnd() = rand(AFArray{Float32}, m, n)
initS() = AFArray(Float32.(rand([1, -1], m, n)))
initS (generic function with 1 method)
# generate padding matrix of circular edge
function genP(n)
P = eye(AFArray{Float32}, n+2)
P[1, 1] = P[end, end] = 0
P[1, end-1] = P[end, 2] = 1
return P
end
genP (generic function with 1 method)
function pconv!(u, ret, K=kernel, Pl=Pl, Pr=Pr)
conved = convolve2(u, K, AF_CONV_EXPAND, AF_CONV_SPATIAL)
ret .= (Pl' * conved * Pr)[2:end-1, 2:end-1]
nothing
end
"""Padding and Convolution
K: kernel
P: padding matrix
size(P) = size(u) + size(K) - 1
P example of free edge:
AFArray: 5×5 Array{Float32,2}:
0.0 1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0 0.0
0.0 0.0 1.0 0.0 0.0
0.0 0.0 0.0 1.0 0.0
0.0 0.0 0.0 1.0 0.0
"""
function pconv(u, K=kernel, Pl=Pl, Pr=Pr)
ret = similar(u)
pconv!(u, ret, K, Pl, Pr)
ret
end
pconv
b = gpu32(-log(1+sqrt(2))) # 2b
kernel = AFArray(Float32.([0 1 0; 1 0 1; 0 1 0]))
function prob(s::AFArray{Float32, 2})::AFArray{Float32, 2}
exp(b .* s .* pconv(s))
end
prob (generic function with 1 method)
function update!(s::AFArray{Float32, 2}, elim=1/10)
s .*= 1 - 2 * signbit(rnd() - prob(s) .* signbit(rnd().-gpu32(elim)))
end
update! (generic function with 2 methods)
#m, n = 1080, 1920
m, n = 360, 640
Pl, Pr = genP(m), genP(n)
s = initS();
b = gpu32(-log(1+sqrt(2)))
afgc(1)
s = initS()
anim = SimpleAnim()
@time for count in 1:450
for i in 1:10
update!(s)
end
frame(anim, Gray.((Array(sync(s))+1)/2))
# when benchmarking, uncomment these 2 lines and comment out frame
#sync(s)
#gc(false)
afgc(3200MB)
end
@time gif(anim, "ising.gif")
7.548005 seconds (573.27 k allocations: 1.753 GiB, 3.72% gc time) 4.014858 seconds (971 allocations: 55.891 KiB)
@time mp4(anim, "ising.mp4")
5.856016 seconds (940 allocations: 51.953 KiB)
Gray.((Array(sync(s))+1)/2)