Advertisement
alexsetyaev

task_11_multi_gpu

Sep 30th, 2021
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <omp.h>
  6.  
  7. __global__ void vecAdd(double *A, double *B, double *C, int n){
  8.     int idx = blockIdx.x * blockDim.x + threadIdx.x;
  9.    
  10.     if (idx < n){
  11.         C[idx] = A[idx] + B[idx];
  12.     }
  13. }
  14.  
  15. int main(int argc, char **argv){
  16.     if (argc != 2){
  17.         std::cerr << "Wrong arguments" << std::endl;
  18.         return 1;
  19.     }
  20.     int n = atoi(argv[1]);
  21.     double *h_a, *h_b, *h_c;
  22.     size_t bytes = n *sizeof(double);
  23.    
  24.     h_a = (double*)malloc(bytes);
  25.     h_b = (double*)malloc(bytes);
  26.     h_c = (double*)malloc(bytes);
  27.    
  28.     cudaHostRegister(h_a, bytes, 0);
  29.     cudaHostRegister(h_b, bytes, 0);
  30.     cudaHostRegister(h_c, bytes, 0);
  31.    
  32.    
  33.     for (int i = 0; i < n; ++i){
  34.         h_a[i] = sin(i)*sin(i);
  35.         h_b[i] = cos(i)*cos(i);
  36.     }
  37.    
  38.     double *d_a0, *d_b0, *d_c0;
  39.     double *d_a1, *d_b1, *d_c1;
  40.    
  41.     size_t bytes_device = ((n-1)/2 + 1)*sizeof(double);
  42.    
  43.     cudaSetDevice(0);
  44.        
  45.     cudaMalloc(&d_a0, bytes_device);
  46.     cudaMalloc(&d_b0, bytes_device);
  47.     cudaMalloc(&d_c0, bytes_device);
  48.    
  49.     cudaSetDevice(1);
  50.    
  51.     cudaMalloc(&d_a1, bytes_device);
  52.         cudaMalloc(&d_b1, bytes_device);
  53.         cudaMalloc(&d_c1, bytes_device);
  54.  
  55.     cudaSetDevice(0);
  56.    
  57.     cudaEvent_t start_gpu, stop_gpu;
  58.         cudaEventCreate(&start_gpu);
  59.         cudaEventCreate(&stop_gpu);
  60.  
  61.         cudaEventRecord(start_gpu);
  62.    
  63.  
  64.     int block_size, grid_size;
  65.         block_size = 1024;
  66.         grid_size = ((n-1)/2)/block_size + 1;
  67.  
  68.     cudaSetDevice(0);
  69.    
  70.         cudaMemcpyAsync(d_a0, &h_a[0], bytes_device, cudaMemcpyHostToDevice);
  71.         cudaMemcpyAsync(d_b0, &h_b[0], bytes_device, cudaMemcpyHostToDevice);
  72.    
  73.  
  74.    
  75.     cudaSetDevice(0);
  76.        
  77.     vecAdd<<<grid_size, block_size>>>(d_a0, d_b0, d_c0, (n-1)/2+1);
  78.    
  79.     cudaMemcpyAsync(&h_c[0], d_c0, bytes_device, cudaMemcpyDeviceToHost);
  80.    
  81.     cudaSetDevice(1);
  82.    
  83.    
  84.     cudaMemcpyAsync(d_a1, &h_a[(n-1)/2 + 1], bytes_device, cudaMemcpyHostToDevice);
  85.         cudaMemcpyAsync(d_b1, &h_b[(n-1)/2 + 1], bytes_device, cudaMemcpyHostToDevice);
  86.    
  87.     vecAdd<<<grid_size, block_size>>>(d_a1, d_b1, d_c1, (n-1)/2+1);
  88.    
  89.     cudaMemcpyAsync(&h_c[(n-1)/2+1], d_c1, bytes_device, cudaMemcpyDeviceToHost);
  90.    
  91.     cudaDeviceSynchronize();
  92.     cudaSetDevice(0);
  93.     cudaDeviceSynchronize();
  94.    
  95.     cudaEventRecord(stop_gpu);
  96.     cudaEventSynchronize(stop_gpu);
  97.    
  98.     float delta = 0.0;
  99.     cudaEventElapsedTime(&delta, start_gpu, stop_gpu);
  100.    
  101.  
  102.    
  103.     /*
  104.     for (int i = 0; i < n; ++i){
  105.         std::cout << h_c[i] << std::endl;
  106.     }*/
  107.    
  108.     for (int i = 0; i < n; ++i){
  109.             if (h_c[i] != h_a[i] + h_b[i]){
  110.             std::cout << "Not equal" << std::endl;
  111.             break;
  112.         }
  113.         if (i == n-1){
  114.             std::cout << "Equal" << std::endl;
  115.         }
  116.         }
  117.    
  118.     printf("Elapsed time %lf\n", delta);
  119.    
  120.    
  121.     cudaFree(d_a0);
  122.         cudaFree(d_b0);
  123.         cudaFree(d_c0);
  124.    
  125.     cudaSetDevice(1);
  126.    
  127.     cudaFree(d_a1);
  128.         cudaFree(d_b1);
  129.         cudaFree(d_c1);
  130.  
  131.     cudaSetDevice(0);
  132.  
  133.     cudaHostUnregister(h_a);
  134.     cudaHostUnregister(h_b);
  135.     cudaHostUnregister(h_c);
  136.  
  137.         free(h_a);
  138.         free(h_b);
  139.         free(h_c);
  140.    
  141.     return 0;
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement