I'm currently considering to write an application that would use OpenCL to offload atom distance calculations to the GPU (the specific situation being Monte Carlo simulations of protein folding), and I have a couple of questions in this regard.

To describe the problem briefly: I have, in the host memory, a float array that stores atom coordinates (with 3 * N elements) and another float array of N * N elements that stores the distances between the atom pairs.

Ideally, I'd like to write an OpenCL kernel that takes the coordinates array as input and writes the pair-wise distances into the output distances array. Since these arrays will be used in other parts of the host code (without assuming OpenCL acceleration), I'd like to map them onto OpenCL memory objects on the device memory in order to avoid doing potentially expensive copies from host to device memory and vice-versa.

After reading the latest OpenCL reference, it seems to me that there are, at least, two ways of achieving this:

1) creating the buffer objects passing CL_MEM_USE_HOST_PTR to clCreateBuffer(), as well as the pointers to the arrays in the host memory. This should use the memory referenced by the host pointers as the storage bits for the memory objects.

2) creating the buffers in device memory, and then mapping them to address space on host memory with clEnqueueMapBuffer(). In this case, the coordinates and distance pointers defined on the host side should store the pointers returned by the calls to clEnqueueMapBuffer().

First of all: am I correct at all in arriving to these two conclusions? If so, which method should be preferred?

Many thanks!
Andres

dbs2

07-21-2009, 11:53 AM

Yes, you are correct.
The specific performance differences will depend on the implementation and the device. (E.g., mapping may be much faster on the CPU because it avoids a copy, but if you need to move the data from a GPU anyway, it might not be much faster.)

Do keep in mind that if you tell CL to use a host pointer, you need to make sure that your kernel has finished executing before you access the data or it may not be valid.

acolubri

07-21-2009, 12:14 PM

Hello,

Thanks for your response. Since my experience with OpenCL is very limited at the moment, I was drawing these conclusions from some OpenGL coding I did recently where I used Vertex Buffer Object (VBO) extensions. VBO's can be mapped to CPU-memory buffers to avoid explicit data copy between CPU and GPU, and although this mapping involves data moving between the two, the performance is still better than doing explicit copies. But I should do some benchmarks to actually see, in OpenCL, what are the consequences of such mappings. These benchmarks will depend, of course, on the specific hardware being used, driver versions, OS, etc.

Andres

acolubri

08-02-2009, 01:42 AM

I managed to run my distance calculation kernel which uses a host pointer. Basically, I create the CL memory object by doing:

My understanding is that the call to clEnqueueReadBuffer shouldn't be needed since cldistances is mapped to distances, so the data copying/caching would be resolved implicitly by CL. Am I missing something here?

affie

08-02-2009, 07:59 PM

You do not have to call clEnqueueReadBuffer. clEnqueueMapBuffer is sufficient. This is what the spec states:

If the buffer or image object is created with CL_MEM_USE_HOST_PTR set in mem_flags, the following will be true:

- The host_ptr specified in clCreateBuffer or clCreateImage{2D|3D} is guaranteed to contain the latest bits in the region being mapped when the clEnqueueMapBuffer or clEnqueueMapImage command has completed.

- The pointer value returned by clEnqueueMapBuffer or clEnqueueMapImage will be derived from the host_ptr specified when the buffer or image object is created.

acolubri

08-08-2009, 03:28 PM

Many thanks for bringing the clEnqueueReadBuffer() function to my attention. It seems it is exactly what I was looking for.

So, the proper order to call the all these functions should be as follows: