-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector_Addition.cu
More file actions
66 lines (53 loc) · 1.34 KB
/
Vector_Addition.cu
File metadata and controls
66 lines (53 loc) · 1.34 KB
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 <algorithm>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <vector>
using std::begin;
using std::copy;
using std::cout;
using std::end;
using std::generate;
using std::vector;
__global__ void vectorAdd(int* a, int* b, int* c, int N) {
int tid = (blk_id.x * blk_dim.x) + threadIdx.x;
if (tid < N) {
c[tid] = a[tid] + b[tid];
}
}
void verify_result(int *a, int *b, int *c, int N) {
for (int i = 0; i < N; i++) {
assert(c[i] == a[i] + b[i]);
}
}
int main() {
constexpr int N = 1 << 26;
size_t bytes = sizeof(int) * N;
int *h_a, *h_b, *h_c;
cudaMallocHost(&h_a, bytes);
cudaMallocHost(&h_b, bytes);
cudaMallocHost(&h_c, bytes);
for(int i = 0; i < N; i++){
h_a[i] = rand() % 100;
h_b[i] = rand() % 100;
}
int *d_a, *d_b, *d_c;
cudaMalloc(&d_a, bytes);
cudaMalloc(&d_b, bytes);
cudaMalloc(&d_c, bytes);
cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);
cudaMemcpy(d_b, h_b, bytes, cudaMemcpyHostToDevice);
int no_threads = 1 << 10;
int no_blk = (N + no_threads - 1) / no_threads;
vectorAdd<<<no_blk, no_threads>>>(d_a, d_b, d_c, N);
cudaMemcpy(h_c, d_c, bytes, cudaMemcpyDeviceToHost);
verify_result(h_a, h_b, h_c, N);
cudaFreeHost(h_a);
cudaFreeHost(h_b);
cudaFreeHost(h_c);
cudaFree(d_a);
cudaFree(d_b);
cudaFree(d_c);
return 0;
}