Skip to content

Instantly share code, notes, and snippets.

@synapticarbors
Last active March 18, 2016 15:45
Show Gist options
  • Save synapticarbors/c6268e25ce22e4d04b4d to your computer and use it in GitHub Desktop.
Save synapticarbors/c6268e25ce22e4d04b4d to your computer and use it in GitHub Desktop.
Minimal example demonstrating cython compilation failure
import numpy as np
cimport numpy as np
cdef packed struct xarr_type:
np.float64_t a
np.int64_t b
np.float64_t c
np.int64_t d
cdef packed struct yarr_type:
np.int32_t a
np.int64_t b
import numpy as np
cimport numpy as np
from libc.math cimport fabs
from data_structures cimport *
DEF _MAX_SIZE = 100
cdef enum:
MAX_SIZE = _MAX_SIZE
cdef xarr_type[:] xarr
cdef xarr_type xarr0[_MAX_SIZE]
cdef inline double funcx(np.int64_t a, np.int64_t b, np.float64_t c):
return 1.0 / (1.0 + fabs(a - b) / c)
import numpy as np
cimport numpy as np
from data_structures cimport *
def init_arrays(x):
cdef:
int k
global xarr
xarr = x
for k in xrange(MAX_SIZE):
xarr0[k] = x[k]
def func(np.int64_t ix):
cdef:
double z
xarr_type* tx
tx = &(xarr[ix])
z = funcx(tx.b, tx.d, tx.a)
return xarr0[ix - 1].a + z
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy
numpy_include = numpy.get_include()
_include = ['.', numpy_include]
_extra = ['-O3', '-w']
if __name__ == '__main__':
extensions = [
Extension('factors', ['factors.pyx',],
depends=['data_structures.pxd'],
include_dirs=_include,
extra_compile_args=_extra),
Extension('xlib', ['xlib.pyx',],
depends=['factors.pxd'],
include_dirs=_include,
extra_compile_args=_extra)
]
setup(
name = "",
ext_modules = cythonize(extensions),
)
import numpy as np
import factors
import xlib
def _funcx(a, b, c):
return 1.0 / (1.0 + abs(a - b) / c)
if __name__ == '__main__':
dtype = [
('a', np.float64),
('b', np.int64),
('c', np.float64),
('d', np.int64)
]
N = 200
data = np.empty(N, dtype=dtype)
data['a'] = np.ones(N, dtype=np.float64)
data['b'] = np.ones(N, dtype=np.int64)
data['c'] = np.linspace(-1, 1, N)
data['d'] = np.zeros(N, dtype=np.int64)
factors.init_arrays(data)
ix = 20
print data[ix - 1]['a'] + _funcx(data[ix]['b'], data[ix]['d'], data[ix]['a'])
print xlib.test(ix)
import numpy as np
cimport numpy as np
from factors import *
from factors cimport *
def test(ix):
return func(ix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment