Skip to content

Instantly share code, notes, and snippets.

@ZacLN
Last active August 29, 2015 14:06
Show Gist options
  • Save ZacLN/3da7be1fe99681a5bd14 to your computer and use it in GitHub Desktop.
Save ZacLN/3da7be1fe99681a5bd14 to your computer and use it in GitHub Desktop.
slow code
#G.n::Int64
#G.d::Int64
#G.q::Int64
#G.grid::Array{Float64,G.n,G.d}
#G.lvl_s::Array{Float64,G.n,G.d}
#G.lvl_l::Array{Float64,G.q+1} indices partitioning 1:G.n
using Sparse
# a simple triangular function
function basis_func(x::Float64,xij::Float64,mi::Float64) :inline
if (mi==1)
return 1.0
elseif (abs(x-xij)<(1.0/(mi-1.0)))
return (1.0-(mi-1.0)*abs(x-xij))
else
return 0.0
end
end
function w_getj(G::sparse_grid,A::Vector{Float64})
Aold = zeros(G.n)
dA = zeros(G.n)
w = zeros(G.n)
@inbounds w[1] = A[1]
fill!(dA,w[1])
Aold += A[1]
for q in 1:G.q
# @inbounds w[G.lvl_l[q]:G.lvl_l[q+1]-1]=A[G.lvl_l[q]:G.lvl_l[q+1]-1]-Aold[G.lvl_l[q]:G.lvl_l[q+1]-1]
for i = G.lvl_l[q]:G.lvl_l[q+1]-1
@inbounds w[i] = A[i] - Aold[i]
end
@simd for i in 1:G.n
temp = 0.0
@inbounds dA[i] = 0.0
for ii in G.lvl_l[q]:G.lvl_l[q+1]-1
temp2=1.0
for d=1:G.d
@inbounds temp2 *= basis_func(G.grid[i,d],G.grid[ii,d],G.lvl_s[ii,d])
end
@inbounds temp += temp2*w[ii]
end
@inbounds dA[i] = temp
end
# Aold += dA
for i = 1:G.n
Aold[i] += dA[i]
end
end
return w
end
G = grid_CC(6,3)
A = sin(G.grid[:,1])+cos(G.grid[:,2]).^2
@time for i = 1:100 w_get(G,A) end
@time for i = 1:100 w_getj(G,A) end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment