Facebook
From Sludgy Gorilla, 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 217
  1. template <int BLOCK_SIZE> __global__ void
  2. matrixMulCUDA_4_1w2w(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[2] = { 0,0 };
  30.  
  31.         // Declaration of the shared memory array As used to
  32.         // store the sub-matrix of A
  33.         __shared__ float Aa[BLOCK_SIZE][BLOCK_SIZE];
  34.         __shared__ float Ab[BLOCK_SIZE][BLOCK_SIZE];
  35.  
  36.         // Declaration of the shared memory array Bs used to
  37.         // store the sub-matrix of B
  38.         __shared__ float Bs[BLOCK_SIZE][2 * BLOCK_SIZE];
  39.         __shared__ float Bb[BLOCK_SIZE][2 * BLOCK_SIZE];
  40.  
  41.         // Initial load
  42.         Aa[ty][tx] = A[aBegin + wA * ty + tx];
  43.         Bs[ty][tx] = B[bBegin + wB * ty + tx];
  44.         Bs[ty][tx + BLOCK_SIZE] = B[bBegin + BLOCK_SIZE * wB * ty + tx];
  45.  
  46.         // Synchronize to make sure that initial matrices are loaded
  47.         __syncthreads();
  48.  
  49.         // Loop over all the sub-matrices of A and B
  50.         // required to compute the block sub-matrix
  51.         for (int a = aBegin, b = bBegin;
  52.                 a <= aEnd;
  53.                 a += aStep, b += bStep)
  54.         {
  55.                 // copy contents betweeen shared matrixes
  56.                 Ab[ty][tx] = Aa[ty][tx];
  57.                 Bb[ty][tx] = Bs[ty][tx];
  58.                 Bb[ty][tx + BLOCK_SIZE] = Bs[ty][tx + BLOCK_SIZE];
  59.  
  60.                 // Synchronize to make sure the matrices are loaded
  61.                 __syncthreads();
  62.  
  63.                 // Load the matrices from device memory
  64.                 // to shared memory; each thread loads
  65.                 // two elements of each matrix
  66.                 if (a + aStep < aEnd) {
  67.                         Aa[ty][tx] = A[a + wA * ty + tx];
  68.                         Bs[ty][tx] = B[b + wB * ty + tx];
  69.                         Bs[ty][tx + BLOCK_SIZE] = B[b + BLOCK_SIZE * wB * ty + tx];
  70.                 }
  71.  
  72.                 // Multiply the two matrices together;
  73.                 // each thread computes one element
  74.                 // of the block sub-matrix
  75.                 for (int g = 0; g < 2; g++) {
  76. #pragma unroll
  77.                         for (int k = 0; k < BLOCK_SIZE; ++k)
  78.                         {
  79.                                 CSub[g] += Ab[ty][k + (g * BLOCK_SIZE)] * Bb[k][tx + (g * BLOCK_SIZE)];
  80.                         }
  81.                 }
  82.                
  83.                 // Synchronize to make sure that the preceding
  84.                 // computation is done before loading two new
  85.                 // sub-matrices of A and B in the next iteration
  86.                 __syncthreads();
  87.         }
  88.        
  89.         // Write the block sub-matrix to device memory;
  90.         // each thread writes one element
  91.         int c = wB * BLOCK_SIZE * by + BLOCK_SIZE * bx;
  92.         for (int g = 0; g < 2; g++) {
  93.                 C[c + (BLOCK_SIZE * g) + wB * ty + tx] = CSub[g];
  94.         }
  95. }

Replies to Untitled rss

Title Name Language When
Re: Untitled Mammoth Goat c 4 Years ago.