Interop passing large two-dimensional array without Marshaling
Question
I have a huge rectangular array (2GB+) that I want to pass to a C++ dll. I want to alloc the memory once in managed code and not marshal it (too big). The data is a 2D array that's filled in sequentially in a loop. I can create it using managed arrays like this:
float[,] masterMatrix = new float[dim1, dim2]; // 2GB +
foreach(Sequence seq in sequences)
{
// get the next matrix & calculate its byte size
float[,] result = seq.Process();
int byteSize = (result.GetLength(0) * result.GetLength(1)) * sizeof(float);
// append it to the master matrix
Buffer.BlockCopy(result, 0, masterMatrix, insertByteIndex, byteSize);
insertByteIndex += byteSize;
}
But if I use an IntPtr (so I can pass it directly to the .dll), how do I copy the result
's data into the unmanaged memory? Marshal.Copy only handles one-dimensional arrays. I know I can do it element by element, but I was hoping there was a better / faster way.
IntPtr pointer = Marshal.AllocHGlobal(maxNumBytes); // 2GB +
foreach(Sequence seq in sequences)
{
// get the next matrix & calculate its byte size
float[,] result = seq.Process();
int byteSize = (result.GetLength(0) * result.GetLength(1)) * sizeof(float);
// append it to the master matrix
Marshal.Copy(/* can't use my result[,] here! */, 0, pointer, byteSize);
insertByteIndex += byteSize;
}
Thanks for any ideas.
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow