[Numpy-discussion] ufunc 's order of execution [relevant when output overlaps with input]
robert.kern at gmail.com
Thu May 12 17:36:01 EDT 2011
On Thu, May 12, 2011 at 16:21, srean <srean.list at gmail.com> wrote:
> is there a guarantee that ufuncs will execute left to right and in
> sequential order ?
By which you mean that it will traverse the elements in a 1D array
left-to-right? No, this is not guaranteed. numpy reserves the right to
reorder the computation for efficiency, to make the inner loop go down
the axis with the smallest stride and in the direction of increasing
memory addresses. We may or may not actually *do* that right now, but
we reserve the right to. :-)
It is possible that we can make an exception for inputs and outputs
that overlap each other and pick a standard traversal. In those cases,
the order of traversal can affect the semantics, so it might be nice
to standardize on the most expected order and guarantee that it won't
> For instance is the following code standards compliant ?
>>>> import numpy as n
> array([0, 1, 2, 3, 4])
>>>> n.add(a[0:-1], a[1:], a[0:-1])
> array([1, 3, 5, 7])
> The idea was to reuse and hence save space. The place where I write to is
> not accessed again.
> I am quite surprised that the following works correctly.
> I guess it uses a buffer and rebinds `a` to that buffer at the end.
No, it's using `a` as the accumulator array. It's essentially doing
out = a + 0
out = a + out
out = a + out
It always reads from a[i] before it writes to out[i], so it's always consistent.
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion