Skip to content

Instantly share code, notes, and snippets.

@eschnett
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eschnett/e7a7cb4555143cdd220b to your computer and use it in GitHub Desktop.
Save eschnett/e7a7cb4555143cdd220b to your computer and use it in GitHub Desktop.
function init(u)
n = length(u)
dx = 1.0 / (n-1)
@fastmath @inbounds @simd for i in 1:n
u[i] = sin(2pi*dx*i)
end
end
function deriv(u, du)
n = length(u)
dx = 1.0 / (n-1)
@fastmath @inbounds du[1] = (u[2] - u[1]) / dx
@fastmath @inbounds @simd for i in 2:n-1
du[i] = (u[i+1] - u[i-1]) / (2*dx)
end
@fastmath @inbounds du[n] = (u[n] - u[n-1]) / dx
end
function norm(u)
n = length(u)
T = eltype(u)
s = zero(T)
@fastmath @inbounds @simd for i in 1:n
s += u[i]^2
end
@fastmath @inbounds return sqrt(s/n)
end
function main()
n = 2000
u = Array(Float64, n)
init(u)
du = similar(u)
deriv(u, du)
nu = norm(du)
@time for i in 1:10^6
deriv(u, du)
nu = norm(du)
end
println(nu)
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment