【发布时间】: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