Created
February 7, 2013 00:03
-
-
Save dfarmer/4727104 to your computer and use it in GitHub Desktop.
LuaJIT laplace solver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'torch' | |
local ffi = require("ffi") | |
function print_mat(mat) | |
for r=1,#mat do | |
row = '' | |
for c=1,#mat[r] do | |
row = row .. mat[r][c] .. '\t' | |
end | |
print(row) | |
end | |
end | |
function calc(N, Niter) | |
local matrix_t = ffi.typeof("double[$][$]", N, N) | |
--u = ffi.new("double[$][$]", N, N | |
u = matrix_t() | |
for i=0,N-1 do | |
u[0][i] = 1 | |
end | |
for i=0,Niter-1 do | |
lua_update(u, N) | |
end | |
return u | |
end | |
function lua_update(u, N) | |
local dx = 0.1 | |
local dy = 0.1 | |
local dx2 = dx*dx | |
local dy2 = dy*dy | |
-- Assume u is square | |
ny,nx = N, N | |
for i=1,nx-1 do | |
for j=1,ny-1 do | |
u[i][j] = ((u[i+1][j] + u[i-1][j]) * dy2 + | |
(u[i][j+1] + u[i][j-1]) * dx2) / (2*(dx2+dy2)) | |
end | |
end | |
end | |
timer = torch.Timer() | |
u = calc(100,8000) | |
print(timer:time().real) | |
print(u[1][1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment