我有以下代码在将其与一维块一起使用时可以正常工作:
__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);
它编译,它运行但从未完成(?!),我在这里做错了什么有什么想法吗?