-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain2.cpp
More file actions
executable file
·321 lines (298 loc) · 9.84 KB
/
main2.cpp
File metadata and controls
executable file
·321 lines (298 loc) · 9.84 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// Created by kunal on 19/11/17.
//
#include <bits/stdc++.h>
#include <omp.h>
using namespace std;
#define N 1'00'000
#define TABLE_SIZE 192
template<class T>
bool contains(const std::vector<T> &vec, const T &value) {
return std::find(vec.begin(), vec.end(), value) != vec.end();
}
vector<unsigned int> keys;
/*void generateData() {
vector<unsigned int> keys;
unsigned int num;
unsigned int maxValue = INT_MAX;
fstream input_data("/Desktop/test.txt", ios::out);
if (!input_data) {
cout << "IO Error";
return;
}
for (unsigned int i = 0; i < N; ++i) {
if (i % 10'000 == 0)
cerr << i << endl;
num = rand() % (maxValue);
while (contains(keys, num))
num = rand() % (maxValue);
keys.push_back(num);
}
for (auto x : keys) {
input_data << x << ' ';
cout << x << ' ';
}
input_data.close();
}*/
void getData(string filename) {
ifstream input_data(filename, ios::in);
if (!input_data) {
cout << "IO Error";
return;
}
unsigned int num;
for (unsigned int i = 0; i < N; ++i) {
input_data >> num;
keys.push_back(num);
}
input_data.close();
}
unsigned int getBucketNumber(unsigned int key, unsigned int noOfBuckets) {
unsigned int c1 ;
unsigned int c0 ;
unsigned int prime = 1900813;
c1=324;
c0=2133;
/*c1 = 1;
c0 = 0;
prime = key + 1;*/
//changed
return (((c0 + c1 * key) % prime) % noOfBuckets); // Else returning negative values.
}
class HashFunctions {
public:
unsigned int c0, c1;
HashFunctions() {
/*this->c0 = this->c1 = 1;*/ //changed
this->c0=rand()%1000;
this->c1=rand()%1000;
}
HashFunctions(unsigned int c0, unsigned int c1) {
this->c0 = c0;
this->c1 = c1;
}
unsigned int g(unsigned int key) {
return (((c0 + c1 * key) % 1900813) % TABLE_SIZE); // Else returning negative values.
}
};
int main() {
// Gets data from the input file.
getData("test.txt"); // Contains 1'00'00'000 unique integers
int j,k,i;
unsigned int noOfBuckets = N/409;
noOfBuckets=noOfBuckets+1;
cout<<noOfBuckets<<endl;
vector<int> vec1;
vector<vector<int> > vec2;
vector<vector<vector<int> > > hashTable;
//hashTable.resize(noOfBuckets);
cout<<"a";
for(i=0;i<191;i++)
vec1.push_back(0);
for(i=0;i<3;i++)
vec2.push_back(vec1);
cout<<noOfBuckets<<endl;
for(i=0;i<noOfBuckets;i++)
hashTable.push_back(vec2);
// Sets up the Hash Tables
/*#pragma omp parallel for
for (unsigned int i = 0; i < noOfBuckets; ++i) {
hashTable[i].resize(3);
#pragma omp parallel for
for (unsigned int j = 0; j < 3; ++j) {
hashTable[i][j].resize(TABLE_SIZE,0);
fill(hashTable[i][j].begin(), hashTable[i][j].end(), 0);
}
}*/
//cout<<"a";
//cout<<"a";
// Check that bucket size is not exceeded while assigning
vector<unsigned int> bucket_size;
for(i=0;i<noOfBuckets;i++)
bucket_size.push_back(0);
for (unsigned int i = 0; i < N; ++i) {
bucket_size[getBucketNumber(keys[i], noOfBuckets)]++;
}
if (*max_element(bucket_size.begin(), bucket_size.end()) > 512) {
cout << "h() failed!" << endl;
return 1;
}
HashFunctions f[3];
// Numbers to be XOR'ed with random number to get corresponding functions.
unsigned int XOR_NUM[3][2] = {{69, 696},
{6969, 69696},
{696969, 6969696}};
/*omp_lock_t table_lock[noOfBuckets][3][TABLE_SIZE];
#pragma omp parallel for
for (unsigned int i = 0; i < noOfBuckets; ++i) {
#pragma omp parallel for
for (unsigned int j = 0; j < 3; ++j) {
#pragma omp parallel for
for (unsigned int k = 0; k < TABLE_SIZE; ++k) {
omp_init_lock(&table_lock[i][j][k]);
}
}
}
*/
// Flag which tells if we need to change the hashing functions due to iterations>=25
// bool flag_change_g = false;
// srand(time(NULL));
unsigned int iterations = 0;
//unsigned int left_out = 0;
unsigned int g[3];
/*
do {
if (flag_change_g) {
cout << "Number of keys left out : " << left_out << endl;
cout << "Last hashing failed, retrying with new random number." << endl;
#pragma omp parallel for
for (unsigned int i = 0; i < noOfBuckets; ++i) {
#pragma omp parallel for
for (unsigned int j = 0; j < 3; ++j) {
fill(hashTable[i][j].begin(), hashTable[i][j].end(), -1);
}
}
}
// Set up hashing functions
unsigned int rand_number = rand()%10000;
for (unsigned int i = 0; i < 3; ++i) {
f[i] = HashFunctions(XOR_NUM[i][0] ^ rand_number, XOR_NUM[i][1] ^ rand_number);
}
cout << "Random number : " << rand_number << endl;
// Set up flags.
flag_change_g = false;
left_out = 0;
iterations = 0;
// Cuckoo Hash in action.
// Keep running until number is saved in the table or infinite loop (iterations>25) is reached
while (iterations < 25) {
#pragma omp parallel for
for (unsigned int i = 0; i < N; ++i) {
unsigned int bucketNumber = getBucketNumber(keys[i], noOfBuckets);
unsigned int tableNumber = iterations % 3;
unsigned int g[3];
// Get the index in each Hash Table.
for (unsigned int j = 0; j < 3; ++j) {
g[j] = f[j].g(keys[i]);
}
if (hashTable[bucketNumber][0][g[0]] != keys[i] && hashTable[bucketNumber][1][g[1]] != keys[i] &&
hashTable[bucketNumber][2][g[2]] != keys[i]) {
omp_set_lock(&table_lock[bucketNumber][tableNumber][g[tableNumber]]);
hashTable[bucketNumber][tableNumber][g[tableNumber]] = keys[i];
omp_unset_lock(&table_lock[bucketNumber][tableNumber][g[tableNumber]]);
}
}
iterations++;
}
// This part checks whether any of the keys are left out, if yes then regenerate the hashing functions.
#pragma omp parallel for
for (unsigned int i = 0; i < N; ++i) {
unsigned int bucketNumber = getBucketNumber(keys[i], noOfBuckets);
unsigned int g[3];
// Get the index in each Hash Table.
for (unsigned int j = 0; j < 3; ++j) {
g[j] = f[j].g(keys[i]);
}
if (hashTable[bucketNumber][0][g[0]] != keys[i] && hashTable[bucketNumber][1][g[1]] != keys[i] &&
hashTable[bucketNumber][2][g[2]] != keys[i]) {
#pragma omp critical
{
// Count of left out keys
left_out++;
flag_change_g = true;
}
}
}
} while (flag_change_g);*/
unsigned int sub_buck;
while(1)
{
vector<int> remaining_keys;
int n1,n2,iterations;
unsigned int rand_number = rand()%10000;
for (unsigned int i = 0; i < 3; ++i) {
f[i] = HashFunctions(XOR_NUM[i][0] ^ rand_number, XOR_NUM[i][1] ^ rand_number);
}
for(i=0;i<N;i++)
{
unsigned int bucketNumber = getBucketNumber(keys[i], noOfBuckets);
for(j=0;j<3;j++)
g[j] = f[j].g(keys[i]);
if(hashTable[bucketNumber][0][g[0]]==0)
hashTable[bucketNumber][0][g[0]]=keys[i];
else if(hashTable[bucketNumber][1][g[1]]==0)
hashTable[bucketNumber][1][g[1]]=keys[i];
else if(hashTable[bucketNumber][2][g[2]]==0)
hashTable[bucketNumber][2][g[2]]=keys[i];
else
remaining_keys.push_back(keys[i]);
}
for(i=0;i<remaining_keys.size();i++)
{
unsigned int bucketNumber = getBucketNumber(remaining_keys[i], noOfBuckets);
sub_buck=rand()%3;
g[sub_buck] = f[sub_buck].g(remaining_keys[i]);
n1=hashTable[bucketNumber][sub_buck][g[sub_buck]];
hashTable[bucketNumber][sub_buck][g[sub_buck]]=remaining_keys[i];
iterations = 0;
while(iterations<=25)
{
for(j=0;j<3;j++)
{
if(j!=sub_buck)
{
bucketNumber= getBucketNumber(n1, noOfBuckets);
g[j]=f[j].g(n1);
if(hashTable[bucketNumber][j][g[j]]==0)
{
hashTable[bucketNumber][j][g[j]]=n1;
break;
}
}
}
if(j==3)
{
for(j=0;j<3;j++)
{
if(j!=sub_buck)
{
n2=hashTable[bucketNumber][j][g[j]];
hashTable[bucketNumber][j][g[j]]=n1;
n1=n2;
sub_buck=j;
iterations=iterations+1;
break;
}
}
}
else
{
break;
}
}
if(iterations>25)
{
break;
}
}
if(i==remaining_keys.size())
{
#pragma omp parallel for
for (unsigned int i = 0; i < noOfBuckets; ++i) {
#pragma omp parallel for
for (unsigned int j = 0; j < 3; ++j) {
fill(hashTable[i][j].begin(), hashTable[i][j].end(), 0);
}
}
cout << "Number of keys left out : " <<remaining_keys.size()-i << endl;
cout << "Last hashing failed, retrying with new random number." << endl;
continue;
}
else
{
cout<<"Successfull"<<endl;
break;
}
}
return 0;
}