Facebook
From Emerald Peafowl, 4 Years ago, written in C.
This paste is a reply to Untitled from Crippled Armadillo - view diff
Embed
Download Paste or View Raw
Hits: 201
  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 arrays 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 arrays used to
  37.         // store the sub-matrix of B
  38.         __shared__ float Ba[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.         Ba[ty][tx] = B[bBegin + wB * ty + tx];
  44.         Ba[ty][tx + BLOCK_SIZE] = B[bBegin + wB * ty + tx + BLOCK_SIZE];
  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.                 if (bx % 2 != 0)
  56.                 {
  57.                         break;
  58.                 }
  59.                 // copy contents betweeen shared matrixes
  60.                 Ab[ty][tx] = Aa[ty][tx];
  61.                 Bb[ty][tx] = Ba[ty][tx];
  62.                 Bb[ty][tx + BLOCK_SIZE] = Ba[ty][tx + BLOCK_SIZE];
  63.  
  64.                 // Synchronize to make sure the matrices are loaded
  65.                 __syncthreads();
  66.  
  67.                 // Load the matrices from device memory
  68.                 // to shared memory; each thread loads
  69.                 // two elements of each matrix
  70.                 if (a + aStep < aEnd) {
  71.                         Aa[ty][tx] = A[a + wA * ty + tx];
  72.                         Ba[ty][tx] = B[b + wB * ty + tx];
  73.                         Ba[ty][tx + BLOCK_SIZE] = B[b * wB * ty + tx + BLOCK_SIZE];
  74.                 }
  75.  
  76.                 // Multiply the two matrices together;
  77.                 // each thread computes one element
  78.                 // of the block sub-matrix
  79. #pragma unroll
  80.                 for (int k = 0; k < BLOCK_SIZE; ++k)
  81.                 {
  82.                         CSub[0] += Ab[ty][k] * Bb[k][tx];
  83.                         CSub[1] += Ab[ty][k] * Bb[k][tx + BLOCK_SIZE];
  84.                 }
  85.  
  86.                 // Synchronize to make sure that the preceding
  87.                 // computation is done before loading two new
  88.                 // sub-matrices of A and B in the next iteration
  89.                 __syncthreads();
  90.         }
  91.  
  92.         // Write the block sub-matrix to device memory;
  93.         // each thread writes one element
  94.         int c = wB * BLOCK_SIZE * by + BLOCK_SIZE * bx;
  95.         C[c + wB * ty + tx] = CSub[0];
  96.         C[c + wB * ty + tx + BLOCK_SIZE] = CSub[1];
  97. }

Replies to Re: Untitled rss

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