forked from goossaert/hashmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
281 lines (228 loc) · 9.29 KB
/
main.cc
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <set>
#include <algorithm>
#include <sys/stat.h>
#include "hashmap.h"
#include "probing_hashmap.h"
#include "tombstone_hashmap.h"
#include "backshift_hashmap.h"
#include "bitmap_hashmap.h"
#include "shadow_hashmap.h"
#include "testcase.h"
std::string concatenate(std::string const& str, int i)
{
std::stringstream s;
s << str << i;
return s.str();
}
uint32_t NearestPowerOfTwo(const uint32_t number) {
uint32_t power = 1;
while (power < number) {
power <<= 1;
}
return power;
}
int exists_or_mkdir(const char *path) {
struct stat sb;
if (stat(path, &sb) == 0) {
if (!S_ISDIR(sb.st_mode)) {
return 1;
}
} else if (mkdir(path, 0777) != 0) {
return 1;
}
return 0;
}
void show_usage() {
fprintf(stdout, "Test program for implementations of open addressing hash table algorithms.\n");
fprintf(stdout, "\n");
fprintf(stdout, "General parameters (mandatory):\n");
fprintf(stdout, " --algo algorithm to use for the hash table. Possible values are:\n");
fprintf(stdout, " * linear: basic linear probing\n");
fprintf(stdout, " * tombstone: Robin Hood hashing with tombstone deletion\n");
fprintf(stdout, " * backshift: Robin Hood hashing with backward shifting deletion\n");
fprintf(stdout, " * bitmap: hopscotch hashing with bitmap representation\n");
fprintf(stdout, " * shadow: hopscotch hashing with shadow representation\n");
fprintf(stdout, " --testcase test case to use. Possible values are:\n");
fprintf(stdout, " * loading: load the table until it is full (does not perform any removals).\n");
fprintf(stdout, " * batch: load the table, then remove a large batch, and re-insert a large batch.\n");
fprintf(stdout, " * ripple: load the table, then do a series of removal-insertion operations.\n");
fprintf(stdout, "\n");
fprintf(stdout, "Parameters for linear probing algorithm (optional):\n");
fprintf(stdout, " --num_buckets number of buckets in the hash table (default=10000)\n");
fprintf(stdout, "\n");
fprintf(stdout, "Parameters for tombstone algorithm (optional):\n");
fprintf(stdout, " --num_buckets number of buckets in the hash table (default=10000)\n");
fprintf(stdout, "\n");
fprintf(stdout, "Parameters for backshift algorithm (optional):\n");
fprintf(stdout, " --num_buckets number of buckets in the hash table (default=10000)\n");
fprintf(stdout, "\n");
fprintf(stdout, "Parameters for bitmap algorithm (optional):\n");
fprintf(stdout, " --num_buckets number of buckets in the hash table (default=10000)\n");
fprintf(stdout, " --size_probing maximum number of buckets used in the probing (default=4096)\n");
fprintf(stdout, "\n");
fprintf(stdout, "Parameters for shadow algorithm (optional):\n");
fprintf(stdout, " --num_buckets number of buckets in the hash table (default=10000)\n");
fprintf(stdout, " --size_probing maximum number of buckets used in the probing (default=4096)\n");
fprintf(stdout, " --size_nh_start starting size of the neighborhoods (default=32)\n");
fprintf(stdout, " --size_nh_end ending size of the neighborhoods (default=32)\n");
fprintf(stdout, "\n");
fprintf(stdout, "Parameters for the batch test case (optional):\n");
fprintf(stdout, " --load_factor_max maxium load factor at which the table should be used (default=.7)\n");
fprintf(stdout, " --load_factor_step load factor by which items in the table should be removed and inserted (default=.1)\n");
fprintf(stdout, "\n");
fprintf(stdout, "Parameters for the ripple test case (optional):\n");
fprintf(stdout, " --load_factor_max maxium load factor at which the table should be used (default=.7)\n");
fprintf(stdout, " --load_factor_step load factor by which items in the table should be removed and inserted (default=.1)\n");
fprintf(stdout, "\n");
fprintf(stdout, "Examples:\n");
fprintf(stdout, "./hashmap --algo backshift --num_buckets 10000 --testcase batch --load_factor_max 0.8 --load_factor_step 0.1\n");
fprintf(stdout, "./hashmap --algo shadow --num_buckets 10000 --size_nh_start 4 --size_nh_end 64 --testcase loading\n");
}
int main(int argc, char **argv) {
bool has_error;
if (argc == 1 || (argc == 2 && strcmp(argv[1], "--help") == 0)) {
show_usage();
exit(-1);
}
if (argc % 2 == 0) {
std::cerr << "Error: invalid number of arguments" << std::endl;
exit(-1);
}
uint32_t size_neighborhood_start = 32;
uint32_t size_neighborhood_end = 32;
uint32_t size_probing = 4096;
uint32_t num_buckets = 10000;
double load_factor_max = 0.7;
double load_factor_step = 0.1;
std::string algorithm = "";
std::string testcase = "";
if (argc > 2) {
for (int i = 1; i < argc; i += 2 ) {
if (strcmp(argv[i], "--algo" ) == 0) {
algorithm = std::string(argv[i+1]);
} else if (strcmp(argv[i], "--num_buckets" ) == 0) {
num_buckets = atoi(argv[i+1]);
} else if (strcmp(argv[i], "--size_nh_start" ) == 0) {
size_neighborhood_start = atoi(argv[i+1]);
} else if (strcmp(argv[i], "--size_nh_end" ) == 0) {
size_neighborhood_end = atoi(argv[i+1]);
} else if (strcmp(argv[i], "--size_probing" ) == 0) {
size_probing = atoi(argv[i+1]);
} else if (strcmp(argv[i], "--testcase" ) == 0) {
testcase = std::string(argv[i+1]);
} else if (strcmp(argv[i], "--load_factor_max" ) == 0) {
load_factor_max = atof(argv[i+1]);
} else if (strcmp(argv[i], "--load_factor_step" ) == 0) {
load_factor_step = atof(argv[i+1]);
} else {
fprintf(stderr, "Unknown parameter [%s]\n", argv[i]);
exit(-1);
}
}
}
int num_items = num_buckets;
//int num_items = NearestPowerOfTwo(num_buckets);
hashmap::HashMap *hm;
if (algorithm == "bitmap") {
hm = new hashmap::BitmapHashMap(num_items, size_probing);
} else if (algorithm == "shadow") {
hm = new hashmap::ShadowHashMap(num_items, size_probing, size_neighborhood_start, size_neighborhood_end);
} else if (algorithm == "linear") {
hm = new hashmap::ProbingHashMap(num_items, size_probing);
} else if (algorithm == "tombstone") {
hm = new hashmap::TombstoneHashMap(num_items);
} else if (algorithm == "backshift") {
hm = new hashmap::BackshiftHashMap(num_items);
} else {
fprintf(stderr, "Unknown algorithm [%s]\n", algorithm.c_str());
exit(-1);
}
if (testcase == "loading") {
//run_testcase2(hm, num_items, load_factor_max);
hashmap::LoadingTestCase tc(hm, num_items);
tc.run();
return 0;
} else if (testcase == "batch") {
//run_testcase2(hm, num_items, load_factor_max);
hashmap::BatchTestCase tc(hm, num_items, load_factor_max, load_factor_step);
tc.run();
return 0;
} else if (testcase == "ripple") {
hashmap::RippleTestCase tc(hm, num_items, load_factor_max, load_factor_step);
tc.run();
return 0;
} else if (testcase != "") {
fprintf(stderr, "Error: testcase is unknown [%s]\n", testcase.c_str());
return 1;
}
hm->Open();
std::string value_out("value_out");
int num_items_reached = 0;
for (int i = 0; i < num_items; i++) {
value_out = "value_out";
std::string key = concatenate( "key", i );
std::string value = concatenate( "value", i );
int ret_put = hm->Put(key, value);
hm->Get(key, &value_out);
if (ret_put != 0) {
std::cout << "Insertion stopped due to clustering at step: " << i << std::endl;
std::cout << "Load factor: " << (double)i/num_items << std::endl;
num_items_reached = i;
break;
}
}
has_error = false;
for (int i = 0; i < num_items_reached; i++) {
value_out = "value_out";
std::string key = concatenate( "key", i );
std::string value = concatenate( "value", i );
int ret_get = hm->Get(key, &value_out);
if (ret_get != 0 || value != value_out) {
std::cout << "Final check: error at step [" << i << "]" << std::endl;
has_error = true;
break;
}
}
if (!has_error) {
std::cout << "Final check: OK" << std::endl;
}
/*
if (hm->monitoring_ != NULL) {
std::cout << "Monitoring: OK" << std::endl;
}
// testcase-algo-metric-runnumber-step.json
// batch50-shadow-density-00001-0001.json
hm->monitoring_->PrintDensity("density.json");
std::cout << "Clustering" << std::endl;
hm->monitoring_->PrintClustering(hm);
hm->monitoring_->PrintDIB("probing_sequence_length_search.json");
hm->monitoring_->PrintNumScannedBlocks("num_scanned_blocks.json");
*/
//hm->CheckDensity();
//hm->BucketCounts();
has_error = false;
for (int i = 0; i < num_items_reached; i++) {
std::string key = concatenate( "key", i );
std::string value = concatenate( "value", i );
int ret_remove = hm->Remove(key);
if (ret_remove != 0) {
std::cout << "Remove: error at step [" << i << "]" << std::endl;
has_error = true;
break;
}
int ret_get = hm->Get(key, &value_out);
if (ret_get == 0) {
std::cout << "Remove: error at step [" << i << "] -- can get after remove" << std::endl;
has_error = true;
break;
}
}
if (!has_error) {
std::cout << "Removing items: OK" << std::endl;
}
return 0;
}