Skip to content

change updateifcopy semantics in nditer #9714

Closed

Description

@mattip

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

Activity

  1. njsmith commented on Nov 8, 2017

    @njsmith
    Member

    @xoviat I don't see how itertools.tee is relevant here, can you elaborate?

  2. mattip commented on Nov 8, 2017

    @mattip
    MemberAuthor

    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.

  3. njsmith commented on Nov 8, 2017

    @njsmith
    Member
  4. ahaldane commented on Nov 8, 2017

    @ahaldane
    Member

    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:

    a = arange(24, dtype='<i4').reshape(2, 3, 4)
    cpy = ascontiguousarray(a)
    i = nditer(cpy, ['buffered'], casting='unsafe',
                    op_dtypes='>f8', buffersize=5)
    # do something here
    a[...] = cpy

    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.

  5. mattip commented on Nov 10, 2017

    @mattip
    MemberAuthor

    pull request #9998 opened for comment and review

  6. mattip commented on Dec 13, 2017

    @mattip
    MemberAuthor

    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.

  7. mattip commented on Apr 21, 2018

    @mattip
    MemberAuthor

    Closed by PR #9998

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions