【发布时间】:2012-02-07 10:28:11
【问题描述】:
鉴于我有数组
Let Sum be 16
dintptr = { 0 , 2, 8,11,13,15}
我想使用 GPU 计算连续索引之间的差异。所以最终的数组应该如下:
count = { 2, 6,3,2,2,1}
下面是我的内核:
//for this function n is 6
__global__ void kernel(int *dintptr, int * count, int n){
int id = blockDim.x * blockIdx.x + threadIdx.x;
__shared__ int indexes[256];
int need = (n % 256 ==0)?0:1;
int allow = 256 * ( n/256 + need);
while(id < allow){
if(id < n ){
indexes[threadIdx.x] = dintptr[id];
}
__syncthreads();
if(id < n - 1 ){
if(threadIdx.x % 255 == 0 ){
count[id] = indexes[threadIdx.x + 1] - indexes[threadIdx.x];
}else{
count[id] = dintptr[id+1] - dintptr[id];
}
}//end if id<n-1
__syncthreads();
id+=(gridDim.x * blockDim.x);
}//end while
}//end kernel
// For last element explicitly set count[n-1] = SUm - dintptr[n-1]
2 个问题:
- 这个内核速度快吗?您能否提出一个更快的实施方案?
- 此内核是否处理任意大小的数组(我认为可以)
【问题讨论】:
-
您是否测试/计时/分析了您的代码?你学到了什么?
-
@programmer:我认为您错过了第三个问题“代码是否正确”,答案是否定的,因为编写时可能会出现死锁。
-
它还假设块大小为 256(我认为)。
-
@talonmies:很好的建议!我通过将第二个 __syncthreads 移到 if(id
-
程序员@talonmies 和我在其他问题中为您提供了相关代码。真的没有必要提出不同的问题。