If a variable is passed to kernel with CL_MEM_USE_HOST_PTR, does it mean any change to the variable in the device would be also shown in host memory?
I am in a scenario where I am using CPU as the device instead of GPU, so everything passed to kernel will be marked with CL_MEM_USE_HOST_PTR.
If this is true, then I no longer need to read everything back to host, which is very convenient.
Your understanding is correct, except one possible pitfall: documentation states that
OpenCL implementations are allowed to cache the buffer contents pointed to by
host_ptr
in device memory. This cached copy can be used when kernels are executed on a device.
This means that changes to data performed by kernel might not be immediately reflected in host_ptr
. In fact, there is no guarantee that host_ptr
contains valid data while it is used for buffer.
In order to have valid and up-to-date data you must force synchronization. The offcial documentation is a little vague about this moment, but buffer mapping/unmapping definetly works:
If the buffer object is created with
CL_MEM_USE_HOST_PTR
set inmem_flags
, thehost_ptr
specified inclCreateBuffer
is guaranteed to contain the latest bits in the region being mapped when theclEnqueueMapBuffer
command has completed; and the pointer value returned byclEnqueueMapBuffer
will be derived from thehost_ptr
specified when the buffer object is created.
Here is an example adapted from Khronos group forum post:
cl_mem device_output = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, size, original_output, NULL);
// run the kernel
void* pointer = clEnqueueMapBuffer(queue, device_output, CL_TRUE, CL_MAP_READ, size, 0, 0, NULL, NULL, NULL);
// work with 'original_output'
clEnqueueUnmapMemObject(queue, device_output, pointer, 0, NULL, NULL);
clReleaseMemObject(device_output);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With