Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCL: Device / Host memory coherence for variables passed to kernel with CL_MEM_USE_HOST_PTR

Tags:

opencl

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.

like image 885
aaronqli Avatar asked Sep 05 '12 14:09

aaronqli


1 Answers

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 in mem_flags, the host_ptr specified in clCreateBuffer is guaranteed to contain the latest bits in the region being mapped when the clEnqueueMapBuffer command has completed; and the pointer value returned by clEnqueueMapBuffer will be derived from the host_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);
like image 116
aland Avatar answered Nov 19 '22 22:11

aland