[Numpy-discussion] performing operations in-place in numpy

Pauli Virtanen pav+sp at iki.fi
Wed Jul 8 19:28:16 EDT 2009


On 2009-07-08, Charles R Harris <charlesr.harris at gmail.com> wrote:
> In that case I don't see a problem offhand. That said, I haven't looked at
> the code yet.

I'm a bit worried about the problem that cropped up in the ticket 
with the complex ufuncs. As Luca noted in the ticket,

    obj3 = PyArray_Conjugate((PyAO *)obj1, NULL);

needed to be replaced with

    Py_INCREF(obj1); 
    obj3 = PyArray_Conjugate((PyAO *)obj1, NULL);
    Py_DECREF(obj1);

to avoid overwriting obj1 when refcount of `obj1 == 1`.


Now, having refcounts of 1 can be a common condition in arrays in 
C code, and PyArray_Conjugate is a public function in Numpy's 
C-API. So, as it stands now, this patch seems to potentially 
break existing C-extension modules that use the Numpy/Numeric 
API. This would need to be fixed.


Perhaps the INCREF/DECREF pair can be moved inside 
PyArray_Conjugate. If I understand correctly similar issues can 
only crop up in functions calling the *GenericUnary* etc. ufunc 
functions directly. Are these functions part of the public API? 
If not, there's a chance of fixing their callers inside Numpy.


If they are a part of the public API, then the issue seems to 
become more hairy. Anyway, it seems there is some auditing to do 
before this change can be safely considered.


Also, the speedups obtained were fairly modest, 20%. Are they 
larger for more complicated expressions? (Ie. is there an 
expression whose execution time is halved?)

-- 
Pauli Virtanen




More information about the NumPy-Discussion mailing list