ComputeBuffer data array improperly pinned
For the ComputeBuffer constructor which takes a T[] data parameter, the array is pinned only for the duration of the call to clCreateBuffer. This appears to be fine if using CopyHostPointer, but is a bug when using UseHostPointer. This is because there's no guarantee that the array won't be moved by GC. Note that a fix to this will also handle the case that's given in the remark, about immediate parameters not working.
Steps to reproduce incorrect behavior:
- Create some temporary objects on the small object heap.
- Create some smallish arrays (< 85k bytes, so they go on the small object heap, so they will be candidates for compaction).
- Create compute buffers from these arrays, using the flag
UseHostPointer. - Create a lot of temporary objects on the small object heap, enough to cause a GC and compaction. The compaction will move your buffer arrays.
- Execute a kernel, and observe that the data isn't correct.
You are totally right, @odomobo. However, as I am not using Cloo anymore I do not have the time to fix this bug. If anyone submits a pull request fixing this bug I will be happy to merge it.
If anyone could point me to a direction I would love to help. First thing may a reproduceable code for the issue , I may find my way by debugging but still I have no idea about codebase.
Writing code reproducing this issue is not easy. You would have to reliably force the garbage collector to move the buffer used in memory.
However, I do not think this is necessary. It is not to complicated to make sure the GCHandle is not freed before the buffer is disposed when the UseHostPointer flag is set.
It would be nice to have test/s ( about issue ) to make sure everything work properly.
Certainly this would be nice, but I am not aware of any way to check if an array is pinned from code. Perhaps there is one. Also I do not know how to reliably force the underlying memory of an array to be moved.