是否只有一个执行内核的线程在CUDA的共享内存中实现数组声明

是否只有一个执行内核的线程在CUDA的共享内存中实现数组声明,cuda,Cuda,我正在实现以下在共享内存中存储阵列的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[

我正在实现以下在共享内存中存储阵列的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(); 
}
块大小是在内核外部声明的常量

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

__shared__ int sh_arr[BOCK_SIZE];

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

CUDA中的共享变量在同一块中的线程之间共享。我不知道它是如何在引擎盖下完成的,但同一线程块中的线程将看到
\uuuuu shared\uuuuu int sh\u arr[BOCK\u SIZE]\uuuuuu shared\uuuu
修饰符,因此只有一个线程将创建数组,而其他线程将仅使用它