-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: Refactor updateifcopy #9269
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9f15a2e
refactor copying code into new API PyArray_ResolveUpdateIfCopy, add t…
mattip 81cbb7b
make nditer into a context manager, utilize for UPDATEIFCOPY
mattip 4effdfb
BUG: test was not using mode
mattip f012769
use __exit__ to force resolution of UPDATEIFCOPY for want of a better…
mattip 6dc0c65
add flag when nditer requires context manager, raise on PyPy
mattip b8c7c38
document nditer as a context manager
mattip 7a63ca4
cleanup, issue DeprecationWarning only if PyPy or -DDEPRECATE_UPDATEI…
mattip 82203e1
internally use new ResolveUpdateIfCopy wherever needed
mattip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Some questions/comments:
ResolveUpdateIfCopy
in thenditer
pywrap code? It seems to me that on the C-Side this should be triggered by theNpyIter_Deallocate
call, so it would be more obvious to me if you would just call that. (Hopefully guarding all the other methods against the iterator beingNULL
or so is not too bad, but it should be done anyway).for i in np.nditer(...):
I know the nditer will be deallocated by the time the for loop ends, I could imagine trying to give a warning when this might not work, and we know it from the reference count, since it should be higher if there is any chance of the object living longer. (I don't care whether this warning is a )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 warning in cpython would be produced on (the first) call to
next
. If there was any way to detect that the code looks likefor i in np.nditer(...)
(and that iteration is finished/aborted) in PyPy that would be cool since it would make the context manager normally unnecessary, but I guess there is not.A short mention in the documentation would be nice of course.
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.
Could also think about doing an automatic copy-back when the iteration end is reached. But since this is a rather low-level api, maybe better not.
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.
Arrg, I guess it might not possible to guess for sure if the iterator is limited to a for-block from the code (though it would be good to be sure/check). Also C-Api usage/cython might make a refcount check impossible I guess.
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.
@seberg "Why is there an explicit ResolveUpdateIfCopy in the nditer pywrap code? It seems to me that on the C-Side this should be triggered by the NpyIter_Deallocate call, ..."
Do you mean the call on line 2397 here in
npyiter_exit
?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.
Well, but does it call
array_dealloc
or does it just reduce the reference count of those arrays? Because it should do the Resolve step specifically.Yes, it would destroy the state of the iterator, but first, on the C-side, that is how you would achieve copy-back I think and second I don't see much use entering the same nditer object twice.
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.
HHmmmpf, I guess I have no clue how exactly pypy works anyway, and yeah for the python side and updateifcopy things get strange anyway if someone looks at the operand arrays I guess. Will have to think about it some more (and actually figure out the exact logic).
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.
So, even on PyPy, if we stay fully within C,
NpyIter_Dealloc
sPy_DECREF
s will actually triggerarray_dealloc
immediatly? I just glanced overhttp://docs.cython.org/en/latest/src/userguide/pypy.html
and I don't quite see that this happens? Only the C-Api "view" should be cleaned up, or does it know that there can't be a Python side object (because there was no access or because it never left the C-api)?If for some reason this works even with python code in between, we could possibly check for access to the operand arrays as well? But probably not worth the trouble, right now I am mostly worried about the C-Api usage being correct for sure.
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.
I will add "updating cython/docs/src/userguide/pypy.rst" to my TODO list. While not wrong, that page is slightly out of date and it should mention that only certain C-API functions will actually create a python-level object linked to the C one. Looking at the implementation of
Py_DECREF
one can see that if the refcount is 0 (which means no python-level object is created, otherwise the refcount would be >=REFCNT_FROM_PYPY
) thenobj.tp_dealloc
is called immediately.I did try to naively add a call to
PyArray_ResolveUpdateIfCopy
insideNpyIter_Deallocate
just before the call toPy_XDECREF(*object)
, but it causes a crash in thetest_nditer.py
tests. It seems there are cases where the operand is not solely owned by the iterator, i.e. in any of the placesNpyIter_GetOperandArray
,NpyIter_Copy
or others are called.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.
As far as nditer is concerned, the context manager completely resolves any needed
PyArray_ResolveUpdateIfCopy
call without relying on refcount semantics or modifyingNpyIter_Deallocate
Now that I issue a warning when
PyArray_ResolveUpdateIfCopy
is called during deallocation, it turns out I was misreading the actual place needing conversion to a context manager. Since I did not convert all the nditer() calls to context managers, and there were places in tests that reuse the same variable name, triggering the deallocation, I actually needed to convert the previous test to a context manager.Again, once I reworked the python-level tests, no warnings of a call to
PyArray_ResolveUpdateIfCopy
are emitted during nditer tests. However, there are a few places it seemsPyArray_ResolveUpdateIfCopy
is still needed, see my comment below