Skip to content

Redesigned __setitem__ implementation #7392

Closed

Description

@davidhassell

This issue, and its accompanying PR, is for a redesign of the extended Array.__setitem__ functionality.

History

Issue #7029 proposed an extended __setitem__ functionality that more closely matched that of numpy.

Whilst this did provide the functionality, it was flawed in that it could create an excessive number of threads leading to a crash (#7261). It was limited in other ways, too: An integer array index had to be strictly monotonically increasing; and integer dask array indices where always computed at the __setitem__ call time, rather than at the__setitem__ compute time.

After the analysis by @sadielbartholomew, it became clear to us that the thread proliferation issue arose because slices of the assignment value created using Array.__getitem__ were being computed from within a function run by map_blocks, i.e. a compute() was being run from with another compute().

The new approach

The basic principle for executing x[index] = v is unchanged from before: for each block work out if it overlaps the index and if so create the corresponding slice of v and assign it to the block's data.

The old approach worked out which blocks needed assignments, and created the slice of v at compute time, all in a function run at compute time (via map_blocks).

The approach of the new code is more along the lines of the Array.__getitem__ implementation (which was a thought I'd had before, but didn't have the experience to see how to make it work back then!).

In the new approach, which has been developed with @sadielbartholomew, the calculation of which blocks need assignments, and the creation of the slices of v, is done up front and lazily in the new setitem_array function that is called directly from Array.__setitem__, and the results are inserted into a new dask graph that is merged with the original graph of x. This new graph layer includes the graphs of v, the graphs of the parts of v that correspond to each block, and any graphs relating to dask array indices. As the whole operation is now contained in a single graph, the computation is wholly straight forward and as expected.

With the new code:

  • There is no "compute within a compute", so no thread proliferation.

  • Integer and boolean indices that are dask arrays are themselves not computed until the __setitem__ compute time.

  • The full flexibility of integer array indices is now allowed (i.e. arbitrary ordering, repeated values, and arbitrary numbers of values are all OK).

  • The new graph does not duplicate blocks which occur in more than one any of x, v or indices.

We would greatly value any input and comments on this new code.

Activity

  1. davidhassell commented on Mar 18, 2021

    @davidhassell
    ContributorAuthor

    Here are some figures that hopefully explain the implementation, that we found useful whilst developing this code: (updated with graph visualization)

    setitem1
    setitem2

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