C++数据结构和CUDA

C++ data structures and CUDA

本文关键字:CUDA 数据结构 C++      更新时间:2023-10-16

我有一个可以是的结构

  struct type1{ double a,b,c;}

也可以是

  struct type2{ double a,b,c,d,e;}

在我的cuda代码宿主函数中,我有一些类似的东西

  void compute(){
      // some code
      // data on devices (up to 10) 
      type *xxx[10];   // this is where i want either type1 or type2 structures
                       // the "type" is not known at compile time but i want to 
                       // determine at runtime based on some input variable. this 
                       // part is not real code rather this is what i want to achive.
      int DevUsed;    // some code to give value to int DevUsed
      for(int idev=0;idev<DevUsed;idev++){
           // set cuda device
          if ( cudaMalloc(&xxx[iDev], sizeof(type)) != cudaSuccess )
            // print error message;
          cudaMemcpy(xxx[iDev], pIF1, sizeof(type), cudaMemcpyHostToDevice);
          function2<<<grid, block>>>(xxx[iDev]);  // where function2 is the kernel
       }
   }

我的问题是,用"type*xxx[10];"

这样的通用代码在type1和type2数据结构之间进行选择的方法是什么

C++模板就是为这种情况而设计的。

template <class T>
void compute(){
  // some code
  // data on devices (up to 10) 
  T xxx[10];   // this is where i want either type1 or type2 structures
                   // the "type" is not known at compile time but i want to 
                   // determine at runtime based on some input variable. this 
                   // part is not real code rather this is what i want to achive.
  int DevUsed;    // some code to give value to int DevUsed
  for(int idev=0;idev<DevUsed;idev++){
       // set cuda device
      if ( cudaMalloc(&xxx[iDev], sizeof(T)) != cudaSuccess )
        // print error message;
      cudaMemcpy(xxx[iDev], pIF1, sizeof(T), cudaMemcpyHostToDevice);
      function2<<<grid, block>>>(xxx[iDev]);  // where function2 is the kernel
   }
}

请注意,您还需要这两种类型的内核模板,如

template <class T>
__global__ void function2(T x)
{
    //....
}