Advertisement
phystota

unrolling_final

Nov 5th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.70 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 512
  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. #define in_4d(i3, i2, i1, i0) input[(i3) * (Channel * Height * Width) + (i2) * (Height * Width) + (i1) * (Width) + i0]
  25. #define out_3d(i1, i0) output[(i1) * (Batch * W_unroll) + i0]  // Adjusted for concatenating batches
  26.  
  27. unsigned int t = blockIdx.x * blockDim.x + threadIdx.x;
  28.  
  29. const int Height_out = Height - K + 1;
  30. const int Width_out = Width - K + 1;
  31. size_t W_unroll = Height_out * Width_out;    // Unrolled width for a single image
  32. size_t H_unroll = Channel * K * K;           // Unrolled height
  33. size_t W_total_unroll = Batch * W_unroll;    // Total width for all images
  34.  
  35. if (t < Channel * W_total_unroll) {
  36.     int c = t / W_total_unroll;          // Channel index
  37.     int w_total_unroll = t % W_total_unroll;  // Combined column index (across batch)
  38.    
  39.     int batch_idx = w_total_unroll / W_unroll; // Find which image (batch) this thread is working on
  40.     int w_unroll = w_total_unroll % W_unroll;  // Column within this image's unrolled matrix
  41.  
  42.     int h_out = w_unroll / Width_out;          // Height index of the output
  43.     int w_out = w_unroll % Width_out;          // Width index of the output
  44.  
  45.     int w_base = c * K * K;                    // Base index for this channel and kernel
  46.     for (int p = 0; p < K; p++) {
  47.         for (int q = 0; q < K; q++) {
  48.             int h_unroll = w_base + p * K + q;  // Row in unrolled matrix
  49.             out_3d(h_unroll, w_total_unroll) = in_4d(batch_idx, c, h_out + p, w_out + q);
  50.         }
  51.     }
  52. }
  53. #undef in_4d
  54. #undef out_3d
  55. }
  56.  
  57. // Tiled matrix multiplication kernel. Computes C = AB
  58. // You don't need to modify this kernel.
  59. __global__ void matrixMultiplyShared(const float *A, const float *B, float *C,
  60.                                      int numARows, int numAColumns,
  61.                                      int numBRows, int numBColumns,
  62.                                      int numCRows, int numCColumns)
  63. {
  64.     __shared__ float tileA[TILE_WIDTH][TILE_WIDTH];
  65.     __shared__ float tileB[TILE_WIDTH][TILE_WIDTH];
  66.  
  67.     int by = blockIdx.y, bx = blockIdx.x, ty = threadIdx.y, tx = threadIdx.x;
  68.  
  69.     int row = by * TILE_WIDTH + ty, col = bx * TILE_WIDTH + tx;
  70.     float val = 0;
  71.  
  72.     for (int tileId = 0; tileId < (numAColumns - 1) / TILE_WIDTH + 1; tileId++) {
  73.         if (row < numARows && tileId * TILE_WIDTH + tx < numAColumns) {
  74.             tileA[ty][tx] = A[(size_t) row * numAColumns + tileId * TILE_WIDTH + tx];
  75.         } else {
  76.             tileA[ty][tx] = 0;
  77.         }
  78.         if (col < numBColumns && tileId * TILE_WIDTH + ty < numBRows) {
  79.             tileB[ty][tx] = B[((size_t) tileId * TILE_WIDTH + ty) * numBColumns + col];
  80.         } else {
  81.             tileB[ty][tx] = 0;
  82.         }
  83.         __syncthreads();
  84.  
  85.         if (row < numCRows && col < numCColumns) {
  86.             for (int i = 0; i < TILE_WIDTH; i++) {
  87.                 val += tileA[ty][i] * tileB[i][tx];
  88.             }
  89.         }
  90.         __syncthreads();
  91.     }
  92.  
  93.     if (row < numCRows && col < numCColumns) {
  94.         C[row * numCColumns + col] = val;
  95.     }
  96. }
  97.  
  98. // Permutes the matmul result.
  99. // The output feature map after matmul is of shape Map_out x Batch x Height_out x Width_out,
  100. // and we need to permute it into Batch x Map_out x Height_out x Width_out.
  101. // You don't need to modify this kernel.
  102. __global__ void matrix_permute_kernel(const float *input, float *output, int Map_out,
  103.                                       int Batch, int image_size) {
  104.     int b = blockIdx.y;
  105.     int x = blockIdx.x * BLOCK_SIZE + threadIdx.x;
  106.     if (x < image_size) {
  107.         for (int m = 0; m < Map_out; m++) {
  108.             output[b * Map_out * image_size + m * image_size + x] =
  109.                     input[m * Batch * image_size + b * image_size + x];
  110.         }
  111.     }
  112. }
  113.  
  114. __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)
  115. {
  116.     // TODO: Allocate memory and copy over the relevant data structures to the GPU
  117.  
  118.     // We pass double pointers for you to initialize the relevant device pointers,
  119.     //  which are passed to the other two functions.
  120.  
  121.     // Useful snippet for error checking
  122.     // cudaError_t error = cudaGetLastError();
  123.     // if(error != cudaSuccess)
  124.     // {
  125.     //     std::cout<<"CUDA error: "<<cudaGetErrorString(error)<<std::endl;
  126.     //     exit(-1);
  127.     // }
  128.  
  129.     //  allocating memory
  130.  
  131.     // Calculate sizes
  132.     const int Height_out = Height - K + 1;
  133.     const int Width_out = Width - K + 1;
  134.    
  135.     const int input_size = Batch * Channel * Height * Width * sizeof(float);
  136.     const int mask_size = Map_out * Channel * K * K * sizeof(float);
  137.     const int output_size = Batch * Map_out * Height_out * Width_out * sizeof(float);
  138.  
  139.     cudaMalloc((void**)device_input_ptr, input_size);
  140.     cudaMalloc((void**)device_mask_ptr, mask_size);
  141.     cudaMalloc((void**)device_output_ptr, output_size);
  142.  
  143.     cudaMemcpy(*device_input_ptr, host_input, input_size, cudaMemcpyHostToDevice);
  144.     cudaMemcpy(*device_mask_ptr, host_mask, mask_size, cudaMemcpyHostToDevice);
  145.  
  146. }
  147.  
  148.  
  149. __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)
  150. {
  151.     const int Height_out = Height - K + 1;
  152.     const int Width_out = Width - K + 1;
  153.     const int Height_unrolled = Channel * K * K;
  154.     const int Width_unrolled = Batch * Height_out * Width_out;
  155.  
  156.     //allocating temping storage of unrolling matrix
  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.     dim3 gridDim((Channel * Width_unrolled + BLOCK_SIZE - 1) / BLOCK_SIZE, Batch, 1);
  164.     matrix_unrolling_kernel<<<gridDim, BLOCK_SIZE>>>(device_input, unrolled_matrix, Batch, Channel, Height, Width, K);
  165.  
  166.     // TODO: Set the kernel dimensions and call the matmul kernel
  167.     dim3 dimGrid((Width_unrolled - 1)/TILE_WIDTH + 1, (Map_out - 1)/TILE_WIDTH + 1, 1);
  168.     dim3 dimBlock(TILE_WIDTH, TILE_WIDTH, 1);
  169.     matrixMultiplyShared<<<dimGrid, dimBlock>>>(device_mask, unrolled_matrix, matmul_output, Map_out, Height_unrolled, Height_unrolled, Width_unrolled,
  170.     Map_out, Width_unrolled);
  171.  
  172.     // Permute the result of matrix multiplication
  173.     const int out_image_size = Height_out * Width_out;
  174.     dim3 permute_kernel_grid_dim((out_image_size - 1) / BLOCK_SIZE + 1, Batch, 1);
  175.     matrix_permute_kernel<<<permute_kernel_grid_dim, BLOCK_SIZE>>>(matmul_output, device_output, Map_out, Batch, out_image_size);
  176.  
  177.     cudaFree(matmul_output);
  178.     cudaFree(unrolled_matrix);
  179. }
  180.  
  181.  
  182. __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)
  183. {
  184.  
  185.     // Calculate output size
  186.     const int Height_out = Height - K + 1;
  187.     const int Width_out = Width - K + 1;
  188.     const int output_size = Batch * Map_out * Height_out * Width_out * sizeof(float);
  189.  
  190.     // TODO: Copy the output back to host
  191.     cudaMemcpy(host_output, device_output, output_size, cudaMemcpyDeviceToHost);
  192.  
  193.     // TODO: Free device memory
  194.     cudaFree(device_output);
  195.     cudaFree(device_input);
  196.     cudaFree(device_mask);
  197. }
  198.  
  199.  
  200. __host__ void GPUInterface::get_device_properties()
  201. {
  202.     int deviceCount;
  203.     cudaGetDeviceCount(&deviceCount);
  204.  
  205.     for(int dev = 0; dev < deviceCount; dev++)
  206.     {
  207.         cudaDeviceProp deviceProp;
  208.         cudaGetDeviceProperties(&deviceProp, dev);
  209.  
  210.         std::cout<<"Device "<<dev<<" name: "<<deviceProp.name<<std::endl;
  211.         std::cout<<"Computational capabilities: "<<deviceProp.major<<"."<<deviceProp.minor<<std::endl;
  212.         std::cout<<"Max Global memory size: "<<deviceProp.totalGlobalMem<<std::endl;
  213.         std::cout<<"Max Constant memory size: "<<deviceProp.totalConstMem<<std::endl;
  214.         std::cout<<"Max Shared memory size per block: "<<deviceProp.sharedMemPerBlock<<std::endl;
  215.         std::cout<<"Max threads per block: "<<deviceProp.maxThreadsPerBlock<<std::endl;
  216.         std::cout<<"Max block dimensions: "<<deviceProp.maxThreadsDim[0]<<" x, "<<deviceProp.maxThreadsDim[1]<<" y, "<<deviceProp.maxThreadsDim[2]<<" z"<<std::endl;
  217.         std::cout<<"Max grid dimensions: "<<deviceProp.maxGridSize[0]<<" x, "<<deviceProp.maxGridSize[1]<<" y, "<<deviceProp.maxGridSize[2]<<" z"<<std::endl;
  218.         std::cout<<"Warp Size: "<<deviceProp.warpSize<<std::endl;
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement