Simple Matrix Multiplication

image.png
N 是 coalsec,M不是

Tiled matrix Multiplication

  1. __global__ void kernel_matrix_multiply(float *M, float *N, float *P, int M_rows, int M_cols, int N_rows, int N_cols){
  2. __shared__ float Mds[BLOCK_SIZE][BLOCK_SIZE];
  3. __shared__ float Nds[BLOCK_SIZE][BLOCK_SIZE];
  4. int bx= blockIdx.x,by=blockIdx.y;
  5. int tx =threadIdx.x,ty=threadIdx.y;
  6. int row =by*BLOCK_SIZE+ty;
  7. int col=bx*BLOCK_SIZE+tx;
  8. float Pvalue=0;
  9. for(int ph=0;ph<(M_cols-1)/BLOCK_SIZE+1;ph++){
  10. if(row<M_rows &&ph*BLOCK_SIZE+tx<M_cols)
  11. Mds[ty][tx]=M[row*M_cols+ph*BLOCK_SIZE+tx];
  12. if((ph*BLOCK_SIZE+ty)<N_rows &&col<N_cols)
  13. Nds[ty][tx]=N[(ph*BLOCK_SIZE+ty)*N_cols+col];
  14. __syncthreads();
  15. for(int k=0;k<BLOCK_SIZE;k++){
  16. Pvalue+=Mds[ty][k]*Nds[k][tx];
  17. }
  18. __syncthreads();
  19. }
  20. if(row<M_rows&&col<N_cols)
  21. {
  22. P[row*N_cols+col]=Pvalue;
  23. }
  24. }

这里Mds,Nds都是coalsec 访问的

linr 15 为什么需要syncthreads():
保证一个block中的所有thread把M,N中的分块全部放进了share memory 中,每个thread才能开始计算
line 20 为什么需要
syncthreads():
保证所有thread 使用完成了share memory 所有值,才能开始下一次搬运。如果没有,会出现一个thread计算完成后,迭代下一次for循环搬运一个分块,而block中其他thread还在用前一个分块的值,导致结果出错。