-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
MAINT: declare that NumPy's C extensions support running without the GIL #26913
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
Nice that we reached this milestone! I guess at some point we need to add some docs for the support, also because I think it needs a warning somewhere that object arrays shouldn't be fully thread-safe (unless they are through some magic I don't understand).
Not 100% sure, but it seems right to just add. But is f2y really safe already? I am worried f2py modules may need locking similar to lapack-lite (there are a lot of |
Odd, I wonder why the asan tests failed. Let's see if it's just a flakey test...
I agree docs should come before 2.1 comes out. I'll try to add a section somewhere. I haven't yet had a chance to explore your object array suggestion, will cover that there.
This is a good point, I seriously doubt it would be. Another thing I can do is mark the RuntimeWarning in the f2py test as an expected warning in the free-threaded build. |
Also failed on my PR, likely something happened...
Sounds like a reasonable path for now? |
Also here: #26908. The log shows an error in a string operation: |
I added some filters to the relevant f2py tests, although there might be more that need it once #26917 is fixed. |
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.
The last commit needs a tweak - filterwarnings
call has a syntax error (should be a raw string it looks like). Then this LGTM - it should probably be merged at the same time as gh-26780.
Re f2py
: whether an extension module is thread-safe doesn't only depend on the wrapper code that f2py
generates (I suspect that that is more or less fine already), but primarily on the Fortran code that it's wrapping. Hence, f2py
needs a CLI option just like Cython to declare a module safe. I suggest that we do that in a separate PR (since it's a slightly larger change than what's in here), and name it --freethreading-compatible
just like Cython did.
OK, if f2py already turns out safe, great! In practice the fortran code seemed to be more in the boat of ufuncs where we just opt-in users to be thread safe based on the fact that most release the GIL anyway. In the case of f2py, there is some mechanism here for that, which seems to be a marker of the fortran function itself:
Although, Python callbacks may or may not add some nuance to the issue (I would think they are fine, unless f2py has some issues in the code it generates, though). I don't think f2py can work with Python objects really? Now there is one problem that when we do not see the So ideally it would seem that when all wrapped functions are marked So |
That seems reasonable. The CLI option could have a default of I do note that this is not 100% foolproof - e.g., from SciPy's
|
The default is foolproof if everything is marked as "threadsafe", if that isn't the case you could guess only. |
Last push fixes the broken test. Not sure why that didn't trigger on my local setup. I'll go ahead and add an item for marking f2py modules as supporting the GIL to the tracking issue and do that in a followup. |
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.
CI is happy now (the two failures are both unrelated), and the last major pending PR to actually make the core numpy internals safe just went in too. So in this one goes.
If I apply this PR and build NumPy using cython/cython#6242 and the following diff:
Then I'm able to get the full numpy unit tests to pass with the GIL disabled without setting
PYTHON_GIL=0
.I'm sending this in separate from the cython changes to ease review.
The one thing I'm not clear about is whether the f2py change needs to be a new configuration option for f2py. NumPy itself doesn't use f2py, I needed to make the changes to get the f2py tests to pass without raising a warning.