Cloo icon indicating copy to clipboard operation
Cloo copied to clipboard

ComputeBuffer data array improperly pinned

Open odomobo opened this issue 4 years ago • 5 comments

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:

  1. Create some temporary objects on the small object heap.
  2. Create some smallish arrays (< 85k bytes, so they go on the small object heap, so they will be candidates for compaction).
  3. Create compute buffers from these arrays, using the flag UseHostPointer.
  4. 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.
  5. Execute a kernel, and observe that the data isn't correct.

odomobo avatar Mar 01 '21 03:03 odomobo

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.

clSharp avatar Apr 01 '21 18:04 clSharp

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.

firatsarlar avatar Sep 07 '21 15:09 firatsarlar

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.

clSharp avatar Sep 13 '21 13:09 clSharp

It would be nice to have test/s ( about issue ) to make sure everything work properly.

firatsarlar avatar Sep 14 '21 05:09 firatsarlar

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.

clSharp avatar Sep 14 '21 10:09 clSharp