【发布时间】:2012-03-03 23:10:44
【问题描述】:
我正在尝试实现简单的并行缩减。我正在使用来自 CUDA sdk 的代码。但是不知何故,我的内核中存在问题,因为共享数组没有获取全局数组的值及其全为零。
extern __ shared __ float4 sdata[];
// each thread loadsone element from global to shared mem
unsigned int tid = threadIdx.x;
unsigned int i= blockIdx.x*blockDim.x+ threadIdx.x;
sdata[tid] = dev_src[i];
__syncthreads();
// do reduction in shared mem
for(unsigned int s=1; s < blockDim.x; s *= 2) {
if(tid % (2*s) == 0){
sdata[tid] += sdata[tid + s];
}
__syncthreads();
}
// write result for this block to global mem
if(tid == 0)
out[blockIdx.x] = sdata[0];
编辑::
好的,我通过删除 extern 关键字并使共享数组的大小固定为 512 来使其正常工作。我现在状态良好。也许有人可以解释为什么它不能使用 extern 关键字
【问题讨论】:
-
所以 dev_src 有正确的值,但 sdata 不知何故没有得到 dev_src 值
-
不要向 cmets 添加更新。更新您的问题。