-
Notifications
You must be signed in to change notification settings - Fork 1
/
bandwidth.h
228 lines (177 loc) · 5.53 KB
/
bandwidth.h
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
typedef struct {
uint64_t item[8];
} item_t;
class BandwidthBench {
private:
static double get_time() {
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec + t.tv_usec*1e-6;
}
static void ShuffleRange(unsigned* base, unsigned count) {
for (unsigned i = 0; i < count; i++) {
base[i] = i;
}
srand(3);
for (unsigned i = 0; i < count; i++) {
unsigned index = rand() % count;
unsigned temp = base[i];
base[i] = base[index];
base[index] = temp;
}
}
public:
static double MeasureMemoryBW (unsigned blockSizeInCL, unsigned totalSizeInCL) {
if (totalSizeInCL%blockSizeInCL != 0) {
cout << "totalSizeInCL not divisible by blockSizeInCL" << endl;
exit(1);
}
unsigned numBlocks = totalSizeInCL/blockSizeInCL;
cout << "numBlocks: " << numBlocks << endl;
item_t* base = (item_t*)aligned_alloc(64, totalSizeInCL*sizeof(item_t));
for (unsigned i = 0; i < totalSizeInCL; i++) {
base[i].item[0] = 2;
}
unsigned* indexes = (unsigned*)malloc(numBlocks*sizeof(unsigned));
ShuffleRange(indexes, numBlocks);
uint64_t accumulation = 0;
double start = get_time();
for (unsigned b = 0; b < numBlocks; b++) {
item_t* blockBase = base + indexes[b]*blockSizeInCL;
for (unsigned i = 0; i < blockSizeInCL; i++) {
__builtin_prefetch(blockBase + 1);
accumulation += blockBase[i].item[0];
}
}
double end = get_time();
uint64_t check = totalSizeInCL*2;
if (accumulation != check) {
cout << "Error, accumulation: " << accumulation << ", check: " << check << endl;
}
else {
cout << "Pass!" << endl;
}
double GBperSecond = (totalSizeInCL*64)/(end-start)/1000000000;
free(base);
free(indexes);
return GBperSecond;
}
static double MeasureDiskBW (unsigned blockSizeInCL, unsigned totalSizeInCL) {
if (totalSizeInCL%blockSizeInCL != 0) {
cout << "totalSizeInCL not divisible by blockSizeInCL" << endl;
exit(1);
}
const int dir_err = mkdir("temp", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (-1 == dir_err) {
cout << "Error creating directory temp" << endl;
exit(1);
}
unsigned numBlocks = totalSizeInCL/blockSizeInCL;
cout << "numBlocks: " << numBlocks << endl;
FILE* file;
item_t* base = (item_t*)aligned_alloc(64, blockSizeInCL*sizeof(item_t));
for (unsigned i = 0; i < blockSizeInCL; i++) {
base[i].item[0] = 2;
}
string name = "temp/file";
for (unsigned b = 0; b < numBlocks; b++) {
string tempName = name + to_string(b);
file = fopen(tempName.c_str(), "wb");
fwrite (base, sizeof(item_t), blockSizeInCL, file);
fclose(file);
}
unsigned* indexes = (unsigned*)malloc(numBlocks*sizeof(unsigned));
ShuffleRange(indexes, numBlocks);
uint64_t accumulation = 0;
system("echo drop caches!");
system("echo 3 | sudo tee /proc/sys/vm/drop_caches");
item_t* base2 = (item_t*)aligned_alloc(64, blockSizeInCL*sizeof(item_t));
double start = get_time();
for (unsigned b = 0; b < numBlocks; b++) {
string tempName = name + to_string(indexes[b]);
file = fopen(tempName.c_str(), "rb");
size_t tempSize = fread(base2, sizeof(item_t), blockSizeInCL, file);
fclose(file);
for (unsigned i = 0; i < blockSizeInCL; i++) {
__builtin_prefetch(base2 + i);
accumulation += base2[i].item[0];
}
}
double end = get_time();
uint64_t check = blockSizeInCL*numBlocks*2;
if (accumulation != check) {
cout << "Error, accumulation: " << accumulation << ", check: " << check << endl;
}
else {
cout << "Pass!" << endl;
}
double GBperSecond = (totalSizeInCL*64)/(end-start)/1000000000;
for (unsigned b = 0; b < numBlocks; b++) {
string tempName = name + to_string(b);
remove(tempName.c_str());
}
rmdir("temp");
free(base);
free(base2);
free(indexes);
return GBperSecond;
}
static double MeasureDiskBWNoWrite (unsigned blockSizeInCL, unsigned totalSizeInCL) {
if (totalSizeInCL%blockSizeInCL != 0) {
cout << "totalSizeInCL not divisible by blockSizeInCL" << endl;
exit(1);
}
unsigned numBlocks = totalSizeInCL/blockSizeInCL;
cout << "numBlocks: " << numBlocks << endl;
unsigned* indexes = (unsigned*)malloc(numBlocks*sizeof(unsigned));
ShuffleRange(indexes, numBlocks);
uint64_t accumulation = 0;
item_t* base = (item_t*)aligned_alloc(64, blockSizeInCL*sizeof(item_t));
FILE* file;
file = fopen("tempFile", "rb");
double start = get_time();
for (unsigned b = 0; b < numBlocks; b++) {
fseek(file, indexes[b]*blockSizeInCL*sizeof(item_t), SEEK_SET);
size_t tempSize = fread(base, sizeof(item_t), blockSizeInCL, file);
for (unsigned i = 0; i < blockSizeInCL; i++) {
__builtin_prefetch(base + i);
accumulation += base[i].item[0];
}
}
double end = get_time();
fclose(file);
uint64_t check = blockSizeInCL*numBlocks*2;
if (accumulation != check) {
cout << "Error, accumulation: " << accumulation << ", check: " << check << endl;
}
else {
cout << "Pass!" << endl;
}
double GBperSecond = (totalSizeInCL*64)/(end-start)/1000000000;
free(base);
free(indexes);
return GBperSecond;
}
static void WriteFile(unsigned totalSizeInCL) {
FILE* file;
item_t* base = (item_t*)aligned_alloc(64, totalSizeInCL*sizeof(item_t));
for (unsigned i = 0; i < totalSizeInCL; i++) {
base[i].item[0] = 2;
}
file = fopen("tempFile", "wb");
fwrite (base, sizeof(item_t), totalSizeInCL, file);
fclose(file);
free(base);
}
};