Skip to content

Commit

Permalink
Added wrapping to cython lecture
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesFergusson committed Nov 26, 2019
1 parent 328e282 commit 4e87599
Show file tree
Hide file tree
Showing 6 changed files with 539 additions and 66 deletions.
558 changes: 494 additions & 64 deletions 9_Cython.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Code/mpipattern8.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
# compute cumulitive sum for our section
x=0
for index, item in enumerate(list1):
x += item
list1[index] = x
x += item
list1[index] = x

# now send cum sum to other ranks. This is a bit fiddly
# we first send from 1mod2 to 0mod2 then from
Expand Down
14 changes: 14 additions & 0 deletions Cython/lib/cexample.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "cexample.h"

int fibonacci(int n){

int i,a,b,tmp;
a=0;
b=1;
for (i=0;i<(n-1);i++){
tmp = a+b;
a = b;
b = tmp;
}
return b;
}
6 changes: 6 additions & 0 deletions Cython/lib/cexample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef C_EXAMPLE_H
#define C_EXAMPLE_H

int fibonacci(int n);

#endif
16 changes: 16 additions & 0 deletions Cython/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

cexample_extension = Extension(
name="wrapcexample",
sources=["wrapcexample.pyx"],
libraries=["cexample"],
library_dirs=["lib"],
include_dirs=["lib"]
)

setup(
name="wrapcexample",
ext_modules=cythonize([cexample_extension])
)
7 changes: 7 additions & 0 deletions Cython/wrapcexample.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cdef extern from "cexample.h":
int fibonacci(int n)

def cfib(n):
cdef m
m = fibonacci(n)
return m

0 comments on commit 4e87599

Please sign in to comment.