[Numpy-discussion] Shared memory check on in-place modification.

Sebastian Berg sebastian at sipsolutions.net
Tue Jul 28 11:59:16 EDT 2015


On Mon Jul 27 22:51:52 2015 GMT+0200, Sturla Molden wrote:
> On 27/07/15 22:10, Anton Akhmerov wrote:
> > Hi everyone,
> >
> > I have encountered an initially rather confusing problem in a piece of
> > code that attempted to symmetrize a matrix: `h += h.T`
> > The problem of course appears due to `h.T` being a view of `h`, and
> > some elements being overwritten during the __iadd__ call.
>

I think the typical proposal is to raise a warning. Note there is np.may_share_memoty. But the logic to give the warning is possibly not quite easy, since this is ok to use sometimes. If someone figures it out (mostly) I would be very happy zo see such warnings.

 
> Here is another example
> 
>  >>> a = np.ones(10)
>  >>> a[1:] += a[:-1]
>  >>> a
> array([ 1.,  2.,  3.,  2.,  3.,  2.,  3.,  2.,  3.,  2.])
> 
> I am not sure I totally dislike this behavior. If it could be made 
> constent it could be used to vectorize recursive algorithms. In the case 
> above I would prefer the output to be:
> 
> array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.,  10.])
> 
> It does not happen because we do not enforce that the result of one 
> operation is stored before the next two operands are read. The only way 
> to speed up recursive equations today is to use compiled code.
> 
> 
> Sturla
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 
>


More information about the NumPy-Discussion mailing list