Skip to content

Commit

Permalink
Added f2py
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Callaghan authored and Martin Callaghan committed Feb 24, 2017
1 parent 3183c00 commit f85b55b
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
diffusion.so
2 changes: 2 additions & 0 deletions 5_f2py/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
diffusion.so: diffusion.f90
f2py -c -m diffusion --fcompiler=gfortran --opt='-O3' diffusion.f90
17 changes: 17 additions & 0 deletions 5_f2py/diffusion.f90
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions 5_f2py/diffusion_fortran.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions codes/fib.py
Original file line number Diff line number Diff line change
@@ -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))
4 changes: 4 additions & 0 deletions f2py/Makefile
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions f2py/diffusion.f90
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions f2py/diffusion_fortran.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f85b55b

Please sign in to comment.