我有以下代码,当它与1维块一起使用时可以正常工作:
__global__ void dot_product_large_arrays( int N, double *a, double *b,
double *res)
{
__shared__ double cache[TILE_DIM];
int tid = threadIdx.x + blockIdx.x * blockDim.x;
int i = 0, cacheIndex = 0;
double temp = 0;
cacheIndex = threadIdx.x;
while (tid < N) {
temp += a[tid] * b[tid];
tid += blockDim.x*gridDim.x;
}
cache[cacheIndex] = temp;
__syncthreads();
for (i = blockDim.x/2; i > 0; i>>=1) {
if (threadIdx.x < i) {
cache[threadIdx.x] += cache[threadIdx.x + i];
}
__syncthreads();
}
__syncthreads();
if (cacheIndex == 0) {
atomicAdd(res, cache[0]);
}
}
现在,我的数组大小为9000*9000
,不适合可用于计算的块数。我曾考虑使用X
和Y
中的块来扩展它,所以我的修改是:
int tid = threadIdx.x + blockIdx.x * blockDim.x +
blockDim.x*gridDim.x*blockIdx.y;
...
while (tid < N) {
temp += a[tid] * b[tid];
tid += blockDim.x*gridDim.x*blockIdx.y*grimDim.y;
}
和我的内核调用
int totalThreads = 9000*9000;
int blockSize = 512;
int blockDimY = 256;
int blockDimX = (totalThreads/( blockSize*blockDimY))+ 1;
dim3 dimGrid(blockDimX,blockDimY);
dim3 dimBlock(blockSize);
dot_product_large_arrays <<< dimGrid, dimBlock >>>(totalThreads, d_a, d_b, d_res);
它编译,它运行但从未完成(?!),我在这里做错了什么想法?
答案 0 :(得分:3)
看起来你正在增加tid的行是问题:
while (tid < N) {
temp += a[tid] * b[tid];
tid += blockDim.x * gridDim.x * blockIdx.y * grimDim.y; //blockIdx.y can be zero
}
至少有一个块的块y索引为0,这意味着您将递增0并导致一个或多个线程进入无限循环。