Skip to content

Commit

Permalink
wio=]
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlei committed Oct 2, 2014
1 parent 15c3e37 commit 53f8f20
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
12 changes: 7 additions & 5 deletions smop/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ timer_py: a.py
clean:
rm -f a.* *.pyc

a.so: a.c
gcc -I /usr/include/python2.7 -L /usr/lib/python2.7 -O2 -shared -o a.so a.c

a.py: solver.m r8_random.m
python main.py $^

a.c: a.py runtime.py
cython a.py
check:
python runtime.py $(FLAGS)
python test_runtime.py $(FLAGS)
Expand All @@ -26,3 +21,10 @@ check:
python test_lexer.py $(FLAGS)
python main.py solver.m r8_random.m && python go.py | tail
#python main.py fastsolver.m && python go.py

%.c: %.pyx
cython -a $^

%.so: %.c
gcc -I /usr/include/python2.7 -O2 -shared -o $@ $^

2 changes: 1 addition & 1 deletion smop/go.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy,time
import a
import solver as a
from runtime import *

def main():
Expand Down
55 changes: 55 additions & 0 deletions smop/solver.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from __future__ import division
import numpy as np
cimport numpy as np
from runtime import *
def solver_(np.ndarray ai,np.ndarray af,int w,int nargout=1):
cdef int nBlocks,m,n,i,j,r,bid,ni,nj,ti,tj,d,dn
cdef np.ndarray a,I,J,mv
rand_(1,2,3)
nBlocks=max_(ai[:])
m,n=size_(ai,nargout=2)
I=matlabarray([0,1,0,- 1])
J=matlabarray([1,0,- 1,0])
a=copy_(ai)
mv=matlabarray([])
while not isequal_(af,a):

bid=ceil_(rand_() * nBlocks)
i,j=find_(a == bid,nargout=2)
r=ceil_(rand_() * 4)
ni=i + I[r]
nj=j + J[r]
if (ni < 1) or (ni > m) or (nj < 1) or (nj > n):
continue
if a[ni,nj] > 0:
continue
ti,tj=find_(af == bid,nargout=2)
d=(ti - i) ** 2 + (tj - j) ** 2
dn=(ti - ni) ** 2 + (tj - nj) ** 2
if (d < dn) and (rand_() > 0.05):
continue
a[ni,nj]=bid
a[i,j]=0
mv[mv.shape[0] + 1,[1,2]]=[bid,r]

return mv
def rand_(*args,**kwargs):
varargin = cellarray(args)
nargin = len(args)+0

global s1,s2,s3
if nargin != 0:
r=0
s1=varargin[1]
s2=varargin[2]
s3=varargin[3]
else:
r,s1,s2,s3=r8_random_(s1,s2,s3,nargout=4)
return r
def r8_random_(double s1,double s2,double s3,nargout=1):
cdef double r
s1=171 * s1 % 30269
s2=172 * s2 % 30307
s3=170 * s3 % 30323
r=(s1 / 30269.0 + s2 / 30307.0 + s3 / 30323.0) % 1.0
return r,s1,s2,s3

0 comments on commit 53f8f20

Please sign in to comment.