Take the 2-minute tour ×

I am beginner in parallel programming. I have a query which might be seem to be silly but I didn't get a definitive answer when I googled it out.

In GPU computing there is a device i.e. the GPU and the host i.e. the CPU. I wrote a simple hello world program which will allocate some memory on the gpu, pass two parameters (say src[] and dest[]) to the kernel, copy src string i.e. Hello world to dest string and get the dest string from gpu to the host.

Is the string "src" read by the GPU or the CPU writes to the GPU ? Also when we get back the string from GPU, is the GPU writing to the CPU or the CPU reading from the GPU?

In transferring the data back and forth there can be four possibilities 1. CPU to GPU - CPU writes to GPU - GPU reads form CPU 2. GPU to CPU - GPU writes to the CPU - CPU reads from GPU

Can someone please explain which of these are possible and which are not?

share|improve this question
add comment

4 Answers

In earlier versions of CUDA and corresponding hardware models, the GPU was more strictly a coprocessor owned by the CPU; the CPU wrote information to the GPU, and read the information back when the GPU was ready. At the lower level, this meant that really all four things were happening: the CPU wrote data to PCIe, the GPU read data from PCIe, the GPU then wrote data to PCIe, and the CPU read back the result. But transactions were initiated by the CPU.

More recently (CUDA 3? 4? maybe even beginning in 2?), some of these details are hidden from the application level, so that, effectively, GPU code can cause transfers to be initiated in much the same way as the CPU can. Consider unified virtual addressing, whereby programmers can access a unified virtual address space for CPU and GPU memory. When the GPU requests memory in the CPU space, this must initiate a transfer from the CPU, essentially reading from the CPU. The ability to put data onto the GPU from the CPU side is also retained. Basically, all ways are possible now, at the top level (at low levels, it's largely the same sort of protocol as always: both read from and write to the PCIe bus, but now, GPUs can initiate transactions as well).

share|improve this answer
Which, via various synchronized controllers boils down to DMA (en.wikipedia.org/wiki/Direct_memory_access). –  ananthonline Jul 2 '12 at 20:21
Thanks a lot. Its good to know that GPU can also initiate the transactions as well. So is there any way to know who actually initiated the transactions using any of the library routines in Opencl? or the routines using which I can force either CPU or the GPU to perform the the transaction..? –  Nike Jul 2 '12 at 20:44
I don't know OpenCL as well as I should; that being said, I'd be surprised if CUDA didn't give you some way to check where a variable lived in the unified virtual address space. That sort of information could be useful, even outside the use-case of explicitly managed transfers (which newer versions should still support). –  Patrick87 Jul 2 '12 at 20:47
add comment

Actually none of these. Your CPU code initiates the copy of data, but while the data is transferred by the memory controller to the memory of the GPU through whatever bus you have on your system. Meanwhile, the CPU can process other data. Similarly, when the GPU has finished running the kernels you launched, your CPU code initiates the copy of data, but meanwhile both GPU and CPU can handle other data or run other code.

The copies are called asynchronous or non-blocking. You can optionally do blocking copies, in which the CPU waits for the copy to be completed.

When launching asynchronous tasks, you usually register an "event", which is some kind of flag that you can check later on, to see if the task is finished or not.

share|improve this answer
add comment

In OpenCL the Host (CPU) is exclusively controlling all the transfers of data between GPU and GPU. The host transfers data to the GPU using buffers. The host transfers (reads) back from the GPU using buffers. For some systems and devices, the transfer isn't physically copying bytes as the Host and GPU use the same physical memory. This is called zero copy.

share|improve this answer
add comment

I just found out in this forum http://devgurus.amd.com/thread/129897 that using CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR in clCreateBuffer allocates memory on the host and that it wont be copied on the device.

There may be issue with performance but this is what I am looking for. Your comments please..

share|improve this answer
add comment

Your Answer


