【问题标题】:A function calls another function in CUDA C++一个函数在 CUDA C++ 中调用另一个函数
【发布时间】:2014-04-28 16:24:50
【问题描述】:

我的 CUDA 编程有问题! 输入是一个矩阵 A( 2 x 2 ) 输出是一个矩阵 A(2 x 2),每个新值都是 **3 旧值的指数** 例子 : 输入:A:{2,2} 输出:A{8,8} { 2,2 } { 8,8 }

我在文件 CudaCode.CU 中有 2 个函数:

   __global__ void Power_of_02(int &a)
{
    a=a*a;
}

 //***************
__global__ void Power_of_03(int &a)
{
    int tempt = a;
    Power_of_02(a); //a=a^2;
    a= a*tempt; // a = a^3
}

和内核:

__global__ void CudaProcessingKernel(int *dataA )    //kernel function  

   {  
        int bx = blockIdx.x;  
    int tx = threadIdx.x;  
        int tid = bx * XTHREADS + tx;  

    if(tid < 16)
    {
    Power_of_03(dataA[tid]);
        }
    __syncthreads();

   }  

我觉得是对的,但是出现错误:calla __global__ function("Power_of_02") from a __global__ function("Power_of_03") 只允许在compute_35及以上架构上使用

为什么我错了?如何修复它?

【问题讨论】:

    标签: function cuda parallel-processing call global


    【解决方案1】:

    这个错误很容易解释。用__global__ 修饰的CUDA 函数代表一个内核。内核可以从主机代码启动。在 cc 3.5 或更高版本的 GPU 上,您还可以从设备代码启动内核。因此,如果您从设备代码调用 __global__ 函数(即从另一个用 __global____device__ 装饰的 CUDA 函数),那么您必须针对适当的架构进行编译。这称为CUDA动态并行,如果你想使用它,你应该read the documentation学习如何使用它。

    当您启动内核时,无论是从主机代码还是设备代码,您都必须提供启动配置,即三人字形符号之间的信息:

    CudaProcessingKernel<<<grid, threads>>>(d_A);
    

    如果您想使用来自另一个内核的 2 次幂代码,您需要以类似的适当方式调用它。

    但是,根据您的代码结构,您似乎可以通过将您的 2 次幂和 3 次幂函数声明为 __device__ 函数来使事情正常进行:

       __device__ void Power_of_02(int &a)
    {
        a=a*a;
    }
    
     //***************
    __device__ void Power_of_03(int &a)
    {
        int tempt = a;
        Power_of_02(a); //a=a^2;
        a= a*tempt; // a = a^3
    }
    

    这可能对你有用,也许是你的意图。用__device__ 修饰的函数不是内核(因此它们不能直接从主机代码调用),但可以直接从任何架构上的设备代码调用。 programming guide 也将有助于解释差异。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多