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

Replies to Re: Untitled rss

Title Name Language When
Re: Re: Untitled Idiotic Parrot c 4 Years ago.