【问题标题】:Setup DimGrid and DimBlock in CUDA在 CUDA 中设置 DimGrid 和 DimBlock
【发布时间】:2015-06-15 07:37:07
【问题描述】:

我在 CUDA 中做矩阵乘法。以下设置有效:

int TILE = 8;
dim3 DimGrid((numCColumns - 1)/TILE + 1, (numCRows - 1)/TILE + 1, 1);
dim3 DimBlock(TILE, TILE, 1);

但是如果我对整个图像使用一个块,它会返回全零。这是什么原因?假设一个块可以包含整个图像(输入为 64x64)。

dim3 DimGrid(1,1,1);
dim3 DimBlock(numCColumns, numCRows, 1);

这是我在主函数中调用内核的方式:

matrixMultiply<<<DimGrid, DimBlock>>>(deviceA, deviceB, deviceC,
                                        numARows, numAColumns,
                                        numBRows, numBColumns,
                                        numCRows, numCColumns);

和内核:

__global__ void matrixMultiply(float * A, float * B, float * C,
                   int numARows, int numAColumns,
                   int numBRows, int numBColumns,
                   int numCRows, int numCColumns) {
    //@@ Insert code to implement matrix multiplication here
    int Row = blockIdx.y * blockDim.y + threadIdx.y;
    int Col = blockIdx.x * blockDim.x + threadIdx.x;

    if ((Row < numCRows) && (Col < numCColumns))
    {
        float value = 0.0;
        for (int i = 0; i < numAColumns; i++)
            value += A[Row * numAColumns + i] * B[i*numBColumns + Col];
        C[Row * numCColumns + Col] = value;
    }
}

【问题讨论】:

    标签: cuda


    【解决方案1】:

    但是如果我对整个图像使用一个块,它会返回全零。这是什么原因?

    一个 CUDA 线程块是limited to a maximum of 1024 threads(请参阅“每个块的最大线程数”)。对于多维线程块,这意味着维度的乘积必须小于或等于 1024(对于 cc2.x 和更新的 GPU。)

    即使是 64x64 的图像,这也行不通:

    dim3 DimBlock(numCColumns, numCRows, 1);
    

    因为numCColumns * numCRows 大于 1024。

    如果您在代码中使用proper cuda error checking,您会得到一个提示(由于内核配置参数无效,您的内核启动失败)。

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 2014-03-17
      • 2017-04-28
      • 2012-04-02
      • 2015-09-24
      • 2011-07-23
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多