0

我是 CUDA 的新手。我有一个内核来做矩阵乘法。这对我来说似乎没问题,但在某些情况下它失败了。请帮我解决问题所在。

__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 (numAColumns != numBRows) return;

    if ((Row < numARows) && (Col < numBColumns)){
        float Cvalue = 0;

        for (int k = 0 ; k < numAColumns ; ++k )
            Cvalue += A[Row*numAColumns + k] * B[k * numBColumns + Col];

        C[Row*numCColumns + Col] = Cvalue;
        __syncthreads();
    }
}

我正在按如下方式调用内核。

int BLOCKX = (int)(ceil((numCRows / 8.0)));
int BLOCKY = (int)(ceil((numCColumns / 8.0)));
printf("Number of blocks: %d\t%d\n", BLOCKX, BLOCKY);
dim3 DimGrid(BLOCKX, BLOCKY);
dim3 DimBlock(8 , 8, 1);
4

1 回答 1

1

您的代码将在下面死锁:

  if ((Row < numARows) && (Col < numBColumns)){
            float Cvalue = 0;

            for (int k = 0 ; k < numAColumns ; ++k )
                Cvalue += A[Row*numAColumns + k] * B[k * numBColumns + Col];

            C[Row*numCColumns + Col] = Cvalue;
            __syncthreads();
        }

考虑一个块,对于某些线程,条件满足,而对于某些线程不满足。在这种情况下,这将陷入僵局。置于条件__syncthreads()之外if

也替换dim3 DimGrid(BLOCKX, BLOCKY);dim3 DimGrid(BLOCKY, BLOCKX);. 那应该解决它

于 2012-12-21T19:27:27.550 回答