I have the following code in Cuda (it\'s not the full code). I\'m trying to check if it copies properly the arrays from host to device and from device to host.
flVecto
This is not doing what you think it is:
int* pass(vector<int>& flVector, int* indeces, int inSize, int* inOnDevice)
{
...
cudaMalloc((void**) &(inOnDevice), sizeof(int) * inSize);
When you pass a pointer to a function this way, you are passing the pointer by value.
If you then take the address of that pointer-passed-by-value inside the function, that address has no connection to anything in the function calling context. Inside the function pass
, there is a local copy of *inOnDevice
, and you are modifying that local copy with the subsequent cudaMalloc
operation.
Instead, you need to pass a pointer-to-a-pointer in this situation (simulated pass-by-reference) or else pass by reference. For the pointer-to-a-pointer example, it would look something like this:
int* pass(vector<int>& flVector, int* indeces, int inSize, int** inOnDevice)
{
...
cudaMalloc((void**) inOnDevice, sizeof(int) * inSize);
cudaMemcpy(*inOnDevice, indeces, inSize*sizeof(int), cudaMemcpyHostToDevice);
And in main
:
flOnDevice = pass(flVector, indeces, indSize, &inOnDevice);
And I think if you had used proper cuda error checking as I suggested to you before, you would have seen an error returned from this line of code:
cudaMemcpy(inde_h,inOnDevice,inSize*sizeof(int),cudaMemcpyDeviceToHost);