Use clSetMemObjectDestructorCallback for CL_MEM_USE_HOST_PTR#2628
Use clSetMemObjectDestructorCallback for CL_MEM_USE_HOST_PTR#2628paulfradgley wants to merge 2 commits intomainfrom
Conversation
|
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" ); |
There was a problem hiding this comment.
Copy paste error.
| print_error(err, " clCreateBuffer failed\n" ); | |
| print_error(err, " clSetMemObjectDestructorCallback failed\n" ); |
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. |
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. |
Use clSetMemObjectDestructorCallback for hostptr freeing for CL_MEM_USE_HOST_PTR buffers in buffers CTS test.
The OpenCL 3.0 API spec states that:
and
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.