-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectoraddition.cu
65 lines (46 loc) · 1.21 KB
/
vectoraddition.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
#include <cassert>
#include <iostream>
__global__
void vectorAddUM(int *a, int *b, int *c, int N) {
int tid = (blockIdx.x * blockDim.x) + threadIdx.x;
if (tid < N) {
c[tid] = a[tid] + b[tid];
}
}
void init_vector(int *a, int *b, int N) {
for (size_t i = 0; i < N; i++)
{
a[i] = 1;
b[i] = 9;
}
}
void verify_result(int *a, int *b, int *c, int N) {
for (int i = 0; i < N; i++) {
if (c[i] != a[i] + b[i]) {
assert(c[i] == a[i] + b[i]);
}
}
}
int main(int argc, char const *argv[])
{
int id = cudaGetDevice(&id);
constexpr int N = 1<<20;
int *a, *b, *c;
size_t bytes = N * sizeof(float);
cudaMallocManaged(&a, bytes);
cudaMallocManaged(&b, bytes);
cudaMallocManaged(&c, bytes);
init_vector(a, b, N);
constexpr const int BLOCKSIZE = 256;
const int GRID_SIZE = (int) ceil(N / BLOCKSIZE);
cudaMemPrefetchAsync(a, bytes, id);
cudaMemPrefetchAsync(b, bytes, id);
vectorAddUM <<<GRID_SIZE, BLOCKSIZE>>> (a, b, c, N);
cudaDeviceSynchronize();
cudaMemPrefetchAsync(c, bytes, cudaCpuDeviceId);
verify_result(a, b, c, N);
cudaFree(a);
cudaFree(b);
cudaFree(c);
return 0;
}