[Numpy-discussion] ANN: numpy.i - added managed deallocation to ARGOUTVIEW_ARRAY1 (ARGOUTVIEWM_ARRAY1)

Egor Zindy ezindy@gmail....
Sun Nov 16 18:11:40 CST 2008

Sorry to reply to myself.

I've managed to get rid of the two warnings I was talking about, added 
proper attribution to the method (thank you Travis Oliphant!) and am now 
raising an exception in the swig wrapper of my example code when running 
out of memory (although using arg1,arg2,arg3 in %exception feels to me a 
bit hack-ish to me).

Oh, and another detail: since I'm allocating memory for int arrays, the 
memory allocated in bytes is of course 4x what I initially wrote it was 
(in test_alloc.py, on my 32 bit machine)!

Anyway, made a new zip with my example. As before, there is only one 
fragment available: ARGOUTVIEWM_ARRAY1, the other fragments still need 
to be written (I guess when the ARGOUTVIEWM_ARRAY1 fragment is validated).

That's all I can think of. As usual, comments welcome!


Egor Zindy wrote:
> Dear List,
> after I tried to write a simple ARGOUTVIEW_ARRAY1 example (see 
> http://code.google.com/p/ezwidgets/wiki/NumpySWIGMinGW#A_simple_ARGOUTVIEW_ARRAY1_example 
> ), I started wondering about memory deallocation. Turns out a few 
> clever people already did all the thinking (see 
> http://blog.enthought.com/?p=62 ) and a few more clever people use 
> this in a swig file (see http://niftilib.sourceforge.net/pynifti, file 
> nifticlib.i).
> All this concentrated knowledge helped me implement a single 
> ARGOUTVIEWM_ARRAY1 fragment in numpy.i to do some testing (see 
> attached numpy.i).
> How to use it? In yourfile.i, the %init function has a few lines added 
> to it:
> %init %{
>   import_array();
>   /* initialize the new Python type for memory deallocation */
>   _MyDeallocType.tp_new = PyType_GenericNew;
>   if (PyType_Ready(&_MyDeallocType) < 0)
>       return; %}
> ... and that's it! then just use ARGOUTVIEWM_ARRAY1 instead of 
> ARGOUTVIEW_ARRAY1 and python does the deallocation for you when the 
> python array is destroyed (see the examples attached).
> Everything compiles, but it would be nice to get rid of the warnings...
> ezalloc_wrap.c:2763: warning: initialization from incompatible pointer 
> type
> ezalloc_wrap.c: In function `_wrap_alloc_managed':
> ezalloc_wrap.c:2844: warning: assignment from incompatible pointer type
> writing build\temp.win32-2.5\Release\_ezalloc.def
> Compilation:
> python setup_alloc.py build
> Testing:
> The attached test_alloc.py does 2048 allocations of 1MB each for 
> managed and unmanaged arrays. Output on my XP laptop with 1GB RAM as 
> follows:
> ARGOUTVIEWM_ARRAY1 (managed arrays) - 2048 allocations (1048576 bytes 
> each)
> Done!
> ARGOUTVIEW_ARRAY1 (unmanaged, leaking) - 2048 allocations (1048576 
> bytes each)
> Step 482 failed
> Find a better name for the methods (not sure I like 
> ARGOUTVIEWM_ARRAY), then do the missing fragments (2D arrays), clear 
> the warnings and verify the method for a possible inclusion in the 
> official numpy.i file.
> Thank you for reading!
> Regards,
> Egor

-------------- next part --------------
A non-text attachment was scrubbed...
Name: ezalloc.zip
Type: application/x-zip-compressed
Size: 9411 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20081117/937c0642/attachment.bin 

More information about the Numpy-discussion mailing list