Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to marshal multi-dimensional arrays

We have some interop code that involves matrices. I was trying to call the native DLL and for the most part it works very reliably.

I am relying on the default marshalling by .net, avoiding unmanaged pointers and rather using .net arrays for the most part, and maybe a byref here and there. The .net article says, that multidimensional arrays are implicitly marshalled as column-major one-dimensional arrays, which would be fine.

The only thing that does not seem to work, is trying to marshal a multi-dimensional array, as the F# compiler complains that float[,] is not allowed in an extern declaration. Is there any way around this limitation?

I am aware of the PinnedArray and PinnedArray2 types from the F# PowerPack, but I was looking for a solution which relies on managed pointers and - more importantly - I'd like to avoid having to include F# PowerPack as a dependency just for the PinnedArray classes.

like image 678
Daniel Fabian Avatar asked Mar 04 '14 19:03

Daniel Fabian


People also ask

How do you handle multidimensional arrays?

To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension. The syntax A(:,:,2) uses a colon in the first and second dimensions to include all rows and all columns from the right-hand side of the assignment. The cat function can be a useful tool for building multidimensional arrays.

Which library can be used to support multidimensional array?

numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object and tools for working with these arrays.


1 Answers

By this discription about Blittable and Non-Blittable Types in the link below you could try the System.Double in place of float, because they do not require conversion when they are passed between managed and unmanaged code wich means no more special handling by the interop marshaler with a plus in performance: https://msdn.microsoft.com/en-us/library/75dwhxf7%28v=vs.110%29.aspx

I did a search and found this related topic wich may help you:

Threat like a single array: http://stackoverflow.com/a/18607817/4597705 
like image 149
André Luís Tomaz Dionisio Avatar answered Sep 27 '22 22:09

André Luís Tomaz Dionisio