-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector_Addition_UM_Prefetching.cu
More file actions
56 lines (41 loc) · 1.31 KB
/
Vector_Addition_UM_Prefetching.cu
File metadata and controls
56 lines (41 loc) · 1.31 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
#include <stdio.h>
#include <cassert>
#include <iostream>
using std::cout;
__global__ void vectorAdd(int *a, int *b, int *c, int N) {
int tid = (blockDim.x * blockIdx.x) + threadIdx.x;
if (tid < N) {
c[tid] = a[tid] + b[tid];
}
}
int main() {
const int N = 1 << 16;
size_t bytes = N * sizeof(int);
int *a, *b, *c;
cudaMallocManaged(&a, bytes);
cudaMallocManaged(&b, bytes);
cudaMallocManaged(&c, bytes);
int id = cudaGetDevice(&id);
cudaMemAdvise(a, bytes, cudaMemAdviseSetPreferredLocation, cudaCpuDeviceId);
cudaMemAdvise(b, bytes, cudaMemAdviseSetPreferredLocation, cudaCpuDeviceId);
cudaMemPrefetchAsync(c, bytes, id);
for (int i = 0; i < N; i++) {
a[i] = rand() % 100;
b[i] = rand() % 100;
}
cudaMemAdvise(a, bytes, cudaMemAdviseSetReadMostly, id);
cudaMemAdvise(b, bytes, cudaMemAdviseSetReadMostly, id);
cudaMemPrefetchAsync(a, bytes, id);
cudaMemPrefetchAsync(b, bytes, id);
int blk_size = 1 << 10;
int grid_size = (N + blk_size - 1) / blk_size;
vectorAdd<<<grid_size, blk_size>>>(a, b, c, N);
cudaDeviceSynchronize();
cudaMemPrefetchAsync(a, bytes, cudaCpuDeviceId);
cudaMemPrefetchAsync(b, bytes, cudaCpuDeviceId);
cudaMemPrefetchAsync(c, bytes, cudaCpuDeviceId);
for (int i = 0; i < N; i++) {
assert(c[i] == a[i] + b[i]);
}
return 0;
}