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

Replies to Re: Re: Re: Untitled rss

Title Name Language When
Re: Re: Re: Re: Untitled Capacious Earthworm c 4 Years ago.