6
votes

I am trying to run a kernel on the GPU and do additional computation on the host (CPU). I see this effect:

only the kernel needs around 2000 ms:

clEnqueueNDRangeKernel ...

clFinish (or clWaitForEvents, I tried both)

I simulated additional computation on the CPU with sleep(10):

clEnqueueNDRangeKernel ...

sleep(10);

clFinish (or clWaitForEvents)

In theory the kernel should run on GPU and after the 10 sec sleep the kernel should be finished. But time measuring said it all needs 12000ms instead of 10000.

Does clFinish or clWaitForEvents invoke the kernel to start or did I miss something?

I'm using an AMD Fusion CPU/GPU und Linux.

Thanks a lot.

2
How large is your data? Is it possible that 2000ms represents the time needed to transfer your data from the GPU to the CPU? - user1202136
The data is under 1 MB. But it shouldn't be in my measuring. I copy data to the device, start measuring, execute the kernel + sleep + clFinish, stop measuring, copy data back to host - Tomas
How exactly are you measuring? Do you copy your data to the device synchronously? - reima
yeah, I copy the data with clEnqueueWriteBuffer(command_queue, Ddata, CL_TRUE, ...) and I use the default in order command queue. For time measuring I take gettimeofday(). - Tomas

2 Answers

6
votes

Try calling clFlush right after clEnqueueNDRangeKernel:

clFlush

Issues all previously queued OpenCL commands in a command-queue to the device associated with the command-queue.

http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/clFlush.html

2
votes

clFinish() only guarantees that the kernel has been finished when the program proceeds ahead this function, but when the kernel will be started to execute is not sure. clFlush() can guarantee that the kernel has been started on device while the program proceeds ahead clFlush() sentence, but when will it be finished is not sure, so you need clFlush() to ensure the kernel has been launched on device, then the time (2000ms) can be overlapped by sleep time (10000ms) on host end. Hope it can be helpful.