Skip to content

Commit

Permalink
basic ffi demo - candidate for python3
Browse files Browse the repository at this point in the history
  • Loading branch information
jonwright committed Sep 6, 2017
1 parent 9dc4c7b commit 4ef502b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions howto_compile_win64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ setenv /x64 /release
... and "python setup.py build" should work


# Using compiler from microsoft for python 2.7 I seem to need this for cffi

set VS90COMNTOOLS="C:\Users\wright\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0"

=========== Using mingwpy ===============

pip install -i https://pypi.anaconda.org/carlkl/simple mingwpy
Expand Down
35 changes: 35 additions & 0 deletions src/build_ffi_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Try some interfacing via cffi ...

from cffi import FFI


ffi = FFI()
ffi.set_source(
"_ffi_diffraction",
r"""
#include "omp.h"
double sum(double numbers[],int num_elements){
int i;
double s=0.0;
#pragma omp parallel for reduction(+: s)
for (i=0; i<num_elements; i++)
{
s = s + numbers[i];
}
return s;
}
""",
Libraries = [] ,
extra_compile_args = ["/openmp",],
extra_link_args = [],
)


ffi.cdef("""
double sum(double [],int);
""")

if __name__ == "__main__":
ffi.compile(verbose=True)

27 changes: 27 additions & 0 deletions src/run_ffi_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


# Try some interfacing via cffi ...

from _ffi_diffraction import ffi, lib
import numpy as np
import time
np.random.seed(42)
# 1K 1M 1G
size = 128 * 1024 * 1024 * 0.5
ar = np.random.random( int(size) )

print ar.shape

N=100
start = time.clock()
for i in range(N):
s = lib.sum( ffi.cast('double *', ar.ctypes.data ), len(ar) )
print "FFI", s , (time.clock()-start)/N

N=10
start = time.clock()
for i in range(N):
sn = ar.sum()
print "Numpy",sn, (time.clock() - start)/N
#raw_input()

0 comments on commit 4ef502b

Please sign in to comment.