Skip to content

Use clSetMemObjectDestructorCallback for CL_MEM_USE_HOST_PTR#2628

Open
paulfradgley wants to merge 2 commits intomainfrom
paulfradgley-clSetMemDestructorCallback-fix
Open

Use clSetMemObjectDestructorCallback for CL_MEM_USE_HOST_PTR#2628
paulfradgley wants to merge 2 commits intomainfrom
paulfradgley-clSetMemDestructorCallback-fix

Conversation

@paulfradgley
Copy link
Contributor

Use clSetMemObjectDestructorCallback for hostptr freeing for CL_MEM_USE_HOST_PTR buffers in buffers CTS test.

The OpenCL 3.0 API spec states that:

CL_MEM_USE_HOST_PTR - If specified, it indicates that the application wants the OpenCL implementation to use memory referenced by host_ptr as the storage bits for the memory object.

and

clSetMemObjectDestructorCallback - the memory object destructor callback provides a mechanism for an application to safely re-use or free a host_ptr that was specified when memobj was created and used as the storage bits for the memory object.

From this it can be inferred that it is unsafe to free the hostptr used to created a CL_MEM_USE_HOST_PTR buffer without using a destructor callback. In some places the CTS buffers test calls free(hostptr) immediately after calling clReleaseMemObject, and in other places it calls free(hostptr) even before calling clReleaseMemObject.

This PR fixes a few of those places.

@paulfradgley
Copy link
Contributor Author

Before fixing the other files in test_buffers that follow the same pattern, I wanted to check that my understanding of the spec is correct.

err = clSetMemObjectDestructorCallback(buffer, callback_align_free, inptr[i]);
if (err != CL_SUCCESS)
{
print_error(err, " clCreateBuffer failed\n" );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy paste error.

Suggested change
print_error(err, " clCreateBuffer failed\n" );
print_error(err, " clSetMemObjectDestructorCallback failed\n" );

Copy link

@Kerilk Kerilk Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appears 6 times

@Kerilk
Copy link

Kerilk commented Mar 4, 2026

Before fixing the other files in test_buffers that follow the same pattern, I wanted to check that my understanding of the spec is correct.

I think it is, but clSetMemObjectDestructorCallback is missing before version 1.1. So if the CTS is supposed to work for 1.0 then we need a version check.

@bashbaug
Copy link
Contributor

bashbaug commented Mar 9, 2026

From this it can be inferred that it is unsafe to free the hostptr used to created a CL_MEM_USE_HOST_PTR buffer without using a destructor callback.

IMHO this is a bit too strong.

It's definitely easier in many ways to free the memory pointed to by a host pointer via a destructor callback, but I don't think it's illegal or unsafe in all cases to free the memory pointed to by a host pointer without a destructor callback. As a trivial example, I think we all would agree that it should be possible to create a buffer, immediately release the buffer, and then free the host pointer, correct?

The non-trivial examples get a bit more complicated, due to commands in flight and implicit reference counts, but as long as the application synchronizes properly, I don't think a destructor callback is required for the complicated examples, either.

In this specific PR, it looks like the test is either doing a blocking call to clEnqueueReadBuffer, or is properly waiting for the event attached to the non-blocking call to clEnqueueReadBuffer. This means that there are no commands in flight and it should be safe to free the memory without a callback.

The one possible bug in the test is that the memory is free'd (explicitly, via align_free) before the buffer is released (implicitly, via the clMemWrapper destructor). I didn't think this was OK, but I'm having a hard time finding a place in the spec where this is described explicitly. If the buffer were released first, though (say, by assigning the clMemWrapper to nullptr), then I think the test would definitely be fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants