0

在下面的作业代码中,我需要找出平铺矩阵乘法工作还需要哪些边界条件。请帮助我,我已经尝试了一个星期来找出问题所在?

#include    <wb.h>

#define TILE_WIDTH 16


__global__ void matrixMultiplyShared(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
//@@ You have to use shared memory for this MP
  __shared__ float s_A[TILE_WIDTH][TILE_WIDTH];
  __shared__ float s_B[TILE_WIDTH][TILE_WIDTH];

  int tx= threadIdx.x; int ty = threadIdx.y;
  int bx= blockIdx.x ; int by = blockIdx.y ;

  int Row = by*TILE_WIDTH + ty;
  int Col = bx*TILE_WIDTH + tx;



   if((Row < numARows  ) && (Col < numBColumns )) {


 float Pvalue =0.0;
  for (int m = 0; m < (numAColumns-1)/TILE_WIDTH+1; ++m) {

    if((Row < numARows) && ( (m*TILE_WIDTH+tx) < numAColumns)) {

      s_A[ty][tx] = A[Row*numAColumns +( m*TILE_WIDTH+tx)];
    }
    else
    {
      s_A[ty][tx] = 0.0;
    }
    if(((m*TILE_WIDTH+ty) < numBRows) && (Col < numBColumns)) {

      s_B[ty][tx] = B[(m*TILE_WIDTH+ty)*numBColumns+Col];
    }
    else
    {
      s_B[ty][tx] = 0.0;
    }
    __syncthreads();

   if((Row < numARows  ) && (Col < numBColumns )) {
    for (int k = 0; k < TILE_WIDTH; ++k)
      {

         Pvalue += s_A[ty][k] * s_B[k][tx];

      }
    __syncthreads();

  }
  }
    if((Row < numARows  ) && (Col < numBColumns )) { 
       C[Row*numCColumns+Col] = Pvalue;
    }
}
else
return;
}
4

1 回答 1

2

问题在于进入循环加载 TILE 的条件,即 if((Row < numARows ) && (Col < numBColumns )),并且下次在进行实际计算时再次检查相同的条件每个加载的 TILE 的结果元素,只有最后一个条件,即写入全局时就足够​​了。您可以在此处找到详细的实现以供参考

于 2014-03-13T10:24:47.580 回答