使用CUDA进行矩阵换位

时间:2012-11-09 20:33:57

标签: matrix cuda

我需要转置一个方阵。我使用矩阵a[i][j] = 0 if i>j, a[i][j] = if i<=j,测试程序,但结果显示并非所有元素都在正确的位置。

这是代码(main()除外):

#include <stdio.h> 
#include <stdlib.h>
__global__ void transpose_kernel (float *a, float *b, int n) {
    unsigned int ax = blockDim.x * blockIdx.x + threadIdx.x;
    unsigned int ay = blockDim.y * blockIdx.y + threadIdx.y;
    unsigned int aIdx = ax + n * ay;
    unsigned int bIdx = ay + n * ax;

    b[bIdx] = a[aIdx];
}

int transpose_host (float *a, float *b, int n) {
    int size = n * n * sizeof (float);
    float *aDev = NULL, *bDev = NULL;

    cudaError_t cuerr = cudaMalloc ((void**)&aDev, size);
    if (cuerr != cudaSuccess) {
        fprintf (stderr, "Cannot allocate GPU memory for aDev: %s\n", cudaGetErrorString (cuerr));
        return (-1);
    }

cuerr = cudaMalloc ((void**)&bDev, size);
if (cuerr != cudaSuccess) {
    fprintf (stderr, "Cannot allocate GPU memory for bDev: %s\n", cudaGetErrorString (cuerr));
    return (-1);
}

dim3 blockSize = dim3 (16, 16, 1);
dim3 gridSize = dim3 (n/16 + 1, n/16 + 1, 1);

cuerr = cudaMemcpy (aDev, a, size, cudaMemcpyHostToDevice);
if (cuerr != cudaSuccess) {
    fprintf (stderr, "Cannot copy data from a to aDev: %s\n", cudaGetErrorString (cuerr));
    return (-1);
}

transpose_kernel <<< gridSize, blockSize >>> (aDev, bDev, n);

cuerr = cudaGetLastError ();
if (cuerr != cudaSuccess) {
    fprintf (stderr, "Cannot launch CUDA kernel: %s\n", cudaGetErrorString (cuerr));
    return (-1);
}

cuerr = cudaDeviceSynchronize ();
if (cuerr != cudaSuccess) {
    fprintf (stderr, "Cannot synchronize CUDA kernel: %s\n", cudaGetErrorString (cuerr));
    return (-1);
}

cuerr = cudaMemcpy (b, bDev, size, cudaMemcpyDeviceToHost);
if (cuerr != cudaSuccess) {
    fprintf (stderr, "Cannot copy data from b to bDev: %s\n", cudaGetErrorString (cuerr));
    return (-1);
}

cudaFree (aDev);
cudaFree (bDev);

    return (0);
}

为什么我的数组不能正确转置?

1 个答案:

答案 0 :(得分:2)

问题在于“额外”线程超出分配的数组。

当您指定网格块时,您会向上舍入(实际上,即使事情均匀分配,也会强制舍入到下一个整数:)

dim3 gridSize = dim3 (n/16 + 1, n/16 + 1, 1);

因此总是存在那些ax或ay落在[0,n]之外的线程。因此,无论如何,当您将a[aIdx]复制到b[bIdx]时,您正在将随机数据复制到内存中,实际上可能会根据计划覆盖“真实”数据。

您可以通过更改内核来检查此问题:

if (ax < n && ay < n)
    b[bIdx] = a[aIdx];

如果事情分配均匀,您可能希望将网格大小的舍入更改为不向上舍入:

dim3 gridSize = dim3 ((n+15)/16, (n+15)/16, 1);