Facebook
From Funky Bird, 5 Years ago, written in C.
This paste is a reply to Re: Re: Untitled from Botched Coyote - view diff
Embed
Download Paste or View Raw
Hits: 153
  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.         // Thread index
  8.         int tx = threadIdx.x % BLOCK_SIZE;
  9.         int ty = threadIdx.y;
  10.  
  11.         for (int x = 1; x <= 2; x++)
  12.         {
  13.                 // Index of the first sub-matrix of A processed by the block
  14.                 int aBegin = wA * BLOCK_SIZE * by;
  15.                 // Index of the last sub-matrix of A processed by the block
  16.                 int aEnd = aBegin + wA - 1;
  17.                 // Step size used to iterate through the sub-matrices of A
  18.                 int aStep = BLOCK_SIZE;
  19.                 // Index of the first sub-matrix of B processed by the block
  20.                 int bBegin = BLOCK_SIZE * bx;
  21.                 // Step size used to iterate through the sub-matrices of B
  22.                 int bStep = BLOCK_SIZE * wB;
  23.                 // Csub is used to store the element of the block sub-matrix
  24.                 // that is computed by the thread
  25.                 float Csub = 0;
  26.                 // Declaration of the shared memory array Bs used to
  27.                 // store the sub-matrix of B
  28.                 __shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE];
  29.                 __shared__ float BBs[BLOCK_SIZE][BLOCK_SIZE];
  30.                 __shared__ float As[BLOCK_SIZE / 2][BLOCK_SIZE];
  31.                 __shared__ float AAs[BLOCK_SIZE / 2][BLOCK_SIZE];
  32.                 // Initial load of data
  33.                 if (x == 1)
  34.                 {
  35.                         if (ty < BLOCK_SIZE / 2)
  36.                         {
  37.                                 As[ty][tx] = A[aBegin + wA * ty + tx];
  38.                         }
  39.                         Bs[ty][tx] = B[bBegin + wB * ty + tx];
  40.                 }
  41.                 else if (x == 2)
  42.                 {
  43.                         if (ty >= BLOCK_SIZE / 2)
  44.                         {
  45.                                 As[ty][tx] = A[aBegin + wA * ty + tx];
  46.                         }
  47.                 }
  48.                 // Loop over all the sub-matrices of A and B
  49.                 // required to compute the block sub-matrix
  50.                 for (int a = aBegin, b = bBegin;
  51.                         a <= aEnd;
  52.                         a += aStep, b += bStep)
  53.                 {
  54.                         // Copy values between matrixes
  55.                         if (x == 1 && ty < BLOCK_SIZE / 2)
  56.                         {
  57.                                 AAs[ty][tx] = As[ty][tx];
  58.                         }
  59.                         else if (x == 2 && ty >= BLOCK_SIZE / 2)
  60.                         {
  61.                                 AAs[ty][tx] = As[ty][tx];
  62.                         }
  63.                         BBs[ty][tx] = Bs[ty][tx];
  64.                         // Synchronize to make sure the matrices are loaded
  65.                         __syncthreads();
  66.                         // Load the matrices from device memory
  67.                         // to shared memory; each thread loads
  68.                         // one element of each matrix
  69.                         if (x == 1)
  70.                         {
  71.                                 if (ty < BLOCK_SIZE / 2)
  72.                                 {
  73.                                         As[ty][tx] = A[a + wA * ty + tx];
  74.                                 }
  75.                                 Bs[ty][tx] = B[b + wB * ty + tx];
  76.                         }
  77.                         else if (x == 2)
  78.                         {
  79.                                 if (ty >= BLOCK_SIZE / 2)
  80.                                 {
  81.                                         As[ty][tx] = A[a + wA * ty + tx];
  82.                                 }
  83.                         }
  84. #pragma unroll
  85.                         for (int k = 0; k < BLOCK_SIZE / 2; k++)
  86.                         {
  87.                                 if (ty < BLOCK_SIZE / 2)
  88.                                 {
  89.                                         Csub += As[ty][k] * Bs[k][tx];
  90.                                 }
  91.                                 else
  92.                                 {
  93.                                         Csub += As[ty][k + BLOCK_SIZE / 2] * Bs[k + BLOCK_SIZE / 2][tx];
  94.                                 }
  95.                         }
  96.                         // Synchronize to make sure that the preceding
  97.                         // computation is done before loading two new
  98.                         // sub-matrices of A and B in the next iteration
  99.                         __syncthreads();
  100.                 }
  101.                 // Write the block sub-matrix to device memory;
  102.                 // each thread writes one element
  103.                 int c = wB * BLOCK_SIZE * by + BLOCK_SIZE * bx;
  104.                 if (ty < BLOCK_SIZE / 2 && x == 1)
  105.                 {
  106.                         C[c + wB * ty + tx] += Csub;
  107.                 }
  108.                 else if (ty >= BLOCK_SIZE / 2 && x == 2)
  109.                 {
  110.                         C[c + wB * ty + tx] += Csub;
  111.                 }
  112.         }
  113. }
captcha