-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.cu
66 lines (50 loc) · 1.22 KB
/
1.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
__global__ void vectorAdd(int *a, int *b, int *c, int n){
int i = blockIdx.x*blockDim.x+threadIdx.x;
if(i<n)
for(int j=0;j<100;j++)
c[i] = a[i] + b[i];
}
int main(void){
int * a, * b, * c;
int * temp;
int n = 1<<24;
cudaMallocManaged(&a, n*sizeof(int));
cudaMallocManaged(&b, n*sizeof(int));
cudaMallocManaged(&c, n*sizeof(int));
temp = new int[n*sizeof(int)];
for(int i=0;i<n;i++){
a[i] = 3;
b[i] = 5;
}
int blockSize = 256;
int numBlocks = n/256;
vectorAdd<<<numBlocks,blockSize>>>(a,b,c,n);
cudaDeviceSynchronize();
temp[0] = a[0];
for(int i=1;i<n;i++)
temp[i] = temp[i-1] + a[i];
vectorAdd<<<numBlocks,blockSize>>>(a,b,c,n);
cudaDeviceSynchronize();
temp[0] = a[0];
for(int i=1;i<n;i++)
temp[i] = temp[i-1] + a[i];
cudaDeviceSynchronize();
temp[0] = b[0];
for(int i=1;i<n;i++)
temp[i] = temp[i-1] + b[i];
vectorAdd<<<numBlocks, blockSize>>>(a,b,c,n);
cudaDeviceSynchronize();
temp[0] = c[0];
for(int i=1;i<n;i++)
temp[i] = temp[i-1] + c[i];
vectorAdd<<<numBlocks, blockSize>>>(a,b,c,n);
cudaDeviceSynchronize();
vectorAdd<<<numBlocks, blockSize>>>(a,b,c,n);
cudaDeviceSynchronize();
cudaFree(a);
cudaFree(b);
cudaFree(c);
delete temp;
return 0;
}