-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector_add.cu
59 lines (43 loc) · 1.25 KB
/
vector_add.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
#include <cuda_runtime.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#define N 65535
__global__ void vecadd(int *a, int *b, int *c) {
int id = blockIdx.x;
if (id < N)
c[id] = a[id] * b[id];
}
void add(int *a, int *b, int *c) {
for (int id=0; id < N; id++)
c[id] = a[id] * b[id];
}
/*
int main() {
int a[N], b[N], c[N];
int *dev_a, *dev_b, *dev_c;
cudaMalloc(&dev_a, N *sizeof(int));
cudaMalloc(&dev_b, N *sizeof(int));
cudaMalloc(&dev_c, N *sizeof(int));
for (int i=0; i<N; ++i) {
a[i] = i + 1;
b[i] = i + 2;
}
cudaMemcpy(dev_a, a, N * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(dev_b, b, N * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(dev_c, c, N * sizeof(int), cudaMemcpyHostToDevice);
clock_t gpu_t = std::clock();
vecadd<<<N, 256>>>(dev_a, dev_b, dev_c);
gpu_t = std::clock() - gpu_t;
cudaFree(dev_a);
cudaFree(dev_b);
cudaFree(dev_c);
clock_t cpu_t = std::clock();
add(a, b, c);
cpu_t = std::clock() - cpu_t;
std::cout << std::setprecision(10) << "GPU: " << double(gpu_t) / double(CLOCKS_PER_SEC) << " sec" << std::endl <<
"CPU: " << double(cpu_t) / double(CLOCKS_PER_SEC) << " sec" << std::endl;
std::cout << std::setprecision(5) << double(gpu_t) / double(cpu_t) << std::endl;
return 0;
}
*/