Facebook
From Obese Hedgehog, 4 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 274
  1. template <int BLOCK_SIZE> __global__ void
  2. matrixMulCUDA_3(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;
  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.         // Loop over all the sub-matrices of A and B
  32.         // required to compute the block sub-matrix
  33.         for (int a = aBegin, b = bBegin;
  34.                 a <= aEnd;
  35.                 a += aStep, b += bStep)
  36.         {
  37.  
  38.                 // Declaration of the shared memory array As used to
  39.                 // store the sub-matrix of A
  40.                 __shared__ float As[BLOCK_SIZE][BLOCK_SIZE];
  41.  
  42.                 // Declaration of the shared memory array Bs used to
  43.                 // store the sub-matrix of B
  44.                 __shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE];
  45.  
  46.                 // Load the matrices from device memory
  47.                 // to shared memory; each thread loads
  48.                 // one element of each matrix
  49.                 As[ty][tx] = A[a + wA * ty + tx];
  50.                 Bs[ty][tx] = B[b + wB * ty + tx];
  51.  
  52.                 // Synchronize to make sure the matrices are loaded
  53.                 __syncthreads();
  54.  
  55.                 // Multiply the two matrices together;
  56.                 // each thread computes one element
  57.                 // of the block sub-matrix
  58. #pragma unroll
  59.  
  60.                 for (int k = 0; k < BLOCK_SIZE; ++k)
  61.                 {
  62.                         Csub += As[ty][k] * Bs[k][tx];
  63.                 }
  64.  
  65.                 // Synchronize to make sure that the preceding
  66.                 // computation is done before loading two new
  67.                 // sub-matrices of A and B in the next iteration
  68.                 __syncthreads();
  69.         }
  70.  
  71.         // Write the block sub-matrix to device memory;
  72.         // each thread writes one element
  73.         int c = wB * BLOCK_SIZE * by + BLOCK_SIZE * bx;
  74.         C[c + wB * ty + tx] = Csub;
  75. }
  76.  

Replies to Untitled rss

Title Name Language When
Re: Untitled Botched Pheasant c 4 Years ago.