Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-11-20 Thread Jeffrey Bush
If you know that they are always numpy arrays, then it doesn't really help. However, if they could be numpy arrays, lists, strings, ... then using buffer() on them gets you the memory buffer of the object, basically an array of bytes you could copy. Basically it helps generalize the problem to mor

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-11-20 Thread Denis Akhiyarov
Jeff, Thank you for quick reply. Can you give any example of how would buffer() help converting numpy/python arrays to managed? For now I developed as decorator (on my personal time) that handles all I/O conversion on python side (assumes one input and one output arbitrary array): def decornet

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-11-05 Thread Jeffrey Bush
To copy from a list or tuple you need to use the Python buffer() function. You can use that on numpy arrays as well. In buffer form, the len() is the byte length so then you don't need to know the data type or size. However, some additional work would be required if you wanted to make sure the C# a

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-11-05 Thread Denis Akhiyarov
How to copy unmanaged array (python list/tuple or numpy array) into managed C# array? I guess using Marshal.Copy, but can anyone point to example? Thanks, Denis On Thu, Oct 30, 2014 at 12:19 PM, Nikhil Garg wrote: > Thanks Brad and Jeff for the detailed info. For now, fromiter is serving > me w

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-11-05 Thread Denis Akhiyarov
And more important question - is it possible to generalize the copying of python array object to managed C# array object without knowing the data type/size/length? On Wed, Nov 5, 2014 at 8:58 AM, Denis Akhiyarov wrote: > How to copy unmanaged array (python list/tuple or numpy array) into > manag

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-11-05 Thread Denis Akhiyarov
Finally decided to generate the managed object on Python side and return it to C# with no conversion necessary in C#. This way I can even wrap regular function with Python with @decorator to handle the conversion. I suppose the dynamic version of pythonnet may have auto conversion for Python 3, bu

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-10-30 Thread Nikhil Garg
Thanks Brad and Jeff for the detailed info. For now, fromiter is serving me well and has reduced my processing time considerably, so I am just going to stick with it. On 29 October 2014 11:04, Jeffrey Bush wrote: > I finally have a chance to chime in, and Bradley is exactly right. > Marshall.Co

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-10-28 Thread Jeffrey Bush
I finally have a chance to chime in, and Bradley is exactly right. Marshall.Copy copies the raw data, and apparently your file library does not store that data in a nice, contiguous, manner. While it is highly likely that copying all the data to an array in C# will be faster than the fromiter in Py

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-10-28 Thread Bradley Friedman
Well it makes sense to me that doing it via an iterator, and element at a time, would be slow. There’s a lot of call overhead associated with each iteration step. Whether it’s done in .net, or in python, or a call from one to the other, it will be slow. It’s still a call where you’d be better

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-10-28 Thread Nikhil
Hello, Yeah, I read data from a file say at each node and each time step, but when i try to use Marshal approach i get gibberish but when i use simple iter i get correct values. i have been trying the approach used in example in the previous post and that example makes sense but it doesnt make s

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array

2014-10-27 Thread Bradley Friedman
You indicate that you are reading from a file. The thread you reference was about copying data in memory. I’d think matters of buffering and read-ahead caches would be far more relevant than anything else. Am I missing something? > On Oct 26, 2014, at 5:18 AM, Nikhil Garg wrote: > > > Hi,

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array.

2014-05-23 Thread Jeffrey Bush
The problem with your current code is that as soon as you call src_hndl.Free() the pointer is not necessarily valid any more! .NET is allowed to move the memory contents of objects at will unless they are pinned (which is what the first line does). By freeing the GCHandle it is no longer pinned and

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array.

2014-05-21 Thread Jeffrey Bush
I was tempted to code it up, and it turns out you can do it in pure python. I thought of 4 ways to copy the data: using a for loop (like you did), using numpy.fromiter, using numpy.fromstring, and using Marshal.Copy. Obviously the for loop is the slowest. numpy.fromiter is still slow, but ~2.5x f

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array.

2014-05-21 Thread Jeffrey Bush
You could write a .NET function to do this with fixed pointers and "memcpy" from the .NET array to the numpy data (the raw data). This would be the absolute fastest way, but does involve a number of assumptions (for example that the data in the two arrays are laid out in the same way). If you want

Re: [Python.NET] Efficient copy of .NET Array to ctypes or numpy array.

2014-05-21 Thread Brad Friedman
An aside that may be useful: .net will skip array bounds checking within simple for-loops, as an optimization. But only if the binaries have all their optimizations turned on. A binary built for debug has them turned off. There is a huge speed up for iterating over an array when these optimizat