我正在编写一个 CUDA 代码,我正在使用 GForce 9500 GT 显卡。
我正在尝试处理 20000000 个整数元素的数组,我使用的线程号是 256
经线大小为 32。计算能力为 1.1
这是硬件http://www.geforce.com/hardware/desktop-gpus/geforce-9500-gt/specifications
现在,块数 = 20000000/256 = 78125 ?
这听起来不正确。如何计算块数?任何帮助,将不胜感激。
我的 CUDA 内核函数如下。这个想法是每个块将计算其总和,然后通过将每个块的总和相加来计算最终总和。
__global__ static void calculateSum(int * num, int * result, int DATA_SIZE)
{
extern __shared__ int shared[];
const int tid = threadIdx.x;
const int bid = blockIdx.x;
shared[tid] = 0;
for (int i = bid * THREAD_NUM + tid; i < DATA_SIZE; i += BLOCK_NUM * THREAD_NUM) {
shared[tid] += num[i];
}
__syncthreads();
int offset = THREAD_NUM / 2;
while (offset > 0) {
if (tid < offset) {
shared[tid] += shared[tid + offset];
}
offset >>= 1;
__syncthreads();
}
if (tid == 0) {
result[bid] = shared[0];
}
}
我称这个函数为
calculateSum <<<BLOCK_NUM, THREAD_NUM, THREAD_NUM * sizeof(int)>>> (gpuarray, result, size);
其中 THREAD_NUM = 256 且 gpu 数组的大小为 20000000。
这里我只是使用块号为 16 但不确定它是否正确?如何确保实现最大并行度?
这是我的 CUDA 占用计算器的输出。它说当块数为 8 时我将有 100% 的占用率。这意味着当块数 = 8 和线程数 = 256 时我将获得最大效率。那是对的吗?
谢谢