Advertisement
phystota

unrolling_1

Nov 5th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.91 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include "gpu-new-forward.h"
  4.  
  5. #define TILE_WIDTH 16
  6. #define BLOCK_SIZE 256
  7.  
  8. __global__ void matrix_unrolling_kernel(const float *input, float *output,
  9.                                         const int Batch, const int Channel,
  10.                                         const int Height, const int Width,
  11.                                         const int K) {
  12.     /*
  13.     Modify this function to implement the input matrix unrolling kernel.
  14.  
  15.     Function paramter definitions:
  16.     input - input
  17.     output - output
  18.     Batch - batch_size (number of images in x)
  19.     Channel - number of input feature maps
  20.     Height - input height dimension
  21.     Width - input width dimension
  22.     K - kernel height and width (K x K)
  23.     */
  24.     unsigned int batchN = blockIdx.y;
  25.     unsigned int t = blockIdx.x * blockDim.x + threadIdx.x;
  26.  
  27.     const int Height_out = Height - K + 1;
  28.     const int Width_out = Width - K + 1;
  29.  
  30.     int W_unroll = Height_out * Width_out;
  31.     int H_unroll = Channel * K * K;
  32.  
  33.     // We have some nice #defs for you below to simplify indexing. Feel free to use them, or create your own.
  34.     // An example use of these macros:
  35.     // float a = in_4d(0,0,0,0)
  36.  
  37.     #define in_4d(i3, i2, i1, i0) input[(i3) * (Channel * Height * Width) + (i2) * (Height * Width) + (i1) * (Width) + i0] // input(batch, channel, height, width)
  38.     #define out_3d(i2,i1,i0) output[(i2) * (H_unroll * W_unroll) + (i1) * (W_unroll) + i0] // output(batch, height, width)
  39.  
  40.  
  41.  
  42.     // Width of the unrolled input feature matrix
  43.  
  44.     if (batchN < Batch && t < Channel * W_unroll) {
  45.         // Channel of the input feature map being collected by the thread
  46.         int c = t / W_unroll;
  47.         // Column index of the unrolled matrix to write a strip of
  48.         // input elements into (also, the linearized index of the output
  49.         // element for which the thread is collecting input elements)
  50.         int w_unroll = t % W_unroll;
  51.         // Horizontal and vertical indices of the output element
  52.         int h_out = w_unroll / Width_out;
  53.         int w_out = w_unroll % Width_out;
  54.         // Starting row index for the unrolled matrix section for channel c
  55.         int w_base = c * K * K;
  56.         for(int p = 0; p < K; p++) {
  57.             for(int q = 0; q < K; q++) {
  58.                 // Row index of the unrolled matrix for the thread to write
  59.                 // the input element into for the current iteration
  60.                 int h_unroll = w_base + p*K + q;
  61.                 out_3d(batchN, h_unroll, w_unroll) = in_4d(batchN, c, h_out + p, w_out + q);
  62.             }
  63.         }
  64.     }
  65.     #undef in_4d
  66. }
  67.  
  68. // Tiled matrix multiplication kernel. Computes C = AB
  69. // You don't need to modify this kernel.
  70. __global__ void matrixMultiplyShared(const float *A, const float *B, float *C,
  71.                                      int numARows, int numAColumns,
  72.                                      int numBRows, int numBColumns,
  73.                                      int numCRows, int numCColumns)
  74. {
  75.     __shared__ float tileA[TILE_WIDTH][TILE_WIDTH];
  76.     __shared__ float tileB[TILE_WIDTH][TILE_WIDTH];
  77.  
  78.     int by = blockIdx.y, bx = blockIdx.x, ty = threadIdx.y, tx = threadIdx.x;
  79.  
  80.     int row = by * TILE_WIDTH + ty, col = bx * TILE_WIDTH + tx;
  81.     float val = 0;
  82.  
  83.     for (int tileId = 0; tileId < (numAColumns - 1) / TILE_WIDTH + 1; tileId++) {
  84.         if (row < numARows && tileId * TILE_WIDTH + tx < numAColumns) {
  85.             tileA[ty][tx] = A[(size_t) row * numAColumns + tileId * TILE_WIDTH + tx];
  86.         } else {
  87.             tileA[ty][tx] = 0;
  88.         }
  89.         if (col < numBColumns && tileId * TILE_WIDTH + ty < numBRows) {
  90.             tileB[ty][tx] = B[((size_t) tileId * TILE_WIDTH + ty) * numBColumns + col];
  91.         } else {
  92.             tileB[ty][tx] = 0;
  93.         }
  94.         __syncthreads();
  95.  
  96.         if (row < numCRows && col < numCColumns) {
  97.             for (int i = 0; i < TILE_WIDTH; i++) {
  98.                 val += tileA[ty][i] * tileB[i][tx];
  99.             }
  100.         }
  101.         __syncthreads();
  102.     }
  103.  
  104.     if (row < numCRows && col < numCColumns) {
  105.         C[row * numCColumns + col] = val;
  106.     }
  107. }
  108.  
  109. // Permutes the matmul result.
  110. // The output feature map after matmul is of shape Map_out x Batch x Height_out x Width_out,
  111. // and we need to permute it into Batch x Map_out x Height_out x Width_out.
  112. // You don't need to modify this kernel.
  113. __global__ void matrix_permute_kernel(const float *input, float *output, int Map_out,
  114.                                       int Batch, int image_size) {
  115.     int b = blockIdx.y;
  116.     int x = blockIdx.x * BLOCK_SIZE + threadIdx.x;
  117.     if (x < image_size) {
  118.         for (int m = 0; m < Map_out; m++) {
  119.             output[b * Map_out * image_size + m * image_size + x] =
  120.                     input[m * Batch * image_size + b * image_size + x];
  121.         }
  122.     }
  123. }
  124.  
  125. __host__ void GPUInterface::conv_forward_gpu_prolog(const float *host_output, const float *host_input, const float *host_mask, float **device_output_ptr, float **device_input_ptr, float **device_mask_ptr, const int Batch, const int Map_out, const int Channel, const int Height, const int Width, const int K)
  126. {
  127.     // TODO: Allocate memory and copy over the relevant data structures to the GPU
  128.  
  129.     // We pass double pointers for you to initialize the relevant device pointers,
  130.     //  which are passed to the other two functions.
  131.  
  132.     // Useful snippet for error checking
  133.     // cudaError_t error = cudaGetLastError();
  134.     // if(error != cudaSuccess)
  135.     // {
  136.     //     std::cout<<"CUDA error: "<<cudaGetErrorString(error)<<std::endl;
  137.     //     exit(-1);
  138.     // }
  139.  
  140.     //  allocating memory
  141.     cudaMalloc((void **)device_input_ptr, Batch * Channel * Height * Width * sizeof(float)); // Allocate memory on GPU for input
  142.     cudaMalloc((void **)device_output_ptr, Batch * Map_out * (Height - K + 1) * (Width - K + 1) * sizeof(float)); // Allocate memory on GPU for output
  143.  
  144.     //  copy memory to GPU
  145.     cudaMemcpy(*device_input_ptr, host_input, Batch * Channel * Height * Width * sizeof(float), cudaMemcpyHostToDevice);
  146.  
  147. }
  148.  
  149.  
  150. __host__ void GPUInterface::conv_forward_gpu(float *device_output, const float *device_input, const float *device_mask, const int Batch, const int Map_out, const int Channel, const int Height, const int Width, const int K)
  151. {
  152.     const int Height_out = Height - K + 1;
  153.     const int Width_out = Width - K + 1;
  154.     const int Height_unrolled = Channel * K * K;
  155.     const int Width_unrolled = Batch * Height_out * Width_out;
  156.  
  157.     float *unrolled_matrix;  // Pointer to device memory for storing the unrolled matrix
  158.     float *matmul_output;    // Pointer to device memory for storing the result of matrix multiplication
  159.     cudaMalloc((void**)&unrolled_matrix, (size_t) Batch * Channel * K * K * Height_out * Width_out * sizeof(float));
  160.     cudaMalloc((void**)&matmul_output, (Batch * Map_out * Height_out * Width_out) * sizeof(float));
  161.  
  162.     // TODO: Set the kernel dimensions and call the matrix unrolling kernel.
  163.  
  164.     // TODO: Set the kernel dimensions and call the matmul kernel
  165.  
  166.     // Permute the result of matrix multiplication
  167.     const int out_image_size = Height_out * Width_out;
  168.     dim3 permute_kernel_grid_dim((out_image_size - 1) / BLOCK_SIZE + 1, Batch, 1);
  169.     matrix_permute_kernel<<<permute_kernel_grid_dim, BLOCK_SIZE>>>(
  170.         matmul_output, device_output, Map_out, Batch, out_image_size
  171.     );
  172.  
  173.     cudaFree(matmul_output);
  174.     cudaFree(unrolled_matrix);
  175. }
  176.  
  177.  
  178. __host__ void GPUInterface::conv_forward_gpu_epilog(float *host_output, float *device_output, float *device_input, float *device_mask, const int Batch, const int Map_out, const int Channel, const int Height, const int Width, const int K)
  179. {
  180.     // TODO: Copy the output back to host
  181.  
  182.     // TODO: Free device memory
  183.  
  184. }
  185.  
  186.  
  187. __host__ void GPUInterface::get_device_properties()
  188. {
  189.     int deviceCount;
  190.     cudaGetDeviceCount(&deviceCount);
  191.  
  192.     for(int dev = 0; dev < deviceCount; dev++)
  193.     {
  194.         cudaDeviceProp deviceProp;
  195.         cudaGetDeviceProperties(&deviceProp, dev);
  196.  
  197.         std::cout<<"Device "<<dev<<" name: "<<deviceProp.name<<std::endl;
  198.         std::cout<<"Computational capabilities: "<<deviceProp.major<<"."<<deviceProp.minor<<std::endl;
  199.         std::cout<<"Max Global memory size: "<<deviceProp.totalGlobalMem<<std::endl;
  200.         std::cout<<"Max Constant memory size: "<<deviceProp.totalConstMem<<std::endl;
  201.         std::cout<<"Max Shared memory size per block: "<<deviceProp.sharedMemPerBlock<<std::endl;
  202.         std::cout<<"Max threads per block: "<<deviceProp.maxThreadsPerBlock<<std::endl;
  203.         std::cout<<"Max block dimensions: "<<deviceProp.maxThreadsDim[0]<<" x, "<<deviceProp.maxThreadsDim[1]<<" y, "<<deviceProp.maxThreadsDim[2]<<" z"<<std::endl;
  204.         std::cout<<"Max grid dimensions: "<<deviceProp.maxGridSize[0]<<" x, "<<deviceProp.maxGridSize[1]<<" y, "<<deviceProp.maxGridSize[2]<<" z"<<std::endl;
  205.         std::cout<<"Warp Size: "<<deviceProp.warpSize<<std::endl;
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement