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

Replies to Re: Re: Untitled rss

Title Name Language When
Re: Re: Re: Untitled Torrid Eider c 4 Years ago.