[SciPy-user] How to free memory allocated to array in weave.inline?
Angus McMorland
amcmorl at gmail.com
Sat Aug 25 19:25:37 EDT 2007
On 25/08/07, Gael Varoquaux <gael.varoquaux at normalesup.org> wrote:
> On Sat, Aug 25, 2007 at 11:37:46PM +1200, Angus McMorland wrote:
> > The code generates a 3-D array, and loops through the array assigning
> > values based on the co-ordinate position. In the final program this
> > array generation has to be done many (ideally 1000s) of times, but
> > when I try this my memory consumption increases.
>
> My rule of thumb when I create arrays in C code, is not to create arrays
> in C code. Your problem is most probably that your memory does not get
> freed, as it it not registered in Python's garbage collector. You could
> learn how to do this (I don't know how). The other option is to use
> numpy.empty to create an empty array, and pass it to your code that will
> populate it. That way you don't have to deal with all this. It alos makes
> your cod emore readable: no PyArrayObject* and co. in it.
Thanks Gaël, that sounds like a very sensible suggestion, but I'm
having trouble getting it to work. The following causes a segfault. Am
I doing something stupid?
If I understand the manual correctly, the weave code should alter the
array in-place, so I don't need to use return_val for that one. Is
that correct?
def build():
xsz, ysz, zsz = (200,200,200)
ar = n.empty((xsz, ysz, zsz), dtype=n.uint8)
code = '''
int i,j,k;
npy_ubyte *curpos;
for (i = 0; i<xsz; i++)
for (j = 0; j<ysz; j++)
for (k = 0; k<zsz; k++)
{
curpos = (npy_ubyte *)PyArray_GETPTR3(ar, i, j, k);
*curpos = 3;
}
'''
weave.inline( code, ['ar', 'xsz', 'ysz', 'zsz'] )
return ar
(As you can probably tell from my other post, ideally I want to use a
boolean array, but that doesn't seem to be supported currently.)
Thanks again,
Angus.
--
AJC McMorland, PhD Student
Physiology, University of Auckland
More information about the SciPy-User
mailing list