|
From: Sasha <nd...@ma...> - 2006-02-03 21:42:52
|
On 2/2/06, Travis Oliphant <oli...@ie...> wrote: > Sasha wrote: > > >Sure. I've started working on a "proof of concept" patch and will post = it soon. > > > Great. Attached patch allows numpy create memory-saving zero-stride arrays. Here is a sample session: >>> from numpy import * >>> x =3D ndarray([5], strides=3D0) >>> x array([12998768, 12998768, 12998768, 12998768, 12998768]) >>> x[0] =3D 0 >>> x array([0, 0, 0, 0, 0]) >>> x.strides =3D 4 Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: strides is not compatible with available memory >>> x.strides (0,) >>> x.data Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: cannot get single-segment buffer for discontiguous array >>> exp(x) array([ 1., 1., 1., 1., 1.]) # Only single-element buffer is required for zero-stride array: >>> y =3D ones(1) >>> z =3D ndarray([10], strides=3D0, buffer=3Dy) >>> z array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) I probably missed some places where buffer size is computed as a product of dimensions, but it should not be hard to review the code for those if we agree that having zero-stride arrays is a good idea. Note that I did not attempt to change any behaviors, the only change is that zero-stride arrays do not use more memory than they need. |