Advertisement
Guest User

Untitled

a guest
Apr 25th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include "cuda_runtime.h"
  2. #include "device_launch_parameters.h"
  3. #include "stdio.h"
  4. #include "windows.h"
  5. #include <locale.h>
  6. #define SIZE 1024
  7. __global__ void VectorAdd(int *a, int *b, int *c, int n) {
  8.     int i = threadIdx.x;
  9.     //for(i = 0; i < n; ++i)
  10.     if (i < n)
  11.         c[i] = a[i] + b[i];
  12. }
  13. int main() {
  14.     setlocale(LC_ALL, "Russian");
  15.     int Time1, Time2, Delay1;
  16.     int *a, *b, *c;
  17.     int *d_a, *d_b, *d_c;
  18.     a = (int *)malloc(SIZE * sizeof(int));
  19.     b = (int *)malloc(SIZE * sizeof(int));
  20.     c = (int *)malloc(SIZE * sizeof(int));
  21.     cudaMalloc(&d_a, SIZE * sizeof(int));
  22.     cudaMalloc(&d_b, SIZE * sizeof(int));
  23.     cudaMalloc(&d_c, SIZE * sizeof(int));
  24.     Time1 = GetTickCount();
  25.     for (int i = 0; i < SIZE; ++i) {
  26.         a[i] = i;
  27.         b[i] = i;
  28.         c[i] = 0;
  29.     }
  30.     cudaMemcpy(d_a, a, SIZE * sizeof(int), cudaMemcpyHostToDevice);
  31.     cudaMemcpy(d_b, b, SIZE * sizeof(int), cudaMemcpyHostToDevice);
  32.     cudaMemcpy(d_c, c, SIZE * sizeof(int), cudaMemcpyHostToDevice);
  33.     VectorAdd <<<1, SIZE >>> (d_a, d_b, d_c, SIZE);
  34.     cudaMemcpy(c, d_c, SIZE * sizeof(int), cudaMemcpyDeviceToHost);
  35.     for (int i = 0; i < 100; ++i)
  36.         printf("c[%d] = %d\n", i, c[i]);
  37.     Time2 = GetTickCount();
  38.     Delay1 = Time2 - Time1;
  39.     free(a);
  40.     free(b);
  41.     free(c);
  42.     cudaFree(d_a);
  43.     cudaFree(d_b);
  44.     cudaFree(d_c);
  45.     printf("\nВремя вычисления CUDA = %d ms\n", Delay1);
  46.     system("pause");
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement