# Transfer data between CPU and GPU

Hi there,

I have a program in which the main process runs on CPU and some part of the calculations during the main process is to be performed on GPU. Basically, what happens in the program is that in each iteration of an outer loop, a system of linear equations (Ax=b) should be solved for four different RHS vectors but with the same matrix. The iterative solution is performed on GPU. But all four RHS vectors are not present initially. In fact, in each outer iteration, first the required matrix and the first RHS are formed initially. However, other RHS vectors are formed on CPU, using the solution of the previous system. So, in each outer iteration, I need to copy the matrix to GPU once and then just send and receive the value of the b and x vector to and from GPU, respectively. In other words, when I send the matrix to GPU , I need to be able to keep it there for subsequent uses, while in the meantime, I need to refer back to CPU to perform other stuff.

Transfer of the b and x vectors between GPU and CPU is straightforward. However, in order to avoid copying the same matrix four times per outer iterations (just copy it form CPU to GPU once and use it several times), What sort of memory allocation can I use for the matrix?

I hope I could describe my question well enough.

Omid

Hello,

If you allocated an array and copy data to it, it will be there until you do changes to it, no special things needed to be done.

So if you have to reuse the array something like this should work:

cudaMalloc

cudamemcpy
the loop swhich uses the array

Hi Pasoleatis,

What if after the GPU does come calculations, then a value is sent back to the cpu which does some calculations on it (meanwhile the GPU is doing nothing and waiting for new data from CPU), and then data is sent from CPU to GPU and again the GPU does some calculations using the new data and of course the same already allocated arrays as previous iterations. Does this change of control between GPU and CPU affect the allocated data on GPU?

Omid

No. As long as you do not run any function, the data will not change. Just try it and check a case with known reults