C++ 数据结构和 CUDA

标签 c++ data-structures cuda

我有一个结构可以是

  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)
{
    //....
}

关于C++ 数据结构和 CUDA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17617492/

相关文章:

c++ - 在 main 中调用公共(public)函数

haskell - 数据结构中的自引用 - 检查相等性

javascript - 数组javascript的两个元素组合

java - 使用什么数据结构来存储 <String, String> 类型的键值对?一键多值

c++ - CMake 的 CUDA 编译问题

c - 如何读取 C 中图像的 RGB channel ?

python - 如何使用 CUDA 通过目前通过 scipy.sparse.csc_matrix.dot 实现的密集向量积来加速稀疏矩阵?

c++ - 为什么不使用std::move将std::unique_ptr复制到另一个?

c++ - 如何在 C++ 中的 2 个函数集之间切换?

c++ - 基类中的虚拟继承和空虚表