viewon01's picture

Cloo : using "constants" and "automatic buffer release"


I have 2 questions about the usage of cloo

1) When I create a ComputerBuffer with "MemFlags.MemReadOnly" (and nothing else) I got an "Invalid Host" error... why ?
When I use "MemFlags.MemReadOnly | MemFlags.MemCopyHostPtr" I don't have any error !

2) I send "constants" information to my kernel by declaring "__constant float *" in my kernel... but in the Cloo library, I have see :

protected override void Dispose( bool manual )
if( Handle != IntPtr.Zero )
CL.ReleaseMemObject( Handle );
Handle = IntPtr.Zero;

So, will the Dispose method by automatically called or should I call it. If it is automatically called by .NET, my "constant" buffer will be release on the Kernel !! ... then I will get an error ?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nythrix's picture

1) Internally ComputeBuffer's constructor uses clCreateBuffer

Khronos wrote:

CL_INVALID_HOST_PTR if host_ptr is NULL and CL_MEM_USE_HOST_PTR or CL_MEM_COPY_HOST_PTR are set in flags or if host_ptr is not NULL but CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR are not set in flags.

Here host_ptr being the data array you pass to the constructor.

2) ComputeBuffer (and most of the other objects) implement a standard Dispose pattern. The method is called automatically as soon as you don't hold any references to the object. Or as late as program termination.

You can also call it manually but you SHOULDN'T do it before the kernel finishes execution. This holds true for any compute objects you pass to the kernel:

Khronos wrote:

A kernel object does not update the reference* count for objects such as memory, sampler objects specified as argument values by clSetKernelArg, Users may not rely on a kernel object to retain objects specified as argument values to the kernel.

Implementations shall not allow cl_kernel objects to hold reference counts to cl_kernel arguments, because no mechanism is provided for the user to tell the kernel to release that ownership right. If the kernel holds ownership rights on kernel args, that would make it impossible for the user to tell with certainty when he may safely release user allocated resources associated with OpenCL objects such as the cl_mem backing store used with CL_MEM_USE_HOST_PTR.

*) My note: The reference above means CL reference not .NET reference. CL references as parts of OpenCL are completely hidden in Cloo for reliability reasons. Unless you're absolutely sure you know what you're doing you shouldn't directly call any clRetain* or clRelease* methods while using Cloo.