Skip to content

Commit

Permalink
Vectorized and devectorized versions of the laplace benchmark now
Browse files Browse the repository at this point in the history
give the same results (#1168)
  • Loading branch information
ViralBShah committed Aug 20, 2012
1 parent 2c74873 commit 6404590
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions test/perf2/laplace.jl
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
function laplace{T}(u::Matrix{T}, dx2, dy2)
uout = zeros(T, size(u))
for i = 2:size(u,1)-1
for j = 2:size(u,2)-1
uout[i,j] = ((u[i-1,j]+u[i+1,j])*dx2 + (u[i,j-1]+u[i,j+1])*dy2)/(2*(dx2+dy2))
end
end
return uout
end

function laplace_iter(u, dx, dy, Niter)
dx2 = dx^2
dy2 = dy^2
for i = 1:Niter
u = laplace(u, dx2, dy2)
function laplace_iter_devec(u, dx2, dy2, Niter, N)
uout = copy(u)
for iter = 1:Niter
for i = 2:N-1
for j = 2:N-1
uout[i,j] = ( (u[i-1,j]+u[i+1,j])*dy2 + (u[i,j-1]+u[i,j+1])*dx2 ) * (1./(2*(dx2+dy2)))
end
end
u, uout = uout, u
end
return u
end

function laplace_devec()
N = 150
u = zeros(N,N)
u = zeros(N, N)
u[1,:] = 1
Niter = 2^10
dx = 0.1
dy = 0.1
u = laplace_iter(u, dx, dy, Niter)
dx2 = dy2 = 0.1*0.1
u = laplace_iter_devec(u, dx2, dy2, Niter, N)
end

function laplace_iter_vec(u, dx, dy, Niter, N)
dx2 = dx*dx;
dy2 = dy*dy;
function laplace_iter_vec(u, dx2, dy2, Niter, N)
for i = 1:Niter
u[2:N-1, 2:N-1] = ((u[1:N-2, 2:N-1] + u[3:N, 2:N-1])*dy2 + (u[2:N-1,1:N-2] + u[2:N-1, 3:N])*dx2) * (1./ (2*(dx2+dy2)));
u[2:N-1, 2:N-1] = ((u[1:N-2, 2:N-1] + u[3:N, 2:N-1])*dy2 + (u[2:N-1,1:N-2] + u[2:N-1, 3:N])*dx2) * (1./ (2*(dx2+dy2)))
end
return u
end
Expand All @@ -41,7 +32,6 @@ function laplace_vec()
u = zeros(N,N)
u[1,:] = 1
Niter = 2^10
dx = 0.1
dy = 0.1
u = laplace_iter_vec(u, dx, dy, Niter, N)
dx2 = dy2 = 0.1*0.1
u = laplace_iter_vec(u, dx2, dy2, Niter, N)
end

1 comment on commit 6404590

@JeffBezanson
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add an assertion during the perf test that they give the same result.

Please sign in to comment.