-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxwell_tlb.cu
166 lines (129 loc) · 4.43 KB
/
maxwell_tlb.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Finds: Maxwell TLB
Soure code based on paper https://arxiv.org/pdf/1509.02308.pdf
*/
#include <stdio.h>
#include <stdint.h>
#include "cuda_runtime.h"
#define LEN 256
__global__ void global_latency(unsigned int* my_array, int N, int iterations, unsigned int* duration, unsigned int* index) {
// data access latencies array
__shared__ unsigned int s_tvalue[LEN];
// accessed data indices array
__shared__ unsigned int s_index[LEN];
// initialize arrays
for (int k = 0; k < LEN; k++) {
s_index[k] = 0;
s_tvalue[k] = 0;
}
// warm up the TLB
unsigned int j = 0;
for (int k = 0; k < LEN*iterations; k++) {
j = my_array[j];
}
// ready to begin benchmarking
unsigned int start_time, end_time;
for (int k = 0; k < LEN; k++) {
start_time = clock();
// traverse array with elements initialized as indices of next memory access
j = my_array[j];
// handles ILP with this data dependency
s_index[k]= j;
end_time = clock();
s_tvalue[k] = end_time - start_time;
}
for(int k = 0; k < LEN; k++){
index[k] = s_index[k];
duration[k] = s_tvalue[k];
}
}
void parametric_measure_global(int N, int iterations, int stride) {
// destroy context
cudaDeviceReset();
cudaError_t error_id;
// host (CPU) array
unsigned int * h_a;
h_a = (unsigned int*) malloc(N * sizeof(unsigned int));
for (int i = 0; i < N; i++) {
h_a[i] = (i+stride) % N;
}
// device (GPU) array
unsigned int * d_a;
error_id = cudaMalloc((void **) &d_a, N * sizeof(unsigned int));
if (error_id != cudaSuccess) {
printf("Error from allocating device array is %s\n", cudaGetErrorString(error_id));
}
error_id = cudaMemcpy(d_a, h_a, N * sizeof(unsigned int), cudaMemcpyHostToDevice);
if (error_id != cudaSuccess) {
printf("Error from copying over host array is %s\n", cudaGetErrorString(error_id));
}
// accessed data indices array on host (CPU)
unsigned int *h_index = (unsigned int*) malloc(LEN * sizeof(unsigned int));
// accessed data indices array on device (GPU)
unsigned int *d_index;
error_id = cudaMalloc((void **) &d_index, LEN * sizeof(unsigned int));
if (error_id != cudaSuccess) {
printf("Error from allocating indices array is %s\n", cudaGetErrorString(error_id));
}
// data access latencies array on host (CPU)
unsigned int *h_duration = (unsigned int*) malloc(LEN * sizeof(unsigned int));
// data access latencies array on device (GPU)
unsigned int *d_duration;
error_id = cudaMalloc ((void**) &d_duration, LEN * sizeof(unsigned int));
if (error_id != cudaSuccess) {
printf("Error from allocating latencies array is %s\n", cudaGetErrorString(error_id));
}
// blocks until the device has completed all preceding requested tasks
cudaThreadSynchronize();
// launch kernel
dim3 Db = dim3(1);
dim3 Dg = dim3(1,1,1);
global_latency<<<Dg, Db>>>(d_a, N, iterations, d_duration, d_index);
cudaThreadSynchronize();
error_id = cudaGetLastError();
if (error_id != cudaSuccess) {
printf("Error from kernel is %s\n", cudaGetErrorString(error_id));
}
cudaThreadSynchronize();
// copy results from GPU to CPU
error_id = cudaMemcpy((void*) h_duration, (void*) d_duration, LEN * sizeof(unsigned int), cudaMemcpyDeviceToHost);
if (error_id != cudaSuccess) {
printf("Error 2.0 is %s\n", cudaGetErrorString(error_id));
}
error_id = cudaMemcpy((void*) h_index, (void*) d_index, LEN * sizeof(unsigned int), cudaMemcpyDeviceToHost);
if (error_id != cudaSuccess) {
printf("Error 2.1 is %s\n", cudaGetErrorString(error_id));
}
cudaThreadSynchronize();
for(int i = 0; i < LEN; i++) {
printf("%d\n", h_duration[i]);
}
// free memory on GPU
cudaFree(d_a);
cudaFree(d_index);
cudaFree(d_duration);
// free memory on CPU
free(h_a);
free(h_index);
free(h_duration);
// destroy context
cudaDeviceReset();
}
void measure_global() {
int iterations = 10;
// 2 MB stride
int stride = 2*1024*1024/sizeof(unsigned int);
for (int N = 120*1024*256; N <= 150*1024*256; N += stride) {
printf("\n=====%3.1f MB array, warm TLB, read 256 element====\n", sizeof(unsigned int)*(float)N/1024/1024);
parametric_measure_global(N, iterations, stride);
printf("===============================================\n\n");
}
}
int main() {
// current device
cudaSetDevice(0);
measure_global();
// destroy context
cudaDeviceReset();
return 0;
}