diff --git a/.DS_Store b/.DS_Store index 731816a..f88ce6d 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..63d12aa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +diffusion.so diff --git a/5_f2py/Makefile b/5_f2py/Makefile new file mode 100644 index 0000000..3468e69 --- /dev/null +++ b/5_f2py/Makefile @@ -0,0 +1,2 @@ +diffusion.so: diffusion.f90 + f2py -c -m diffusion --fcompiler=gfortran --opt='-O3' diffusion.f90 diff --git a/5_f2py/diffusion.f90 b/5_f2py/diffusion.f90 new file mode 100644 index 0000000..6cb5821 --- /dev/null +++ b/5_f2py/diffusion.f90 @@ -0,0 +1,17 @@ +SUBROUTINE evolve(grid, scratch, D, dt, N, M) + !f2py threadsafe + !f2py intent(in) grid + !f2py intent(inplace) scratch + !f2py intent(in) D + !f2py intent(in) dt + !f2py intent(hide) N + !f2py intent(hide) M + INTEGER :: N, M + DOUBLE PRECISION, DIMENSION(N,M) :: grid, scratch + DOUBLE PRECISION, DIMENSION(N-2, M-2) :: laplacian + DOUBLE PRECISION :: D, dt + + laplacian = grid(3:N, 2:M-1) + grid(1:N-2, 2:M-1) + & + grid(2:N-1, 3:M) + grid(2:N-1, 1:M-2) - 4 * grid(2:N-1, 2:M-1) + scratch(2:N-1, 2:M-1) = grid(2:N-1, 2:M-1) + D * dt * laplacian +END SUBROUTINE evolve diff --git a/5_f2py/diffusion_fortran.py b/5_f2py/diffusion_fortran.py new file mode 100644 index 0000000..06b1ce1 --- /dev/null +++ b/5_f2py/diffusion_fortran.py @@ -0,0 +1,24 @@ +from diffusion import evolve +import numpy as np +import time + +grid_shape = (512, 512) + + +def run_experiment(num_iterations): + scratch = np.zeros(grid_shape, dtype=np.double, order='F') + grid = np.zeros(grid_shape, dtype=np.double, order='F') + + block_low = int(grid_shape[0] * .4) + block_high = int(grid_shape[0] * .5) + grid[block_low:block_high, block_low:block_high] = 0.005 + + start = time.time() + for i in range(num_iterations): + evolve(grid, scratch, 1.0, 0.1) + grid, scratch = scratch, grid + return time.time() - start + +if __name__ == "__main__": + t = run_experiment(500) + print t diff --git a/codes/fib.py b/codes/fib.py new file mode 100644 index 0000000..efaa582 --- /dev/null +++ b/codes/fib.py @@ -0,0 +1,8 @@ +def fib(n): + """Print the Fibonacci series up to n.""" + a, b = 0, 1 + while b < n: + print (b) + a, b = b, a + b + +print (fib(2000)) diff --git a/f2py/Makefile b/f2py/Makefile new file mode 100644 index 0000000..6da5d16 --- /dev/null +++ b/f2py/Makefile @@ -0,0 +1,4 @@ +diffusion.so: diffusion.c + gcc -O3 -std=gnu99 -c diffusion.c + gcc -shared -o diffusion.so diffusion.o + rm -rf diffusion.o diff --git a/f2py/diffusion.f90 b/f2py/diffusion.f90 new file mode 100644 index 0000000..6cb5821 --- /dev/null +++ b/f2py/diffusion.f90 @@ -0,0 +1,17 @@ +SUBROUTINE evolve(grid, scratch, D, dt, N, M) + !f2py threadsafe + !f2py intent(in) grid + !f2py intent(inplace) scratch + !f2py intent(in) D + !f2py intent(in) dt + !f2py intent(hide) N + !f2py intent(hide) M + INTEGER :: N, M + DOUBLE PRECISION, DIMENSION(N,M) :: grid, scratch + DOUBLE PRECISION, DIMENSION(N-2, M-2) :: laplacian + DOUBLE PRECISION :: D, dt + + laplacian = grid(3:N, 2:M-1) + grid(1:N-2, 2:M-1) + & + grid(2:N-1, 3:M) + grid(2:N-1, 1:M-2) - 4 * grid(2:N-1, 2:M-1) + scratch(2:N-1, 2:M-1) = grid(2:N-1, 2:M-1) + D * dt * laplacian +END SUBROUTINE evolve diff --git a/f2py/diffusion_fortran.py b/f2py/diffusion_fortran.py new file mode 100644 index 0000000..06b1ce1 --- /dev/null +++ b/f2py/diffusion_fortran.py @@ -0,0 +1,24 @@ +from diffusion import evolve +import numpy as np +import time + +grid_shape = (512, 512) + + +def run_experiment(num_iterations): + scratch = np.zeros(grid_shape, dtype=np.double, order='F') + grid = np.zeros(grid_shape, dtype=np.double, order='F') + + block_low = int(grid_shape[0] * .4) + block_high = int(grid_shape[0] * .5) + grid[block_low:block_high, block_low:block_high] = 0.005 + + start = time.time() + for i in range(num_iterations): + evolve(grid, scratch, 1.0, 0.1) + grid, scratch = scratch, grid + return time.time() - start + +if __name__ == "__main__": + t = run_experiment(500) + print t