versioninfo() # GeForce GTX 980 ENV["JULIA_DEBUG"] = "CUDAnative"; using CUDAnative #using CuArrays #using CUDAdrv using Random: rand! using CuArrays.CURAND using Parameters # Show results # ]add ImageMagick # ]add https://github.com/Lirimy/ImageContainers.jl # ]add https://github.com/Lirimy/SimpleHeatmaps.jl using SimpleHeatmaps function shrinkshow(s, ratio::Int=4) r = ratio ss = Array(s) m, n = size(ss) @assert mod(m, r) == 0 @assert mod(n, r) == 0 shrinked = r^2 \ reshape( sum(sum( reshape(ss, (r, m÷r, r, n÷r)), dims=1), dims=3), (m÷r, n÷r) ) matshow(@. 2 \ (shrinked + 1)) end struct IsingPlan rnd m n β::Float32 threads blocks end function genplan(m=1024, n=1024, β=log(1+sqrt(2))) rnd = curand(m, n) s = @. Int32(2 * (rnd < 0.5f0) - 1) β = Float32(β) threads = (32, 32) blocks = @. ceil(Int, (m, n) / threads) (s, IsingPlan(rnd, m, n, β, threads, blocks)) end s, iplan = genplan(3072, 4096) function _update!(s, rnd, m, n, β, white=false) i = (blockIdx().x-1) * blockDim().x + threadIdx().x j = (blockIdx().y-1) * blockDim().y + threadIdx().y if i ≤ m && j ≤ n && iseven(i+j) == white @inbounds ajs = ( s[ifelse(i+1 ≤ m, i+1, 1), j] + s[ifelse(i-1 ≥ 1, i-1, m), j] + s[i, ifelse(j+1 ≤ n, j+1, 1)] + s[i, ifelse(j-1 ≥ 1, j-1, n)] ) @inbounds prob = CUDAnative.exp_fast(-β * s[i, j] * ajs) @inbounds s[i, j] = ifelse(rnd[i, j] < prob, -s[i, j], s[i, j]) end return end function update!(s, iplan) @unpack rnd, m, n, β, threads, blocks = iplan rand!(rnd) @cuda blocks=blocks threads=threads _update!(s, rnd, m, n, β, true) @cuda blocks=blocks threads=threads _update!(s, rnd, m, n, β, false) nothing end # compile update!(s, iplan) # Main @time for i in 1:10000 update!(s, iplan) end shrinkshow(s, 8) # Animation anim = @time openanim(:gif) do aio for i in 1:10 #frames for j in 1:16 update!(s, iplan) end img = shrinkshow(s, 16) addframe(aio, img) end end