How to return a single variable from a CUDA kernel function?

前端 未结 2 1912
梦谈多话
梦谈多话 2021-02-04 03:26

I have a CUDA search function which calculate one single variable. How can I return it back.

__global__ 
void G_SearchByNameID(node* Node, long nodeCount, long s         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-02-04 03:38

    To get a single result you have to Memcpy it, ie:

    #include 
    
    __global__ void g_singleAnswer(long* answer){ *answer = 2; }
    
    int main(){
    
      long h_answer;
      long* d_answer;
      cudaMalloc(&d_answer, sizeof(long));
      g_singleAnswer<<<1,1>>>(d_answer);
      cudaMemcpy(&h_answer, d_answer, sizeof(long), cudaMemcpyDeviceToHost); 
      cudaFree(d_answer);
      assert(h_answer == 2);
      return 0;
    }
    

    I guess the error come because you are passing a long value, instead of a pointer to a long value.

提交回复
热议问题