-
Notifications
You must be signed in to change notification settings - Fork 2
/
simpleManagedPrefetch.cu
107 lines (90 loc) · 2.78 KB
/
simpleManagedPrefetch.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <cstdio>
#include <cinttypes>
#include <cuda_runtime.h>
#include "common.hh"
static __global__ void
f(const uint64_t a[], const uint64_t b[], uint64_t c[], int64_t N)
{
int64_t index = threadIdx.x + blockIdx.x * blockDim.x;
int64_t stride = blockDim.x * gridDim.x;
for (int64_t i = index; i < N; i += stride) {
c[i] = a[i] * b[i];
}
}
static void
doit(const uint64_t a[], const uint64_t b[], uint64_t c[], int64_t N)
{
int blockSize = 256;
int64_t numBlocks = (N + blockSize - 1) / blockSize;
f<<<numBlocks, blockSize>>>(a, b, c, N);
}
int
main(int argc, char *argv[])
{
size_t N = 10000000;
clock_t start_program, end_program;
clock_t start, end;
uint64_t *a, *b, *c;
size_t count;
if (argc == 2) {
N = checked_strtosize(argv[1]);
}
count = checked_mul(N, sizeof(uint64_t));
/* Initialize context */
check(cudaMallocManaged(&a, 128));
check(cudaDeviceSynchronize());
check(cudaFree(a));
start_program = clock();
start = clock();
check(cudaMallocManaged(&a, count, cudaMemAttachHost));
check(cudaMallocManaged(&b, count, cudaMemAttachHost));
check(cudaMallocManaged(&c, count));
end = clock();
log("host: MallocManaged", start, end);
start = clock();
for (size_t i = 0; i < N; i++) {
a[i] = 3;
b[i] = 5;
}
end = clock();
log("host: init arrays", start, end);
start = clock();
check(cudaStreamAttachMemAsync(NULL, a, 0, cudaMemAttachGlobal));
check(cudaStreamAttachMemAsync(NULL, b, 0, cudaMemAttachGlobal));
doit(a, b, c, N);
check(cudaStreamAttachMemAsync(NULL, a, 0, cudaMemAttachHost));
check(cudaStreamAttachMemAsync(NULL, b, 0, cudaMemAttachHost));
check(cudaStreamAttachMemAsync(NULL, c, 0, cudaMemAttachHost));
check(cudaStreamSynchronize(NULL));
end = clock();
log("device: uvm+compute+synchronize", start, end);
start = clock();
for (size_t i = 0; i < N; i++) {
if (a[i] != 3 || b[i] != 5 || c[i] != 15) {
fprintf(stderr, "unexpected result a: %lu b: %lu c: %lu\n",
a[i], b[i], c[i]);
exit(1);
}
}
end = clock();
log("host: access all arrays", start, end);
start = clock();
for (size_t i = 0; i < N; i++) {
if (a[i] != 3 || b[i] != 5 || c[i] != 15) {
fprintf(stderr, "unexpected result a: %lu b: %lu c: %lu\n",
a[i], b[i], c[i]);
exit(1);
}
}
end = clock();
log("host: access all arrays a second time", start, end);
start = clock();
check(cudaFree(a));
check(cudaFree(b));
check(cudaFree(c));
end = clock();
log("host: free", start, end);
end_program = clock();
log("total", start_program, end_program);
return 0;
}