Advertisement
Isaacmm

Cuda C bloki/watki razem

May 25th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define N 1024
  4.  
  5. __global__ void dodawanie(int *a, int *b, int *c)
  6. {
  7.     int indeks = threadIdx.x + blockIdx.x * blockDim.x;
  8.     while (indeks < N) {
  9.         c[indeks] = a[indeks] + b[indeks];
  10.         indeks += blockDim.x * gridDim.x;
  11.     }
  12. }
  13.  
  14. __global__ void funkcja(int *a, int *b, int *c)
  15. {
  16.     int indeks = threadIdx.x;
  17.     if (indeks < N) {
  18.         c[indeks]= a[indeks] + b[indeks];
  19.     }
  20. }
  21.  
  22. int main(void)
  23. {
  24.     int *a, *b, *c;
  25.     cudaMallocManaged(&a, N*sizeof(int));
  26.     cudaMallocManaged(&b, N*sizeof(int));
  27.     cudaMallocManaged(&c, N*sizeof(int));
  28.  
  29.    
  30.     for (int i = 0; i<N; i++) {
  31.         a[i] = 2 *i;
  32.         b[i] = 2 *i;
  33.     }
  34.     dodawanie<<<16,16>>>(a, b, c);
  35.     cudaDeviceSynchronize();
  36.     /*for (int i = 0; i< N; i++) {
  37.         printf("%d + %d = %d\n", a[i], b[i], c[i]);
  38.     }*/
  39.     printf("%d + %d = %d\n", a[N-1], b[N-1], c[N-1]);
  40.     cudaFree(a);
  41.     cudaFree(b);
  42.     cudaFree(c);
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement