簡體   English   中英

多指針訪問導致 CUDA 中 printf 失敗

[英]Multiple pointer accesses cause failure of printf in CUDA

我目前正在嘗試在 CUDA 中實現 Blelloch 算法。 我想使用 printf 進行調試,但它有一個奇怪的行為,當我訪問內核中同一個數組的不同位置時它不起作用。 cudaDeviceSynchronize()在兩個內核調用之后使用。

這是上掃代碼:

__global__
void inclusive_scan_up_sweep(const Ray ray, float *scannedAngles)
    uint i = blockDim.x * blockIdx.x + threadIdx.x;
    uint index = (i * 2) + 1;
    int depth = log2((double)ray.length);

    for (int d = 0; d < depth; d++) {
        uint stride = pow(2.0, (double)d);
        if (((index + 1) % stride) == 0) {
            //this line stops printf from working
            //printf works if I remove '+ scannedAngles[index - stride]' from the equation
            scannedAngles[index] = scannedAngles[index] + scannedAngles[index - stride]; 
        }
        __syncthreads();
    }
}

這是向下掃描代碼:

__global__
void inclusive_scan_down_sweep(const Ray ray, float *scannedAngles)
{
    uint i = blockDim.x * blockIdx.x + threadIdx.x;
    uint index = (i * 2) + 1;
    int depth = log2((double)ray.length);

    //first zero last element
    if ((index + 1) == ray.length)
        scannedAngles[index] = 0;

    for (int d = depth - 1; d >= 0; d--) {
        uint stride = pow(2.0, (double)d);
        if (((index + 1) % stride) == 0) {
            float tmp = scannedAngles[index - stride];
            // these two line cause the same issue, however the second line
            // does not create any issues when I replace the 'tmp' with a fixed number 
            scannedAngles[index - stride] = scannedAngles[index];
            scannedAngles[index] += tmp;
        }
        __syncthreads();
    }

    printf("Thread: %d Result: %f\n", (index - 1), scannedAngles[index - 1]);
}

有誰知道這里發生了什么? 這可能是內存訪問優化的一些奇怪的副作用嗎?

問題似乎只是 index 和 stride 是 unsigned int 並且括號運算符將它們解釋為signed int,最有可能導致訪問沖突並且內核崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM