Cuda读书笔记之shared memory

转自 http://blog.csdn.net/abcjennifer/article/details/42528569


下面通过一个经典例子来看shared memory作用:矩阵乘法

目的:实现C=A*B,方法:c[i,j] = A[i,:] * B[:,j], 

其中矩阵用row-major表示,即c[i,j] = *(c.elements + i*c.width + j)



1. 不用shared memory优化版:

设A为m*t的矩阵;B为t*n的矩阵;

每个线程读取A的一行,B的一列,计算C的对应值;

所以这样需要从global memory中读n次A,m次B。

[cpp]  view plain  copy
  1. // Matrices are stored in row-major order:  
  2. // M(row, col) = *(M.elements + row * M.width + col)  
  3. typedef struct {  
  4.     int width;  
  5.     int height;  
  6.     float* elements;  
  7. } Matrix;  
  8.   
  9. // Thread block size  
  10. #define BLOCK_SIZE 16  
  11.   
  12. // Forward declaration of the matrix multiplication kernel  
  13. __global__ void MatMulKernel(const Matrix, const Matrix, Matrix);  
  14.   
  15. // Matrix multiplication - Host code  
  16. // Matrix dimensions are assumed to be multiples of BLOCK_SIZE  
  17. void MatMul(const Matrix A, const Matrix B, Matrix C)  
  18. {  
  19.     // Load A and B to device memory  
  20.     Matrix d_A;  
  21.     d_A.width = A.width; d_A.height = A.height;  
  22.     size_t size = A.width * A.height * sizeof(float);  
  23.     cudaMalloc(&d_A.elements, size);  
  24.     cudaMemcpy(d_A.elements, A.elements, size,  
  25.     cudaMemcpyHostToDevice);  
  26.     Matrix d_B;  
  27.     d_B.width = B.width; d_B.height = B.height;  
  28.     size = B.width * B.height * sizeof(float);  
  29.     cudaMalloc(&d_B.elements, size);  
  30.     cudaMemcpy(d_B.elements, B.elements, size,  
  31.     cudaMemcpyHostToDevice);  
  32.   
  33.     // Allocate C in device memory  
  34.     Matrix d_C;  
  35.     d_C.width = C.width; d_C.height = C.height;  
  36.     size = C.width * C.height * sizeof(float);  
  37.     cudaMalloc(&d_C.elements, size);  
  38.   
  39.     // Invoke kernel  
  40.     dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);  
  41.     dim3 dimGrid(B.width / dimBlock.x, A.height / dimBlock.y);  
  42.     MatMulKernel<<<dimGrid, dimBlock>>>(d_A, d_B, d_C);  
  43.   
  44.     // Read C from device memory  
  45.     cudaMemcpy(C.elements, Cd.elements, size,  
  46.     cudaMemcpyDeviceToHost);  
  47.     }  
  48.   
  49.     // Free device memory  
  50.     cudaFree(d_A.elements);  
  51.     cudaFree(d_B.elements);  
  52.     cudaFree(d_C.elements);  
  53. }  
  54.   
  55. // Matrix multiplication kernel called by MatMul()  
  56. __global__ void MatMulKernel(Matrix A, Matrix B, Matrix C)  
  57. {  
  58.     // Each thread computes one element of C  
  59.     // by accumulating results into Cvalue  
  60.     float Cvalue = 0;  
  61.     int row = blockIdx.y * blockDim.y + threadIdx.y;  
  62.     int col = blockIdx.x * blockDim.x + threadIdx.x;  
  63.     for (int e = 0; e < A.width; ++e)  
  64.     Cvalue += A.elements[row * A.width + e]* B.elements[e * B.width + col];  
  65.     C.elements[row * C.width + col] = Cvalue;  
  66. }  





2. 利用shared memory

每个thread block负责计算一个子矩阵Csub, 其中每个thread负责计算Csub中的一个元素。如下图所示。为了将fit设备资源,A,B都分割成很多block_size维的方形matrix,Csub将这些方形matrix的乘积求和而得。每次计算一个乘积时,先将两个对应方形矩阵从global memory 载入 shared memory(一个thread负责载入A, B两个sub matrix的元素),然后每个thread计算乘积的一个元素,再由每个thread将这些product加和,存入一个register,最后一次性写入global memory。计算时注意同步,详见代码。


设A为m*t的矩阵;B为t*n的矩阵;

这样呢,A只从global memory读了n/block_size次,B只读了m/block_size次;





Kernel Code:

[cpp]  view plain  copy
  1. __global__ void MatMulKernel(Matrix A, Matrix B, Matrix C)  
  2. {  
  3.     // Block row and column  
  4.     int blockRow = blockIdx.y;  
  5.     int blockCol = blockIdx.x;  
  6.   
  7.     // Each thread block computes one sub-matrix Csub of C  
  8.     Matrix Csub = GetSubMatrix(C, blockRow, blockCol);  
  9.   
  10.     // Each thread computes one element of Csub by accumulating results into Cvalue  
  11.   
  12.     float Cvalue = 0;  
  13.   
  14.     // Thread row and column within Csub  
  15.     int row = threadIdx.y;  
  16.     int col = threadIdx.x;  
  17.   
  18.     // Loop over all the sub-matrices of A and B that are  
  19.     // required to compute Csub  
  20.     // Multiply each pair of sub-matrices together  
  21.     // and accumulate the results  
  22.   
  23.     for (int m = 0; m < (A.width / BLOCK_SIZE); ++m) {  
  24.   
  25.         // Get sub-matrix Asub of A  
  26.         Matrix Asub = GetSubMatrix(A, blockRow, m);  
  27.         // Get sub-matrix Bsub of B  
  28.         Matrix Bsub = GetSubMatrix(B, m, blockCol);  
  29.   
  30.         // Shared memory used to store Asub and Bsub respectively  
  31.         __shared__ float As[BLOCK_SIZE][BLOCK_SIZE];  
  32.         __shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE];  
  33.   
  34.         // Load Asub and Bsub from device memory to shared memory  
  35.         // Each thread loads one element of each sub-matrix  
  36.         As[row][col] = GetElement(Asub, row, col);  
  37.         Bs[row][col] = GetElement(Bsub, row, col);  
  38.   
  39.         // Synchronize to make sure the sub-matrices are loaded  
  40.         // before starting the computation  
  41.         __syncthreads();  
  42.   
  43.         // Multiply Asub and Bsub together  
  44.         for (int e = 0; e < BLOCK_SIZE; ++e)  
  45.             Cvalue += As[row][e] * Bs[e][col];  
  46.   
  47.         // Synchronize to make sure that the preceding  
  48.         // computation is done before loading two new  
  49.         // sub-matrices of A and B in the next iteration  
  50.         __syncthreads();  
  51.     }  
  52.   
  53.     // Write Csub to device memory  
  54.     // Each thread writes one element  
  55.     SetElement(Csub, row, col, Cvalue);  
  56. }  




Host Code:

[cpp]  view plain  copy
  1. // Invoke kernel  
  2. dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);  
  3. dim3 dimGrid(B.width / dimBlock.x, A.height / dimBlock.y);  
  4. MatMulKernel<<<dimGrid, dimBlock>>>(d_A, d_B, d_C);  

猜你喜欢

转载自blog.csdn.net/chinabing/article/details/75676394