Cuda allocation and return array from gpu to cpu

前端 未结 1 2030
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 23:51

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

相关标签:
1条回答
  • 2021-01-26 00:33

    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);
    
    0 讨论(0)
提交回复
热议问题