Pass double pointer in a struct to CUDA

2020-04-12 09:01发布

问题:

I've got the following struct:

 struct Param
{
double** K_RP;                              
};

And I wanna perform the following operations on "K_RP" in CUDA

 __global__  void Test( struct Param prop)
       {
       int ix = threadIdx.x;
       int iy = threadIdx.y;
       prop.K_RP[ix][iy]=2.0;
       }

If "prop" has the following form, how should I do my "cudaMalloc" and "cudaMemcpy" operations?

int main( )
{
Param prop;
Param cuda_prop;

prop.K_RP=alloc2D(Imax,Jmax);

//cudaMalloc cuda_prop ?

//cudaMemcpyH2D prop to cuda_prop ?

Test<<< (1,1), (Imax,Jmax)>>> ( cuda_prop);   

//cudaMemcpyD2H cuda_prop to prop ?

return (0);

}

回答1:

Questions like this get asked from time to time. If you search on the cuda tag, you'll find a variety of examples with answers. Here's one example.

  • In general, dynamically allocated data contained within structures or other objects requires special handling. This question/answer explains why and how to do it for the single pointer (*) case.
  • Handling double pointers (**) is difficult enough that most people would recommend "flattening" the storage so that it can be handled by reference with a single pointer (*). If you really want to see how the double pointer (**) method works, review this question/answer. It's not trivial.