-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_array_sum.cu
48 lines (44 loc) · 7.49 KB
/
two_array_sum.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//#include "cuda_runtime.h"
//#include "device_launch_parameters.h"
#include <stdio.h>
#include<cuda.h>
#define n 5
__global__ void add(int *a, int *b, int *c) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) {
c[i] = a[i] + b[i];
}
}
int main(){
int a[n];
int b[n];
int i;
int c[n]= {0};
int* dev_a;
int* dev_b;
int* dev_c;
cudaMalloc((void**)&dev_c, n * sizeof(int));
cudaMalloc((void**)&dev_a, n * sizeof(int));
cudaMalloc((void**)&dev_b, n * sizeof(int));
printf("\narray elements (1st):\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\narray elements (2nd):\n");
for(i=0;i<n;i++){
scanf("%d",&b[i]);
}
cudaMemcpy(dev_a, a, n * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(dev_b, b, n* sizeof(int), cudaMemcpyHostToDevice);
add<<<n,1>>>(dev_a, dev_b, dev_c);
cudaMemcpy(c, dev_c, n* sizeof(int), cudaMemcpyDeviceToHost);
printf("\nsum is\n");
for(i=0;i<n;i++){
printf("%d\n",c[i]);
}
cudaFree(dev_c);
cudaFree(dev_a);
cudaFree(dev_b);
cudaDeviceReset();
return 0;
}