You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
a continuation of issue #7054
pr #9639 starts deprecating the resolution of UPDATEIFCOPY semantics in ndarray dealloc. The nditer class also uses the UPDATEIFCOPY mechanism, whenever a read-write iterator is used or if the updateifcopy flag is explicitly set via opflags, leading to code like this::
a = arange(24, dtype='<i4').reshape(2, 3, 4)
i = nditer(a, ['buffered'], order='F', casting='unsafe',
op_dtypes='>f8', buffersize=5)
j = i.copy()
i = None
I can see two solutions to removing the need to do i = None to trigger the resolution of temporary data:
totally deprecate the use of these semantics in the case of nditers, and raise an error if the nditer instantiation would require updateifcopy semantics
add context manager semantics to nditer, so that the code above would become
a = arange(24, dtype='<i4').reshape(2, 3, 4)
with nditer(a, ['buffered'], order='F', casting='unsafe',
op_dtypes='>f8', buffersize=5) as i:
j = i.copy()
I can issue a pull request for either option, which one is preferable? Once we have a resolution for this issue, we can enable the DeprecationWarning on any use of UPDATEIFCOPY in favor of WRITEBACKIFCOPY + use of PyArray_ResolveWritebackIfCopy
The example is not a good one. Here is a better one:
a = np.arange(24, dtype='f8').reshape(2, 3, 4).T
i = np.nditer(a, [], [['readwrite', 'updateifcopy']],
casting='same_kind', op_dtypes=[np.dtype('f4')])
# Check that UPDATEIFCOPY is activated
i.operands[0][2, 1, 1] = -12.5
assert a[2, 1, 1] != -12.5
i = None # magic!!!
assert a[2, 1, 1] == -12.5
The real issue is the nditer creation/destruction which uses a scratch buffer via UPDATEIFCOPY semantics. The operands have a different memory layout than the original array a and so a scratch buffer is needed to allow operand manipulation
The scratch buffer is copied back to a when i is destroyed, The new API requires a less magic, explicit means of resolving or copying back to a, the one that pops to mind for me is a context manager.
Alternatively we could deprecate this whole mess, under the assumption that no-one uses it.
Ah, nditer is unfortunately a much more subtle and complicated beast than a
regular iterator. And this isn't really about copy() anyway; that's just a
way of demonstrating the problem.
The problem here is that nditer assumes it can pass back an UPDATEIFCOPY
pseudo-view to python code, and that the writeback will be triggered
magically when that python code is done with it. This is fundamentally
broken on PyPy with it's delayed gc. That's what needs to be fixed somehow,
either by removing the functionality or by giving nditer a way to
explicitly trigger the writeback.
On Nov 7, 2017 18:53, "xoviat" ***@***.***> wrote:
The assumption here is that nditer should have a .copy() method. But that
assumption isn't backed by any other examples in Python, so the question
is: why should the method exist? .copy() functionality is already
provided by Python itself with itertools.tee, and if that has a
performance penalty, there's nothing stopping implementations from
rewriting it in C or rpython.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#9714 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAlOaCyKx9Raxwdrf0EkD88uwagykVKxks5s0PtygaJpZM4PcvdP>
.
So when we talk about "removing the functionality", we mean raising an error and requiring the user to make an iterable copy of their array themselves? Ie, they should write:
I have only just learned about all of this. But personally the context-manager seems friendlier, and doesn't seem too hard to implement given you already wrote the C-api for all of this in PyArray_SetWritebackIfCopyBase.
pull request #10184 solves this issue via a third alternative, a python-level nditer.close which provides a path for PyPy compatibility without requiring current code to be refactored.
Activity
njsmith commentedon Nov 8, 2017
@xoviat I don't see how
itertools.teeis relevant here, can you elaborate?mattip commentedon Nov 8, 2017
The example is not a good one. Here is a better one:
The real issue is the nditer creation/destruction which uses a scratch buffer via UPDATEIFCOPY semantics. The operands have a different memory layout than the original array
aand so a scratch buffer is needed to allow operand manipulationThe scratch buffer is copied back to
awheniis destroyed, The new API requires a less magic, explicit means of resolving or copying back toa, the one that pops to mind for me is a context manager.Alternatively we could deprecate this whole mess, under the assumption that no-one uses it.
njsmith commentedon Nov 8, 2017
ahaldane commentedon Nov 8, 2017
So when we talk about "removing the functionality", we mean raising an error and requiring the user to make an iterable copy of their array themselves? Ie, they should write:
I have only just learned about all of this. But personally the context-manager seems friendlier, and doesn't seem too hard to implement given you already wrote the C-api for all of this in
PyArray_SetWritebackIfCopyBase.mattip commentedon Nov 10, 2017
pull request #9998 opened for comment and review
mattip commentedon Dec 13, 2017
pull request #10184 solves this issue via a third alternative, a python-level
nditer.closewhich provides a path for PyPy compatibility without requiring current code to be refactored.mattip commentedon Apr 21, 2018
Closed by PR #9998