Example cython code appears to compile correctly, but; always fails to load the .pyd as shown below:
%%cython
cimport cython
from libc.math cimport exp, sqrt, pow, log, erf
@cython.cdivision(True)
cdef double std_norm_cdf(double x) nogil:
return 0.5*(1+erf(x/sqrt(2.0)))
@cython.cdivision(True)
def black_scholes(double s, double k, double t, double v,
double rf, double div, double cp):
"""Price an option using the Black-Scholes model.
s : initial stock price
k : strike price
t : expiration time
v : volatility
rf : risk-free rate
div : dividend
cp : +1/-1 for call/put
"""
cdef double d1, d2, optprice
with nogil:
d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))
d2 = d1 - v*sqrt(t)
optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \
cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)
return optprice
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-16-4d3d17a3a5b5> in <module>()
----> 1 get_ipython().run_cell_magic('cython', '', 'cimport cython\nfrom libc.math cimport exp, sqrt, pow, log, erf\n\n@cython.cdivision(True)\ncdef double std_norm_cdf(double x) nogil:\n return 0.5* (1+erf(x/sqrt(2.0)))\n\n@cython.cdivision(True)\ndef black_scholes(double s, double k, double t, double v,\n double rf, double div, double cp):\n """Price an option using the Black-Scholes model.\n \n s : initial stock price\n k : strike price\n t : expiration time\n v : volatility\n rf : risk-free rate\n div : dividend\n cp : +1/-1 for call/put\n """\n cdef double d1, d2, optprice\n with nogil:\n d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))\n d2 = d1 - v*sqrt(t)\n optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\\n cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)\n return optprice')
C:\Users\Mike\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
2160 magic_arg_s = self.var_expand(line, stack_depth)
2161 with self.builtin_trap:
-> 2162 result = fn(magic_arg_s, cell)
2163 return result
2164
C:\Users\Mike\Anaconda3\lib\site-packages\IPython\extensions\cythonmagic.py in cython(self, line, cell)
C:\Users\Mike\Anaconda3\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
191 # but it's overkill for just that one bit of state.
192 def magic_deco(arg):
--> 193 call = lambda f, *a, **k: f(*a, **k)
194
195 if callable(arg):
C:\Users\Mike\Anaconda3\lib\site- packages\IPython\extensions\cythonmagic.py in cython(self, line, cell)
269 self._code_cache[key] = module_name
270
--> 271 module = imp.load_dynamic(module_name, module_path)
272 self._import_all(module)
273
MemoryError:
%debug
> c:\users\mike\anaconda3\lib\site- packages\ipython\extensions\cythonmagic.py(271)cython()
270
--> 271 module = imp.load_dynamic(module_name, module_path)
272 self._import_all(module)
ipdb> display module_name
display module_name: '_cython_magic_f60db5be6ea30a0e9aabaffef198b964'
ipdb> display module_path
display module_path: 'C:\\Users\\Mike\\.ipython\\cython\\_cython_magic_f60db5be6ea30a0e9aabaffef198b964.pyd'
The cython_magic_f60db<...>.pyd is found int the correct directory. No other details are given. It does not appear to be a system memory exhaustion.
Thanks for any help. While I've seen several people elude to having similar problems, it appears none have been completely answered.
PS. Ran same cython cell with Anaconda IPython with python 2.7 level... in this case, the same error occurred, but; the following addition error message was produced...
ImportError Traceback (most recent call last)
<ipython-input-3-17bfa2bc0438> in <module>()
----> 1 get_ipython().run_cell_magic(u'cython', u'', u'cimport cython\nfrom libc.math cimport exp, sqrt, pow, log, erf\n\n@cython.cdivision(True)\ncdef double std_norm_cdf(double x) nogil:\n return 0.5* (1+erf(x/sqrt(2.0)))\n\n@cython.cdivision(True)\ndef black_scholes(double s, double k, double t, double v,\n double rf, double div, double cp):\n """Price an option using the Black-Scholes model.\n \n s : initial stock price\n k : strike price\n t : expiration time\n v : volatility\n rf : risk-free rate\n div : dividend\n cp : +1/-1 for call/put\n """\n cdef double d1, d2, optprice\n with nogil:\n d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))\n d2 = d1 - v*sqrt(t)\n optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \\\n cp*k*exp(- rf*t)*std_norm_cdf(cp*d2)\n return optprice')
C:\Users\Mike\Anaconda\lib\site-packages\IPython\core\interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
2160 magic_arg_s = self.var_expand(line, stack_depth)
2161 with self.builtin_trap:
-> 2162 result = fn(magic_arg_s, cell)
2163 return result
2164
C:\Users\Mike\Anaconda\lib\site-packages\IPython\extensions\cythonmagic.py in cython(self, line, cell)
C:\Users\Mike\Anaconda\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)
191 # but it's overkill for just that one bit of state.
192 def magic_deco(arg):
--> 193 call = lambda f, *a, **k: f(*a, **k)
194
195 if callable(arg):
C:\Users\Mike\Anaconda\lib\site-packages\IPython\extensions\cythonmagic.py in cython(self, line, cell)
269 self._code_cache[key] = module_name
270
--> 271 module = imp.load_dynamic(module_name, module_path)
272 self._import_all(module)
273
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.
the above failure being exactly the same as with python 3.4 except that the error indicates an ImportError and points to a problem with the DLL initialization routine.
I checked the source code for the cython, but found no standard linkage pragma indicating the dll
From @mlisanke on April 1, 2015 5:40
Example cython code appears to compile correctly, but; always fails to load the .pyd as shown below:
Thanks for any help. While I've seen several people elude to having similar problems, it appears none have been completely answered.
PS. Ran same cython cell with Anaconda IPython with python 2.7 level... in this case, the same error occurred, but; the following addition error message was produced...
the above failure being exactly the same as with python 3.4 except that the error indicates an ImportError and points to a problem with the DLL initialization routine.
I checked the source code for the cython, but found no standard linkage pragma indicating the dll
Copied from original issue: conda/conda#1238