[Numpy-discussion] understanding buffering done when broadcasting

Eli Bendersky eliben at gmail.com
Mon Nov 23 16:31:36 EST 2015


I'm trying to understand the buffering done by the Numpy iterator interface
(the new post 1.6-one) when running ufuncs on arrays that require
broadcasting. Consider this simple case:

In [35]: m = np.arange(16).reshape(4,4)
In [37]: n = np.arange(4)
In [39]: m + n
array([[ 0,  2,  4,  6],
       [ 4,  6,  8, 10],
       [ 8, 10, 12, 14],
       [12, 14, 16, 18]])

If I instrument Numpy (setting NPY_IT_DBG_TRACING and such), I see that
when the add() ufunc is called, 'n' is copied into a temporary buffer by
the iterator. The ufunc then gets the buffer as its data.

My question is: why is this buffering needed? It seems wasteful, since no
casting is required here, no special alignment problems and also 'n' is
contiguously laid out in memory. It seems that it would be more efficient
to just use 'n' in the ufunc instead of passing in the buffer. What am I

Thanks in advance,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20151123/0fceed25/attachment.html>

More information about the NumPy-Discussion mailing list