-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
ENH: Cython interface for special #6195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
9eb801c
to
0a0d83b
Compare
This looks quite promising @person142! There are 2 test failures for the latest commit, those are real. |
Benchmarks are quite consistent; numpy and cython same speed (except ~400ns less overhead for small arrays with cython), Python significantly slower:
And autogenerating benchmarks is a nice touch. |
Yes, I need to track those failures down. I ran into an issue before that became gh-6208; might be more of the same. I have noticed one circumstance where cython is slower than numpy, and that's functions with a lot of complex arguments and an underlying c function that's using numpy cdoubles (e.g. the |
It would be surprising if the time difference comes from casting, since that should not be very many cpu cycles, compared to the floating point math the function in the end does. |
I looked at the generated cython wrappers for I marked the radial spheroidal wave functions as known failures; if people are not happy with this I'll dig deeper into the Fortran code and try to pinpoint the error. |
@person142 it would be good to advertise this PR on the mailing list. I'm sure some people will be interested. |
Addressing that separately is ok I think. |
Cython API for Special Functions | ||
================================ | ||
|
||
Scalar, typed versions of many of the functions in ``scipy.special`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be simpler to just list all the available C signatures, rather than the internal representation used in generate.py --- this way it would be more self-explanatory.
While I won't be able to review the details, I played a bit with this from a user perspective and it looks great. Don't know if this makes this release or the next one, but this definitely should be a highlight of a release. One longer-term question, not for this PR. Would it be possible to add some magic to dispatch to these functions automatically when a special function is called with scalar arguments? |
@@ -77,7 +77,7 @@ npy_cdouble clngamma_wrap( npy_cdouble z) { | |||
|
|||
npy_cdouble chyp2f1_wrap( double a, double b, double c, npy_cdouble z) { | |||
npy_cdouble outz; | |||
int l1, l0; | |||
int l1, l0, isfer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initialize isfer = 0
--- since it's not set by the fortran routine always, the result is undefined.
Ok, a few comments:
I have fixes for the above in my branch, cf person142/scipy@cython-interface...pv:pr-6195 |
Apart from that, looks great to me. |
Thanks for the fixes @pv. I moved the known failure check to earlier in the test so that those functions never get evaluated. I've had |
Other stuff: |
I think I fixed it. |
This defines typed, scalar versions of many of the functions in scipy.special and makes them available to cimport in Cython. It uses fused types so that the functions have the same names and signatures as their ufunc counterparts. It piggybacks off of the existing code generation in generate_ufuncs.py. In particular: - The process for adding new ufuncs is unchanged - When a new ufunc is added a Cython version will automatically be created (subject to some restrictions mentioned at the end), and the documentation and tests will be automatically updated. This does not attempt to add Cython versions of: - Ufuncs with multiple return arguments - Ufuncs that have additional layers of python wrappers. With more work these restrictions could be removed. Closes scipygh-5964.
This mimics the behavior of iter_varaints in generate_ufuncs.py
Hopefully this will fix issues with conversions.
Modifies specfun.f to return sf_error codes in added flag variables instead of printing warnings. The error codes are caught by the c wrapper functions and the appropriate sf_error is raised. This silences a lot of noise in test_cython_special.py.
Also make generate_ufuncs.py automatically generate the benchmarks.
Functions with multiple return values are handled by passing in pointers as the final arguments. This is intended to be analogous to providing output arrays to a ufunc.
Functions are listed by their c signatures instead of typecodes
3b3d064
to
be3e37f
Compare
And rebased. |
Thanks, merged based on positive feedback. |
Thanks for taking the time to review @pv. |
This defines typed, scalar versions of many of the functions in
scipy.special and makes them available to cimport in Cython. It uses
fused types so that the functions have the same names and signatures
as their ufunc counterparts.
It piggybacks off of the existing code generation in
generate_ufuncs.py. In particular:
created (subject to some restrictions mentioned at the end), and the
documentation and tests will be automatically updated.
This does not attempt to add Cython versions of:
Ufuncs with multiple return argumentsUfuncs with multiple return arguments are now supportedWith more work these restrictions could be removed.
Closes gh-5964.