【问题标题】:Does only one Thread executing a Kernel implement the declaration of an array in Shared Memory in CUDA是否只有一个执行内核的线程在 CUDA 的共享内存中实现数组的声明
【发布时间】:2014-12-28 04:31:12
【问题描述】:

我正在实现以下将数组存储在共享内存中的 CUDA 内核:

// Difference between adjacent array elements
__global__ void kernel( int* in, int* out )  {
   int i  = threadIdx.x + blockDim.x * blockIdx.x;

   // Allocate a shared array, one element per thread
   __shared__ int sh_arr[BOCK_SIZE];

   // each thread reads one element to sh_arr
   sh_arr[i] = in[i];

   // Ensure reads from all Threads in Block complete before continuing
   __syncthreads();
   if( i > 0 )
      out[i] = sh_arr[i] - sh_arr[i-1];
   // Ensure writes from all Threads in Block complete before continuing
   __syncthreads(); 
}

BLOCK_SIZE 是在内核外部声明的常量。

似乎每个执行这个内核的线程都会创建一个新数组,因为每个执行这个内核的线程都会看到这一行:

__shared__ int sh_arr[BOCK_SIZE];

是不是只有第一个执行这个Kernel的Thread才会“看到”这一行,而所有后续的内核都忽略了这一行?

【问题讨论】:

    标签: cuda


    【解决方案1】:

    CUDA 中的共享变量在同一块中的线程之间共享。我不知道它是如何在幕后完成的,但是同一线程块中的线程会看到 __shared__ int sh_arr[BOCK_SIZE]; 但是,由于它具有 __shared__ 修饰符,因此只有一个线程会创建数组,而其他线程只会使用它。

    【讨论】:

      猜你喜欢
      • 2011-12-31
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2019-07-26
      • 1970-01-01
      • 2013-05-27
      相关资源
      最近更新 更多