forked from ARCTraining/swd6_hpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Martin Callaghan
authored and
Martin Callaghan
committed
Feb 24, 2017
1 parent
3183c00
commit f85b55b
Showing
9 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
diffusion.so |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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)) |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |