Cuda矩阵乘法

时间:2012-04-26 05:30:32

标签: cuda matrix-multiplication

我正在尝试用cuda写一个矩阵乘法代码,这与Nvidia的cuda编程指南很相似,但是它没有用。它应该做C = alpha * A * B + beta * C,但对于每一个A,B C保持不变。

__global__ void MatMulKernel(int m,int n,int k,double *A,double *B,double *C,double alpha,double beta)
{
    double Ctemp = 0.0;
    int row = blockIdx.y * blockDim.y + threadIdx.y;
    int col = blockIdx.x * blockDim.x + threadIdx.x;
    int ind;
    for (ind = 0; ind < k; ++ind)
    {
       Ctemp += A[row+ind*m]*B[ind+col*k];
    }

   C[row+m*col] = alpha*Ctemp+beta*C[row+m*col];
//C[row+m*col] = Ctemp;
   __syncthreads();
}

extern "C" void
local_mm_cuda (const int m, const int n, const int k, const double alpha,
  const double *A, const int lda, const double *B, const int ldb,
  const double beta, double *C, const int ldc)
{

 int row, col;

  /* Verify the sizes of lda, ldb, and ldc */
  assert (lda >= m);
  assert (ldb >= k);
  assert (ldc >= m);

  // allocating memory for device array
  double *dA,*dB,*dC;
  size_t sizeA = sizeof(double)*m*k;
  size_t sizeB = sizeof(double)*n*k;
  size_t sizeC = sizeof(double)*m*n;

  cudaMalloc((void**)&dA,sizeA);
  cudaMalloc((void**)&dB,sizeB);
  cudaMalloc((void**)&dC,sizeC);

  cudaMemcpy(dA, A, sizeA, cudaMemcpyHostToDevice);
  cudaMemcpy(dB, B, sizeB, cudaMemcpyHostToDevice);
  cudaMemcpy(dC, C, sizeC, cudaMemcpyHostToDevice);

  // calling matrix multiplication kernal
  dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);
  dim3 dimGrid( n/dimBlock.x, m/dimBlock.y);
  MatMulKernel<<<dimGrid, dimBlock>>>(m,n,k,dA,dB,dC,alpha,beta);
  cudaThreadSynchronize();

  // saving C calculated back in C
  cudaMemcpy(dC,C, sizeC,cudaMemcpyDeviceToHost);
  cudaFree(dA);
  cudaFree(dB);
  cudaFree(dC);
}

1 个答案:

答案 0 :(得分:4)

尝试修改

"dim3 dimGrid( n/dimBlock.x, m/dimBlock.y);"

"dim3 dimGrid( (n+dimBlock.x-1)/dimBlock.x, (m+dimBlock.y-1)/dimBlock.y); "