从全局函数调用设备函数

Dam*_*ian 1 c cuda

我应该如何在'print'函数中访问'do_sth'函数(查看代码)?为什么在没有使用cudaMemcpy的情况下,GPU可见'N'(看代码)变量/常量?

 __device__ void do_sth(char *a, int N)
 {
         int idx = blockIdx.x * blockDim.x + threadIdx.x;
         if(idx < N)
         {       
                 a[idx] = a[idx]; 
         }
 }


 __global__ void print(char *a, int N) 
 {     
         //question_1: why there is an access to N, it is now in GPU memory, how?
         int idx = blockIdx.x * blockDim.x + threadIdx.x;

         //do_sth<<<nblock2,blocksize2>>>(a,N); //error_1: a host function call can not be configured
         //do_sth(&&a,N); //error_2: expected an expression

         if(idx<N)
         {       
                 a[idx]=a[idx];
         }
 }
Run Code Online (Sandbox Code Playgroud)

Cyg*_*sX1 6

  • __global__函数(又名"内核")已驻留在GPU上.它的所有参数(变量aN)在调用时通过共享或常量内存(取决于您的设备类型)传递,因此您可以直接访问这些变量.参数大小有限 - 前费米卡上256B,费米上16KB(?) 4KB,所以如果要传输大块数据,就无法避免cudaMemcpy功能.

  • __global__ 不应修改功能参数.

  • 当调用__device____global__不能在三联括号指定的配置参数.该__device__函数将由到达内核调用的所有线程调用.请注意,您可以从内部if语句中调用函数,以防止某些线程执行它.

  • 在当前版本的CUDA中,在内核执行期间不可能产生更多线程.

  • &&CUDA C++中没有一元运算符(普通C++中没有这样的运算符,现在新标准出现时不确定)

  • 根据http://developer.download.nvidia.com/compute/DevZone/docs/html/C/doc/CUDA_C_Programming_Guide.pdf上的编程指南,可计算2.x的设备可获得4KB (2认同)