
CLOO: copy two dimensional array
Posted Monday, 26 April, 2010 - 07:31 by pragma inHi there,
I'm experimenting a bit with Cloo (very cool project) and ran into a problem:
I'm trying to copy a 2 dimensional float array onto the device something like
float[,] weights = new float[count, count];
obviously I can't copy it via:
ComputeBuffer<float> weightsBuffer = new ComputeBuffer<float>(context, ComputeMemoryFlags.CopyHostPointer, weights);
How can I copy this array to the device?
I could manually flatten the array :
float[] weightsFlat = new float[count* count]; int x = 0; for (int i=0; i<count; i++) for (int j=0; j<count; j++) weightsFlat[x++] = weights[i,j];
but I would really want to avoid that.
greetings pragma


Comments
Re: CLOO: copy two dimensional array
The following seems to work:
Two things to keep in mind:
1) I'm not sure if the input array has to be pinned or rather I don't know if UnsafeAddrOfPinnedArrayElement does it and
2) coming back from the ComputeBuffer it might be hard to reconstruct the original 2D array.
Edit: I'm a total stick. You CAN reconstruct the 2D (and probably 3D, 4D, 5D, ...) array using the same pointer mechanism and this call:
Re: CLOO: copy two dimensional array
Of course, another solution is to provide additional overloads for 2d and 3d arrays in Cloo. That's what OpenTK does for such functions, e.g. GL.BufferData:
Re: CLOO: copy two dimensional array
Of course, another solution is to provide additional overloads for 2d and 3d arrays in Cloo.
That's even a better idea.
Re: CLOO: copy two dimensional array
Ah thank you.
i tried that Marshal.UnsafeAddrOfPinnedArrayElement thing and it seems to work :)
btw: it seems like you do have to pin it before retrieving the address
Re: CLOO: copy two dimensional array
I know this is probably wayyy late for this. .NET has a method in the LINQ namespace called SelectMany. It works like this.
you can do this for any dimensional arrays. I currently have it working on a 3d jagged array, taking the 0th index and casting the rest to an array. Works nicely and is relatively fast.