[英]Does only one Thread executing a Kernel implement the declaration of an array in Shared Memory in 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[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];
是否只有执行该内核的第一个线程“看到”这一行,而所有后续内核都忽略了这一行?
CUDA中的共享变量在同一块中的线程之间共享。 我不知道到底是怎么做的,但是在同一线程块中的线程会看到__shared__ int sh_arr[BOCK_SIZE];
但是,由于它具有__shared__
修饰符,因此只有一个线程将创建该数组,而其他线程将仅使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.