pyodide/benchmark/benchmarks/fdtd.py

37 lines
1.0 KiB
Python
Raw Normal View History

2018-10-03 18:59:01 +00:00
# http://stackoverflow.com/questions/19367488/converting-function-to-numbapro-cuda
# setup: N = 10 ; import numpy ; a = numpy.random.rand(N,N)
# run: fdtd(a,10)
2018-04-05 22:07:33 +00:00
2018-10-03 18:59:01 +00:00
# pythran export fdtd(float[][], int)
2018-04-05 22:07:33 +00:00
import numpy as np
2018-10-03 18:59:01 +00:00
2018-04-05 22:07:33 +00:00
def fdtd(input_grid, steps):
grid = input_grid.copy()
old_grid = np.zeros_like(input_grid)
previous_grid = np.zeros_like(input_grid)
l_x = grid.shape[0]
l_y = grid.shape[1]
for i in range(steps):
np.copyto(previous_grid, old_grid)
np.copyto(old_grid, grid)
for x in range(l_x):
for y in range(l_y):
2018-10-03 18:59:01 +00:00
grid[x, y] = 0.0
if 0 < x + 1 < l_x:
grid[x, y] += old_grid[x + 1, y]
if 0 < x - 1 < l_x:
grid[x, y] += old_grid[x - 1, y]
if 0 < y + 1 < l_y:
grid[x, y] += old_grid[x, y + 1]
if 0 < y - 1 < l_y:
grid[x, y] += old_grid[x, y - 1]
grid[x, y] /= 2.0
grid[x, y] -= previous_grid[x, y]
2018-04-05 22:07:33 +00:00
return grid