alexsetyaev

task_1_sum_of_arrays

Sep 28th, 2021
536
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. __global__ void vecAdd(double *A, double *B, double *C, int n){
  6.     int idx = blockIdx.x * blockDim.x + threadIdx.x;
  7.    
  8.     if (idx < n){
  9.         C[idx] = A[idx] + B[idx];
  10.     }
  11. }
  12.  
  13. int main(int argc, char **argv){
  14.     if (argc != 2){
  15.         std::cerr << "Wrong arguments" << std::endl;
  16.         return 1;
  17.     }
  18.     int n = atoi(argv[1]);
  19.     double *h_a, *h_b, *h_c;
  20.     size_t bytes = n *sizeof(double);
  21.    
  22.     h_a = (double*)malloc(bytes);
  23.     h_b = (double*)malloc(bytes);
  24.     h_c = (double*)malloc(bytes);
  25.    
  26.     for (int i = 0; i < n; ++i){
  27.         h_a[i] = sin(i)*sin(i);
  28.         h_b[i] = cos(i)*cos(i);
  29.     }
  30.    
  31.     double *d_a, *d_b, *d_c;
  32.    
  33.     cudaMalloc(&d_a, bytes);
  34.     cudaMalloc(&d_b, bytes);
  35.     cudaMalloc(&d_c, bytes);
  36.    
  37.     cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);
  38.     cudaMemcpy(d_b, h_b, bytes, cudaMemcpyHostToDevice);
  39.    
  40.     int block_size, grid_size;
  41.     block_size = 1024;
  42.     grid_size = (n-1)/block_size + 1;
  43.    
  44.  
  45.     cudaEvent_t start_gpu, stop_gpu;
  46.     cudaEventCreate(&start_gpu);
  47.     cudaEventCreate(&stop_gpu);
  48.    
  49.     cudaEventRecord(start_gpu);
  50.        
  51.     vecAdd<<<grid_size, block_size>>>(d_a, d_b, d_c, n);
  52.    
  53.     cudaDeviceSynchronize();
  54.     cudaEventRecord(stop_gpu);
  55.    
  56.     float delta = 0.0;
  57.     cudaEventElapsedTime(&delta, start_gpu, stop_gpu);
  58.    
  59.  
  60.    
  61.     cudaMemcpy(h_c, d_c, bytes, cudaMemcpyDeviceToHost);
  62.     /*
  63.     for (int i = 0; i < n; ++i){
  64.         std::cout << h_c[i] << std::endl;
  65.     }*/
  66.    
  67.     for (int i = 0; i < n; ++i){
  68.             if (h_c[i] != h_a[i] + h_b[i]){
  69.             std::cout << "Not equal" << std::endl;
  70.             break;
  71.         }
  72.         if (i == n-1){
  73.             std::cout << "Equal" << std::endl;
  74.         }
  75.         }
  76.    
  77.     std::cout << "Elapsed time" << delta << std::endl;
  78.    
  79.     cudaFree(d_a);
  80.     cudaFree(d_b);
  81.     cudaFree(d_c);
  82.    
  83.     free(h_a);
  84.     free(h_b);
  85.     free(h_c);
  86.    
  87.    
  88.     return 0;
  89. }
  90.  
RAW Paste Data