<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: NumPy arrays with pre-allocated memory</title>
	<atom:link href="http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/</link>
	<description>The latest from us to the open-source projects and communities we love.</description>
	<lastBuildDate>Mon, 29 Aug 2011 21:39:25 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Nick</title>
		<link>http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/comment-page-1/#comment-4349</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Fri, 19 Feb 2010 23:28:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.enthought.com/?p=62#comment-4349</guid>
		<description>Fantastic stuff. Been looking for something like this to help solve my conundrum over at

http://stackoverflow.com/questions/2290007/beginner-extending-c-with-python-specifically-numpy

If I have any success, I&#039;ll post a solution with a link to this blog post. Thanks</description>
		<content:encoded><![CDATA[<p>Fantastic stuff. Been looking for something like this to help solve my conundrum over at</p>
<p><a href="http://stackoverflow.com/questions/2290007/beginner-extending-c-with-python-specifically-numpy" rel="nofollow">http://stackoverflow.com/questions/2290007/beginner-extending-c-with-python-specifically-numpy</a></p>
<p>If I have any success, I&#8217;ll post a solution with a link to this blog post. Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Egor Zindy</title>
		<link>http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/comment-page-1/#comment-4150</link>
		<dc:creator>Egor Zindy</dc:creator>
		<pubDate>Sat, 19 Dec 2009 22:50:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.enthought.com/?p=62#comment-4150</guid>
		<description>Mike, not sure if that&#039;s any use:

I have included Travis&#039;s code for automatic handling of array creation from malloc-ed memory (without copying the data), in a custom version of Bill Spotz&#039;s numpy.i.
When the numpy array is deleted on the python side, the malloc block is also automatically de-allocated to prevent any memory leak.

I have written an entry in the scipy cookbook (explanations + complete example):
http://www.scipy.org/Cookbook/SWIG_Memory_Deallocation

Regards,
Egor</description>
		<content:encoded><![CDATA[<p>Mike, not sure if that&#8217;s any use:</p>
<p>I have included Travis&#8217;s code for automatic handling of array creation from malloc-ed memory (without copying the data), in a custom version of Bill Spotz&#8217;s numpy.i.<br />
When the numpy array is deleted on the python side, the malloc block is also automatically de-allocated to prevent any memory leak.</p>
<p>I have written an entry in the scipy cookbook (explanations + complete example):<br />
<a href="http://www.scipy.org/Cookbook/SWIG_Memory_Deallocation" rel="nofollow">http://www.scipy.org/Cookbook/SWIG_Memory_Deallocation</a></p>
<p>Regards,<br />
Egor</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/comment-page-1/#comment-3905</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Wed, 02 Dec 2009 19:12:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.enthought.com/?p=62#comment-3905</guid>
		<description>Travis,

Thanks for the info.  I was able to use SWIG to make a typemap that reads the C structure that contains the arrays, and uses PyArray_SimpleNewFromData to build the PyArrays and send them back to a dictionary/list in Python using PyArray_Return, which works.

Thanks again.</description>
		<content:encoded><![CDATA[<p>Travis,</p>
<p>Thanks for the info.  I was able to use SWIG to make a typemap that reads the C structure that contains the arrays, and uses PyArray_SimpleNewFromData to build the PyArrays and send them back to a dictionary/list in Python using PyArray_Return, which works.</p>
<p>Thanks again.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Travis Oliphant</title>
		<link>http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/comment-page-1/#comment-3831</link>
		<dc:creator>Travis Oliphant</dc:creator>
		<pubDate>Tue, 24 Nov 2009 23:37:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.enthought.com/?p=62#comment-3831</guid>
		<description>Mike,

This is a C-extension example.  Once you return the object created to Python it can be used just like any other NumPy array (you can set and get data or manipulate however you like). 

There are many ways to get access to memory allocated in a C program (including SWIG or f2py or using this approach).  The approach described here is really for being able to manage deallocation of the data.  If you know that your data will never change, then you can just use PyArray_SimpleNewFromData.</description>
		<content:encoded><![CDATA[<p>Mike,</p>
<p>This is a C-extension example.  Once you return the object created to Python it can be used just like any other NumPy array (you can set and get data or manipulate however you like). </p>
<p>There are many ways to get access to memory allocated in a C program (including SWIG or f2py or using this approach).  The approach described here is really for being able to manage deallocation of the data.  If you know that your data will never change, then you can just use PyArray_SimpleNewFromData.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/comment-page-1/#comment-3830</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Tue, 24 Nov 2009 18:09:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.enthought.com/?p=62#comment-3830</guid>
		<description>Travis,

I know this is a bit old, but I have a question about this...

I have an issue where I have a C program that generates multiple arrays that can each be several million cells long, just 1-D though, so like 1x4,000,000.  Each array is a malloc&#039;d array of type double.  The program as is stores the data in a structure with other information about the data set (name, size, etc).

I need to get this data into a Python workspace.  I can do it with SWIG using memory copies, but that is undesirable because it greatly slows down the program and will cause more memory to be used.

I would like to be able to return all the data as Numpy arrays, and this method seems like it could almost work (maybe in conjunction with SWIG to fill up a dictionary with the structure info), but not quite...

For this method, how do you use the &quot;arr&quot; array you create?  Is it manipulatable (arr[i][j] = x)? or do you manipulate the data beforehand by using mymem?

Thanks,
Mike</description>
		<content:encoded><![CDATA[<p>Travis,</p>
<p>I know this is a bit old, but I have a question about this&#8230;</p>
<p>I have an issue where I have a C program that generates multiple arrays that can each be several million cells long, just 1-D though, so like 1&#215;4,000,000.  Each array is a malloc&#8217;d array of type double.  The program as is stores the data in a structure with other information about the data set (name, size, etc).</p>
<p>I need to get this data into a Python workspace.  I can do it with SWIG using memory copies, but that is undesirable because it greatly slows down the program and will cause more memory to be used.</p>
<p>I would like to be able to return all the data as Numpy arrays, and this method seems like it could almost work (maybe in conjunction with SWIG to fill up a dictionary with the structure info), but not quite&#8230;</p>
<p>For this method, how do you use the &#8220;arr&#8221; array you create?  Is it manipulatable (arr[i][j] = x)? or do you manipulate the data beforehand by using mymem?</p>
<p>Thanks,<br />
Mike</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stefan</title>
		<link>http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/comment-page-1/#comment-3156</link>
		<dc:creator>Stefan</dc:creator>
		<pubDate>Mon, 17 Aug 2009 02:24:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.enthought.com/?p=62#comment-3156</guid>
		<description>Also see Lisandro Dalcin&#039;s comment on the NumPy list [1] where he mentions that

PyCObject_FromVoidPtrAndDesc [2]

can be used instead of creating a new type.

[1] http://www.mail-archive.com/numpy-discussion@scipy.org/msg12442.html

[2] http://docs.python.org/c-api/cobject.html</description>
		<content:encoded><![CDATA[<p>Also see Lisandro Dalcin&#8217;s comment on the NumPy list [1] where he mentions that</p>
<p>PyCObject_FromVoidPtrAndDesc [2]</p>
<p>can be used instead of creating a new type.</p>
<p>[1] <a href="http://www.mail-archive.com/numpy-discussion@scipy.org/msg12442.html" rel="nofollow">http://www.mail-archive.com/numpy-discussion@scipy.org/msg12442.html</a></p>
<p>[2] <a href="http://docs.python.org/c-api/cobject.html" rel="nofollow">http://docs.python.org/c-api/cobject.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Travis Oliphant</title>
		<link>http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/comment-page-1/#comment-1780</link>
		<dc:creator>Travis Oliphant</dc:creator>
		<pubDate>Tue, 31 Mar 2009 22:38:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.enthought.com/?p=62#comment-1780</guid>
		<description>If you know for sure that you are using the same memory allocator as NumPy (currently malloc, but this could possibly change) then you would be fine just setting the array flag to NPY_OWNDATA and having NumPy deallocate for you.  

Otherwise, you need to set up this indirection.   You might also be able to handle the indirection with Cython though. 

I think this pattern should actually be wrapped up and placed in NumPy itself, but I haven&#039;t gotten the chance to do it.</description>
		<content:encoded><![CDATA[<p>If you know for sure that you are using the same memory allocator as NumPy (currently malloc, but this could possibly change) then you would be fine just setting the array flag to NPY_OWNDATA and having NumPy deallocate for you.  </p>
<p>Otherwise, you need to set up this indirection.   You might also be able to handle the indirection with Cython though. </p>
<p>I think this pattern should actually be wrapped up and placed in NumPy itself, but I haven&#8217;t gotten the chance to do it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DSW</title>
		<link>http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/comment-page-1/#comment-1655</link>
		<dc:creator>DSW</dc:creator>
		<pubDate>Sat, 07 Mar 2009 19:43:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.enthought.com/?p=62#comment-1655</guid>
		<description>Whew, that looks like a mess of code!  I&#039;m wondering if it&#039;s okay to malloc the memory, set a bit array.flags &#124;= NPY_OWNDATA, and that&#039;s all.   Doesn&#039;t numpy ultimately deep within use malloc, so there&#039;d be no problem even if my extension is the caller of malloc?</description>
		<content:encoded><![CDATA[<p>Whew, that looks like a mess of code!  I&#8217;m wondering if it&#8217;s okay to malloc the memory, set a bit array.flags |= NPY_OWNDATA, and that&#8217;s all.   Doesn&#8217;t numpy ultimately deep within use malloc, so there&#8217;d be no problem even if my extension is the caller of malloc?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: NumPy arrays that use memory allocated from other libraries or tools &#124; keyongtech</title>
		<link>http://blog.enthought.com/python/numpy-arrays-with-pre-allocated-memory/comment-page-1/#comment-1573</link>
		<dc:creator>NumPy arrays that use memory allocated from other libraries or tools &#124; keyongtech</dc:creator>
		<pubDate>Sun, 18 Jan 2009 17:36:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.enthought.com/?p=62#comment-1573</guid>
		<description>[...] code that has it&#039;s own memory management scheme. Comments and feedback is welcome.  The post is  http://blog.enthought.com/?p=62   Best regards,  -Travis [...]</description>
		<content:encoded><![CDATA[<p>[...] code that has it&#8217;s own memory management scheme. Comments and feedback is welcome.  The post is  <a href="http://blog.enthought.com/?p=62" rel="nofollow">http://blog.enthought.com/?p=62</a>   Best regards,  -Travis [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

