CUDA矩阵乘法:输出零

时间:2012-12-23 06:10:18

标签: cuda matrix-multiplication

尝试运行程序以在CUDA中执行Matrix Multiplication。我想我已经正确设置了一切,程序运行并执行。问题是输出。有人看到我的代码有什么问题吗?无论输入是什么,显然输出矩阵的值为0。我认为问题是我必须将使用int Width作为Kernal函数中的参数转换为使用行数/列数。我没想到会/应该是一个问题,但是出了点问题......谢谢你的帮助!

#define TILE_WIDTH 16

// Compute C = A * B
__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
    float Cvalue = 0.0;
    int Row = blockIdx.y * blockDim.y + threadIdx.y;
    int Col = blockIdx.x * blockDim.x + threadIdx.x;

    if ((Row < numCRows) && (Col < numCColumns)) 
    {       
        float Pvalue = 0;
        for (int k = 0; k < numCRows; ++k) Pvalue += A[Row*numCColumns+k] * B[k*numCRows+Col];
        C[Row*numCRows+Col] = Cvalue;
    }

}

int main(int argc, char ** argv) {
wbArg_t args;
float * hostA; // The A matrix
float * hostB; // The B matrix
float * hostC; // The output C matrix
float * deviceA;
float * deviceB;
float * deviceC;
int numARows; // number of rows in the matrix A
int numAColumns; // number of columns in the matrix A
int numBRows; // number of rows in the matrix B
int numBColumns; // number of columns in the matrix B
int numCRows; // number of rows in the matrix C (you have to set this)
int numCColumns; // number of columns in the matrix C (you have to set this)

args = wbArg_read(argc, argv);
wbTime_start(Generic, "Importing data and creating memory on host");
hostA = (float *) wbImport(wbArg_getInputFile(args, 0), &numARows, &numAColumns);
hostB = (float *) wbImport(wbArg_getInputFile(args, 1), &numBRows, &numBColumns);
//@@ Set numCRows and numCColumns
numCRows = numBRows;
numCColumns = numAColumns;  
int sizeA = numARows * numAColumns * sizeof(float);
int sizeB = numBRows * numBColumns * sizeof(float);
int sizeC = numCRows * numCColumns * sizeof(float);
//@@ Allocate the hostC matrix
hostC = (float *) malloc(sizeC);
wbTime_stop(Generic, "Importing data and creating memory on host");

wbLog(TRACE, "The dimensions of A are ", numARows, " x ", numAColumns);
wbLog(TRACE, "The dimensions of B are ", numBRows, " x ", numBColumns);

wbTime_start(GPU, "Allocating GPU memory.");
//@@ Allocate GPU memory here
cudaMalloc((void **) &deviceA, sizeA);      
cudaMalloc((void **) &deviceB, sizeB);
cudaMalloc((void **) &deviceC, sizeC);
wbTime_stop(GPU, "Allocating GPU memory.");

wbTime_start(GPU, "Copying input memory to the GPU.");
//@@ Copy memory to the GPU here
cudaMemcpy(deviceA, hostA, sizeA, cudaMemcpyHostToDevice);
cudaMemcpy(deviceB, hostB, sizeB, cudaMemcpyHostToDevice);
wbTime_stop(GPU, "Copying input memory to the GPU.");

//@@ Initialize the grid and block dimensions here
dim3 dimGrid(numCRows/TILE_WIDTH, numCColumns/TILE_sWIDTH, 1);
dim3 dimBlock(TILE_WIDTH, TILE_WIDTH, 1);

wbTime_start(Compute, "Performing CUDA computation");
//@@ Launch the GPU Kernel here
matrixMultiply<<<dimGrid,dimBlock>>>(deviceA, deviceB, deviceC,
                                        numARows, numAColumns,
                                        numBRows, numBColumns,
                                        numCRows, numCColumns);
cudaThreadSynchronize();
wbTime_stop(Compute, "Performing CUDA computation");

wbTime_start(Copy, "Copying output memory to the CPU");
//@@ Copy the GPU memory back to the CPU here      
cudaMemcpy(hostC, deviceC, sizeC, cudaMemcpyDeviceToHost);
wbTime_stop(Copy, "Copying output memory to the CPU");

wbTime_start(GPU, "Freeing GPU Memory");
//@@ Free the GPU memory here    
cudaFree(deviceA);
cudaFree(deviceB);
cudaFree(deviceC);
wbTime_stop(GPU, "Freeing GPU Memory");

wbSolution(args, hostC, numCRows, numCColumns);

free(hostA);
free(hostB);
free(hostC);

return 0;
}

1 个答案:

答案 0 :(得分:1)

好的,因为它返回零的原因是因为我的结果从未被保存到我的输出矩阵中,因为我的代码在循环中一遍又一遍地保存在未使用但初始化的值中。此外,我搞砸了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 Cvalue = 0;
        for (int k = 0; k < numCRows; ++k) 
        {
          Cvalue += A[Row*numAColumns+k] * B[k*numBColumns+Col];
        }
          C[Row*numCColumns+Col] = Cvalue;
    }

}

另外在主要代码中:

numCRows = numARows;
numCColumns = numBColumns;