Facebook
From Capacious Earthworm, 5 Years ago, written in C.
This paste is a reply to Re: Re: Re: Untitled from Torrid Eider - view diff
Embed
Download Paste or View Raw
Hits: 332
  1. template <int BLOCK_SIZE> __global__ void
  2. matrixMulCUDA_4_2w1w(float* C, float* A, float* B, int wA, int wB)
  3. {
  4.         // Block index
  5.         int bx = blockIdx.x;
  6.         int by = blockIdx.y;
  7.  
  8.         // Thread index
  9.         int tx = threadIdx.x % BLOCK_SIZE;
  10.         int ty = threadIdx.y;
  11.  
  12.         // Index of the first sub-matrix of A processed by the block
  13.         int aBegin = wA * BLOCK_SIZE * by;
  14.  
  15.         // Index of the last sub-matrix of A processed by the block
  16.         int aEnd = aBegin + wA - 1;
  17.  
  18.         // Step size used to iterate through the sub-matrices of A
  19.         int aStep = BLOCK_SIZE;
  20.  
  21.         // Index of the first sub-matrix of B processed by the block
  22.         int bBegin = BLOCK_SIZE * bx;
  23.  
  24.         // Step size used to iterate through the sub-matrices of B
  25.         int bStep = BLOCK_SIZE * wB;
  26.  
  27.         // Csub is used to store the element of the block sub-matrix
  28.         // that is computed by the thread
  29.         float Csub = 0;
  30.  
  31.         // Declaration of the shared memory array Bs used to
  32.         // store the sub-matrix of B
  33.         __shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE];
  34.         __shared__ float BBs[BLOCK_SIZE][BLOCK_SIZE];
  35.         __shared__ float As[BLOCK_SIZE][BLOCK_SIZE];
  36.         __shared__ float AAs[BLOCK_SIZE][BLOCK_SIZE];
  37.  
  38.         // Initial load of data
  39.         As[ty][tx] = A[aBegin + wA * ty + tx];
  40.         Bs[ty][tx] = B[bBegin + wB * ty + tx];
  41.  
  42.         // Loop over all the sub-matrices of A and B
  43.         // required to compute the block sub-matrix
  44.         for (int a = aBegin, b = bBegin;
  45.                 a <= aEnd;
  46.                 a += aStep, b += bStep)
  47.         {
  48.  
  49.                 // Copy values between matrixes
  50.                 AAs[ty][tx] = As[ty][tx];
  51.                 BBs[ty][tx] = Bs[ty][tx];
  52.  
  53.                 // Synchronize to make sure the matrices are loaded
  54.                 __syncthreads();
  55.  
  56.                 // Load the matrices from device memory
  57.                 // to shared memory; each thread loads
  58.                 // one element of each matrix
  59.                 As[ty][tx] = A[a + wA * ty + tx];
  60.                 Bs[ty][tx] = B[b + wB * ty + tx];
  61. #pragma unroll
  62.                 for (int k = 0; k < BLOCK_SIZE / 2; k++)
  63.                 {
  64.                         Csub += AAs[ty][k + (BLOCK_SIZE / 2) * tx] * BBs[k + (BLOCK_SIZE / 2) * tx][tx];
  65.                 }
  66.  
  67.                 // Synchronize to make sure that the preceding
  68.                 // computation is done before loading two new
  69.                 // sub-matrices of A and B in the next iteration
  70.                 __syncthreads();
  71.         }
  72.  
  73.         // Write the block sub-matrix to device memory;
  74.         // each thread writes one element
  75.         int c = wB * BLOCK_SIZE * by + BLOCK_SIZE * bx;
  76.         C[c + wB * ty + tx] += Csub;
  77. }
  78.