Skip to content
Browse files

fin

  • Loading branch information...
1 parent 4908d29 commit 52e26d2c1cdb4cd32b1d0712b383e6f0598276a6 @nzjrs nzjrs committed Apr 13, 2012
View
3 week3/lib/ctnplibprime.py
@@ -17,6 +17,9 @@ class Prime:
def __init__(self, n):
self._ctx = lib.prime_new(n)
+ def _print(self):
+ lib.prime_print(self._ctx)
+
def get_data(self):
l = ct.c_int()
data = lib.prime_get_data(self._ctx, ct.byref(l))
View
2 week3/lib/libprime.h
@@ -3,6 +3,8 @@
#define PRIME_MAX 10000
+/* A library for computing sequences of primes */
+
typedef struct _ctx ctx_t;
ctx_t *prime_new(unsigned int len);
View
8 week3/lib/modprime.c
@@ -4,20 +4,14 @@
static PyObject* wrap_primes(PyObject* self, PyObject* args)
{
- int *data;
unsigned int l, i;
-
if (!PyArg_ParseTuple(args, "I", &l))
return NULL;
-
- data = calloc(l,sizeof(int));
- calculate_primes(data, l);
-
+ int *data = create_primes(l);
PyObject *lst = PyList_New(l);
for (i = 0; i < l; i++)
PyList_SET_ITEM(lst, i, PyInt_FromLong(data[i]));
free(data);
-
return lst;
}
View
1 week3/lib/prime.py
@@ -1,4 +1,5 @@
def primes(kmax):
+ """ A really bad routine to compute kmax primes """
if kmax > 100000:
kmax = 100000
k = 0
View
21 week3/lib/test.sh
@@ -2,25 +2,30 @@
export LD_LIBRARY_PATH=`pwd`
-echo -e "\ntestlibprime"
+echo -e "\n python"
+time $(python -c 'import prime; print prime.primes(2000)' > /dev/null)
+
+echo -e "\n c"
time $(./testlibprime >/dev/null)
-echo -e "\npyxlibprime"
+echo -e "\n pyxlibprime"
time $(python -c 'import pyxlibprime; pyxlibprime.Prime(2000)._print()' > /dev/null)
-echo -e "\nctlibprime"
+echo -e "\n ctlibprime"
time $(python -c 'import ctlibprime; ctlibprime.Prime(2000)._print()' > /dev/null)
-echo -e "\npyxprime"
+echo -e "\n ctnplibprime"
+time $(python -c 'import ctnplibprime; print ctnplibprime.calculate_primes(2000)' > /dev/null)
+
+echo -e "\n pyxprime"
time $(python -c 'import pyxprime; print pyxprime.primes(2000)' > /dev/null)
+echo -e "\n pyprime"
+time $(python -c 'import pyprime; print pyprime.primes(2000)' > /dev/null)
+
#echo -e "\npyxnpprime"
#time $(python -c 'import pyxnpprime; print pyxnpprime.primes(2000)' > /dev/null)
-echo -e "\npyprime"
-time $(python -c 'import pyprime; print pyprime.primes(2000)' > /dev/null)
-echo -e "\nprime"
-time $(python -c 'import prime; print prime.primes(2000)' > /dev/null)
View
31 week3/misc/doweave.py
@@ -0,0 +1,31 @@
+import time
+from numpy import *
+
+import weavedemo
+
+N_array = 10000
+N_py = 200
+N_c = 10000
+
+ratio = float(N_c) / N_py
+
+arr = [0]*N_array
+t1 = time.time()
+for i in xrange(N_py):
+ weavedemo.ramp(arr, N_array, 0.0, 1.0)
+t2 = time.time()
+py_time = (t2 - t1) * ratio
+print 'python (seconds*ratio):', py_time
+print 'arr[500]:', arr[500]
+
+arr1 = array([0]*N_array,float)
+# First call compiles function or loads from cache.
+# I'm not including this in the timing.
+weavedemo.ramp_numeric1(arr1, 0.0, 1.0)
+t1 = time.time()
+for i in xrange(N_c):
+ weavedemo.ramp_numeric1(arr1, 0.0, 1.0)
+t2 = time.time()
+c_time = (t2 - t1)
+print 'compiled numeric1 (seconds, speed up):', c_time, py_time/ c_time
+print 'arr[500]:', arr1[500]
View
8 week3/misc/rp.py
@@ -0,0 +1,8 @@
+from rpy2.robjects import r
+r('x <- rnorm(100)') # generate x at R
+r('y <- x + rnorm(100,sd=0.5)') # generate y at R
+r('plot(x,y)') # have R plot them
+r('lmout <- lm(y~x)') # run the regression
+r('print(lmout)') # print from R
+loclmout = r('lmout') # download lmout from R to Python
+print loclmout # print locally
View
18 week3/misc/weavedemo.py
@@ -0,0 +1,18 @@
+import scipy.weave as weave
+
+def ramp(result, size, start, end):
+ step = (end-start)/(size-1)
+ for i in xrange(size):
+ result[i] = start + step*i
+
+def ramp_numeric1(result,start,end):
+ code = """
+ const int size = Nresult[0];
+ const double step = (end-start)/(size-1);
+ double val = start;
+ for (int i = 0; i < size; i++)
+ *result++ = start + step*i;
+ """
+ weave.inline(code,['result','start','end'],compiler='gcc')
+
+
View
60 week3/presentation.pin
@@ -13,32 +13,60 @@ Why
✔ speeding up hot loops
✔ interfacing with native libraries
- ✔ improving performance in multi-threaded applications
- ✔ interfacing with other computational environments
+ ✔ improving multi-threaded performance (GIL)
+ ✔ interfacing with other environments
✔ enjoying segfaults
--- [slide-contents-exec=source-highlight -s c -i lib/libprime.h -o STDOUT --outlang-def=./pango.outlang]
+How
+ ✔ Python modules
+ ✔ ctypes
+ ✔ cython
+ ✗ SWIG
--- [slide-contents-exec=source-highlight -s py -i lib/modlibprime.pyx -o STDOUT --outlang-def=./pango.outlang]
+-- [command=gnome-terminal] [slide-contents-exec=source-highlight -s py -i lib/prime.py -o STDOUT --outlang-def=./pango.outlang]
+
+-- [command=gnome-terminal] [slide-contents-exec=source-highlight -s c -i lib/libprime.h -o STDOUT --outlang-def=./pango.outlang]
+
+-- [slide-contents-exec=source-highlight -s c -i lib/modprime.c -o STDOUT --outlang-def=./pango.outlang]
+
+-- [slide-contents-exec=source-highlight -s py -i lib/modprimesetup.py -o STDOUT --outlang-def=./pango.outlang]
+
+-- [slide-contents-exec=source-highlight -s py -i lib/pyxprime.pyx -o STDOUT --outlang-def=./pango.outlang]
+
+-- [black] [text-color=white] [font=Sans 40px] [shading-color=black] [shading-opacity=0.66]
+Interfacing with Object Orientated Libraries
-- [slide-contents-exec=source-highlight -s py -i lib/ctlibprime.py -o STDOUT --outlang-def=./pango.outlang]
-R example
-GObject / Qt
+-- [slide-contents-exec=source-highlight -s py -i lib/pyxlibprime.pyx -o STDOUT --outlang-def=./pango.outlang]
+
+-- [black] [text-color=white] [font=Sans 40px] [shading-color=black] [shading-opacity=0.66]
+Memory Management and Numpy
-http://davidz25.blogspot.com/2011/07/writing-c-library-intro-conclusion-and.html
-http://0pointer.de/blog/projects/libabc.html
+-- [slide-contents-exec=source-highlight -s py -i lib/pyxnpprime.pyx -o STDOUT --outlang-def=./pango.outlang]
+
+-- [slide-contents-exec=source-highlight -s py -i lib/ctnplibprime.py -o STDOUT --outlang-def=./pango.outlang]
+
+-- [black] [text-color=white] [font=Sans 40px] [shading-color=black] [shading-opacity=0.66]
+Other...
+
+-- [command=_vbc python misc/doweave.py] [slide-contents-exec=source-highlight -s py -i misc/weavedemo.py -o STDOUT --outlang-def=./pango.outlang]
+
+-- [command=_vbc python misc/rp.py] [slide-contents-exec=source-highlight -s py -i misc/rp.py -o STDOUT --outlang-def=./pango.outlang]
+
+--
+Writing Nice libraries
+ - http://davidz25.blogspot.com/2011/07/writing-c-library-intro-conclusion-and.html
+ - http://0pointer.de/blog/projects/libabc.html
Profiling
-http://packages.python.org/line_profiler/
+ - http://packages.python.org/line_profiler/
GIL
-http://wiki.python.org/moin/GlobalInterpreterLock
-
-Weave/etc (from email)
+ - http://wiki.python.org/moin/GlobalInterpreterLock
Numpy
-http://www.scipy.org/PerformancePython
-http://www.scipy.org/Cookbook/Ctypes
-http://rebrained.com/?p=458
-http://technicaldiscovery.blogspot.com/2011/06/speeding-up-python-numpy-cython-and.html
+ - http://www.scipy.org/PerformancePython
+ - http://www.scipy.org/Cookbook/Ctypes
+ - http://rebrained.com/?p=458
+ - http://technicaldiscovery.blogspot.com/2011/06/speeding-up-python-numpy-cython-and.html

0 comments on commit 52e26d2

Please sign in to comment.
Something went wrong with that request. Please try again.