Facebook
From Botched Coyote, 4 Years ago, written in C.
This paste is a reply to Re: Untitled from Emerald Peafowl - view diff
Embed
Download Paste or View Raw
Hits: 176
  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.         // Thread index
  8.         int tx = threadIdx.x;
  9.         int ty = threadIdx.y;
  10.  
  11.         for (int x = 1; x <= 2; x++)
  12.         {
  13.                 // Index of the first sub-matrix of A processed by the block
  14.                 int aBegin = wA * BLOCK_SIZE * by;
  15.                 // Index of the last sub-matrix of A processed by the block
  16.                 int aEnd = aBegin + wA - 1;
  17.                 // Step size used to iterate through the sub-matrices of A
  18.                 int aStep = BLOCK_SIZE;
  19.                 // Index of the first sub-matrix of B processed by the block
  20.                 int bBegin = BLOCK_SIZE * bx;
  21.                 // Step size used to iterate through the sub-matrices of B
  22.                 int bStep = BLOCK_SIZE * wB;
  23.                 // Csub is used to store the element of the block sub-matrix
  24.                 // that is computed by the thread
  25.                 float Csub = 0;
  26.                 // Loop over all the sub-matrices of A and B
  27.                 // required to compute the block sub-matrix
  28.                 for (int a = aBegin, b = bBegin;
  29.                         a <= aEnd;
  30.                         a += aStep, b += bStep)
  31.                 {
  32.                         // Declaration of the shared memory array As used to
  33.                         // store the sub-matrix of A
  34.                         __shared__ float As[BLOCK_SIZE / 2][BLOCK_SIZE];
  35.                         // Declaration of the shared memory array Bs used to
  36.                         // store the sub-matrix of B
  37.                         __shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE];
  38.                         // Load the matrices from device memory
  39.                         // to shared memory; each thread loads
  40.                         // one element of each matrix
  41.                         if (x == 1)
  42.                         {
  43.                                 if (ty < BLOCK_SIZE / 2)
  44.                                 {
  45.                                         As[ty][tx] = A[a + wA * ty + tx];
  46.                                 }
  47.                                 Bs[ty][tx] = B[b + wB * ty + tx];
  48.                         }
  49.                         else if (x == 2)
  50.                         {
  51.                                 if (ty >= BLOCK_SIZE / 2)
  52.                                 {
  53.                                         As[ty][tx] = A[a + wA * ty + tx];
  54.                                 }
  55.                         }
  56.                         // Synchronize to make sure the matrices are loaded
  57.                         __syncthreads();
  58.                         // Multiply the two matrices together;
  59.                         // 2 thread computes one element
  60.                         // of the block sub-matrix
  61. #pragma unroll
  62.                         for (int k = 0; k < BLOCK_SIZE / 2; k++)
  63.                         {
  64.                                 if (ty < BLOCK_SIZE / 2)
  65.                                 {
  66.                                         Csub += As[ty][k] * Bs[k][tx];
  67.                                 }
  68.                                 else
  69.                                 {
  70.                                         Csub += As[ty][k + BLOCK_SIZE / 2] * Bs[k + BLOCK_SIZE / 2][tx];
  71.                                 }
  72.                         }
  73.                         // Synchronize to make sure that the preceding
  74.                         // computation is done before loading two new
  75.                         // sub-matrices of A and B in the next iteration
  76.                         __syncthreads();
  77.                 }
  78.                 // Write the block sub-matrix to device memory;
  79.                 // each thread writes one element
  80.                 int c = wB * BLOCK_SIZE * by + BLOCK_SIZE * bx;
  81.                 if (ty < BLOCK_SIZE / 2 && x == 1)
  82.                 {
  83.                         C[c + wB * ty + tx] += Csub;
  84.                 }
  85.                 else if (ty >= BLOCK_SIZE / 2 && x == 2)
  86.                 {
  87.                         C[c + wB * ty + tx] += Csub;
  88.                 }
  89.         }
  90. }

Replies to Re: Re: Untitled rss

Title Name Language When
Re: Re: Re: Untitled Funky Bird c 4 Years ago.
captcha