使用CUDA 4.2和驱动程序295.41非常有趣的行为

时间:2012-08-25 18:01:56

标签: linux cuda driver gpgpu

在Linux上使用CUDA 4.2和驱动程序295.41时,我目睹了一个非常有趣的行为。 代码本身只不过是找到随机矩阵的最大值并将位置标记为1。

#include <stdio.h>
#include <stdlib.h>

const int MAX = 8;

static __global__ void position(int* d, int len) {
    int idx = threadIdx.x + blockIdx.x*blockDim.x;
    if (idx < len) 
        d[idx] = (d[idx] == MAX) ? 1 : 0;
}

int main(int argc, const char** argv) {
    int colNum = 16*512, rowNum = 1024;
    int len = rowNum * colNum;

    int* h = (int*)malloc(len*sizeof(int));
    int* d = NULL;
    cudaMalloc((void**)&d, len*sizeof(int));

    // get a random matrix
    for (int i = 0; i < len; i++) {
        h[i] = rand()%(MAX+1);
    }   

    // launch kernel
    int threads = 128;
    cudaMemcpy(d, h, len*sizeof(int), cudaMemcpyHostToDevice);
    position<<<(len-1)/threads+1, threads>>>(d, len);
    cudaMemcpy(h, d, len*sizeof(int), cudaMemcpyDeviceToHost);

    cudaFree(d);
    free(h);
    return 0;
}

当我设置rowNum = 1024时,代码根本不起作用,就好像内核从未启动过一样。 如果rowNum = 1023,一切正常。

这个rowNum值以某种方式与块大小进行盘旋(在本例中为128),如果我将块大小更改为512,则行为发生在rowNum = 4095和4096之间。

我不太确定这是一个错误还是我错过了什么?

1 个答案:

答案 0 :(得分:1)

调用CUDA函数后,总是检查错误。例如,在您的代码中,在内核启动期间发生invalid configuration argument错误。

这通常意味着网格或块尺寸无效。

使用colNum = 16*512, rowNum = 1024,您尝试运行65536个块x 128个线程,超过最大网格维度(对于计算能力为1.x和2.x的GPU,为65535个块,不确定为3.x) 。

如果你需要运行更多的线程,你可以增加块大小(你已经尝试了它并产生了一些效果)或使用2D / 3D网格(3D仅适用于计算能力为2.0或更高的设备)。